Generate new version of SDK resources and Add Tests (#331)

This commit is contained in:
Todd Knight authored and GitHub committed 2020-09-08 16:27:51 -04:00
1 parent 96e4b1cdba
commit c3ecea172d
93 files changed
+3414 -2826

No files matched your search

+22 -448
View File
@@ -28,252 +28,31 @@ type HostSet struct {
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
// Client is a client for this collection
type Client struct {
client *api.Client
}
// Creates a new client for this collection. The submitted API client is cloned;
// modifications to it after generating this client will not have effect. If you
// need to make changes to the underlying API client, use ApiClient() to access
// it.
func NewClient(c *api.Client) *Client {
return &Client{client: c}
return &Client{client: c.Clone()}
}
// ApiClient returns the underlying API client
func (c *Client) ApiClient() *api.Client {
return c.client
}
func (c *Client) Create(ctx context.Context, hostCatalogId string, opt ...Option) (*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Create request")
}
opts, apiOpts := getOpts(opt...)
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/host-sets", hostCatalogId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Create response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
return target, apiErr, nil
}
func (c *Client) Read(ctx context.Context, hostCatalogId string, hostSetId string, opt ...Option) (*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Read request")
}
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into Read request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("host-catalogs/%s/host-sets/%s", hostCatalogId, hostSetId), nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
return target, apiErr, nil
}
func (c *Client) Update(ctx context.Context, hostCatalogId string, hostSetId string, version uint32, opt ...Option) (*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Update request")
}
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into Update request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into Update request and automatic versioning not specified")
}
existingTarget, existingApiErr, existingErr := c.Read(ctx, hostCatalogId, hostSetId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
if existingApiErr != nil {
return nil, nil, fmt.Errorf("error from controller when performing initial check-and-set read: %s", pretty.Sprint(existingApiErr))
}
if existingTarget == nil {
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
}
version = existingTarget.Version
}
opts.postMap["version"] = version
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("host-catalogs/%s/host-sets/%s", hostCatalogId, hostSetId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
return target, apiErr, nil
}
func (c *Client) Delete(ctx context.Context, hostCatalogId string, hostSetId string, opt ...Option) (bool, *api.Error, error) {
if hostCatalogId == "" {
return false, nil, fmt.Errorf("empty hostCatalogId value passed into Delete request")
}
if hostSetId == "" {
return false, nil, fmt.Errorf("empty hostSetId value passed into Delete request")
}
if c.client == nil {
return false, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("host-catalogs/%s/host-sets/%s", hostCatalogId, hostSetId), nil, apiOpts...)
if err != nil {
return false, nil, fmt.Errorf("error creating Delete request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
}
type deleteResponse struct {
Existed bool
}
target := &deleteResponse{}
apiErr, err := resp.Decode(target)
if err != nil {
return false, nil, fmt.Errorf("error decoding Delete response: %w", err)
}
if apiErr != nil {
return false, apiErr, nil
}
return target.Existed, apiErr, nil
}
func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option) ([]*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into List request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("host-catalogs/%s/host-sets", hostCatalogId), nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating List request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
}
type listResponse struct {
Items []*HostSet
}
target := &listResponse{}
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
return target.Items, apiErr, nil
}
func (c *Client) Create2(ctx context.Context, hostCatalogId string, opt ...Option) (*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Create request")
}
opts, apiOpts := getOpts(opt...)
apiOpts = append(apiOpts, api.WithNewStyle())
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
@@ -309,7 +88,7 @@ func (c *Client) Create2(ctx context.Context, hostCatalogId string, opt ...Optio
return target, apiErr, nil
}
func (c *Client) Read2(ctx context.Context, hostSetId string, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) Read(ctx context.Context, hostSetId string, opt ...Option) (*HostSet, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into Read request")
}
@@ -318,7 +97,6 @@ func (c *Client) Read2(ctx context.Context, hostSetId string, opt ...Option) (*H
}
opts, apiOpts := getOpts(opt...)
apiOpts = append(apiOpts, api.WithNewStyle())
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("host-sets/%s", hostSetId), nil, apiOpts...)
if err != nil {
@@ -349,7 +127,7 @@ func (c *Client) Read2(ctx context.Context, hostSetId string, opt ...Option) (*H
return target, apiErr, nil
}
func (c *Client) Update2(ctx context.Context, hostSetId string, version uint32, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) Update(ctx context.Context, hostSetId string, version uint32, opt ...Option) (*HostSet, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into Update request")
}
@@ -358,13 +136,12 @@ func (c *Client) Update2(ctx context.Context, hostSetId string, version uint32,
}
opts, apiOpts := getOpts(opt...)
apiOpts = append(apiOpts, api.WithNewStyle())
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into Update request and automatic versioning not specified")
}
existingTarget, existingApiErr, existingErr := c.Read2(ctx, hostSetId, opt...)
existingTarget, existingApiErr, existingErr := c.Read(ctx, hostSetId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
@@ -408,7 +185,7 @@ func (c *Client) Update2(ctx context.Context, hostSetId string, version uint32,
return target, apiErr, nil
}
func (c *Client) Delete2(ctx context.Context, hostSetId string, opt ...Option) (bool, *api.Error, error) {
func (c *Client) Delete(ctx context.Context, hostSetId string, opt ...Option) (bool, *api.Error, error) {
if hostSetId == "" {
return false, nil, fmt.Errorf("empty hostSetId value passed into Delete request")
}
@@ -417,7 +194,6 @@ func (c *Client) Delete2(ctx context.Context, hostSetId string, opt ...Option) (
}
opts, apiOpts := getOpts(opt...)
apiOpts = append(apiOpts, api.WithNewStyle())
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("host-sets/%s", hostSetId), nil, apiOpts...)
if err != nil {
@@ -451,7 +227,7 @@ func (c *Client) Delete2(ctx context.Context, hostSetId string, opt ...Option) (
return target.Existed, apiErr, nil
}
func (c *Client) List2(ctx context.Context, hostCatalogId string, opt ...Option) ([]*HostSet, *api.Error, error) {
func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option) ([]*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into List request")
}
@@ -460,7 +236,6 @@ func (c *Client) List2(ctx context.Context, hostCatalogId string, opt ...Option)
}
opts, apiOpts := getOpts(opt...)
apiOpts = append(apiOpts, api.WithNewStyle())
opts.queryMap["host_catalog_id"] = hostCatalogId
req, err := c.client.NewRequest(ctx, "GET", "host-sets", nil, apiOpts...)
@@ -495,10 +270,7 @@ func (c *Client) List2(ctx context.Context, hostCatalogId string, opt ...Option)
return target.Items, apiErr, nil
}
func (c *Client) AddHosts(ctx context.Context, hostCatalogId string, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into AddHosts request")
}
func (c *Client) AddHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into AddHosts request")
}
@@ -512,203 +284,7 @@ func (c *Client) AddHosts(ctx context.Context, hostCatalogId string, hostSetId s
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into AddHosts request")
}
existingTarget, existingApiErr, existingErr := c.Read(ctx, hostCatalogId, hostSetId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
if existingApiErr != nil {
return nil, nil, fmt.Errorf("error from controller when performing initial check-and-set read: %s", pretty.Sprint(existingApiErr))
}
if existingTarget == nil {
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
}
version = existingTarget.Version
}
opts.postMap["version"] = version
if len(hostIds) > 0 {
opts.postMap["host_ids"] = hostIds
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/host-sets/%s:add-hosts", hostCatalogId, hostSetId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating AddHosts request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during AddHosts call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding AddHosts response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
return target, apiErr, nil
}
func (c *Client) SetHosts(ctx context.Context, hostCatalogId string, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into SetHosts request")
}
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into SetHosts request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into SetHosts request")
}
existingTarget, existingApiErr, existingErr := c.Read(ctx, hostCatalogId, hostSetId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
if existingApiErr != nil {
return nil, nil, fmt.Errorf("error from controller when performing initial check-and-set read: %s", pretty.Sprint(existingApiErr))
}
if existingTarget == nil {
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
}
version = existingTarget.Version
}
opts.postMap["version"] = version
if len(hostIds) > 0 {
opts.postMap["host_ids"] = hostIds
} else if hostIds != nil {
// In this function, a non-nil but empty list means clear out
opts.postMap["host_ids"] = nil
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/host-sets/%s:set-hosts", hostCatalogId, hostSetId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating SetHosts request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during SetHosts call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding SetHosts response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
return target, apiErr, nil
}
func (c *Client) RemoveHosts(ctx context.Context, hostCatalogId string, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into RemoveHosts request")
}
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into RemoveHosts request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into RemoveHosts request")
}
existingTarget, existingApiErr, existingErr := c.Read(ctx, hostCatalogId, hostSetId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
if existingApiErr != nil {
return nil, nil, fmt.Errorf("error from controller when performing initial check-and-set read: %s", pretty.Sprint(existingApiErr))
}
if existingTarget == nil {
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
}
version = existingTarget.Version
}
opts.postMap["version"] = version
if len(hostIds) > 0 {
opts.postMap["host_ids"] = hostIds
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/host-sets/%s:remove-hosts", hostCatalogId, hostSetId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating RemoveHosts request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during RemoveHosts call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding RemoveHosts response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
return target, apiErr, nil
}
func (c *Client) AddHosts2(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into AddHosts request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
apiOpts = append(apiOpts, api.WithNewStyle())
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into AddHosts request")
}
existingTarget, existingApiErr, existingErr := c.Read2(ctx, hostSetId, opt...)
existingTarget, existingApiErr, existingErr := c.Read(ctx, hostSetId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
@@ -756,7 +332,7 @@ func (c *Client) AddHosts2(ctx context.Context, hostSetId string, version uint32
return target, apiErr, nil
}
func (c *Client) SetHosts2(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) SetHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into SetHosts request")
}
@@ -765,13 +341,12 @@ func (c *Client) SetHosts2(ctx context.Context, hostSetId string, version uint32
}
opts, apiOpts := getOpts(opt...)
apiOpts = append(apiOpts, api.WithNewStyle())
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into SetHosts request")
}
existingTarget, existingApiErr, existingErr := c.Read2(ctx, hostSetId, opt...)
existingTarget, existingApiErr, existingErr := c.Read(ctx, hostSetId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
@@ -822,7 +397,7 @@ func (c *Client) SetHosts2(ctx context.Context, hostSetId string, version uint32
return target, apiErr, nil
}
func (c *Client) RemoveHosts2(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) RemoveHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into RemoveHosts request")
}
@@ -831,13 +406,12 @@ func (c *Client) RemoveHosts2(ctx context.Context, hostSetId string, version uin
}
opts, apiOpts := getOpts(opt...)
apiOpts = append(apiOpts, api.WithNewStyle())
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into RemoveHosts request")
}
existingTarget, existingApiErr, existingErr := c.Read2(ctx, hostSetId, opt...)
existingTarget, existingApiErr, existingErr := c.Read(ctx, hostSetId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
+153 -236
View File
@@ -18,129 +18,95 @@ import (
)
func TestCustom(t *testing.T) {
for _, newStyle := range []bool{false, true} {
t.Run(fmt.Sprintf("custom_%t", newStyle), func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
token := tc.Token()
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
client := tc.Client().Clone()
client.SetScopeId(proj.GetPublicId())
client := tc.Client()
token := tc.Token()
client.SetToken(token.Token)
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
hc, apiErr, err := hostcatalogs.NewClient(client).Create(tc.Context(), "static")
require.NoError(err)
require.Nil(apiErr)
hc, apiErr, err := hostcatalogs.NewClient(client).Create(tc.Context(), "static", proj.GetPublicId())
require.NoError(err)
require.Nil(apiErr)
hClient := hosts.NewClient(client)
h1, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
require.Nil(apiErr)
h2, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
require.Nil(apiErr)
hClient := hosts.NewClient(client)
h1, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
require.Nil(apiErr)
h2, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
require.Nil(apiErr)
hSetClient := hostsets.NewClient(client)
var hSet *hostsets.HostSet
if newStyle {
hSet, apiErr, err = hSetClient.Create2(tc.Context(), hc.Id)
} else {
hSet, apiErr, err = hSetClient.Create(tc.Context(), hc.Id)
}
require.NoError(err)
require.Nil(apiErr)
hSetClient := hostsets.NewClient(client)
var hSet *hostsets.HostSet
hSet, apiErr, err = hSetClient.Create(tc.Context(), hc.Id)
require.NoError(err)
require.Nil(apiErr)
if newStyle {
hSet, apiErr, err = hSetClient.AddHosts2(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id, h2.Id})
} else {
hSet, apiErr, err = hSetClient.AddHosts(tc.Context(), hc.Id, hSet.Id, hSet.Version, []string{h1.Id, h2.Id})
}
require.NoError(err)
require.Nil(apiErr)
assert.Contains(hSet.HostIds, h1.Id, h2.Id)
hSet, apiErr, err = hSetClient.AddHosts(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id, h2.Id})
require.NoError(err)
require.Nil(apiErr)
assert.Contains(hSet.HostIds, h1.Id, h2.Id)
if newStyle {
hSet, apiErr, err = hSetClient.SetHosts2(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id})
} else {
hSet, apiErr, err = hSetClient.SetHosts(tc.Context(), hc.Id, hSet.Id, hSet.Version, []string{h1.Id})
}
require.NoError(err)
require.Nil(apiErr, pretty.Sprint(apiErr))
assert.ElementsMatch([]string{h1.Id}, hSet.HostIds)
hSet, apiErr, err = hSetClient.SetHosts(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id})
require.NoError(err)
require.Nil(apiErr, pretty.Sprint(apiErr))
assert.ElementsMatch([]string{h1.Id}, hSet.HostIds)
if newStyle {
hSet, apiErr, err = hSetClient.RemoveHosts2(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id})
} else {
hSet, apiErr, err = hSetClient.RemoveHosts(tc.Context(), hc.Id, hSet.Id, hSet.Version, []string{h1.Id})
}
require.NoError(err)
require.Nil(apiErr)
assert.Empty(hSet.HostIds)
})
}
hSet, apiErr, err = hSetClient.RemoveHosts(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id})
require.NoError(err)
require.Nil(apiErr)
assert.Empty(hSet.HostIds)
}
func TestSet_List(t *testing.T) {
for _, newStyle := range []bool{false, true} {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
func TestList(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
client := tc.Client()
token := tc.Token()
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
client.SetScopeId(proj.GetPublicId())
client := tc.Client()
token := tc.Token()
client.SetToken(token.Token)
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
hc, apiErr, err := hostcatalogs.NewClient(client).Create(tc.Context(), "static")
require.NoError(err)
require.Nil(apiErr)
require.NotNil(hc)
hc, apiErr, err := hostcatalogs.NewClient(client).Create(tc.Context(), "static", proj.GetPublicId())
require.NoError(err)
require.Nil(apiErr)
require.NotNil(hc)
hClient := hostsets.NewClient(client)
var ul []*hostsets.HostSet
hClient := hostsets.NewClient(client)
var ul []*hostsets.HostSet
if newStyle {
ul, apiErr, err = hClient.List2(tc.Context(), hc.Id)
} else {
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
}
require.NoError(err)
require.Nil(apiErr)
assert.Empty(ul)
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
require.NoError(err)
require.Nil(apiErr)
assert.Empty(ul)
var expected []*hostsets.HostSet
for i := 0; i < 10; i++ {
expected = append(expected, &hostsets.HostSet{Name: fmt.Sprint(i)})
}
expected[0], apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName(expected[0].Name))
require.NoError(err)
require.Nil(apiErr)
if newStyle {
ul, apiErr, err = hClient.List2(tc.Context(), hc.Id)
} else {
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
}
require.NoError(err)
require.Nil(apiErr)
assert.ElementsMatch(comparableSetSlice(expected[:1]), comparableSetSlice(ul))
for i := 1; i < 10; i++ {
expected[i], apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName(expected[i].Name))
require.NoError(err)
require.Nil(apiErr)
}
if newStyle {
ul, apiErr, err = hClient.List2(tc.Context(), hc.Id)
} else {
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
}
require.NoError(err)
require.Nil(apiErr)
assert.ElementsMatch(comparableSetSlice(expected), comparableSetSlice(ul))
var expected []*hostsets.HostSet
for i := 0; i < 10; i++ {
expected = append(expected, &hostsets.HostSet{Name: fmt.Sprint(i)})
}
expected[0], apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName(expected[0].Name))
require.NoError(err)
require.Nil(apiErr)
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
require.NoError(err)
require.Nil(apiErr)
assert.ElementsMatch(comparableSetSlice(expected[:1]), comparableSetSlice(ul))
for i := 1; i < 10; i++ {
expected[i], apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName(expected[i].Name))
require.NoError(err)
require.Nil(apiErr)
}
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
require.NoError(err)
require.Nil(apiErr)
assert.ElementsMatch(comparableSetSlice(expected), comparableSetSlice(ul))
}
func comparableSetSlice(in []*hostsets.HostSet) []hostsets.HostSet {
@@ -158,146 +124,97 @@ func comparableSetSlice(in []*hostsets.HostSet) []hostsets.HostSet {
return filtered
}
func TestSet_Crud(t *testing.T) {
for _, newStyle := range []bool{false, true} {
t.Run(fmt.Sprintf("crud_%t", newStyle), func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
func TestCrud(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
client := tc.Client()
token := tc.Token()
org, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
client.SetScopeId(org.GetPublicId())
projClient := client.Clone()
projClient.SetScopeId(proj.GetPublicId())
client := tc.Client()
token := tc.Token()
client.SetToken(token.Token)
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
hc, apiErr, err := hostcatalogs.NewClient(projClient).Create(tc.Context(), "static")
require.NoError(err)
require.Nil(apiErr)
require.NotNil(hc)
hc, apiErr, err := hostcatalogs.NewClient(client).Create(tc.Context(), "static", proj.GetPublicId())
require.NoError(err)
require.Nil(apiErr)
require.NotNil(hc)
checkHost := func(t *testing.T, step string, h *hostsets.HostSet, apiErr *api.Error, err error, wantedName string, wantVersion uint32) {
t.Helper()
require.NoError(err, step)
if !assert.Nil(apiErr, step) && apiErr.Message != "" {
t.Errorf("ApiError message: %q", apiErr.Message)
}
assert.NotNil(h, "returned no resource", step)
gotName := ""
if h.Name != "" {
gotName = h.Name
}
assert.Equal(wantedName, gotName, step)
assert.Equal(wantVersion, h.Version)
}
hClient := hostsets.NewClient(projClient)
var h *hostsets.HostSet
if newStyle {
h, apiErr, err = hClient.Create2(tc.Context(), hc.Id, hostsets.WithName("foo"))
} else {
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
}
checkHost(t, "create", h, apiErr, err, "foo", 1)
if newStyle {
h, apiErr, err = hClient.Read2(tc.Context(), h.Id)
} else {
h, apiErr, err = hClient.Read(tc.Context(), hc.Id, h.Id)
}
checkHost(t, "read", h, apiErr, err, "foo", 1)
if newStyle {
h, apiErr, err = hClient.Update2(tc.Context(), h.Id, h.Version, hostsets.WithName("bar"))
} else {
h, apiErr, err = hClient.Update(tc.Context(), hc.Id, h.Id, h.Version, hostsets.WithName("bar"))
}
checkHost(t, "update", h, apiErr, err, "bar", 2)
if newStyle {
h, apiErr, err = hClient.Update2(tc.Context(), h.Id, h.Version, hostsets.DefaultName())
} else {
h, apiErr, err = hClient.Update(tc.Context(), hc.Id, h.Id, h.Version, hostsets.DefaultName())
}
checkHost(t, "update", h, apiErr, err, "", 3)
var existed bool
if newStyle {
existed, apiErr, err = hClient.Delete2(tc.Context(), h.Id)
} else {
existed, apiErr, err = hClient.Delete(tc.Context(), hc.Id, h.Id)
}
assert.NoError(err)
assert.True(existed, "Expected existing catalog when deleted, but it wasn't.")
if newStyle {
existed, apiErr, err = hClient.Delete2(tc.Context(), h.Id)
} else {
existed, apiErr, err = hClient.Delete(tc.Context(), hc.Id, h.Id)
}
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
})
checkHost := func(t *testing.T, step string, h *hostsets.HostSet, apiErr *api.Error, err error, wantedName string, wantVersion uint32) {
t.Helper()
require.NoError(err, step)
if !assert.Nil(apiErr, step) && apiErr.Message != "" {
t.Errorf("ApiError message: %q", apiErr.Message)
}
assert.NotNil(h, "returned no resource", step)
gotName := ""
if h.Name != "" {
gotName = h.Name
}
assert.Equal(wantedName, gotName, step)
assert.Equal(wantVersion, h.Version)
}
hClient := hostsets.NewClient(client)
var h *hostsets.HostSet
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
checkHost(t, "create", h, apiErr, err, "foo", 1)
h, apiErr, err = hClient.Read(tc.Context(), h.Id)
checkHost(t, "read", h, apiErr, err, "foo", 1)
h, apiErr, err = hClient.Update(tc.Context(), h.Id, h.Version, hostsets.WithName("bar"))
checkHost(t, "update", h, apiErr, err, "bar", 2)
h, apiErr, err = hClient.Update(tc.Context(), h.Id, h.Version, hostsets.DefaultName())
checkHost(t, "update", h, apiErr, err, "", 3)
var existed bool
existed, apiErr, err = hClient.Delete(tc.Context(), h.Id)
assert.NoError(err)
assert.True(existed, "Expected existing catalog when deleted, but it wasn't.")
existed, apiErr, err = hClient.Delete(tc.Context(), h.Id)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
}
// TODO: Get better coverage for expected errors and error formats.
func TestSet_Errors(t *testing.T) {
for _, newStyle := range []bool{false, true} {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
func TestErrors(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
defer tc.Shutdown()
client := tc.Client()
token := tc.Token()
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
client.SetScopeId(proj.GetPublicId())
client := tc.Client()
token := tc.Token()
client.SetToken(token.Token)
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
hc, apiErr, err := hostcatalogs.NewClient(client).Create(tc.Context(), "static")
require.NoError(err)
require.Nil(apiErr)
require.NotNil(hc)
hc, apiErr, err := hostcatalogs.NewClient(client).Create(tc.Context(), "static", proj.GetPublicId())
require.NoError(err)
require.Nil(apiErr)
require.NotNil(hc)
hClient := hostsets.NewClient(client)
hClient := hostsets.NewClient(client)
var h *hostsets.HostSet
if newStyle {
h, apiErr, err = hClient.Create2(tc.Context(), hc.Id, hostsets.WithName("foo"))
} else {
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
}
require.NoError(err)
require.Nil(apiErr)
assert.NotNil(h)
var h *hostsets.HostSet
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
require.NoError(err)
require.Nil(apiErr)
assert.NotNil(h)
if newStyle {
h, apiErr, err = hClient.Create2(tc.Context(), hc.Id, hostsets.WithName("foo"))
} else {
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
}
require.NoError(err)
assert.NotNil(apiErr)
assert.Nil(h)
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
require.NoError(err)
assert.NotNil(apiErr)
assert.Nil(h)
if newStyle {
_, apiErr, err = hClient.Read2(tc.Context(), static.HostSetPrefix+"_doesntexis")
} else {
_, apiErr, err = hClient.Read(tc.Context(), hc.Id, static.HostSetPrefix+"_doesntexis")
}
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
_, apiErr, err = hClient.Read(tc.Context(), static.HostSetPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
if newStyle {
_, apiErr, err = hClient.Read2(tc.Context(), "invalid id")
} else {
_, apiErr, err = hClient.Read(tc.Context(), hc.Id, "invalid id")
}
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
}
_, apiErr, err = hClient.Read(tc.Context(), "invalid id")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
}
+2 -12
View File
@@ -16,7 +16,6 @@ type Option func(*options)
type options struct {
postMap map[string]interface{}
queryMap map[string]string
withScopeId string
withAutomaticVersioning bool
}
@@ -33,25 +32,16 @@ func getOpts(opt ...Option) (options, []api.Option) {
o(&opts)
}
var apiOpts []api.Option
if opts.withScopeId != "" {
apiOpts = append(apiOpts, api.WithScopeId(opts.withScopeId))
}
return opts, apiOpts
}
func WithScopeId(id string) Option {
return func(o *options) {
o.withScopeId = id
}
}
// If set, and if the version is zero during an update, the API will perform a
// fetch to get the current version of the resource and populate it during the
// update call. This is convenient but opens up the possibility for subtle
// order-of-modification issues, so use carefully.
func WithAutomaticVersioning() Option {
func WithAutomaticVersioning(enable bool) Option {
return func(o *options) {
o.withAutomaticVersioning = true
o.withAutomaticVersioning = enable
}
}