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
+2
-12
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26
-6
@@ -30,24 +30,42 @@ type Role struct {
|
||||
Grants []*Grant `json:"grants,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) (*Role, *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) (*Role, *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", "roles", 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 {
|
||||
@@ -74,9 +92,8 @@ func (c *Client) Create(ctx context.Context, opt ...Option) (*Role, *api.Error,
|
||||
|
||||
func (c *Client) Read(ctx context.Context, roleId string, opt ...Option) (*Role, *api.Error, error) {
|
||||
if roleId == "" {
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into Read request")
|
||||
return nil, nil, fmt.Errorf("empty roleId value passed into Read request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
@@ -174,7 +191,6 @@ func (c *Client) Delete(ctx context.Context, roleId string, opt ...Option) (bool
|
||||
if roleId == "" {
|
||||
return false, nil, fmt.Errorf("empty roleId value passed into Delete request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return false, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
@@ -213,12 +229,16 @@ func (c *Client) Delete(ctx context.Context, roleId string, opt ...Option) (bool
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *Client) List(ctx context.Context, opt ...Option) ([]*Role, *api.Error, error) {
|
||||
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Role, *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", "roles", nil, apiOpts...)
|
||||
if err != nil {
|
||||
|
||||
+65
-91
@@ -18,36 +18,29 @@ import (
|
||||
|
||||
func TestCustom(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(),
|
||||
},
|
||||
}
|
||||
|
||||
user, apiErr, err := users.NewClient(client).Create(tc.Context())
|
||||
user, apiErr, err := users.NewClient(client).Create(tc.Context(), org.GetPublicId())
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
|
||||
@@ -69,15 +62,15 @@ func TestCustom(t *testing.T) {
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
g, apiErr, err := groups.NewClient(tt.scopeClient).Create(tc.Context())
|
||||
g, apiErr, err := groups.NewClient(client).Create(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(g)
|
||||
|
||||
rc := roles.NewClient(tt.scopeClient)
|
||||
rc := roles.NewClient(client)
|
||||
var version uint32 = 1
|
||||
|
||||
r, apiErr, err := rc.Create(tc.Context(), roles.WithName("foo"))
|
||||
r, apiErr, err := rc.Create(tc.Context(), tt.scopeId, roles.WithName("foo"))
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(r)
|
||||
@@ -129,65 +122,60 @@ func TestCustom(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRole_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) {
|
||||
roleClient := roles.NewClient(tt.scopeClient)
|
||||
p1, apiErr, err := roleClient.List(tc.Context())
|
||||
var expected []*roles.Role
|
||||
|
||||
roleClient := roles.NewClient(client)
|
||||
p1, apiErr, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
require.Len(p1, 0)
|
||||
require.Len(p1, 1)
|
||||
expected = append(expected, p1[0])
|
||||
|
||||
var expected []*roles.Role
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := 1; i < 11; i++ {
|
||||
expected = append(expected, &roles.Role{Name: fmt.Sprint(i)})
|
||||
}
|
||||
|
||||
expected[0], apiErr, err = roleClient.Create(tc.Context(), roles.WithName(expected[0].Name))
|
||||
expected[1], apiErr, err = roleClient.Create(tc.Context(), tt.scopeId, roles.WithName(expected[1].Name))
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
|
||||
p2, apiErr, err := roleClient.List(tc.Context())
|
||||
p2, apiErr, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableSlice(expected[:1]), comparableSlice(p2))
|
||||
assert.ElementsMatch(comparableSlice(expected[0:2]), comparableSlice(p2))
|
||||
|
||||
for i := 1; i < 10; i++ {
|
||||
expected[i], apiErr, err = roleClient.Create(tc.Context(), roles.WithName(expected[i].Name))
|
||||
for i := 2; i < 11; i++ {
|
||||
expected[i], apiErr, err = roleClient.Create(tc.Context(), tt.scopeId, roles.WithName(expected[i].Name))
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
}
|
||||
p3, apiErr, err := roleClient.List(tc.Context())
|
||||
p3, apiErr, err := roleClient.List(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.ElementsMatch(comparableSlice(expected), comparableSlice(p3))
|
||||
@@ -210,34 +198,27 @@ func comparableSlice(in []*roles.Role) []roles.Role {
|
||||
return filtered
|
||||
}
|
||||
|
||||
func TestRole_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))
|
||||
|
||||
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(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -257,8 +238,8 @@ func TestRole_Crud(t *testing.T) {
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
roleClient := roles.NewClient(tt.scopeClient)
|
||||
g, apiErr, err := roleClient.Create(tc.Context(), roles.WithName("foo"))
|
||||
roleClient := roles.NewClient(client)
|
||||
g, apiErr, err := roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("foo"))
|
||||
checkRole("create", g, apiErr, err, "foo", 1)
|
||||
|
||||
g, apiErr, err = roleClient.Read(tc.Context(), g.Id)
|
||||
@@ -278,47 +259,40 @@ func TestRole_Crud(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRole_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) {
|
||||
roleClient := roles.NewClient(tt.scopeClient)
|
||||
u, apiErr, err := roleClient.Create(tc.Context(), roles.WithName("first"))
|
||||
roleClient := roles.NewClient(client)
|
||||
u, apiErr, err := roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("first"))
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.NotNil(u)
|
||||
|
||||
// Create another resource with the same name.
|
||||
_, apiErr, err = roleClient.Create(tc.Context(), roles.WithName("first"))
|
||||
_, apiErr, err = roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("first"))
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
|
||||
|
||||
Reference in new issue
Block a user