コード例 #1
0
ファイル: kloud_test.go プロジェクト: koding/koding
func TestReadProviders(t *testing.T) {
	cases := map[string]struct {
		tmpl      string
		providers []string
		err       bool
	}{
		"single provider": {
			`{"provider": {"google": {}}}`,
			[]string{"google"},
			false,
		},
		"multiple providers": {
			`{"provider": {"google": {}, "aws": {}}}`,
			[]string{"aws", "google"},
			false,
		},
		"no providers": {
			`{"provider": {}}`,
			[]string{},
			false,
		},
		"invalid template": {
			"[]",
			nil,
			true,
		},
	}

	for name, cas := range cases {
		t.Run(name, func(t *testing.T) {
			providers, err := stack.ReadProviders([]byte(cas.tmpl))

			if cas.err {
				if err == nil {
					t.Fatal("expected err to be non-nil")
				}

				return
			}

			if !reflect.DeepEqual(providers, cas.providers) {
				t.Fatalf("got %#v, want %#v", providers, cas.providers)
			}
		})
	}
}
コード例 #2
0
func (c *Client) Create(opts *CreateOptions) (*stack.ImportResponse, error) {
	if err := opts.Valid(); err != nil {
		return nil, err
	}

	data, err := c.jsonReencode(opts.Template)
	if err != nil {
		return nil, fmt.Errorf("stack: template encoding error: %s", err)
	}

	providers, err := kloudstack.ReadProviders(data)
	if err != nil {
		return nil, fmt.Errorf("stack: unable to read providers: %s", err)
	}

	if len(providers) == 0 {
		return nil, errors.New("stack: unable to read providers")
	}

	req := &stack.ImportRequest{
		Template:    data,
		Team:        opts.Team,
		Title:       opts.Title,
		Credentials: make(map[string][]string),
	}

	if req.Team == "" {
		req.Team = team.Used().Name
	}

	var resp stack.ImportResponse

	for _, identifier := range opts.Credentials {
		provider, err := c.credential().Provider(identifier)
		if err != nil {
			return nil, fmt.Errorf("stack: unable to read provider of %q: %s", identifier, err)
		}

		req.Credentials[provider] = append(req.Credentials[provider], identifier)
	}

	used := c.credential().Used()

	for _, provider := range providers {
		if _, ok := req.Credentials[provider]; ok {
			continue
		}

		identifier, ok := used[provider]
		if !ok || identifier == "" {
			return nil, fmt.Errorf("stack: missing credential for %q provider", provider)
		}

		req.Credentials[provider] = []string{identifier}
	}

	if err := c.kloud().Call("import", req, &resp); err != nil {
		return nil, fmt.Errorf("stack: unable to communicate with Kloud: %s", err)
	}

	return &resp, nil
}