Convert SDK to the new options API (#238)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-07 12:55:02 -04:00
1 parent b5acf6afdf
commit 908a299ea3
162 files changed
+6440 -5891

No files matched your search

-42
View File
@@ -1,42 +0,0 @@
// Code generated by "make api"; DO NOT EDIT.
package users
import (
"context"
"fmt"
"github.com/hashicorp/watchtower/api"
)
func (s User) CreateUser(ctx context.Context, r *User) (*User, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in CreateUser 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", "users", r, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating CreateUser request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during CreateUser call: %w", err)
}
target := new(User)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding CreateUser repsonse: %w", err)
}
target.Client = s.Client
return target, apiErr, nil
}
-49
View File
@@ -1,49 +0,0 @@
// Code generated by "make api"; DO NOT EDIT.
package users
import (
"context"
"fmt"
"github.com/hashicorp/watchtower/api"
)
// DeleteUser returns true iff the User existed when the delete attempt was made.
func (s User) DeleteUser(ctx context.Context, id string) (bool, *api.Error, error) {
if id == "" {
return false, nil, fmt.Errorf("empty id provided to DeleteUser request")
}
if s.Client == nil {
return false, nil, fmt.Errorf("nil client in DeleteUser 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", "users", id), nil, opts...)
if err != nil {
return false, nil, fmt.Errorf("error creating DeleteUser request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during DeleteUser 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 DeleteUser repsonse: %w", err)
}
return target.Existed, apiErr, nil
}
-50
View File
@@ -1,50 +0,0 @@
// Code generated by "make api"; DO NOT EDIT.
package users
import (
"context"
"fmt"
"github.com/hashicorp/watchtower/api"
)
func (s User) ListUsers(ctx context.Context) ([]*User, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in ListUser 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", "users", nil, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating ListUsers request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during ListUsers call: %w", err)
}
type listResponse struct {
Items []*User
}
target := &listResponse{}
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding ListUsers response: %w", err)
}
for _, t := range target.Items {
t.Client = s.Client
}
return target.Items, apiErr, nil
}
+90
View File
@@ -0,0 +1,90 @@
package users
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
}
}
-46
View File
@@ -1,46 +0,0 @@
// Code generated by "make api"; DO NOT EDIT.
package users
import (
"context"
"fmt"
"github.com/hashicorp/watchtower/api"
)
func (s User) ReadUser(ctx context.Context, id string) (*User, *api.Error, error) {
if id == "" {
return nil, nil, fmt.Errorf("empty ID value passed into ReadUser request")
}
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in ReadUser 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", "users", id), nil, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating ReadUser request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during ReadUser call: %w", err)
}
target := new(User)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding ReadUser repsonse: %w", err)
}
target.Client = s.Client
return target, apiErr, nil
}
-52
View File
@@ -1,52 +0,0 @@
// Code generated by "make api"; DO NOT EDIT.
package users
import (
"context"
"fmt"
"github.com/hashicorp/watchtower/api"
)
func (s User) UpdateUser(ctx context.Context, r *User) (*User, *api.Error, error) {
if r == nil {
}
if r.Id == "" {
return nil, nil, fmt.Errorf("empty User ID field in CreateUser request")
}
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in CreateUser 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", "users", id), r, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating CreateUser request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during UpdateUser call: %w", err)
}
target := new(User)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding UpdateUser repsonse: %w", err)
}
target.Client = s.Client
return target, apiErr, nil
}
+177 -51
View File
@@ -2,71 +2,197 @@
package users
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 User 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"`
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"`
}
func (s *User) SetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "id": "id", "name": "name", "scope": "scope", "updatedtime": "updated_time"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
return
type userClient struct {
client *api.Client
}
func NewUserClient(c *api.Client) *userClient {
return &userClient{client: c}
}
func (c *userClient) Create(ctx context.Context, opt ...Option) (*User, *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("users"), 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(User)
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 *userClient) Read(ctx context.Context, userId string, opt ...Option) (*User, *api.Error, error) {
if userId == "" {
return nil, nil, fmt.Errorf("empty userId 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("users/%s", userId), 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(User)
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 *userClient) Update(ctx context.Context, userId string, version uint32, opt ...Option) (*User, *api.Error, error) {
if userId == "" {
return nil, nil, fmt.Errorf("empty userId 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 *User) UnsetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "id": "id", "name": "name", "scope": "scope", "updatedtime": "updated_time"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
return
existingTarget, existingApiErr, existingErr := c.Read(ctx, userId, 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("users/%s", userId), 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(User)
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 User) MarshalJSON() ([]byte, error) {
m := structs.Map(s)
if m == nil {
m = make(map[string]interface{})
func (c *userClient) Delete(ctx context.Context, userId string, opt ...Option) (bool, *api.Error, error) {
if userId == "" {
return false, nil, fmt.Errorf("empty userId 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("users/%s", userId), 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 *userClient) List(ctx context.Context, opt ...Option) ([]*User, *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("users"), 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 []*User
}
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
}