Files
threshold/api/accounts/setpassword.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
1.9 KiB
Go
Raw Normal View History

2020-09-24 14:34:58 -07:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2023-02-02 17:15:17 -08:00
package accounts
2020-08-17 09:37:15 -07:00
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/boundary/api"
)
2020-09-24 14:34:58 -07:00
func (c *Client) SetPassword(ctx context.Context, accountId, password string, version uint32, opt ...Option) (*AccountUpdateResult, error) {
2020-08-17 09:37:15 -07:00
if accountId == "" {
2020-09-24 14:34:58 -07:00
return nil, fmt.Errorf("empty accountId value passed into SetPassword request")
2020-08-17 09:37:15 -07:00
}
if c.client == nil {
2020-09-24 14:34:58 -07:00
return nil, fmt.Errorf("nil client in SetPassword request")
2020-08-17 09:37:15 -07:00
}
opts, apiOpts := getOpts(opt...)
if version == 0 {
if !opts.withAutomaticVersioning {
2020-09-24 14:34:58 -07:00
return nil, errors.New("zero version number passed into Update request and automatic versioning not specified")
2020-08-17 09:37:15 -07:00
}
2020-09-24 14:34:58 -07:00
existingTarget, existingErr := c.Read(ctx, accountId, opt...)
2020-08-17 09:37:15 -07:00
if existingErr != nil {
2020-09-24 14:34:58 -07:00
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)
2020-08-17 09:37:15 -07:00
}
if existingTarget == nil {
2020-09-24 14:34:58 -07:00
return nil, errors.New("nil resource found when performing initial check-and-set read")
2020-08-17 09:37:15 -07:00
}
version = existingTarget.Item.Version
2020-08-17 09:37:15 -07:00
}
2022-10-25 18:29:38 -05:00
reqBody := map[string]any{
2020-08-17 09:37:15 -07:00
"version": version,
"password": password,
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("accounts/%s:set-password", accountId), reqBody, apiOpts...)
2020-08-17 09:37:15 -07:00
if err != nil {
2020-09-24 14:34:58 -07:00
return nil, fmt.Errorf("error creating SetPassword request: %w", err)
2020-08-17 09:37:15 -07:00
}
resp, err := c.client.Do(req)
if err != nil {
2020-09-24 14:34:58 -07:00
return nil, fmt.Errorf("error performing client request during SetPassword call: %w", err)
2020-08-17 09:37:15 -07:00
}
target := new(AccountUpdateResult)
target.Item = new(Account)
apiErr, err := resp.Decode(target.Item)
2020-08-17 09:37:15 -07:00
if err != nil {
2020-09-24 14:34:58 -07:00
return nil, fmt.Errorf("error decoding SetPassword response: %w", err)
}
if apiErr != nil {
return nil, apiErr
2020-08-17 09:37:15 -07:00
}
target.Response = resp
2020-09-24 14:34:58 -07:00
return target, nil
2020-08-17 09:37:15 -07:00
}