Move update versioning to query parameter, and fix up scope API tests (#244)

* Move update versioning to query parameter, and fix up scope API tests

* Add version checks for update requests in other services
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-07 17:56:55 -04:00
1 parent 0d0660b061
commit bbd200101f
16 files changed
+240 -102

No files matched your search

+31 -9
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"time"
"github.com/kr/pretty"
@@ -44,7 +45,7 @@ func (c *rolesClient) Create(ctx context.Context, opt ...Option) (*Role, *api.Er
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("roles"), opts.valueMap, apiOpts...)
req, err := c.client.NewRequest(ctx, "POST", "roles", opts.valueMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
}
@@ -119,13 +120,16 @@ func (c *rolesClient) Update(ctx context.Context, roleId string, version uint32,
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("roles/%s", roleId), opts.valueMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
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)
@@ -180,7 +184,7 @@ func (c *rolesClient) List(ctx context.Context, opt ...Option) ([]*Role, *api.Er
_, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("roles"), nil, apiOpts...)
req, err := c.client.NewRequest(ctx, "GET", "roles", nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating List request: %w", err)
}
@@ -228,7 +232,6 @@ func (c *rolesClient) AddGrants(ctx context.Context, roleId string, version uint
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
if len(grantStrings) > 0 {
opts.valueMap["grant_strings"] = grantStrings
@@ -239,6 +242,10 @@ func (c *rolesClient) AddGrants(ctx context.Context, roleId string, version uint
return nil, nil, fmt.Errorf("error creating AddGrants request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during AddGrants call: %w", err)
@@ -279,7 +286,6 @@ func (c *rolesClient) AddPrincipals(ctx context.Context, roleId string, version
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
if len(principalIds) > 0 {
opts.valueMap["principal_ids"] = principalIds
@@ -290,6 +296,10 @@ func (c *rolesClient) AddPrincipals(ctx context.Context, roleId string, version
return nil, nil, fmt.Errorf("error creating AddPrincipals request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during AddPrincipals call: %w", err)
@@ -330,7 +340,6 @@ func (c *rolesClient) SetGrants(ctx context.Context, roleId string, version uint
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
if len(grantStrings) > 0 {
opts.valueMap["grant_strings"] = grantStrings
@@ -344,6 +353,10 @@ func (c *rolesClient) SetGrants(ctx context.Context, roleId string, version uint
return nil, nil, fmt.Errorf("error creating SetGrants request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during SetGrants call: %w", err)
@@ -384,7 +397,6 @@ func (c *rolesClient) SetPrincipals(ctx context.Context, roleId string, version
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
if len(principalIds) > 0 {
opts.valueMap["principal_ids"] = principalIds
@@ -398,6 +410,10 @@ func (c *rolesClient) SetPrincipals(ctx context.Context, roleId string, version
return nil, nil, fmt.Errorf("error creating SetPrincipals request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during SetPrincipals call: %w", err)
@@ -438,7 +454,6 @@ func (c *rolesClient) RemoveGrants(ctx context.Context, roleId string, version u
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
if len(grantStrings) > 0 {
opts.valueMap["grant_strings"] = grantStrings
@@ -449,6 +464,10 @@ func (c *rolesClient) RemoveGrants(ctx context.Context, roleId string, version u
return nil, nil, fmt.Errorf("error creating RemoveGrants request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during RemoveGrants call: %w", err)
@@ -489,7 +508,6 @@ func (c *rolesClient) RemovePrincipals(ctx context.Context, roleId string, versi
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
if len(principalIds) > 0 {
opts.valueMap["principal_ids"] = principalIds
@@ -500,6 +518,10 @@ func (c *rolesClient) RemovePrincipals(ctx context.Context, roleId string, versi
return nil, nil, fmt.Errorf("error creating RemovePrincipals request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during RemovePrincipals call: %w", err)