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
+48
-50
@@ -9,8 +9,6 @@ import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/kr/pretty"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/scopes"
|
||||
)
|
||||
@@ -109,22 +107,22 @@ func (c *Client) ApiClient() *api.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *Client) Create(ctx context.Context, authMethodId string, opt ...Option) (*AccountCreateResult, *api.Error, error) {
|
||||
func (c *Client) Create(ctx context.Context, authMethodId string, opt ...Option) (*AccountCreateResult, error) {
|
||||
if authMethodId == "" {
|
||||
return nil, nil, fmt.Errorf("empty authMethodId value passed into Create request")
|
||||
return nil, fmt.Errorf("empty authMethodId 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["auth_method_id"] = authMethodId
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", "accounts", 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 {
|
||||
@@ -137,36 +135,36 @@ func (c *Client) Create(ctx context.Context, authMethodId string, opt ...Option)
|
||||
|
||||
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(AccountCreateResult)
|
||||
target.Item = new(Account)
|
||||
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, accountId string, opt ...Option) (*AccountReadResult, *api.Error, error) {
|
||||
func (c *Client) Read(ctx context.Context, accountId string, opt ...Option) (*AccountReadResult, error) {
|
||||
if accountId == "" {
|
||||
return nil, nil, fmt.Errorf("empty accountId value passed into Read request")
|
||||
return nil, fmt.Errorf("empty accountId 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("accounts/%s", accountId), 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 {
|
||||
@@ -179,49 +177,49 @@ func (c *Client) Read(ctx context.Context, accountId string, opt ...Option) (*Ac
|
||||
|
||||
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(AccountReadResult)
|
||||
target.Item = new(Account)
|
||||
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, accountId string, version uint32, opt ...Option) (*AccountUpdateResult, *api.Error, error) {
|
||||
func (c *Client) Update(ctx context.Context, accountId string, version uint32, opt ...Option) (*AccountUpdateResult, error) {
|
||||
if accountId == "" {
|
||||
return nil, nil, fmt.Errorf("empty accountId value passed into Update request")
|
||||
return nil, fmt.Errorf("empty accountId 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, accountId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, accountId, 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
|
||||
}
|
||||
@@ -230,7 +228,7 @@ func (c *Client) Update(ctx context.Context, accountId string, version uint32, o
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("accounts/%s", accountId), 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 {
|
||||
@@ -243,36 +241,36 @@ func (c *Client) Update(ctx context.Context, accountId string, version uint32, o
|
||||
|
||||
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(AccountUpdateResult)
|
||||
target.Item = new(Account)
|
||||
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, accountId string, opt ...Option) (*AccountDeleteResult, *api.Error, error) {
|
||||
func (c *Client) Delete(ctx context.Context, accountId string, opt ...Option) (*AccountDeleteResult, error) {
|
||||
if accountId == "" {
|
||||
return nil, nil, fmt.Errorf("empty accountId value passed into Delete request")
|
||||
return nil, fmt.Errorf("empty accountId 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("accounts/%s", accountId), 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 {
|
||||
@@ -285,30 +283,30 @@ func (c *Client) Delete(ctx context.Context, accountId string, opt ...Option) (*
|
||||
|
||||
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 := &AccountDeleteResult{
|
||||
responseBody: resp.Body,
|
||||
responseMap: resp.Map,
|
||||
}
|
||||
return target, nil, nil
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (c *Client) List(ctx context.Context, authMethodId string, opt ...Option) (*AccountListResult, *api.Error, error) {
|
||||
func (c *Client) List(ctx context.Context, authMethodId string, opt ...Option) (*AccountListResult, error) {
|
||||
if authMethodId == "" {
|
||||
return nil, nil, fmt.Errorf("empty authMethodId value passed into List request")
|
||||
return nil, fmt.Errorf("empty authMethodId 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...)
|
||||
@@ -316,7 +314,7 @@ func (c *Client) List(ctx context.Context, authMethodId string, opt ...Option) (
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", "accounts", 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 {
|
||||
@@ -329,18 +327,18 @@ func (c *Client) List(ctx context.Context, authMethodId string, opt ...Option) (
|
||||
|
||||
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(AccountListResult)
|
||||
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
|
||||
}
|
||||
@@ -30,41 +30,35 @@ func TestList(t *testing.T) {
|
||||
client.SetToken(token.Token)
|
||||
org := iam.TestOrg(t, tc.IamRepo(), iam.WithUserId(token.UserId))
|
||||
amClient := authmethods.NewClient(client)
|
||||
amResult, apiErr, err := amClient.Create(tc.Context(), "password", org.GetPublicId())
|
||||
amResult, err := amClient.Create(tc.Context(), "password", org.GetPublicId())
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(amResult)
|
||||
am := amResult.Item
|
||||
|
||||
accountClient := accounts.NewClient(client)
|
||||
|
||||
lr, apiErr, err := accountClient.List(tc.Context(), am.Id)
|
||||
lr, err := accountClient.List(tc.Context(), am.Id)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
expected := lr.Items
|
||||
assert.Len(expected, 0)
|
||||
|
||||
expected = append(expected, &accounts.Account{Attributes: map[string]interface{}{"login_name": "loginname0"}})
|
||||
|
||||
cr, apiErr, err := accountClient.Create(tc.Context(), am.Id, accounts.WithPasswordAccountLoginName(expected[0].Attributes["login_name"].(string)))
|
||||
cr, err := accountClient.Create(tc.Context(), am.Id, accounts.WithPasswordAccountLoginName(expected[0].Attributes["login_name"].(string)))
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
expected[0] = cr.Item
|
||||
|
||||
ulResult, apiErr, err := accountClient.List(tc.Context(), am.Id)
|
||||
ulResult, err := accountClient.List(tc.Context(), am.Id)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableSlice(expected[:1]), comparableSlice(ulResult.Items))
|
||||
|
||||
for i := 1; i < 10; i++ {
|
||||
newAcctResult, apiErr, err := accountClient.Create(tc.Context(), am.Id, accounts.WithPasswordAccountLoginName(fmt.Sprintf("loginname%d", i)))
|
||||
newAcctResult, err := accountClient.Create(tc.Context(), am.Id, accounts.WithPasswordAccountLoginName(fmt.Sprintf("loginname%d", i)))
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
expected = append(expected, newAcctResult.Item)
|
||||
}
|
||||
ulResult, apiErr, err = accountClient.List(tc.Context(), am.Id)
|
||||
ulResult, err = accountClient.List(tc.Context(), am.Id)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableSlice(expected), comparableSlice(ulResult.Items))
|
||||
}
|
||||
|
||||
@@ -95,11 +89,8 @@ func TestCrud(t *testing.T) {
|
||||
amId := token.AuthMethodId
|
||||
accountClient := accounts.NewClient(client)
|
||||
|
||||
checkAccount := func(step string, u *accounts.Account, apiErr *api.Error, err error, wantedName string, wantedVersion uint32) {
|
||||
checkAccount := func(step string, u *accounts.Account, err error, wantedName string, wantedVersion uint32) {
|
||||
assert.NoError(err, step)
|
||||
if !assert.Nil(apiErr, step) && apiErr.Message != "" {
|
||||
t.Errorf("ApiError message: %q", apiErr.Message)
|
||||
}
|
||||
require.NotNil(u, "returned no resource", step)
|
||||
gotName := ""
|
||||
if u.Name != "" {
|
||||
@@ -109,21 +100,20 @@ func TestCrud(t *testing.T) {
|
||||
assert.EqualValues(wantedVersion, u.Version)
|
||||
}
|
||||
|
||||
u, apiErr, err := accountClient.Create(tc.Context(), amId, accounts.WithName("foo"), accounts.WithPasswordAccountLoginName("loginname"))
|
||||
checkAccount("create", u.Item, apiErr, err, "foo", 1)
|
||||
u, err := accountClient.Create(tc.Context(), amId, accounts.WithName("foo"), accounts.WithPasswordAccountLoginName("loginname"))
|
||||
checkAccount("create", u.Item, err, "foo", 1)
|
||||
|
||||
u, apiErr, err = accountClient.Read(tc.Context(), u.Item.Id)
|
||||
checkAccount("read", u.Item, apiErr, err, "foo", 1)
|
||||
u, err = accountClient.Read(tc.Context(), u.Item.Id)
|
||||
checkAccount("read", u.Item, err, "foo", 1)
|
||||
|
||||
u, apiErr, err = accountClient.Update(tc.Context(), u.Item.Id, u.Item.Version, accounts.WithName("bar"))
|
||||
checkAccount("update", u.Item, apiErr, err, "bar", 2)
|
||||
u, err = accountClient.Update(tc.Context(), u.Item.Id, u.Item.Version, accounts.WithName("bar"))
|
||||
checkAccount("update", u.Item, err, "bar", 2)
|
||||
|
||||
u, apiErr, err = accountClient.Update(tc.Context(), u.Item.Id, u.Item.Version, accounts.DefaultName())
|
||||
checkAccount("update", u.Item, apiErr, err, "", 3)
|
||||
u, err = accountClient.Update(tc.Context(), u.Item.Id, u.Item.Version, accounts.DefaultName())
|
||||
checkAccount("update", u.Item, err, "", 3)
|
||||
|
||||
_, apiErr, err = accountClient.Delete(tc.Context(), u.Item.Id)
|
||||
_, err = accountClient.Delete(tc.Context(), u.Item.Id)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
}
|
||||
|
||||
func TestCustomMethods(t *testing.T) {
|
||||
@@ -138,22 +128,19 @@ func TestCustomMethods(t *testing.T) {
|
||||
|
||||
accountClient := accounts.NewClient(client)
|
||||
|
||||
al, apiErr, err := accountClient.List(tc.Context(), amId)
|
||||
al, err := accountClient.List(tc.Context(), amId)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.Len(al.Items, 1)
|
||||
|
||||
acct := al.Items[0]
|
||||
|
||||
setAcct, apiErr, err := accountClient.SetPassword(tc.Context(), acct.Id, "setpassword", acct.Version)
|
||||
setAcct, err := accountClient.SetPassword(tc.Context(), acct.Id, "setpassword", acct.Version)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(setAcct)
|
||||
assert.Equal(acct.Version+1, setAcct.Item.Version)
|
||||
|
||||
changeAcct, apiErr, err := accountClient.ChangePassword(tc.Context(), acct.Id, "setpassword", "changepassword", setAcct.Item.Version)
|
||||
changeAcct, err := accountClient.ChangePassword(tc.Context(), acct.Id, "setpassword", "changepassword", setAcct.Item.Version)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(changeAcct)
|
||||
assert.Equal(setAcct.Item.Version+1, changeAcct.Item.Version)
|
||||
}
|
||||
@@ -169,34 +156,38 @@ func TestErrors(t *testing.T) {
|
||||
amId := token.AuthMethodId
|
||||
accountClient := accounts.NewClient(client)
|
||||
|
||||
u, apiErr, err := accountClient.Create(tc.Context(), amId, accounts.WithPasswordAccountLoginName("first"))
|
||||
u, err := accountClient.Create(tc.Context(), amId, accounts.WithPasswordAccountLoginName("first"))
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.NotNil(u)
|
||||
|
||||
// Updating the wrong version should fail.
|
||||
_, apiErr, err = accountClient.Update(tc.Context(), u.Item.Id, 73, accounts.WithName("anything"))
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
_, err = accountClient.Update(tc.Context(), u.Item.Id, 73, accounts.WithName("anything"))
|
||||
require.Error(err)
|
||||
apiErr := api.AsServerError(err)
|
||||
require.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusNotFound, apiErr.Status)
|
||||
|
||||
// Create another resource with the same name.
|
||||
_, apiErr, err = accountClient.Create(tc.Context(), amId, accounts.WithPasswordAccountLoginName("first"))
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
_, err = accountClient.Create(tc.Context(), amId, accounts.WithPasswordAccountLoginName("first"))
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
require.NotNil(apiErr)
|
||||
|
||||
_, apiErr, err = accountClient.Read(tc.Context(), password.AccountPrefix+"_doesntexis")
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
_, err = accountClient.Read(tc.Context(), password.AccountPrefix+"_doesntexis")
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
require.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusNotFound, apiErr.Status)
|
||||
|
||||
_, apiErr, err = accountClient.Read(tc.Context(), "invalid id")
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
_, err = accountClient.Read(tc.Context(), "invalid id")
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
require.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
|
||||
|
||||
_, apiErr, err = accountClient.Update(tc.Context(), u.Item.Id, u.Item.Version)
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
_, err = accountClient.Update(tc.Context(), u.Item.Id, u.Item.Version)
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
require.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
|
||||
}
|
||||
@@ -6,32 +6,31 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/kr/pretty"
|
||||
)
|
||||
|
||||
func (c *Client) ChangePassword(ctx context.Context, accountId, currentPassword, newPassword string, version uint32, opt ...Option) (*AccountUpdateResult, *api.Error, error) {
|
||||
func (c *Client) ChangePassword(ctx context.Context, accountId, currentPassword, newPassword string, version uint32, opt ...Option) (*AccountUpdateResult, error) {
|
||||
if accountId == "" {
|
||||
return nil, nil, fmt.Errorf("empty accountId value passed into ChangePassword request")
|
||||
return nil, fmt.Errorf("empty accountId value passed into ChangePassword request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in ChangePassword request")
|
||||
return nil, fmt.Errorf("nil client in ChangePassword request")
|
||||
}
|
||||
|
||||
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, accountId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, accountId, 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 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
|
||||
}
|
||||
@@ -44,20 +43,24 @@ func (c *Client) ChangePassword(ctx context.Context, accountId, currentPassword,
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("accounts/%s:change-password", accountId), reqBody, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating ChangePassword request: %w", err)
|
||||
return nil, fmt.Errorf("error creating ChangePassword request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during ChangePassword call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during ChangePassword call: %w", err)
|
||||
}
|
||||
|
||||
target := new(AccountUpdateResult)
|
||||
target.Item = new(Account)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding ChangePassword response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding ChangePassword response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
if apiErr != nil {
|
||||
return nil, apiErr
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
+17
-15
@@ -6,32 +6,31 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/kr/pretty"
|
||||
)
|
||||
|
||||
func (c *Client) SetPassword(ctx context.Context, accountId, password string, version uint32, opt ...Option) (*AccountUpdateResult, *api.Error, error) {
|
||||
func (c *Client) SetPassword(ctx context.Context, accountId, password string, version uint32, opt ...Option) (*AccountUpdateResult, error) {
|
||||
if accountId == "" {
|
||||
return nil, nil, fmt.Errorf("empty accountId value passed into SetPassword request")
|
||||
return nil, fmt.Errorf("empty accountId value passed into SetPassword request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in SetPassword request")
|
||||
return nil, fmt.Errorf("nil client in SetPassword request")
|
||||
}
|
||||
|
||||
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, accountId, opt...)
|
||||
existingTarget, existingErr := c.Read(ctx, accountId, 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 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
|
||||
}
|
||||
@@ -43,20 +42,23 @@ func (c *Client) SetPassword(ctx context.Context, accountId, password string, ve
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("accounts/%s:set-password", accountId), reqBody, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating SetPassword request: %w", err)
|
||||
return nil, fmt.Errorf("error creating SetPassword request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during SetPassword call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during SetPassword call: %w", err)
|
||||
}
|
||||
|
||||
target := new(AccountUpdateResult)
|
||||
target.Item = new(Account)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding SetPassword response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding SetPassword response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
Reference in new issue
Block a user