Convert SDK to the new options API (#238)
This commit is contained in:
1 parent
b5acf6afdf
commit
908a299ea3
162 files changed
+6440
-5891
No files matched your search
@@ -1,42 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s Group) CreateGroup(ctx context.Context, r *Group) (*Group, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateGroup request")
|
||||
}
|
||||
|
||||
var opts []api.Option
|
||||
if s.Scope.Id != "" {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
opts = append(opts, api.WithScopeId(s.Scope.Id))
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", "groups", r, opts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateGroup request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during CreateGroup call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding CreateGroup repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s Group) AddMembers(ctx context.Context, memberIds []string) (*Group, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in AddMembers request")
|
||||
}
|
||||
// We assume that the client provided has the org and optionally the project id of the request.
|
||||
|
||||
if len(memberIds) == 0 {
|
||||
return nil, nil, fmt.Errorf("empty memberIds slice")
|
||||
}
|
||||
|
||||
body := map[string]interface{}{
|
||||
"version": s.Version,
|
||||
"member_ids": memberIds,
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:add-members", s.Id), body)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating AddMembers request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during AddMembers call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding AddMembers repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Group) SetMembers(ctx context.Context, memberIds []string) (*Group, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in SetMembers request")
|
||||
}
|
||||
// We assume that the client provided has the org and optionally the project id of the request.
|
||||
|
||||
body := map[string]interface{}{
|
||||
"version": s.Version,
|
||||
}
|
||||
if len(memberIds) > 0 {
|
||||
body["member_ids"] = memberIds
|
||||
} else if memberIds != nil {
|
||||
// In this function, a non-nil but empty list means clear out
|
||||
body["member_ids"] = nil
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:set-members", s.Id), body)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating SetMembers request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during SetMembers call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding SetMembers repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Group) RemoveMembers(ctx context.Context, memberIds []string) (*Group, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in RemoveMembers request")
|
||||
}
|
||||
// We assume that the client provided has the org and optionally the project id of the request.
|
||||
|
||||
if len(memberIds) == 0 {
|
||||
return nil, nil, fmt.Errorf("empty memberIds slice")
|
||||
}
|
||||
|
||||
body := map[string]interface{}{
|
||||
"version": s.Version,
|
||||
"member_ids": memberIds,
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:remove-members", s.Id), body)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating RemoveMembers request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during RemoveMembers call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding RemoveMembers repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
// DeleteGroup returns true iff the Group existed when the delete attempt was made.
|
||||
func (s Group) DeleteGroup(ctx context.Context, id string) (bool, *api.Error, error) {
|
||||
if id == "" {
|
||||
return false, nil, fmt.Errorf("empty id provided to DeleteGroup request")
|
||||
}
|
||||
|
||||
if s.Client == nil {
|
||||
return false, nil, fmt.Errorf("nil client in DeleteGroup request")
|
||||
}
|
||||
|
||||
var opts []api.Option
|
||||
if s.Scope.Id != "" {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
opts = append(opts, api.WithScopeId(s.Scope.Id))
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("%s/%s", "groups", id), nil, opts...)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error creating DeleteGroup request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error performing client request during DeleteGroup 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 DeleteGroup repsonse: %w", err)
|
||||
}
|
||||
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
+335
-61
@@ -2,81 +2,355 @@
|
||||
package groups
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
"github.com/kr/pretty"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/info"
|
||||
"github.com/hashicorp/watchtower/api/internal/strutil"
|
||||
"github.com/hashicorp/watchtower/api/scopes"
|
||||
)
|
||||
|
||||
type Group struct {
|
||||
Client *api.Client `json:"-"`
|
||||
|
||||
defaultFields []string
|
||||
|
||||
// The ID of the Project
|
||||
// Output only.
|
||||
Id string `json:"id,omitempty"`
|
||||
// Scope information for this resource
|
||||
// Output only.
|
||||
Scope info.Scope `json:"scope,omitempty"`
|
||||
// Optional name for identification purposes
|
||||
Name *string `json:"name,omitempty"`
|
||||
// Optional user-set descripton for identification purposes
|
||||
Description *string `json:"description,omitempty"`
|
||||
// The time this resource was created
|
||||
// Output only.
|
||||
CreatedTime time.Time `json:"created_time,omitempty"`
|
||||
// The time this resource was last updated.
|
||||
// Output only.
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
// Whether the resource is disabled
|
||||
Disabled *bool `json:"disabled,omitempty"`
|
||||
// The version can be used in subsiquent write requests to ensure this resource
|
||||
// has not changed and to fail the write if it has.
|
||||
// Output only.
|
||||
Version uint32 `json:"version,omitempty"`
|
||||
// Contains the list of member ids in this group.
|
||||
// Output only.
|
||||
MemberIds []string `json:"member_ids,omitempty"`
|
||||
// The members of this group.
|
||||
// Output only.
|
||||
Members []*Member `json:"members,omitempty"`
|
||||
Id string `json:"id,omitempty"`
|
||||
Scope *scopes.ScopeInfo `json:"scope,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
CreatedTime time.Time `json:"created_time,omitempty"`
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
Version uint32 `json:"version,omitempty"`
|
||||
MemberIds []string `json:"member_ids,omitempty"`
|
||||
Members []*Member `json:"members,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Group) SetDefault(key string) {
|
||||
lowerKey := strings.ToLower(key)
|
||||
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "id": "id", "memberids": "member_ids", "members": "members", "name": "name", "scope": "scope", "updatedtime": "updated_time", "version": "version"}
|
||||
for k, v := range validMap {
|
||||
if k == lowerKey || v == lowerKey {
|
||||
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
|
||||
return
|
||||
type groupClient struct {
|
||||
client *api.Client
|
||||
}
|
||||
|
||||
func NewGroupClient(c *api.Client) *groupClient {
|
||||
return &groupClient{client: c}
|
||||
}
|
||||
|
||||
func (c *groupClient) Create(ctx context.Context, opt ...Option) (*Group, *api.Error, error) {
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups"), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
|
||||
}
|
||||
|
||||
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(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Create response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *groupClient) Read(ctx context.Context, groupId string, opt ...Option) (*Group, *api.Error, error) {
|
||||
if groupId == "" {
|
||||
return nil, nil, fmt.Errorf("empty groupId value passed into Read request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("groups/%s", groupId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
|
||||
}
|
||||
|
||||
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(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *groupClient) Update(ctx context.Context, groupId string, version uint32, opt ...Option) (*Group, *api.Error, error) {
|
||||
if groupId == "" {
|
||||
return nil, nil, fmt.Errorf("empty groupId 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Group) UnsetDefault(key string) {
|
||||
lowerKey := strings.ToLower(key)
|
||||
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "id": "id", "memberids": "member_ids", "members": "members", "name": "name", "scope": "scope", "updatedtime": "updated_time", "version": "version"}
|
||||
for k, v := range validMap {
|
||||
if k == lowerKey || v == lowerKey {
|
||||
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
|
||||
return
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, groupId, 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.valueMap["version"] = version
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("groups/%s", groupId), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
|
||||
}
|
||||
|
||||
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(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Group) MarshalJSON() ([]byte, error) {
|
||||
m := structs.Map(s)
|
||||
if m == nil {
|
||||
m = make(map[string]interface{})
|
||||
func (c *groupClient) Delete(ctx context.Context, groupId string, opt ...Option) (bool, *api.Error, error) {
|
||||
if groupId == "" {
|
||||
return false, nil, fmt.Errorf("empty groupId value passed into Delete request")
|
||||
}
|
||||
for _, k := range s.defaultFields {
|
||||
m[k] = nil
|
||||
|
||||
if c.client == nil {
|
||||
return false, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
return json.Marshal(m)
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("groups/%s", groupId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error creating Delete request: %w", err)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *groupClient) List(ctx context.Context, opt ...Option) ([]*Group, *api.Error, error) {
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("groups"), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating List request: %w", err)
|
||||
}
|
||||
|
||||
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 []*Group
|
||||
}
|
||||
target := &listResponse{}
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
|
||||
}
|
||||
|
||||
return target.Items, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *groupClient) AddMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*Group, *api.Error, error) {
|
||||
if groupId == "" {
|
||||
return nil, nil, fmt.Errorf("empty groupId value passed into AddMembers 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 AddMembers request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, groupId, 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.valueMap["version"] = version
|
||||
|
||||
if len(memberIds) > 0 {
|
||||
opts.valueMap["member_ids"] = memberIds
|
||||
}
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:add-members", groupId), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating AddMembers request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during AddMembers call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding AddMembers response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *groupClient) SetMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*Group, *api.Error, error) {
|
||||
if groupId == "" {
|
||||
return nil, nil, fmt.Errorf("empty groupId value passed into SetMembers 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 SetMembers request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, groupId, 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.valueMap["version"] = version
|
||||
|
||||
if len(memberIds) > 0 {
|
||||
opts.valueMap["member_ids"] = memberIds
|
||||
} else if memberIds != nil {
|
||||
// In this function, a non-nil but empty list means clear out
|
||||
opts.valueMap["member_ids"] = nil
|
||||
}
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:set-members", groupId), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating SetMembers request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during SetMembers call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding SetMembers response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *groupClient) RemoveMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*Group, *api.Error, error) {
|
||||
if groupId == "" {
|
||||
return nil, nil, fmt.Errorf("empty groupId value passed into RemoveMembers 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 RemoveMembers request")
|
||||
}
|
||||
existingTarget, existingApiErr, existingErr := c.Read(ctx, groupId, 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.valueMap["version"] = version
|
||||
|
||||
if len(memberIds) > 0 {
|
||||
opts.valueMap["member_ids"] = memberIds
|
||||
}
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:remove-members", groupId), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating RemoveMembers request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during RemoveMembers call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding RemoveMembers response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s Group) ListGroups(ctx context.Context) ([]*Group, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in ListGroup request")
|
||||
}
|
||||
|
||||
var opts []api.Option
|
||||
if s.Scope.Id != "" {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
opts = append(opts, api.WithScopeId(s.Scope.Id))
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "GET", "groups", nil, opts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating ListGroups request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during ListGroups call: %w", err)
|
||||
}
|
||||
|
||||
type listResponse struct {
|
||||
Items []*Group
|
||||
}
|
||||
target := &listResponse{}
|
||||
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding ListGroups response: %w", err)
|
||||
}
|
||||
|
||||
for _, t := range target.Items {
|
||||
|
||||
t.Client = s.Client
|
||||
|
||||
}
|
||||
|
||||
return target.Items, apiErr, nil
|
||||
}
|
||||
@@ -2,14 +2,7 @@
|
||||
package groups
|
||||
|
||||
type Member struct {
|
||||
|
||||
// The ID of the member.
|
||||
// Output only.
|
||||
Id string `json:"id,omitempty"`
|
||||
// The type of the member.
|
||||
// Output only.
|
||||
Type string `json:"type,omitempty"`
|
||||
// The scope ID of the member.
|
||||
// Output only.
|
||||
Id string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
ScopeId string `json:"scope_id,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package groups
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
// Option is a func that sets optional attributes for a call. This does not need
|
||||
// to be used directly, but instead option arguments are built from the
|
||||
// functions in this package. WithX options set a value to that given in the
|
||||
// argument; DefaultX options indicate that the value should be set to its
|
||||
// default. When an API call is made options are processed in ther order they
|
||||
// appear in the function call, so for a given argument X, a succession of WithX
|
||||
// or DefaultX calls will result in the last call taking effect.
|
||||
type Option func(*options)
|
||||
|
||||
type options struct {
|
||||
valueMap map[string]interface{}
|
||||
withScopeId string
|
||||
withAutomaticVersioning bool
|
||||
}
|
||||
|
||||
func getDefaultOptions() options {
|
||||
return options{
|
||||
valueMap: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
func getOpts(opt ...Option) (options, []api.Option) {
|
||||
opts := getDefaultOptions()
|
||||
for _, o := range opt {
|
||||
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 {
|
||||
return func(o *options) {
|
||||
o.withAutomaticVersioning = true
|
||||
}
|
||||
}
|
||||
|
||||
func WithDescription(inDescription string) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["description"] = inDescription
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultDescription() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["description"] = nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithDisabled(inDisabled bool) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["disabled"] = inDisabled
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultDisabled() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["disabled"] = nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithName(inName string) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["name"] = inName
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultName() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["name"] = nil
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s Group) ReadGroup(ctx context.Context, id string) (*Group, *api.Error, error) {
|
||||
if id == "" {
|
||||
return nil, nil, fmt.Errorf("empty ID value passed into ReadGroup request")
|
||||
}
|
||||
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in ReadGroup request")
|
||||
}
|
||||
|
||||
var opts []api.Option
|
||||
if s.Scope.Id != "" {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
opts = append(opts, api.WithScopeId(s.Scope.Id))
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "groups", id), nil, opts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating ReadGroup request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during ReadGroup call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding ReadGroup repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s Group) UpdateGroup(ctx context.Context, r *Group) (*Group, *api.Error, error) {
|
||||
if r == nil {
|
||||
|
||||
}
|
||||
if r.Id == "" {
|
||||
return nil, nil, fmt.Errorf("empty Group ID field in CreateGroup request")
|
||||
}
|
||||
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateGroup request")
|
||||
}
|
||||
|
||||
var opts []api.Option
|
||||
if s.Scope.Id != "" {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
opts = append(opts, api.WithScopeId(s.Scope.Id))
|
||||
}
|
||||
|
||||
id := r.Id
|
||||
r.Id = ""
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("%s/%s", "groups", id), r, opts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateGroup request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during UpdateGroup call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Group)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding UpdateGroup repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
Reference in new issue
Block a user