Add Result types to Go SDK and properly populate body/map fields (#358)
This commit is contained in:
1 parent
d8dc1ed0b2
commit
a00ee7a948
61 files changed
+1888
-1099
No files matched your search
+98
-36
@@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
@@ -26,16 +25,68 @@ type Scope struct {
|
||||
Version uint32 `json:"version,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
|
||||
lastResponseBody *bytes.Buffer
|
||||
lastResponseMap map[string]interface{}
|
||||
responseBody *bytes.Buffer
|
||||
responseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n Scope) LastResponseBody() *bytes.Buffer {
|
||||
return n.lastResponseBody
|
||||
func (n Scope) ResponseBody() *bytes.Buffer {
|
||||
return n.responseBody
|
||||
}
|
||||
|
||||
func (n Scope) LastResponseMap() map[string]interface{} {
|
||||
return n.lastResponseMap
|
||||
func (n Scope) ResponseMap() map[string]interface{} {
|
||||
return n.responseMap
|
||||
}
|
||||
|
||||
type ScopeReadResult struct {
|
||||
Item *Scope
|
||||
responseBody *bytes.Buffer
|
||||
responseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n ScopeReadResult) GetItem() interface{} {
|
||||
return n.Item
|
||||
}
|
||||
|
||||
func (n ScopeReadResult) GetResponseBody() *bytes.Buffer {
|
||||
return n.responseBody
|
||||
}
|
||||
|
||||
func (n ScopeReadResult) GetResponseMap() map[string]interface{} {
|
||||
return n.responseMap
|
||||
}
|
||||
|
||||
type ScopeCreateResult = ScopeReadResult
|
||||
type ScopeUpdateResult = ScopeReadResult
|
||||
|
||||
type ScopeDeleteResult struct {
|
||||
responseBody *bytes.Buffer
|
||||
responseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n ScopeDeleteResult) GetResponseBody() *bytes.Buffer {
|
||||
return n.responseBody
|
||||
}
|
||||
|
||||
func (n ScopeDeleteResult) GetResponseMap() map[string]interface{} {
|
||||
return n.responseMap
|
||||
}
|
||||
|
||||
type ScopeListResult struct {
|
||||
Items []*Scope
|
||||
responseBody *bytes.Buffer
|
||||
responseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n ScopeListResult) GetItems() interface{} {
|
||||
return n.Items
|
||||
}
|
||||
|
||||
func (n ScopeListResult) GetResponseBody() *bytes.Buffer {
|
||||
return n.responseBody
|
||||
}
|
||||
|
||||
func (n ScopeListResult) GetResponseMap() map[string]interface{} {
|
||||
return n.responseMap
|
||||
}
|
||||
|
||||
// Client is a client for this collection
|
||||
@@ -56,7 +107,7 @@ func (c *Client) ApiClient() *api.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*Scope, *api.Error, error) {
|
||||
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*ScopeCreateResult, *api.Error, error) {
|
||||
if scopeId == "" {
|
||||
return nil, nil, fmt.Errorf("empty scopeId value passed into Create request")
|
||||
}
|
||||
@@ -87,18 +138,21 @@ func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*Sc
|
||||
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Scope)
|
||||
apiErr, err := resp.Decode(target)
|
||||
target := new(ScopeCreateResult)
|
||||
target.Item = new(Scope)
|
||||
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, scopeId string, opt ...Option) (*Scope, *api.Error, error) {
|
||||
func (c *Client) Read(ctx context.Context, scopeId string, opt ...Option) (*ScopeReadResult, *api.Error, error) {
|
||||
if scopeId == "" {
|
||||
return nil, nil, fmt.Errorf("empty scopeId value passed into Read request")
|
||||
}
|
||||
@@ -126,18 +180,21 @@ func (c *Client) Read(ctx context.Context, scopeId string, opt ...Option) (*Scop
|
||||
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Scope)
|
||||
apiErr, err := resp.Decode(target)
|
||||
target := new(ScopeReadResult)
|
||||
target.Item = new(Scope)
|
||||
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, scopeId string, version uint32, opt ...Option) (*Scope, *api.Error, error) {
|
||||
func (c *Client) Update(ctx context.Context, scopeId string, version uint32, opt ...Option) (*ScopeUpdateResult, *api.Error, error) {
|
||||
if scopeId == "" {
|
||||
return nil, nil, fmt.Errorf("empty scopeId value passed into Update request")
|
||||
}
|
||||
@@ -159,9 +216,12 @@ func (c *Client) Update(ctx context.Context, scopeId string, version uint32, opt
|
||||
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
|
||||
@@ -184,30 +244,33 @@ func (c *Client) Update(ctx context.Context, scopeId string, version uint32, opt
|
||||
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Scope)
|
||||
apiErr, err := resp.Decode(target)
|
||||
target := new(ScopeUpdateResult)
|
||||
target.Item = new(Scope)
|
||||
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, scopeId string, opt ...Option) (bool, *api.Error, error) {
|
||||
func (c *Client) Delete(ctx context.Context, scopeId string, opt ...Option) (*ScopeDeleteResult, *api.Error, error) {
|
||||
if scopeId == "" {
|
||||
return false, nil, fmt.Errorf("empty scopeId value passed into Delete request")
|
||||
return nil, nil, fmt.Errorf("empty scopeId 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("scopes/%s", scopeId), 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 {
|
||||
@@ -220,25 +283,25 @@ func (c *Client) Delete(ctx context.Context, scopeId string, opt ...Option) (boo
|
||||
|
||||
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 := &ScopeDeleteResult{
|
||||
responseBody: resp.Body,
|
||||
responseMap: resp.Map,
|
||||
}
|
||||
return target, nil, nil
|
||||
}
|
||||
|
||||
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Scope, *api.Error, error) {
|
||||
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*ScopeListResult, *api.Error, error) {
|
||||
if scopeId == "" {
|
||||
return nil, nil, fmt.Errorf("empty scopeId value passed into List request")
|
||||
}
|
||||
@@ -267,10 +330,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Sc
|
||||
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
|
||||
}
|
||||
|
||||
type listResponse struct {
|
||||
Items []*Scope
|
||||
}
|
||||
target := &listResponse{}
|
||||
target := new(ScopeListResult)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
|
||||
@@ -278,5 +338,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Sc
|
||||
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,23 +1,10 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package scopes
|
||||
|
||||
import "bytes"
|
||||
|
||||
type ScopeInfo struct {
|
||||
Id string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
ParentScopeId string `json:"parent_scope_id,omitempty"`
|
||||
|
||||
lastResponseBody *bytes.Buffer
|
||||
lastResponseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n ScopeInfo) LastResponseBody() *bytes.Buffer {
|
||||
return n.lastResponseBody
|
||||
}
|
||||
|
||||
func (n ScopeInfo) LastResponseMap() map[string]interface{} {
|
||||
return n.lastResponseMap
|
||||
}
|
||||
@@ -28,18 +28,19 @@ func TestList(t *testing.T) {
|
||||
pl, apiErr, err := scps.List(tc.Context(), org.GetPublicId())
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.Empty(pl)
|
||||
assert.Empty(pl.Items)
|
||||
|
||||
expected := make([]*scopes.Scope, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
expected[i], apiErr, err = scps.Create(tc.Context(), org.GetPublicId(), scopes.WithName(fmt.Sprintf("%d", i)))
|
||||
scr, apiErr, err := scps.Create(tc.Context(), org.GetPublicId(), scopes.WithName(fmt.Sprintf("%d", i)))
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
expected[i] = scr.Item
|
||||
}
|
||||
pl, apiErr, err = scps.List(tc.Context(), org.GetPublicId())
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableSlice(expected), comparableSlice(pl))
|
||||
assert.ElementsMatch(comparableSlice(expected), comparableSlice(pl.Items))
|
||||
}
|
||||
|
||||
func comparableSlice(in []*scopes.Scope) []scopes.Scope {
|
||||
@@ -83,26 +84,25 @@ func TestCrud(t *testing.T) {
|
||||
}
|
||||
|
||||
s, apiErr, err := scps.Create(tc.Context(), org.GetPublicId(), scopes.WithName("foo"))
|
||||
checkProject("create", s, apiErr, err, "foo", 1)
|
||||
checkProject("create", s.Item, apiErr, err, "foo", 1)
|
||||
|
||||
s, apiErr, err = scps.Read(tc.Context(), s.Id)
|
||||
checkProject("read", s, apiErr, err, "foo", 1)
|
||||
s, apiErr, err = scps.Read(tc.Context(), s.Item.Id)
|
||||
checkProject("read", s.Item, apiErr, err, "foo", 1)
|
||||
|
||||
s, apiErr, err = scps.Update(tc.Context(), s.Id, s.Version, scopes.WithName("bar"))
|
||||
checkProject("update", s, apiErr, err, "bar", 2)
|
||||
s, apiErr, err = scps.Update(tc.Context(), s.Item.Id, s.Item.Version, scopes.WithName("bar"))
|
||||
checkProject("update", s.Item, apiErr, err, "bar", 2)
|
||||
|
||||
s, apiErr, err = scps.Update(tc.Context(), s.Id, s.Version, scopes.DefaultName())
|
||||
checkProject("update, unset name", s, apiErr, err, "", 3)
|
||||
s, apiErr, err = scps.Update(tc.Context(), s.Item.Id, s.Item.Version, scopes.DefaultName())
|
||||
checkProject("update, unset name", s.Item, apiErr, err, "", 3)
|
||||
|
||||
existed, apiErr, err := scps.Delete(tc.Context(), s.Id)
|
||||
_, apiErr, err = scps.Delete(tc.Context(), s.Item.Id)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.True(existed, "Expected existing project when deleted, but it wasn't.")
|
||||
|
||||
existed, apiErr, err = scps.Delete(tc.Context(), s.Id)
|
||||
_, apiErr, err = scps.Delete(tc.Context(), s.Item.Id)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.False(existed)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusNotFound, apiErr.Status)
|
||||
}
|
||||
|
||||
// TODO: Get better coverage for expected errors and error formats.
|
||||
Reference in new issue
Block a user