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

+128 -48
View File
@@ -6,7 +6,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -29,16 +28,68 @@ type HostSet struct {
HostIds []string `json:"host_ids,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
lastResponseBody *bytes.Buffer
lastResponseMap map[string]interface{}
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n HostSet) LastResponseBody() *bytes.Buffer {
return n.lastResponseBody
func (n HostSet) ResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n HostSet) LastResponseMap() map[string]interface{} {
return n.lastResponseMap
func (n HostSet) ResponseMap() map[string]interface{} {
return n.responseMap
}
type HostSetReadResult struct {
Item *HostSet
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n HostSetReadResult) GetItem() interface{} {
return n.Item
}
func (n HostSetReadResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n HostSetReadResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type HostSetCreateResult = HostSetReadResult
type HostSetUpdateResult = HostSetReadResult
type HostSetDeleteResult struct {
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n HostSetDeleteResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n HostSetDeleteResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type HostSetListResult struct {
Items []*HostSet
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n HostSetListResult) GetItems() interface{} {
return n.Items
}
func (n HostSetListResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n HostSetListResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
// Client is a client for this collection
@@ -59,7 +110,7 @@ func (c *Client) ApiClient() *api.Client {
return c.client
}
func (c *Client) Create(ctx context.Context, hostCatalogId string, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) Create(ctx context.Context, hostCatalogId string, opt ...Option) (*HostSetCreateResult, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Create request")
}
@@ -90,18 +141,21 @@ func (c *Client) Create(ctx context.Context, hostCatalogId string, opt ...Option
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
target := new(HostSetCreateResult)
target.Item = new(HostSet)
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, hostSetId string, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) Read(ctx context.Context, hostSetId string, opt ...Option) (*HostSetReadResult, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into Read request")
}
@@ -129,18 +183,21 @@ func (c *Client) Read(ctx context.Context, hostSetId string, opt ...Option) (*Ho
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
target := new(HostSetReadResult)
target.Item = new(HostSet)
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, hostSetId string, version uint32, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) Update(ctx context.Context, hostSetId string, version uint32, opt ...Option) (*HostSetUpdateResult, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into Update request")
}
@@ -162,9 +219,12 @@ func (c *Client) Update(ctx context.Context, hostSetId string, version uint32, o
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
@@ -187,30 +247,33 @@ func (c *Client) Update(ctx context.Context, hostSetId string, version uint32, o
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
target := new(HostSetUpdateResult)
target.Item = new(HostSet)
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, hostSetId string, opt ...Option) (bool, *api.Error, error) {
func (c *Client) Delete(ctx context.Context, hostSetId string, opt ...Option) (*HostSetDeleteResult, *api.Error, error) {
if hostSetId == "" {
return false, nil, fmt.Errorf("empty hostSetId value passed into Delete request")
return nil, nil, fmt.Errorf("empty hostSetId 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("host-sets/%s", hostSetId), 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 {
@@ -223,25 +286,25 @@ func (c *Client) Delete(ctx context.Context, hostSetId string, opt ...Option) (b
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 := &HostSetDeleteResult{
responseBody: resp.Body,
responseMap: resp.Map,
}
return target, nil, nil
}
func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option) ([]*HostSet, *api.Error, error) {
func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option) (*HostSetListResult, *api.Error, error) {
if hostCatalogId == "" {
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into List request")
}
@@ -270,10 +333,7 @@ func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option)
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
}
type listResponse struct {
Items []*HostSet
}
target := &listResponse{}
target := new(HostSetListResult)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
@@ -281,10 +341,12 @@ func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option)
if apiErr != nil {
return nil, apiErr, nil
}
return target.Items, apiErr, nil
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) AddHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) AddHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSetUpdateResult, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into AddHosts request")
}
@@ -309,9 +371,12 @@ func (c *Client) AddHosts(ctx context.Context, hostSetId 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
@@ -335,18 +400,21 @@ func (c *Client) AddHosts(ctx context.Context, hostSetId string, version uint32,
return nil, nil, fmt.Errorf("error performing client request during AddHosts call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
target := new(HostSetUpdateResult)
target.Item = new(HostSet)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding AddHosts 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) SetHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) SetHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSetUpdateResult, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into SetHosts request")
}
@@ -369,9 +437,12 @@ func (c *Client) SetHosts(ctx context.Context, hostSetId 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
@@ -395,18 +466,21 @@ func (c *Client) SetHosts(ctx context.Context, hostSetId string, version uint32,
return nil, nil, fmt.Errorf("error performing client request during SetHosts call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
target := new(HostSetUpdateResult)
target.Item = new(HostSet)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding SetHosts 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) RemoveHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSet, *api.Error, error) {
func (c *Client) RemoveHosts(ctx context.Context, hostSetId string, version uint32, hostIds []string, opt ...Option) (*HostSetUpdateResult, *api.Error, error) {
if hostSetId == "" {
return nil, nil, fmt.Errorf("empty hostSetId value passed into RemoveHosts request")
}
@@ -431,9 +505,12 @@ func (c *Client) RemoveHosts(ctx context.Context, hostSetId string, version uint
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
@@ -457,13 +534,16 @@ func (c *Client) RemoveHosts(ctx context.Context, hostSetId string, version uint
return nil, nil, fmt.Errorf("error performing client request during RemoveHosts call: %w", err)
}
target := new(HostSet)
apiErr, err := resp.Decode(target)
target := new(HostSetUpdateResult)
target.Item = new(HostSet)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding RemoveHosts response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
+34 -37
View File
@@ -32,33 +32,32 @@ func TestCustom(t *testing.T) {
require.Nil(apiErr)
hClient := hosts.NewClient(client)
h1, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithStaticHostAddress("someaddress"))
h1, apiErr, err := hClient.Create(tc.Context(), hc.Item.Id, hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
require.Nil(apiErr)
h2, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithStaticHostAddress("someaddress"))
h2, apiErr, err := hClient.Create(tc.Context(), hc.Item.Id, hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
require.Nil(apiErr)
hSetClient := hostsets.NewClient(client)
var hSet *hostsets.HostSet
hSet, apiErr, err = hSetClient.Create(tc.Context(), hc.Id)
hSet, apiErr, err := hSetClient.Create(tc.Context(), hc.Item.Id)
require.NoError(err)
require.Nil(apiErr)
hSet, apiErr, err = hSetClient.AddHosts(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id, h2.Id})
hSet, apiErr, err = hSetClient.AddHosts(tc.Context(), hSet.Item.Id, hSet.Item.Version, []string{h1.Item.Id, h2.Item.Id})
require.NoError(err)
require.Nil(apiErr)
assert.Contains(hSet.HostIds, h1.Id, h2.Id)
assert.Contains(hSet.Item.HostIds, h1.Item.Id, h2.Item.Id)
hSet, apiErr, err = hSetClient.SetHosts(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id})
hSet, apiErr, err = hSetClient.SetHosts(tc.Context(), hSet.Item.Id, hSet.Item.Version, []string{h1.Item.Id})
require.NoError(err)
require.Nil(apiErr, pretty.Sprint(apiErr))
assert.ElementsMatch([]string{h1.Id}, hSet.HostIds)
assert.ElementsMatch([]string{h1.Item.Id}, hSet.Item.HostIds)
hSet, apiErr, err = hSetClient.RemoveHosts(tc.Context(), hSet.Id, hSet.Version, []string{h1.Id})
hSet, apiErr, err = hSetClient.RemoveHosts(tc.Context(), hSet.Item.Id, hSet.Item.Version, []string{h1.Item.Id})
require.NoError(err)
require.Nil(apiErr)
assert.Empty(hSet.HostIds)
assert.Empty(hSet.Item.HostIds)
}
func TestList(t *testing.T) {
@@ -77,36 +76,37 @@ func TestList(t *testing.T) {
require.NotNil(hc)
hClient := hostsets.NewClient(client)
var ul []*hostsets.HostSet
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
ul, apiErr, err := hClient.List(tc.Context(), hc.Item.Id)
require.NoError(err)
require.Nil(apiErr)
assert.Empty(ul)
assert.Empty(ul.Items)
var expected []*hostsets.HostSet
for i := 0; i < 10; i++ {
expected = append(expected, &hostsets.HostSet{Name: fmt.Sprint(i)})
}
expected[0], apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName(expected[0].Name))
hcr, apiErr, err := hClient.Create(tc.Context(), hc.Item.Id, hostsets.WithName(expected[0].Name))
require.NoError(err)
require.Nil(apiErr)
expected[0] = hcr.Item
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
ul, apiErr, err = hClient.List(tc.Context(), hc.Item.Id)
require.NoError(err)
require.Nil(apiErr)
assert.ElementsMatch(comparableSetSlice(expected[:1]), comparableSetSlice(ul))
assert.ElementsMatch(comparableSetSlice(expected[:1]), comparableSetSlice(ul.Items))
for i := 1; i < 10; i++ {
expected[i], apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName(expected[i].Name))
hcr, apiErr, err = hClient.Create(tc.Context(), hc.Item.Id, hostsets.WithName(expected[i].Name))
require.NoError(err)
require.Nil(apiErr)
expected[i] = hcr.Item
}
ul, apiErr, err = hClient.List(tc.Context(), hc.Id)
ul, apiErr, err = hClient.List(tc.Context(), hc.Item.Id)
require.NoError(err)
require.Nil(apiErr)
assert.ElementsMatch(comparableSetSlice(expected), comparableSetSlice(ul))
assert.ElementsMatch(comparableSetSlice(expected), comparableSetSlice(ul.Items))
}
func comparableSetSlice(in []*hostsets.HostSet) []hostsets.HostSet {
@@ -156,27 +156,25 @@ func TestCrud(t *testing.T) {
hClient := hostsets.NewClient(client)
var h *hostsets.HostSet
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
checkHost(t, "create", h, apiErr, err, "foo", 1)
h, apiErr, err := hClient.Create(tc.Context(), hc.Item.Id, hostsets.WithName("foo"))
checkHost(t, "create", h.Item, apiErr, err, "foo", 1)
h, apiErr, err = hClient.Read(tc.Context(), h.Id)
checkHost(t, "read", h, apiErr, err, "foo", 1)
h, apiErr, err = hClient.Read(tc.Context(), h.Item.Id)
checkHost(t, "read", h.Item, apiErr, err, "foo", 1)
h, apiErr, err = hClient.Update(tc.Context(), h.Id, h.Version, hostsets.WithName("bar"))
checkHost(t, "update", h, apiErr, err, "bar", 2)
h, apiErr, err = hClient.Update(tc.Context(), h.Item.Id, h.Item.Version, hostsets.WithName("bar"))
checkHost(t, "update", h.Item, apiErr, err, "bar", 2)
h, apiErr, err = hClient.Update(tc.Context(), h.Id, h.Version, hostsets.DefaultName())
checkHost(t, "update", h, apiErr, err, "", 3)
h, apiErr, err = hClient.Update(tc.Context(), h.Item.Id, h.Item.Version, hostsets.DefaultName())
checkHost(t, "update", h.Item, apiErr, err, "", 3)
var existed bool
existed, apiErr, err = hClient.Delete(tc.Context(), h.Id)
_, apiErr, err = hClient.Delete(tc.Context(), h.Item.Id)
assert.NoError(err)
assert.True(existed, "Expected existing catalog when deleted, but it wasn't.")
existed, apiErr, err = hClient.Delete(tc.Context(), h.Id)
require.NoError(err)
assert.Nil(apiErr)
assert.False(existed)
_, apiErr, err = hClient.Delete(tc.Context(), h.Item.Id)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
}
// TODO: Get better coverage for expected errors and error formats.
@@ -197,13 +195,12 @@ func TestErrors(t *testing.T) {
hClient := hostsets.NewClient(client)
var h *hostsets.HostSet
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
h, apiErr, err := hClient.Create(tc.Context(), hc.Item.Id, hostsets.WithName("foo"))
require.NoError(err)
require.Nil(apiErr)
assert.NotNil(h)
h, apiErr, err = hClient.Create(tc.Context(), hc.Id, hostsets.WithName("foo"))
h, apiErr, err = hClient.Create(tc.Context(), hc.Item.Id, hostsets.WithName("foo"))
require.NoError(err)
assert.NotNil(apiErr)
assert.Nil(h)