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

+198
View File
@@ -0,0 +1,198 @@
// Code generated by "make api"; DO NOT EDIT.
package authmethods
import (
"context"
"fmt"
"time"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/scopes"
)
type Account struct {
Id string `json:"id,omitempty"`
Scope *scopes.ScopeInfo `json:"scope,omitempty"`
AuthMethodId string `json:"auth_method_id,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"`
Type string `json:"type,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
type accountClient struct {
client *api.Client
}
func NewAccountClient(c *api.Client) *accountClient {
return &accountClient{client: c}
}
func (c *accountClient) Create(ctx context.Context, authMethodId string, opt ...Option) (*Account, *api.Error, error) {
if authMethodId == "" {
return nil, nil, fmt.Errorf("empty authMethodId value passed into Create request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("auth-methods/%s/accounts", authMethodId), 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(Account)
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 *accountClient) Read(ctx context.Context, authMethodId string, accountId string, opt ...Option) (*Account, *api.Error, error) {
if authMethodId == "" {
return nil, nil, fmt.Errorf("empty authMethodId value passed into Read request")
}
if accountId == "" {
return nil, nil, fmt.Errorf("empty accountId 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("auth-methods/%s/accounts/%s", authMethodId, accountId), 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(Account)
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 *accountClient) Update(ctx context.Context, authMethodId string, accountId string, version uint32, opt ...Option) (*Account, *api.Error, error) {
if authMethodId == "" {
return nil, nil, fmt.Errorf("empty authMethodId value passed into Update request")
}
if accountId == "" {
return nil, nil, fmt.Errorf("empty accountId value passed into Update request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("auth-methods/%s/accounts/%s", authMethodId, accountId), 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(Account)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
}
return target, apiErr, nil
}
func (c *accountClient) Delete(ctx context.Context, authMethodId string, accountId string, opt ...Option) (bool, *api.Error, error) {
if authMethodId == "" {
return false, nil, fmt.Errorf("empty authMethodId value passed into Delete request")
}
if accountId == "" {
return false, nil, fmt.Errorf("empty accountId value passed into Delete request")
}
if c.client == nil {
return false, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("auth-methods/%s/accounts/%s", authMethodId, accountId), 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 *accountClient) List(ctx context.Context, authMethodId string, opt ...Option) ([]*Account, *api.Error, error) {
if authMethodId == "" {
return nil, nil, fmt.Errorf("empty authMethodId value passed into List request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("auth-methods/%s/accounts", authMethodId), 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 []*Account
}
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
}
+48
View File
@@ -0,0 +1,48 @@
package authmethods
import (
"context"
"fmt"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/authtokens"
)
// TODO: This will need to be changed when we add Auth Method API to watchtower. We'll also need a better
// way to handle different auth method types.
func (c *authmethodClient) Authenticate(ctx context.Context, authMethodId, name, password string, opt ...Option) (*authtokens.AuthToken, *api.Error, error) {
if c.client == nil {
return nil, nil, fmt.Errorf("nil client in Authenticate request")
}
_, apiOpts := getOpts(opt...)
reqBody := map[string]interface{}{
"credentials": map[string]string{
"name": name,
"password": password,
},
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("auth-methods/%s:authenticate", authMethodId), reqBody, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Authenticate request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Authenticate call: %w", err)
}
target := new(authtokens.AuthToken)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Authenticate response: %w", err)
}
if target.Token != "" {
c.client.SetToken(target.Token)
}
return target, apiErr, nil
}
+177
View File
@@ -0,0 +1,177 @@
// Code generated by "make api"; DO NOT EDIT.
package authmethods
import (
"context"
"fmt"
"time"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/scopes"
)
type AuthMethod struct {
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"`
Type string `json:"type,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
type authmethodClient struct {
client *api.Client
}
func NewAuthMethodClient(c *api.Client) *authmethodClient {
return &authmethodClient{client: c}
}
func (c *authmethodClient) Create(ctx context.Context, opt ...Option) (*AuthMethod, *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("auth-methods"), 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(AuthMethod)
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 *authmethodClient) Read(ctx context.Context, authMethodId string, opt ...Option) (*AuthMethod, *api.Error, error) {
if authMethodId == "" {
return nil, nil, fmt.Errorf("empty authMethodId 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("auth-methods/%s", authMethodId), 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(AuthMethod)
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 *authmethodClient) Update(ctx context.Context, authMethodId string, version uint32, opt ...Option) (*AuthMethod, *api.Error, error) {
if authMethodId == "" {
return nil, nil, fmt.Errorf("empty authMethodId value passed into Update request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("auth-methods/%s", authMethodId), 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(AuthMethod)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
}
return target, apiErr, nil
}
func (c *authmethodClient) Delete(ctx context.Context, authMethodId string, opt ...Option) (bool, *api.Error, error) {
if authMethodId == "" {
return false, nil, fmt.Errorf("empty authMethodId value passed into Delete request")
}
if c.client == nil {
return false, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("auth-methods/%s", authMethodId), 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 *authmethodClient) List(ctx context.Context, opt ...Option) ([]*AuthMethod, *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("auth-methods"), 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 []*AuthMethod
}
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
}
+210
View File
@@ -0,0 +1,210 @@
package authmethods
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 WithAttributes(inAttributes map[string]interface{}) Option {
return func(o *options) {
o.valueMap["attributes"] = inAttributes
}
}
func DefaultAttributes() Option {
return func(o *options) {
o.valueMap["attributes"] = nil
}
}
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 WithPasswordAuthMethodMinPasswordLength(inMinPasswordLength uint32) Option {
return func(o *options) {
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["min_password_length"] = inMinPasswordLength
o.valueMap["attributes"] = val
}
}
func DefaultPasswordAuthMethodMinPasswordLength() Option {
return func(o *options) {
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["min_password_length"] = nil
o.valueMap["attributes"] = val
}
}
func WithPasswordAuthMethodMinUserNameLength(inMinUserNameLength uint32) Option {
return func(o *options) {
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["min_user_name_length"] = inMinUserNameLength
o.valueMap["attributes"] = val
}
}
func DefaultPasswordAuthMethodMinUserNameLength() Option {
return func(o *options) {
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["min_user_name_length"] = nil
o.valueMap["attributes"] = val
}
}
func WithName(inName string) Option {
return func(o *options) {
o.valueMap["name"] = inName
}
}
func DefaultName() Option {
return func(o *options) {
o.valueMap["name"] = nil
}
}
func WithPasswordAccountPassword(inPassword string) Option {
return func(o *options) {
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["password"] = inPassword
o.valueMap["attributes"] = val
}
}
func DefaultPasswordAccountPassword() Option {
return func(o *options) {
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["password"] = nil
o.valueMap["attributes"] = val
}
}
func WithType(inType string) Option {
return func(o *options) {
o.valueMap["type"] = inType
}
}
func DefaultType() Option {
return func(o *options) {
o.valueMap["type"] = nil
}
}
func WithPasswordAccountUsername(inUsername string) Option {
return func(o *options) {
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["username"] = inUsername
o.valueMap["attributes"] = val
}
}
func DefaultPasswordAccountUsername() Option {
return func(o *options) {
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["username"] = nil
o.valueMap["attributes"] = val
}
}
@@ -0,0 +1,7 @@
// Code generated by "make api"; DO NOT EDIT.
package authmethods
type PasswordAccountAttributes struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
@@ -0,0 +1,7 @@
// Code generated by "make api"; DO NOT EDIT.
package authmethods
type PasswordAuthMethodAttributes struct {
MinUserNameLength uint32 `json:"min_user_name_length,omitempty"`
MinPasswordLength uint32 `json:"min_password_length,omitempty"`
}