SDK error revamp (#432)
SDK methods now return 1 error. To differentiate between Client errors and Server errors I've also implemented the AsServerError function which returns an *api.Error if it is an error originating from the remote server or nil otherwise.
This commit is contained in:
1 parent
94f9d952be
commit
fd0da998e8
53 files changed
+1512
-1635
No files matched your search
+148
-150
@@ -9,8 +9,6 @@ import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/kr/pretty"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/scopes"
|
||||
)
|
||||
@@ -112,22 +110,22 @@ func (c *Client) ApiClient() *api.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*RoleCreateResult, *api.Error, error) {
|
||||
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*RoleCreateResult, error) {
|
||||
if scopeId == "" {
|
||||
return nil, nil, fmt.Errorf("empty scopeId value passed into Create request")
|
||||
return nil, fmt.Errorf("empty scopeId value passed into Create request")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
return nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts.postMap["scope_id"] = scopeId
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", "roles", opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
|
||||
return nil, fmt.Errorf("error creating Create request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -140,36 +138,36 @@ func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*Ro
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during Create call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleCreateResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Create response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding Create response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) Read(ctx context.Context, roleId string, opt ...Option) (*RoleReadResult, *api.Error, error) {
|
||||
func (c *Client) Read(ctx context.Context, roleId string, opt ...Option) (*RoleReadResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into Read request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into Read request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
return nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("roles/%s", roleId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
|
||||
return nil, fmt.Errorf("error creating Read request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -182,49 +180,49 @@ func (c *Client) Read(ctx context.Context, roleId string, opt ...Option) (*RoleR
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during Read call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleReadResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding Read response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) Update(ctx context.Context, roleId string, version uint32, opt ...Option) (*RoleUpdateResult, *api.Error, error) {
|
||||
func (c *Client) Update(ctx context.Context, roleId string, version uint32, opt ...Option) (*RoleUpdateResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into Update request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into Update request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
return 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")
|
||||
return nil, errors.New("zero version number passed into Update request and automatic versioning not specified")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, roleId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, roleId, 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 api.AsServerError(existingErr) != nil {
|
||||
return nil, fmt.Errorf("error from controller when performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
return nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
if existingTarget == nil {
|
||||
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
}
|
||||
if existingTarget.Item == nil {
|
||||
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
}
|
||||
version = existingTarget.Item.Version
|
||||
}
|
||||
@@ -233,7 +231,7 @@ func (c *Client) Update(ctx context.Context, roleId string, version uint32, opt
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("roles/%s", roleId), opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
|
||||
return nil, fmt.Errorf("error creating Update request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -246,36 +244,36 @@ func (c *Client) Update(ctx context.Context, roleId string, version uint32, opt
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during Update call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleUpdateResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding Update response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) Delete(ctx context.Context, roleId string, opt ...Option) (*RoleDeleteResult, *api.Error, error) {
|
||||
func (c *Client) Delete(ctx context.Context, roleId string, opt ...Option) (*RoleDeleteResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into Delete request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into Delete request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
return nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("roles/%s", roleId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Delete request: %w", err)
|
||||
return nil, fmt.Errorf("error creating Delete request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -288,30 +286,30 @@ func (c *Client) Delete(ctx context.Context, roleId string, opt ...Option) (*Rol
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during Delete call: %w", err)
|
||||
}
|
||||
|
||||
apiErr, err := resp.Decode(nil)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Delete response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding Delete response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
|
||||
target := &RoleDeleteResult{
|
||||
responseBody: resp.Body,
|
||||
responseMap: resp.Map,
|
||||
}
|
||||
return target, nil, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*RoleListResult, *api.Error, error) {
|
||||
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*RoleListResult, error) {
|
||||
if scopeId == "" {
|
||||
return nil, nil, fmt.Errorf("empty scopeId value passed into List request")
|
||||
return nil, fmt.Errorf("empty scopeId value passed into List request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
return nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
@@ -319,7 +317,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*Role
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", "roles", nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating List request: %w", err)
|
||||
return nil, fmt.Errorf("error creating List request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -332,51 +330,51 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*Role
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during List call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleListResult)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding List response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) AddGrants(ctx context.Context, roleId string, version uint32, grantStrings []string, opt ...Option) (*RoleUpdateResult, *api.Error, error) {
|
||||
func (c *Client) AddGrants(ctx context.Context, roleId string, version uint32, grantStrings []string, opt ...Option) (*RoleUpdateResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into AddGrants request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into AddGrants request")
|
||||
}
|
||||
if len(grantStrings) == 0 {
|
||||
return nil, nil, errors.New("empty grantStrings passed into AddGrants request")
|
||||
return nil, errors.New("empty grantStrings passed into AddGrants request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, errors.New("nil client")
|
||||
return nil, errors.New("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
if version == 0 {
|
||||
if !opts.withAutomaticVersioning {
|
||||
return nil, nil, errors.New("zero version number passed into AddGrants request")
|
||||
return nil, errors.New("zero version number passed into AddGrants request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, roleId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, roleId, 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 api.AsServerError(existingErr) != nil {
|
||||
return nil, fmt.Errorf("error from controller when performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
return nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
if existingTarget == nil {
|
||||
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
}
|
||||
if existingTarget.Item == nil {
|
||||
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
}
|
||||
version = existingTarget.Item.Version
|
||||
}
|
||||
@@ -386,7 +384,7 @@ func (c *Client) AddGrants(ctx context.Context, roleId string, version uint32, g
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:add-grants", roleId), opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating AddGrants request: %w", err)
|
||||
return nil, fmt.Errorf("error creating AddGrants request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -399,52 +397,52 @@ func (c *Client) AddGrants(ctx context.Context, roleId string, version uint32, g
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during AddGrants call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during AddGrants call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleUpdateResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding AddGrants response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding AddGrants response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) AddPrincipals(ctx context.Context, roleId string, version uint32, principalIds []string, opt ...Option) (*RoleUpdateResult, *api.Error, error) {
|
||||
func (c *Client) AddPrincipals(ctx context.Context, roleId string, version uint32, principalIds []string, opt ...Option) (*RoleUpdateResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into AddPrincipals request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into AddPrincipals request")
|
||||
}
|
||||
if len(principalIds) == 0 {
|
||||
return nil, nil, errors.New("empty principalIds passed into AddPrincipals request")
|
||||
return nil, errors.New("empty principalIds passed into AddPrincipals request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, errors.New("nil client")
|
||||
return nil, errors.New("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
if version == 0 {
|
||||
if !opts.withAutomaticVersioning {
|
||||
return nil, nil, errors.New("zero version number passed into AddPrincipals request")
|
||||
return nil, errors.New("zero version number passed into AddPrincipals request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, roleId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, roleId, 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 api.AsServerError(existingErr) != nil {
|
||||
return nil, fmt.Errorf("error from controller when performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
return nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
if existingTarget == nil {
|
||||
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
}
|
||||
if existingTarget.Item == nil {
|
||||
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
}
|
||||
version = existingTarget.Item.Version
|
||||
}
|
||||
@@ -454,7 +452,7 @@ func (c *Client) AddPrincipals(ctx context.Context, roleId string, version uint3
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:add-principals", roleId), opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating AddPrincipals request: %w", err)
|
||||
return nil, fmt.Errorf("error creating AddPrincipals request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -467,50 +465,50 @@ func (c *Client) AddPrincipals(ctx context.Context, roleId string, version uint3
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during AddPrincipals call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during AddPrincipals call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleUpdateResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding AddPrincipals response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding AddPrincipals response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) SetGrants(ctx context.Context, roleId string, version uint32, grantStrings []string, opt ...Option) (*RoleUpdateResult, *api.Error, error) {
|
||||
func (c *Client) SetGrants(ctx context.Context, roleId string, version uint32, grantStrings []string, opt ...Option) (*RoleUpdateResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into SetGrants request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into SetGrants request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, errors.New("nil client")
|
||||
return nil, errors.New("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
if version == 0 {
|
||||
if !opts.withAutomaticVersioning {
|
||||
return nil, nil, errors.New("zero version number passed into SetGrants request")
|
||||
return nil, errors.New("zero version number passed into SetGrants request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, roleId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, roleId, 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 api.AsServerError(existingErr) != nil {
|
||||
return nil, fmt.Errorf("error from controller when performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
return nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
if existingTarget == nil {
|
||||
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
}
|
||||
if existingTarget.Item == nil {
|
||||
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
}
|
||||
version = existingTarget.Item.Version
|
||||
}
|
||||
@@ -520,7 +518,7 @@ func (c *Client) SetGrants(ctx context.Context, roleId string, version uint32, g
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:set-grants", roleId), opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating SetGrants request: %w", err)
|
||||
return nil, fmt.Errorf("error creating SetGrants request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -533,50 +531,50 @@ func (c *Client) SetGrants(ctx context.Context, roleId string, version uint32, g
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during SetGrants call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during SetGrants call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleUpdateResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding SetGrants response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding SetGrants response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) SetPrincipals(ctx context.Context, roleId string, version uint32, principalIds []string, opt ...Option) (*RoleUpdateResult, *api.Error, error) {
|
||||
func (c *Client) SetPrincipals(ctx context.Context, roleId string, version uint32, principalIds []string, opt ...Option) (*RoleUpdateResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into SetPrincipals request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into SetPrincipals request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, errors.New("nil client")
|
||||
return nil, errors.New("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
if version == 0 {
|
||||
if !opts.withAutomaticVersioning {
|
||||
return nil, nil, errors.New("zero version number passed into SetPrincipals request")
|
||||
return nil, errors.New("zero version number passed into SetPrincipals request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, roleId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, roleId, 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 api.AsServerError(existingErr) != nil {
|
||||
return nil, fmt.Errorf("error from controller when performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
return nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
if existingTarget == nil {
|
||||
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
}
|
||||
if existingTarget.Item == nil {
|
||||
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
}
|
||||
version = existingTarget.Item.Version
|
||||
}
|
||||
@@ -586,7 +584,7 @@ func (c *Client) SetPrincipals(ctx context.Context, roleId string, version uint3
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:set-principals", roleId), opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating SetPrincipals request: %w", err)
|
||||
return nil, fmt.Errorf("error creating SetPrincipals request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -599,52 +597,52 @@ func (c *Client) SetPrincipals(ctx context.Context, roleId string, version uint3
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during SetPrincipals call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during SetPrincipals call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleUpdateResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding SetPrincipals response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding SetPrincipals response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) RemoveGrants(ctx context.Context, roleId string, version uint32, grantStrings []string, opt ...Option) (*RoleUpdateResult, *api.Error, error) {
|
||||
func (c *Client) RemoveGrants(ctx context.Context, roleId string, version uint32, grantStrings []string, opt ...Option) (*RoleUpdateResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into RemoveGrants request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into RemoveGrants request")
|
||||
}
|
||||
if len(grantStrings) == 0 {
|
||||
return nil, nil, errors.New("empty grantStrings passed into RemoveGrants request")
|
||||
return nil, errors.New("empty grantStrings passed into RemoveGrants request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, errors.New("nil client")
|
||||
return nil, errors.New("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
if version == 0 {
|
||||
if !opts.withAutomaticVersioning {
|
||||
return nil, nil, errors.New("zero version number passed into RemoveGrants request")
|
||||
return nil, errors.New("zero version number passed into RemoveGrants request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, roleId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, roleId, 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 api.AsServerError(existingErr) != nil {
|
||||
return nil, fmt.Errorf("error from controller when performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
return nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
if existingTarget == nil {
|
||||
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
}
|
||||
if existingTarget.Item == nil {
|
||||
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
}
|
||||
version = existingTarget.Item.Version
|
||||
}
|
||||
@@ -654,7 +652,7 @@ func (c *Client) RemoveGrants(ctx context.Context, roleId string, version uint32
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:remove-grants", roleId), opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating RemoveGrants request: %w", err)
|
||||
return nil, fmt.Errorf("error creating RemoveGrants request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -667,52 +665,52 @@ func (c *Client) RemoveGrants(ctx context.Context, roleId string, version uint32
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during RemoveGrants call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during RemoveGrants call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleUpdateResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding RemoveGrants response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding RemoveGrants response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) RemovePrincipals(ctx context.Context, roleId string, version uint32, principalIds []string, opt ...Option) (*RoleUpdateResult, *api.Error, error) {
|
||||
func (c *Client) RemovePrincipals(ctx context.Context, roleId string, version uint32, principalIds []string, opt ...Option) (*RoleUpdateResult, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into RemovePrincipals request")
|
||||
return nil, fmt.Errorf("empty roleId value passed into RemovePrincipals request")
|
||||
}
|
||||
if len(principalIds) == 0 {
|
||||
return nil, nil, errors.New("empty principalIds passed into RemovePrincipals request")
|
||||
return nil, errors.New("empty principalIds passed into RemovePrincipals request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, errors.New("nil client")
|
||||
return nil, errors.New("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
if version == 0 {
|
||||
if !opts.withAutomaticVersioning {
|
||||
return nil, nil, errors.New("zero version number passed into RemovePrincipals request")
|
||||
return nil, errors.New("zero version number passed into RemovePrincipals request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, roleId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, roleId, 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 api.AsServerError(existingErr) != nil {
|
||||
return nil, fmt.Errorf("error from controller when performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
return nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
|
||||
}
|
||||
if existingTarget == nil {
|
||||
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource response found when performing initial check-and-set read")
|
||||
}
|
||||
if existingTarget.Item == nil {
|
||||
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
return nil, errors.New("nil resource found when performing initial check-and-set read")
|
||||
}
|
||||
version = existingTarget.Item.Version
|
||||
}
|
||||
@@ -722,7 +720,7 @@ func (c *Client) RemovePrincipals(ctx context.Context, roleId string, version ui
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:remove-principals", roleId), opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating RemovePrincipals request: %w", err)
|
||||
return nil, fmt.Errorf("error creating RemovePrincipals request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
@@ -735,19 +733,19 @@ func (c *Client) RemovePrincipals(ctx context.Context, roleId string, version ui
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during RemovePrincipals call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during RemovePrincipals call: %w", err)
|
||||
}
|
||||
|
||||
target := new(RoleUpdateResult)
|
||||
target.Item = new(Role)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding RemovePrincipals response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding RemovePrincipals response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr, nil
|
||||
return nil, apiErr
|
||||
}
|
||||
target.responseBody = resp.Body
|
||||
target.responseMap = resp.Map
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
+41
-56
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/hashicorp/boundary/api/users"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller"
|
||||
"github.com/kr/pretty"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -40,9 +39,8 @@ func TestCustom(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
user, apiErr, err := users.NewClient(client).Create(tc.Context(), org.GetPublicId())
|
||||
user, err := users.NewClient(client).Create(tc.Context(), org.GetPublicId())
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
|
||||
hasPrincipal := func(role *roles.Role, principalId string) bool {
|
||||
var foundInPrincipals bool
|
||||
@@ -62,59 +60,51 @@ func TestCustom(t *testing.T) {
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
g, apiErr, err := groups.NewClient(client).Create(tc.Context(), tt.scopeId)
|
||||
g, err := groups.NewClient(client).Create(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(g)
|
||||
|
||||
rc := roles.NewClient(client)
|
||||
var version uint32 = 1
|
||||
|
||||
r, apiErr, err := rc.Create(tc.Context(), tt.scopeId, roles.WithName("foo"))
|
||||
r, err := rc.Create(tc.Context(), tt.scopeId, roles.WithName("foo"))
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(r)
|
||||
require.EqualValues(r.Item.Version, version)
|
||||
version++
|
||||
|
||||
updatedRole, apiErr, err := rc.AddPrincipals(tc.Context(), r.Item.Id, r.Item.Version, []string{g.Item.Id})
|
||||
updatedRole, err := rc.AddPrincipals(tc.Context(), r.Item.Id, r.Item.Version, []string{g.Item.Id})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", pretty.Sprint(apiErr))
|
||||
assert.EqualValues(updatedRole.Item.Version, version)
|
||||
assert.True(hasPrincipal(updatedRole.Item, g.Item.Id))
|
||||
version++
|
||||
|
||||
updatedRole, apiErr, err = rc.SetPrincipals(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{user.Item.Id})
|
||||
updatedRole, err = rc.SetPrincipals(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{user.Item.Id})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", apiErr)
|
||||
assert.EqualValues(updatedRole.Item.Version, version)
|
||||
assert.True(hasPrincipal(updatedRole.Item, user.Item.Id))
|
||||
version++
|
||||
|
||||
updatedRole, apiErr, err = rc.RemovePrincipals(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{user.Item.Id})
|
||||
updatedRole, err = rc.RemovePrincipals(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{user.Item.Id})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", apiErr)
|
||||
assert.EqualValues(updatedRole.Item.Version, version)
|
||||
assert.Empty(updatedRole.Item.Principals)
|
||||
version++
|
||||
|
||||
updatedRole, apiErr, err = rc.AddGrants(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{"id=*;actions=read"})
|
||||
updatedRole, err = rc.AddGrants(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{"id=*;actions=read"})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", apiErr)
|
||||
assert.EqualValues(updatedRole.Item.Version, version)
|
||||
assert.Contains(updatedRole.Item.GrantStrings, "id=*;actions=read")
|
||||
version++
|
||||
|
||||
updatedRole, apiErr, err = rc.SetGrants(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{"id=*;actions=*"})
|
||||
updatedRole, err = rc.SetGrants(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{"id=*;actions=*"})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", apiErr)
|
||||
assert.EqualValues(updatedRole.Item.Version, version)
|
||||
assert.Contains(updatedRole.Item.GrantStrings, "id=*;actions=*")
|
||||
version++
|
||||
|
||||
updatedRole, apiErr, err = rc.RemoveGrants(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{"id=*;actions=*"})
|
||||
updatedRole, err = rc.RemoveGrants(tc.Context(), updatedRole.Item.Id, updatedRole.Item.Version, []string{"id=*;actions=*"})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", apiErr)
|
||||
assert.EqualValues(updatedRole.Item.Version, version)
|
||||
assert.Empty(updatedRole.Item.Grants)
|
||||
version++
|
||||
@@ -151,9 +141,8 @@ func TestList(t *testing.T) {
|
||||
var expected []*roles.Role
|
||||
|
||||
roleClient := roles.NewClient(client)
|
||||
p1, apiErr, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
p1, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
require.Len(p1.Items, 1)
|
||||
expected = append(expected, p1.Items[0])
|
||||
|
||||
@@ -161,25 +150,21 @@ func TestList(t *testing.T) {
|
||||
expected = append(expected, &roles.Role{Name: fmt.Sprint(i)})
|
||||
}
|
||||
|
||||
rcr, apiErr, err := roleClient.Create(tc.Context(), tt.scopeId, roles.WithName(expected[1].Name))
|
||||
rcr, err := roleClient.Create(tc.Context(), tt.scopeId, roles.WithName(expected[1].Name))
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
expected[1] = rcr.Item
|
||||
|
||||
p2, apiErr, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
p2, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableSlice(expected[0:2]), comparableSlice(p2.Items))
|
||||
|
||||
for i := 2; i < 11; i++ {
|
||||
rcr, apiErr, err = roleClient.Create(tc.Context(), tt.scopeId, roles.WithName(expected[i].Name))
|
||||
rcr, err = roleClient.Create(tc.Context(), tt.scopeId, roles.WithName(expected[i].Name))
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
expected[i] = rcr.Item
|
||||
}
|
||||
p3, apiErr, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
p3, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableSlice(expected), comparableSlice(p3.Items))
|
||||
})
|
||||
}
|
||||
@@ -224,11 +209,8 @@ func TestCrud(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
checkRole := func(step string, g *roles.Role, apiErr *api.Error, err error, wantedName string, wantedVersion uint32) {
|
||||
checkRole := func(step string, g *roles.Role, err error, wantedName string, wantedVersion uint32) {
|
||||
assert.NoError(err, step)
|
||||
if !assert.Nil(apiErr, step) && apiErr.Message != "" {
|
||||
t.Errorf("ApiError message: %q", apiErr.Message)
|
||||
}
|
||||
assert.NotNil(g, "returned no resource", step)
|
||||
gotName := ""
|
||||
if g.Name != "" {
|
||||
@@ -241,27 +223,25 @@ func TestCrud(t *testing.T) {
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
roleClient := roles.NewClient(client)
|
||||
g, apiErr, err := roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("foo"))
|
||||
checkRole("create", g.Item, apiErr, err, "foo", 1)
|
||||
g, err := roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("foo"))
|
||||
checkRole("create", g.Item, err, "foo", 1)
|
||||
|
||||
g, apiErr, err = roleClient.Read(tc.Context(), g.Item.Id)
|
||||
checkRole("read", g.Item, apiErr, err, "foo", 1)
|
||||
g, err = roleClient.Read(tc.Context(), g.Item.Id)
|
||||
checkRole("read", g.Item, err, "foo", 1)
|
||||
|
||||
g, apiErr, err = roleClient.Update(tc.Context(), g.Item.Id, g.Item.Version, roles.WithName("bar"))
|
||||
checkRole("update", g.Item, apiErr, err, "bar", 2)
|
||||
g, err = roleClient.Update(tc.Context(), g.Item.Id, g.Item.Version, roles.WithName("bar"))
|
||||
checkRole("update", g.Item, err, "bar", 2)
|
||||
|
||||
g, apiErr, err = roleClient.Update(tc.Context(), g.Item.Id, g.Item.Version, roles.DefaultName())
|
||||
checkRole("update", g.Item, apiErr, err, "", 3)
|
||||
g, err = roleClient.Update(tc.Context(), g.Item.Id, g.Item.Version, roles.DefaultName())
|
||||
checkRole("update", g.Item, err, "", 3)
|
||||
|
||||
_, apiErr, err = roleClient.Delete(tc.Context(), g.Item.Id)
|
||||
_, err = roleClient.Delete(tc.Context(), g.Item.Id)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
|
||||
@@ -286,35 +266,40 @@ func TestErrors(t *testing.T) {
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
roleClient := roles.NewClient(client)
|
||||
u, apiErr, err := roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("first"))
|
||||
u, err := roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("first"))
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.NotNil(u)
|
||||
|
||||
// Updating the wrong version should fail.
|
||||
_, apiErr, err = roleClient.Update(tc.Context(), u.Item.Id, 73, roles.WithName("anything"))
|
||||
require.NoError(err)
|
||||
_, err = roleClient.Update(tc.Context(), u.Item.Id, 73, roles.WithName("anything"))
|
||||
require.Error(err)
|
||||
apiErr := api.AsServerError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusNotFound, apiErr.Status)
|
||||
|
||||
// Create another resource with the same name.
|
||||
_, apiErr, err = roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("first"))
|
||||
require.NoError(err)
|
||||
_, err = roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("first"))
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
assert.NotNil(apiErr)
|
||||
|
||||
_, apiErr, err = roleClient.Read(tc.Context(), iam.RolePrefix+"_doesntexis")
|
||||
require.NoError(err)
|
||||
_, err = roleClient.Read(tc.Context(), iam.RolePrefix+"_doesntexis")
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusNotFound, apiErr.Status)
|
||||
|
||||
_, apiErr, err = roleClient.Read(tc.Context(), "invalid id")
|
||||
require.NoError(err)
|
||||
_, err = roleClient.Read(tc.Context(), "invalid id")
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
|
||||
|
||||
_, apiErr, err = roleClient.Update(tc.Context(), u.Item.Id, u.Item.Version)
|
||||
require.NoError(err)
|
||||
_, err = roleClient.Update(tc.Context(), u.Item.Id, u.Item.Version)
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user