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"
|
||||
|
||||
@@ -29,16 +28,68 @@ type Host struct {
|
||||
HostSetIds []string `json:"host_set_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 Host) LastResponseBody() *bytes.Buffer {
|
||||
return n.lastResponseBody
|
||||
func (n Host) ResponseBody() *bytes.Buffer {
|
||||
return n.responseBody
|
||||
}
|
||||
|
||||
func (n Host) LastResponseMap() map[string]interface{} {
|
||||
return n.lastResponseMap
|
||||
func (n Host) ResponseMap() map[string]interface{} {
|
||||
return n.responseMap
|
||||
}
|
||||
|
||||
type HostReadResult struct {
|
||||
Item *Host
|
||||
responseBody *bytes.Buffer
|
||||
responseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n HostReadResult) GetItem() interface{} {
|
||||
return n.Item
|
||||
}
|
||||
|
||||
func (n HostReadResult) GetResponseBody() *bytes.Buffer {
|
||||
return n.responseBody
|
||||
}
|
||||
|
||||
func (n HostReadResult) GetResponseMap() map[string]interface{} {
|
||||
return n.responseMap
|
||||
}
|
||||
|
||||
type HostCreateResult = HostReadResult
|
||||
type HostUpdateResult = HostReadResult
|
||||
|
||||
type HostDeleteResult struct {
|
||||
responseBody *bytes.Buffer
|
||||
responseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n HostDeleteResult) GetResponseBody() *bytes.Buffer {
|
||||
return n.responseBody
|
||||
}
|
||||
|
||||
func (n HostDeleteResult) GetResponseMap() map[string]interface{} {
|
||||
return n.responseMap
|
||||
}
|
||||
|
||||
type HostListResult struct {
|
||||
Items []*Host
|
||||
responseBody *bytes.Buffer
|
||||
responseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n HostListResult) GetItems() interface{} {
|
||||
return n.Items
|
||||
}
|
||||
|
||||
func (n HostListResult) GetResponseBody() *bytes.Buffer {
|
||||
return n.responseBody
|
||||
}
|
||||
|
||||
func (n HostListResult) 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) (*Host, *api.Error, error) {
|
||||
func (c *Client) Create(ctx context.Context, hostCatalogId string, opt ...Option) (*HostCreateResult, *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(Host)
|
||||
apiErr, err := resp.Decode(target)
|
||||
target := new(HostCreateResult)
|
||||
target.Item = new(Host)
|
||||
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, hostId string, opt ...Option) (*Host, *api.Error, error) {
|
||||
func (c *Client) Read(ctx context.Context, hostId string, opt ...Option) (*HostReadResult, *api.Error, error) {
|
||||
if hostId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostId value passed into Read request")
|
||||
}
|
||||
@@ -129,18 +183,21 @@ func (c *Client) Read(ctx context.Context, hostId string, opt ...Option) (*Host,
|
||||
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Host)
|
||||
apiErr, err := resp.Decode(target)
|
||||
target := new(HostReadResult)
|
||||
target.Item = new(Host)
|
||||
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, hostId string, version uint32, opt ...Option) (*Host, *api.Error, error) {
|
||||
func (c *Client) Update(ctx context.Context, hostId string, version uint32, opt ...Option) (*HostUpdateResult, *api.Error, error) {
|
||||
if hostId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostId value passed into Update request")
|
||||
}
|
||||
@@ -162,9 +219,12 @@ func (c *Client) Update(ctx context.Context, hostId 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
|
||||
@@ -187,30 +247,33 @@ func (c *Client) Update(ctx context.Context, hostId string, version uint32, opt
|
||||
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Host)
|
||||
apiErr, err := resp.Decode(target)
|
||||
target := new(HostUpdateResult)
|
||||
target.Item = new(Host)
|
||||
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, hostId string, opt ...Option) (bool, *api.Error, error) {
|
||||
func (c *Client) Delete(ctx context.Context, hostId string, opt ...Option) (*HostDeleteResult, *api.Error, error) {
|
||||
if hostId == "" {
|
||||
return false, nil, fmt.Errorf("empty hostId value passed into Delete request")
|
||||
return nil, nil, fmt.Errorf("empty hostId 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("hosts/%s", hostId), 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, hostId string, opt ...Option) (bool
|
||||
|
||||
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 := &HostDeleteResult{
|
||||
responseBody: resp.Body,
|
||||
responseMap: resp.Map,
|
||||
}
|
||||
return target, nil, nil
|
||||
}
|
||||
|
||||
func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option) ([]*Host, *api.Error, error) {
|
||||
func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option) (*HostListResult, *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 []*Host
|
||||
}
|
||||
target := &listResponse{}
|
||||
target := new(HostListResult)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
|
||||
@@ -281,5 +341,7 @@ 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
|
||||
}
|
||||
+26
-24
@@ -32,34 +32,36 @@ func TestList(t *testing.T) {
|
||||
|
||||
hClient := hosts.NewClient(client)
|
||||
|
||||
ul, apiErr, err := hClient.List(tc.Context(), hc.Id)
|
||||
ul, apiErr, err := hClient.List(tc.Context(), hc.Item.Id)
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.Empty(ul)
|
||||
assert.Empty(ul.Items)
|
||||
|
||||
var expected []*hosts.Host
|
||||
for i := 0; i < 10; i++ {
|
||||
expected = append(expected, &hosts.Host{Name: fmt.Sprint(i)})
|
||||
}
|
||||
|
||||
expected[0], apiErr, err = hClient.Create(tc.Context(), hc.Id, hosts.WithName(expected[0].Name), hosts.WithStaticHostAddress("someaddress"))
|
||||
hcr, apiErr, err := hClient.Create(tc.Context(), hc.Item.Id, hosts.WithName(expected[0].Name), hosts.WithStaticHostAddress("someaddress"))
|
||||
assert.NoError(err)
|
||||
assert.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)
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableHostSlice(expected[:1]), comparableHostSlice(ul))
|
||||
assert.ElementsMatch(comparableHostSlice(expected[:1]), comparableHostSlice(ul.Items))
|
||||
|
||||
for i := 1; i < 10; i++ {
|
||||
expected[i], apiErr, err = hClient.Create(tc.Context(), hc.Id, hosts.WithName(expected[i].Name), hosts.WithStaticHostAddress("someaddress"))
|
||||
hcr, apiErr, err = hClient.Create(tc.Context(), hc.Item.Id, hosts.WithName(expected[i].Name), hosts.WithStaticHostAddress("someaddress"))
|
||||
assert.NoError(err)
|
||||
assert.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)
|
||||
assert.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableHostSlice(expected), comparableHostSlice(ul))
|
||||
assert.ElementsMatch(comparableHostSlice(expected), comparableHostSlice(ul.Items))
|
||||
}
|
||||
|
||||
func comparableHostSlice(in []*hosts.Host) []hosts.Host {
|
||||
@@ -108,26 +110,26 @@ func TestCrud(t *testing.T) {
|
||||
|
||||
hClient := hosts.NewClient(client)
|
||||
|
||||
h, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
|
||||
checkHost("create", h, apiErr, err, "foo", 1)
|
||||
h, apiErr, err := hClient.Create(tc.Context(), hc.Item.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
|
||||
checkHost("create", h.Item, apiErr, err, "foo", 1)
|
||||
|
||||
h, apiErr, err = hClient.Read(tc.Context(), h.Id)
|
||||
checkHost("read", h, apiErr, err, "foo", 1)
|
||||
h, apiErr, err = hClient.Read(tc.Context(), h.Item.Id)
|
||||
checkHost("read", h.Item, apiErr, err, "foo", 1)
|
||||
|
||||
h, apiErr, err = hClient.Update(tc.Context(), h.Id, h.Version, hosts.WithName("bar"))
|
||||
checkHost("update", h, apiErr, err, "bar", 2)
|
||||
h, apiErr, err = hClient.Update(tc.Context(), h.Item.Id, h.Item.Version, hosts.WithName("bar"))
|
||||
checkHost("update", h.Item, apiErr, err, "bar", 2)
|
||||
|
||||
h, apiErr, err = hClient.Update(tc.Context(), h.Id, h.Version, hosts.DefaultName())
|
||||
checkHost("update", h, apiErr, err, "", 3)
|
||||
h, apiErr, err = hClient.Update(tc.Context(), h.Item.Id, h.Item.Version, hosts.DefaultName())
|
||||
checkHost("update", h.Item, apiErr, err, "", 3)
|
||||
|
||||
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.
|
||||
@@ -148,12 +150,12 @@ func TestErrors(t *testing.T) {
|
||||
|
||||
hClient := hosts.NewClient(client)
|
||||
|
||||
h, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
|
||||
h, apiErr, err := hClient.Create(tc.Context(), hc.Item.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
assert.NotNil(h)
|
||||
|
||||
_, apiErr, err = hClient.Create(tc.Context(), hc.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
|
||||
_, apiErr, err = hClient.Create(tc.Context(), hc.Item.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
|
||||
|
||||
@@ -1,19 +1,6 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package hosts
|
||||
|
||||
import "bytes"
|
||||
|
||||
type StaticHostAttributes struct {
|
||||
Address string `json:"address,omitempty"`
|
||||
|
||||
lastResponseBody *bytes.Buffer
|
||||
lastResponseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n StaticHostAttributes) LastResponseBody() *bytes.Buffer {
|
||||
return n.lastResponseBody
|
||||
}
|
||||
|
||||
func (n StaticHostAttributes) LastResponseMap() map[string]interface{} {
|
||||
return n.lastResponseMap
|
||||
}
|
||||
Reference in new issue
Block a user