Generate new version of SDK resources and Add Tests (#331)
This commit is contained in:
1 parent
96e4b1cdba
commit
c3ecea172d
93 files changed
+3414
-2826
No files matched your search
+26
-6
@@ -27,24 +27,42 @@ type Group struct {
|
||||
Members []*Member `json:"members,omitempty"`
|
||||
}
|
||||
|
||||
// Client is a client for this collection
|
||||
type Client struct {
|
||||
client *api.Client
|
||||
}
|
||||
|
||||
// Creates a new client for this collection. The submitted API client is cloned;
|
||||
// modifications to it after generating this client will not have effect. If you
|
||||
// need to make changes to the underlying API client, use ApiClient() to access
|
||||
// it.
|
||||
func NewClient(c *api.Client) *Client {
|
||||
return &Client{client: c}
|
||||
return &Client{client: c.Clone()}
|
||||
}
|
||||
|
||||
func (c *Client) Create(ctx context.Context, opt ...Option) (*Group, *api.Error, error) {
|
||||
// ApiClient returns the underlying API client
|
||||
func (c *Client) ApiClient() *api.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*Group, *api.Error, error) {
|
||||
if scopeId == "" {
|
||||
return nil, nil, fmt.Errorf("empty scopeId value passed into Create request")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts.postMap["scope_id"] = scopeId
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", "groups", opts.postMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
|
||||
}
|
||||
|
||||
if len(opts.queryMap) > 0 {
|
||||
q := url.Values{}
|
||||
for k, v := range opts.queryMap {
|
||||
@@ -71,9 +89,8 @@ func (c *Client) Create(ctx context.Context, opt ...Option) (*Group, *api.Error,
|
||||
|
||||
func (c *Client) Read(ctx context.Context, groupId string, opt ...Option) (*Group, *api.Error, error) {
|
||||
if groupId == "" {
|
||||
return nil, nil, fmt.Errorf("empty groupId value passed into Read request")
|
||||
return nil, nil, fmt.Errorf("empty groupId value passed into Read request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
@@ -171,7 +188,6 @@ func (c *Client) Delete(ctx context.Context, groupId string, opt ...Option) (boo
|
||||
if groupId == "" {
|
||||
return false, nil, fmt.Errorf("empty groupId value passed into Delete request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return false, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
@@ -210,12 +226,16 @@ func (c *Client) Delete(ctx context.Context, groupId string, opt ...Option) (boo
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *Client) List(ctx context.Context, opt ...Option) ([]*Group, *api.Error, error) {
|
||||
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Group, *api.Error, error) {
|
||||
if scopeId == "" {
|
||||
return nil, nil, fmt.Errorf("empty scopeId value passed into List request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
opts.queryMap["scope_id"] = scopeId
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", "groups", nil, apiOpts...)
|
||||
if err != nil {
|
||||
|
||||
+44
-65
@@ -14,52 +14,45 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGroup_List(t *testing.T) {
|
||||
func TestList(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
amId := "ampw_1234567890"
|
||||
tc := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
DisableAuthorizationFailures: true,
|
||||
DefaultAuthMethodId: amId,
|
||||
DefaultLoginName: "user",
|
||||
DefaultPassword: "passpass",
|
||||
})
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
token := tc.Token()
|
||||
client.SetToken(token.Token)
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
scopeClient *api.Client
|
||||
name string
|
||||
scopeId string
|
||||
}{
|
||||
{
|
||||
name: "org",
|
||||
scopeClient: client,
|
||||
name: "org",
|
||||
scopeId: org.GetPublicId(),
|
||||
},
|
||||
{
|
||||
name: "proj",
|
||||
scopeClient: projClient,
|
||||
name: "proj",
|
||||
scopeId: proj.GetPublicId(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
grps := groups.NewClient(tt.scopeClient)
|
||||
pl, apiErr, err := grps.List(tc.Context())
|
||||
grps := groups.NewClient(client)
|
||||
pl, apiErr, err := grps.List(tc.Context(), tt.scopeId)
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.Empty(pl)
|
||||
|
||||
expected := make([]*groups.Group, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
expected[i], apiErr, err = grps.Create(tc.Context(), groups.WithName(fmt.Sprint(i)))
|
||||
expected[i], apiErr, err = grps.Create(tc.Context(), tt.scopeId, groups.WithName(fmt.Sprint(i)))
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
}
|
||||
pl, apiErr, err = grps.List(tc.Context())
|
||||
pl, apiErr, err = grps.List(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
require.NotNil(pl)
|
||||
@@ -84,22 +77,15 @@ func comparableSlice(in []*groups.Group) []groups.Group {
|
||||
return filtered
|
||||
}
|
||||
|
||||
func TestGroup_Crud(t *testing.T) {
|
||||
func TestCrud(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
amId := "ampw_1234567890"
|
||||
tc := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
DisableAuthorizationFailures: true,
|
||||
DefaultAuthMethodId: amId,
|
||||
DefaultLoginName: "user",
|
||||
DefaultPassword: "passpass",
|
||||
})
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
token := tc.Token()
|
||||
client.SetToken(token.Token)
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
|
||||
|
||||
checkGroup := func(step string, g *groups.Group, apiErr *api.Error, err error, wantedName string, wantedVersion uint32, expectedUserIds []string) {
|
||||
require.NoError(err, step)
|
||||
@@ -120,31 +106,31 @@ func TestGroup_Crud(t *testing.T) {
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
scopeClient *api.Client
|
||||
name string
|
||||
scopeId string
|
||||
}{
|
||||
{
|
||||
name: "org",
|
||||
scopeClient: client,
|
||||
name: "org",
|
||||
scopeId: org.GetPublicId(),
|
||||
},
|
||||
{
|
||||
name: "proj",
|
||||
scopeClient: projClient,
|
||||
name: "proj",
|
||||
scopeId: proj.GetPublicId(),
|
||||
},
|
||||
}
|
||||
|
||||
user1, apiErr, err := users.NewClient(client).Create(tc.Context())
|
||||
user1, apiErr, err := users.NewClient(client).Create(tc.Context(), org.GetPublicId())
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
|
||||
user2, apiErr, err := users.NewClient(client).Create(tc.Context())
|
||||
user2, apiErr, err := users.NewClient(client).Create(tc.Context(), org.GetPublicId())
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
groupsClient := groups.NewClient(tt.scopeClient)
|
||||
g, apiErr, err := groupsClient.Create(tc.Context(), groups.WithName("foo"))
|
||||
groupsClient := groups.NewClient(client)
|
||||
g, apiErr, err := groupsClient.Create(tc.Context(), tt.scopeId, groups.WithName("foo"))
|
||||
checkGroup("create", g, apiErr, err, "foo", 1, nil)
|
||||
|
||||
g, apiErr, err = groupsClient.Read(tc.Context(), g.Id)
|
||||
@@ -177,48 +163,41 @@ func TestGroup_Crud(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroup_Errors(t *testing.T) {
|
||||
func TestErrors(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
amId := "ampw_1234567890"
|
||||
tc := controller.NewTestController(t, &controller.TestControllerOpts{
|
||||
DisableAuthorizationFailures: true,
|
||||
DefaultAuthMethodId: amId,
|
||||
DefaultLoginName: "user",
|
||||
DefaultPassword: "passpass",
|
||||
})
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
token := tc.Token()
|
||||
client.SetToken(token.Token)
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
scopeClient *api.Client
|
||||
name string
|
||||
scopeId string
|
||||
}{
|
||||
{
|
||||
name: "org",
|
||||
scopeClient: client,
|
||||
name: "org",
|
||||
scopeId: org.GetPublicId(),
|
||||
},
|
||||
{
|
||||
name: "proj",
|
||||
scopeClient: projClient,
|
||||
name: "proj",
|
||||
scopeId: proj.GetPublicId(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
groupClient := groups.NewClient(tt.scopeClient)
|
||||
groupClient := groups.NewClient(client)
|
||||
|
||||
g, apiErr, err := groupClient.Create(tc.Context(), groups.WithName("first"))
|
||||
g, apiErr, err := groupClient.Create(tc.Context(), tt.scopeId, groups.WithName("first"))
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.NotNil(g)
|
||||
|
||||
// Create another resource with the same name.
|
||||
_, apiErr, err = groupClient.Create(tc.Context(), groups.WithName("first"))
|
||||
_, apiErr, err = groupClient.Create(tc.Context(), tt.scopeId, groups.WithName("first"))
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ type Option func(*options)
|
||||
type options struct {
|
||||
postMap map[string]interface{}
|
||||
queryMap map[string]string
|
||||
withScopeId string
|
||||
withAutomaticVersioning bool
|
||||
}
|
||||
|
||||
@@ -33,25 +32,16 @@ func getOpts(opt ...Option) (options, []api.Option) {
|
||||
o(&opts)
|
||||
}
|
||||
var apiOpts []api.Option
|
||||
if opts.withScopeId != "" {
|
||||
apiOpts = append(apiOpts, api.WithScopeId(opts.withScopeId))
|
||||
}
|
||||
return opts, apiOpts
|
||||
}
|
||||
|
||||
func WithScopeId(id string) Option {
|
||||
return func(o *options) {
|
||||
o.withScopeId = id
|
||||
}
|
||||
}
|
||||
|
||||
// If set, and if the version is zero during an update, the API will perform a
|
||||
// fetch to get the current version of the resource and populate it during the
|
||||
// update call. This is convenient but opens up the possibility for subtle
|
||||
// order-of-modification issues, so use carefully.
|
||||
func WithAutomaticVersioning() Option {
|
||||
func WithAutomaticVersioning(enable bool) Option {
|
||||
return func(o *options) {
|
||||
o.withAutomaticVersioning = true
|
||||
o.withAutomaticVersioning = enable
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user