Add Result types to Go SDK and properly populate body/map fields (#358)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-09-15 01:08:21 -04:00
1 parent d8dc1ed0b2
commit a00ee7a948
61 files changed
+1888 -1099

No files matched your search

+4 -3
View File
@@ -8,7 +8,7 @@ import (
"github.com/hashicorp/boundary/api/authtokens"
)
func (c *Client) Authenticate(ctx context.Context, authMethodId string, credentials map[string]interface{}, opt ...Option) (*authtokens.AuthToken, *api.Error, error) {
func (c *Client) Authenticate(ctx context.Context, authMethodId string, credentials map[string]interface{}, opt ...Option) (*authtokens.AuthTokenReadResult, *api.Error, error) {
if c.client == nil {
return nil, nil, fmt.Errorf("nil client in Authenticate request")
}
@@ -29,8 +29,9 @@ func (c *Client) Authenticate(ctx context.Context, authMethodId string, credenti
return nil, nil, fmt.Errorf("error performing client request during Authenticate call: %w", err)
}
target := new(authtokens.AuthToken)
apiErr, err := resp.Decode(target)
target := new(authtokens.AuthTokenReadResult)
target.Item = new(authtokens.AuthToken)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Authenticate response: %w", err)
}
+1 -1
View File
@@ -32,5 +32,5 @@ func TestAuthenticate(t *testing.T) {
_, apiErr, err = methods.Authenticate(tc.Context(), amId, map[string]interface{}{"login_name": "user", "password": "wrong"})
require.NoError(err)
require.NotNil(t, apiErr)
assert.EqualValuesf(http.StatusUnauthorized, apiErr.Status, "Expected unauthenticated, got %q", apiErr.Message)
assert.EqualValuesf(http.StatusUnauthorized, apiErr.Status, "Expected unauthorized, got %q", apiErr.Message)
}
+98 -36
View File
@@ -6,7 +6,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -28,16 +27,68 @@ type AuthMethod struct {
Type string `json:"type,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
lastResponseBody *bytes.Buffer
lastResponseMap map[string]interface{}
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n AuthMethod) LastResponseBody() *bytes.Buffer {
return n.lastResponseBody
func (n AuthMethod) ResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n AuthMethod) LastResponseMap() map[string]interface{} {
return n.lastResponseMap
func (n AuthMethod) ResponseMap() map[string]interface{} {
return n.responseMap
}
type AuthMethodReadResult struct {
Item *AuthMethod
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n AuthMethodReadResult) GetItem() interface{} {
return n.Item
}
func (n AuthMethodReadResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n AuthMethodReadResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type AuthMethodCreateResult = AuthMethodReadResult
type AuthMethodUpdateResult = AuthMethodReadResult
type AuthMethodDeleteResult struct {
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n AuthMethodDeleteResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n AuthMethodDeleteResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type AuthMethodListResult struct {
Items []*AuthMethod
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n AuthMethodListResult) GetItems() interface{} {
return n.Items
}
func (n AuthMethodListResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n AuthMethodListResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
// Client is a client for this collection
@@ -58,7 +109,7 @@ func (c *Client) ApiClient() *api.Client {
return c.client
}
func (c *Client) Create(ctx context.Context, resourceType string, scopeId string, opt ...Option) (*AuthMethod, *api.Error, error) {
func (c *Client) Create(ctx context.Context, resourceType string, scopeId string, opt ...Option) (*AuthMethodCreateResult, *api.Error, error) {
if scopeId == "" {
return nil, nil, fmt.Errorf("empty scopeId value passed into Create request")
}
@@ -94,18 +145,21 @@ func (c *Client) Create(ctx context.Context, resourceType string, scopeId string
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
}
target := new(AuthMethod)
apiErr, err := resp.Decode(target)
target := new(AuthMethodCreateResult)
target.Item = new(AuthMethod)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Create response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) Read(ctx context.Context, authMethodId string, opt ...Option) (*AuthMethod, *api.Error, error) {
func (c *Client) Read(ctx context.Context, authMethodId string, opt ...Option) (*AuthMethodReadResult, *api.Error, error) {
if authMethodId == "" {
return nil, nil, fmt.Errorf("empty authMethodId value passed into Read request")
}
@@ -133,18 +187,21 @@ func (c *Client) Read(ctx context.Context, authMethodId string, opt ...Option) (
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
}
target := new(AuthMethod)
apiErr, err := resp.Decode(target)
target := new(AuthMethodReadResult)
target.Item = new(AuthMethod)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) Update(ctx context.Context, authMethodId string, version uint32, opt ...Option) (*AuthMethod, *api.Error, error) {
func (c *Client) Update(ctx context.Context, authMethodId string, version uint32, opt ...Option) (*AuthMethodUpdateResult, *api.Error, error) {
if authMethodId == "" {
return nil, nil, fmt.Errorf("empty authMethodId value passed into Update request")
}
@@ -166,9 +223,12 @@ func (c *Client) Update(ctx context.Context, authMethodId string, version uint32
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 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")
}
version = existingTarget.Version
version = existingTarget.Item.Version
}
opts.postMap["version"] = version
@@ -191,30 +251,33 @@ func (c *Client) Update(ctx context.Context, authMethodId string, version uint32
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
}
target := new(AuthMethod)
apiErr, err := resp.Decode(target)
target := new(AuthMethodUpdateResult)
target.Item = new(AuthMethod)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) Delete(ctx context.Context, authMethodId string, opt ...Option) (bool, *api.Error, error) {
func (c *Client) Delete(ctx context.Context, authMethodId string, opt ...Option) (*AuthMethodDeleteResult, *api.Error, error) {
if authMethodId == "" {
return false, nil, fmt.Errorf("empty authMethodId value passed into Delete request")
return nil, nil, fmt.Errorf("empty authMethodId value passed into Delete request")
}
if c.client == nil {
return false, nil, fmt.Errorf("nil client")
return nil, nil, fmt.Errorf("nil client")
}
opts, 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)
return nil, nil, fmt.Errorf("error creating Delete request: %w", err)
}
if len(opts.queryMap) > 0 {
@@ -227,25 +290,25 @@ func (c *Client) Delete(ctx context.Context, authMethodId string, opt ...Option)
resp, err := c.client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
return nil, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
}
apiErr, err := resp.Decode(nil)
if err != nil {
return false, nil, fmt.Errorf("error decoding Delete response: %w", err)
return nil, nil, fmt.Errorf("error decoding Delete response: %w", err)
}
if apiErr != nil {
// We don't treat a 404 in this case as failure, in order for deletes to
// be idempotent
if apiErr.Status == http.StatusNotFound {
return false, nil, nil
}
return false, apiErr, nil
return nil, apiErr, nil
}
return true, nil, nil
target := &AuthMethodDeleteResult{
responseBody: resp.Body,
responseMap: resp.Map,
}
return target, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*AuthMethod, *api.Error, error) {
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*AuthMethodListResult, *api.Error, error) {
if scopeId == "" {
return nil, nil, fmt.Errorf("empty scopeId value passed into List request")
}
@@ -274,10 +337,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Au
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
}
type listResponse struct {
Items []*AuthMethod
}
target := &listResponse{}
target := new(AuthMethodListResult)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
@@ -285,5 +345,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Au
if apiErr != nil {
return nil, apiErr, nil
}
return target.Items, apiErr, nil
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
@@ -1,20 +1,7 @@
// Code generated by "make api"; DO NOT EDIT.
package authmethods
import "bytes"
type PasswordAuthMethodAttributes struct {
MinLoginNameLength uint32 `json:"min_login_name_length,omitempty"`
MinPasswordLength uint32 `json:"min_password_length,omitempty"`
lastResponseBody *bytes.Buffer
lastResponseMap map[string]interface{}
}
func (n PasswordAuthMethodAttributes) LastResponseBody() *bytes.Buffer {
return n.lastResponseBody
}
func (n PasswordAuthMethodAttributes) LastResponseMap() map[string]interface{} {
return n.lastResponseMap
}