Update API codes (#336)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-09-08 23:57:26 -04:00
1 parent 99d5456d7a
commit f94f21fd97
74 files changed
+1557 -1802

No files matched your search

+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -212,18 +213,19 @@ func (c *Client) Delete(ctx context.Context, accountId string, opt ...Option) (b
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, authMethodId string, opt ...Option) ([]*Account, *api.Error, error) {
+1 -1
View File
@@ -180,7 +180,7 @@ func TestErrors(t *testing.T) {
_, apiErr, err = accountClient.Read(tc.Context(), password.AccountPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = accountClient.Read(tc.Context(), "invalid id")
require.NoError(err)
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -217,18 +218,19 @@ func (c *Client) Delete(ctx context.Context, authMethodId string, opt ...Option)
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*AuthMethod, *api.Error, error) {
+1 -1
View File
@@ -157,7 +157,7 @@ func TestErrors(t *testing.T) {
_, apiErr, err := tokens.Read(tc.Context(), authtoken.AuthTokenPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = tokens.Read(tc.Context(), "invalid id")
require.NoError(err)
+8 -6
View File
@@ -4,6 +4,7 @@ package authtokens
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
@@ -109,18 +110,19 @@ func (c *Client) Delete(ctx context.Context, authTokenId string, opt ...Option)
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*AuthToken, *api.Error, error) {
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -212,18 +213,19 @@ func (c *Client) Delete(ctx context.Context, groupId string, opt ...Option) (boo
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Group, *api.Error, error) {
+3 -3
View File
@@ -157,8 +157,8 @@ func TestCrud(t *testing.T) {
existed, apiErr, err = groupsClient.Delete(tc.Context(), g.Id)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.Nil(apiErr)
assert.False(existed)
})
}
}
@@ -204,7 +204,7 @@ func TestErrors(t *testing.T) {
_, apiErr, err = groupClient.Read(tc.Context(), iam.GroupPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = groupClient.Read(tc.Context(), "invalid id")
require.NoError(err)
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -217,18 +218,19 @@ func (c *Client) Delete(ctx context.Context, hostCatalogId string, opt ...Option
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*HostCatalog, *api.Error, error) {
+3 -3
View File
@@ -114,8 +114,8 @@ func TestCrud(t *testing.T) {
existed, apiErr, err = hcClient.Delete(tc.Context(), hc.Id)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.Nil(apiErr)
assert.False(existed)
}
// TODO: Get better coverage for expected errors and error formats.
@@ -143,7 +143,7 @@ func TestErrors(t *testing.T) {
require.NoError(err)
// TODO: Should this be nil instead of just a catalog that has no values set
assert.NotNil(apiErr)
assert.EqualValues(apiErr.Status, http.StatusForbidden)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = pc.Read(tc.Context(), "invalid id")
require.NoError(err)
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -213,18 +214,19 @@ func (c *Client) Delete(ctx context.Context, hostId string, opt ...Option) (bool
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option) ([]*Host, *api.Error, error) {
+3 -3
View File
@@ -126,8 +126,8 @@ func TestCrud(t *testing.T) {
existed, apiErr, err = hClient.Delete(tc.Context(), h.Id)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.Nil(apiErr)
assert.False(existed)
}
// TODO: Get better coverage for expected errors and error formats.
@@ -160,7 +160,7 @@ func TestErrors(t *testing.T) {
_, apiErr, err = hClient.Read(tc.Context(), static.HostPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(apiErr.Status, http.StatusForbidden)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = hClient.Read(tc.Context(), "invalid id")
require.NoError(err)
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -213,18 +214,19 @@ func (c *Client) Delete(ctx context.Context, hostSetId string, opt ...Option) (b
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, hostCatalogId string, opt ...Option) ([]*HostSet, *api.Error, error) {
+3 -3
View File
@@ -175,8 +175,8 @@ func TestCrud(t *testing.T) {
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.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.Nil(apiErr)
assert.False(existed)
}
// TODO: Get better coverage for expected errors and error formats.
@@ -211,7 +211,7 @@ func TestErrors(t *testing.T) {
_, apiErr, err = hClient.Read(tc.Context(), static.HostSetPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = hClient.Read(tc.Context(), "invalid id")
require.NoError(err)
+7 -6
View File
@@ -321,18 +321,19 @@ func (c *Client) Delete(ctx context.Context, {{ .ResourceFunctionArg }} string,
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
`))
+4 -6
View File
@@ -41,10 +41,6 @@ func (r *Response) Decode(inStruct interface{}) (*Error, error) {
}, nil
}
if inStruct == nil {
return nil, fmt.Errorf("nil value given to decode into and not a 204 response")
}
apiErr := &Error{
Status: int32(r.resp.StatusCode),
}
@@ -59,8 +55,10 @@ func (r *Response) Decode(inStruct interface{}) (*Error, error) {
if r.resp.StatusCode >= 400 {
inStruct = apiErr
}
if err := dec.Decode(inStruct); err != nil {
return nil, fmt.Errorf("error decoding response: %w; response was %s", err, r.Body.String())
if inStruct != nil {
if err := dec.Decode(inStruct); err != nil {
return nil, fmt.Errorf("error decoding response: %w; response was %s", err, r.Body.String())
}
}
}
}
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -215,18 +216,19 @@ func (c *Client) Delete(ctx context.Context, roleId string, opt ...Option) (bool
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Role, *api.Error, error) {
+1 -1
View File
@@ -299,7 +299,7 @@ func TestErrors(t *testing.T) {
_, apiErr, err = roleClient.Read(tc.Context(), iam.RolePrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = roleClient.Read(tc.Context(), "invalid id")
require.NoError(err)
+4 -4
View File
@@ -99,10 +99,10 @@ func TestCrud(t *testing.T) {
assert.Nil(apiErr)
assert.True(existed, "Expected existing project when deleted, but it wasn't.")
_, apiErr, err = scps.Delete(tc.Context(), s.Id)
existed, apiErr, err = scps.Delete(tc.Context(), s.Id)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status, "Expected project to not exist when deleted, but it did.")
assert.Nil(apiErr)
assert.False(existed)
}
// TODO: Get better coverage for expected errors and error formats.
@@ -127,7 +127,7 @@ func TestErrors(t *testing.T) {
require.NoError(err)
// TODO: Should this be nil instead of just a Project that has no values set
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = scps.Read(tc.Context(), "invalid id")
assert.NoError(err)
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -210,18 +211,19 @@ func (c *Client) Delete(ctx context.Context, scopeId string, opt ...Option) (boo
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Scope, *api.Error, error) {
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -219,18 +220,19 @@ func (c *Client) Delete(ctx context.Context, targetId string, opt ...Option) (bo
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Target, *api.Error, error) {
+3 -3
View File
@@ -164,8 +164,8 @@ func TestCrud(t *testing.T) {
existed, apiErr, err = tarClient.Delete(tc.Context(), tar.Id)
assert.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(apiErr.Status, http.StatusForbidden)
assert.Nil(apiErr)
assert.False(existed)
}
// TODO: Get better coverage for expected errors and error formats.
@@ -193,7 +193,7 @@ func TestSet_Errors(t *testing.T) {
_, apiErr, err = tarClient.Read(tc.Context(), target.TcpTargetPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = tarClient.Read(tc.Context(), "invalid id")
require.NoError(err)
+8 -6
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -210,18 +211,19 @@ func (c *Client) Delete(ctx context.Context, userId string, opt ...Option) (bool
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)
apiErr, err := resp.Decode(nil)
if err != nil {
return false, 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 target.Existed, apiErr, nil
return true, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*User, *api.Error, error) {
+3 -3
View File
@@ -113,8 +113,8 @@ func TestCrud(t *testing.T) {
existed, apiErr, err = userClient.Delete(tc.Context(), u.Id)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.Nil(apiErr)
assert.False(existed)
}
func TestErrors(t *testing.T) {
@@ -141,7 +141,7 @@ func TestErrors(t *testing.T) {
_, apiErr, err = userClient.Read(tc.Context(), iam.UserPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = userClient.Read(tc.Context(), "invalid id")
require.NoError(err)