Create Role CUDLR API and SDK (#135)
* Implement the Role CRUD API. * Implement the Role List API. * Adding Roles SDK.
This commit is contained in:
1 parent
1023c90729
commit
5b1e4a680d
20 files changed
+3928
No files matched your search
@@ -39,6 +39,11 @@ var createFuncs = map[string][]*createInfo{
|
||||
targetType: "groups.Group",
|
||||
path: `"groups"`,
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "roles.Role",
|
||||
path: `"roles"`,
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "users.User",
|
||||
|
||||
@@ -27,6 +27,11 @@ var deleteFuncs = map[string][]*deleteInfo{
|
||||
"groups.Group",
|
||||
"groups",
|
||||
},
|
||||
{
|
||||
"Organization",
|
||||
"roles.Role",
|
||||
"roles",
|
||||
},
|
||||
{
|
||||
"Organization",
|
||||
"users.User",
|
||||
|
||||
@@ -125,6 +125,17 @@ var inputStructs = []*structInfo{
|
||||
"",
|
||||
templateTypeResource,
|
||||
},
|
||||
{
|
||||
os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/roles/role.pb.go",
|
||||
"Role",
|
||||
os.Getenv("GEN_BASEPATH") + "/api/roles/role.gen.go",
|
||||
"Role",
|
||||
"roles",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
templateTypeResource,
|
||||
},
|
||||
{
|
||||
os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host_catalog.pb.go",
|
||||
"StaticHostCatalogDetails",
|
||||
|
||||
@@ -27,6 +27,11 @@ var readFuncs = map[string][]*readInfo{
|
||||
targetType: "groups.Group",
|
||||
path: "groups",
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "roles.Role",
|
||||
path: "roles",
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "users.User",
|
||||
|
||||
@@ -27,6 +27,11 @@ var updateFuncs = map[string][]*updateInfo{
|
||||
targetType: "groups.Group",
|
||||
path: "groups",
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "roles.Role",
|
||||
path: "roles",
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "users.User",
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package roles
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/internal/strutil"
|
||||
)
|
||||
|
||||
type Role struct {
|
||||
Client *api.Client `json:"-"`
|
||||
|
||||
defaultFields []string
|
||||
|
||||
// The ID of the Role
|
||||
// Output only.
|
||||
Id string `json:"id,omitempty"`
|
||||
// Optional name for identification purposes
|
||||
Name *string `json:"name,omitempty"`
|
||||
// Optional user-set description for identification purposes
|
||||
Description *string `json:"description,omitempty"`
|
||||
// The time this resource was created
|
||||
// Output only.
|
||||
CreatedTime time.Time `json:"created_time,omitempty"`
|
||||
// The time this resource was last updated.
|
||||
// Output only.
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
// Whether the resource is disabled
|
||||
Disabled *bool `json:"disabled,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Role) SetDefault(key string) {
|
||||
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, key)
|
||||
}
|
||||
|
||||
func (s *Role) UnsetDefault(key string) {
|
||||
s.defaultFields = strutil.StrListDelete(s.defaultFields, key)
|
||||
}
|
||||
|
||||
func (s Role) MarshalJSON() ([]byte, error) {
|
||||
m := structs.Map(s)
|
||||
if m == nil {
|
||||
m = make(map[string]interface{})
|
||||
}
|
||||
for _, k := range s.defaultFields {
|
||||
m[k] = nil
|
||||
}
|
||||
return json.Marshal(m)
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package roles_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/roles"
|
||||
"github.com/hashicorp/watchtower/api/scopes"
|
||||
"github.com/hashicorp/watchtower/internal/iam"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRole_Crud(t *testing.T) {
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := &scopes.Organization{
|
||||
Client: client,
|
||||
}
|
||||
|
||||
checkRole := func(step string, g *roles.Role, apiErr *api.Error, err error, wantedName string) {
|
||||
assert := assert.New(t)
|
||||
assert.NoError(err, step)
|
||||
if !assert.Nil(apiErr, step) && apiErr.Message != nil {
|
||||
t.Errorf("ApiError message: %q", *apiErr.Message)
|
||||
}
|
||||
assert.NotNil(g, "returned no resource", step)
|
||||
gotName := ""
|
||||
if g.Name != nil {
|
||||
gotName = *g.Name
|
||||
}
|
||||
assert.Equal(wantedName, gotName, step)
|
||||
}
|
||||
|
||||
g, apiErr, err := org.CreateRole(tc.Context(), &roles.Role{Name: api.String("foo")})
|
||||
checkRole("create", g, apiErr, err, "foo")
|
||||
|
||||
g, apiErr, err = org.ReadRole(tc.Context(), &roles.Role{Id: g.Id})
|
||||
checkRole("read", g, apiErr, err, "foo")
|
||||
|
||||
g = &roles.Role{Id: g.Id}
|
||||
g.Name = api.String("bar")
|
||||
g, apiErr, err = org.UpdateRole(tc.Context(), g)
|
||||
checkRole("update", g, apiErr, err, "bar")
|
||||
|
||||
g = &roles.Role{Id: g.Id}
|
||||
g.SetDefault("name")
|
||||
g, apiErr, err = org.UpdateRole(tc.Context(), g)
|
||||
checkRole("update", g, apiErr, err, "")
|
||||
|
||||
existed, apiErr, err := org.DeleteRole(tc.Context(), g)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, existed, "Expected existing user when deleted, but it wasn't.")
|
||||
|
||||
existed, apiErr, err = org.DeleteRole(tc.Context(), g)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, existed, "Expected user to not exist when deleted, but it did.")
|
||||
}
|
||||
|
||||
func TestRole_Errors(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
ctx := tc.Context()
|
||||
|
||||
client := tc.Client()
|
||||
org := &scopes.Organization{
|
||||
Client: client,
|
||||
}
|
||||
|
||||
u, apiErr, err := org.CreateRole(ctx, &roles.Role{Name: api.String("first")})
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.NotNil(u)
|
||||
|
||||
// Create another resource with the same name.
|
||||
_, apiErr, err = org.CreateRole(ctx, &roles.Role{Name: api.String("first")})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
|
||||
_, apiErr, err = org.ReadRole(ctx, &roles.Role{Id: iam.RolePrefix + "_doesntexis"})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusNotFound)
|
||||
|
||||
_, apiErr, err = org.ReadRole(ctx, &roles.Role{Id: "invalid id"})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
||||
|
||||
_, apiErr, err = org.UpdateRole(ctx, &roles.Role{Id: u.Id})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/groups"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
"github.com/hashicorp/watchtower/api/roles"
|
||||
"github.com/hashicorp/watchtower/api/users"
|
||||
)
|
||||
|
||||
@@ -88,6 +89,44 @@ func (s Organization) CreateGroup(ctx context.Context, r *groups.Group) (*groups
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Organization) CreateRole(ctx context.Context, r *roles.Role) (*roles.Role, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in Createroles.Role request")
|
||||
}
|
||||
if s.Id == "" {
|
||||
|
||||
// Assume the client has been configured with organization already and
|
||||
// move on
|
||||
|
||||
} else {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
|
||||
ctx = context.WithValue(ctx, "org", s.Id)
|
||||
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", "roles", r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Createroles.Role request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Createroles.Role call: %w", err)
|
||||
}
|
||||
|
||||
target := new(roles.Role)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Createroles.Role repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Organization) CreateUser(ctx context.Context, r *users.User) (*users.User, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in Createusers.User request")
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/groups"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
"github.com/hashicorp/watchtower/api/roles"
|
||||
"github.com/hashicorp/watchtower/api/users"
|
||||
)
|
||||
|
||||
@@ -99,6 +100,50 @@ func (s Organization) DeleteGroup(ctx context.Context, r *groups.Group) (bool, *
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
|
||||
// DeleteRole returns true iff the roles.Role existed when the delete attempt was made.
|
||||
func (s Organization) DeleteRole(ctx context.Context, r *roles.Role) (bool, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return false, nil, fmt.Errorf("nil client in DeleteRole request")
|
||||
}
|
||||
if s.Id == "" {
|
||||
|
||||
// Assume the client has been configured with organization already and
|
||||
// move on
|
||||
|
||||
} else {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
|
||||
ctx = context.WithValue(ctx, "org", s.Id)
|
||||
|
||||
}
|
||||
if r.Id == "" {
|
||||
return false, nil, fmt.Errorf("empty roles.Role ID field in DeleteRole request")
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("%s/%s", "roles", r.Id), nil)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error creating DeleteRole request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error performing client request during DeleteRole call: %w", err)
|
||||
}
|
||||
|
||||
type deleteResponse struct {
|
||||
Existed bool
|
||||
}
|
||||
target := &deleteResponse{}
|
||||
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error decoding DeleteRole repsonse: %w", err)
|
||||
}
|
||||
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
|
||||
// DeleteUser returns true iff the users.User existed when the delete attempt was made.
|
||||
func (s Organization) DeleteUser(ctx context.Context, r *users.User) (bool, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/groups"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
"github.com/hashicorp/watchtower/api/roles"
|
||||
"github.com/hashicorp/watchtower/api/users"
|
||||
)
|
||||
|
||||
@@ -94,6 +95,47 @@ func (s Organization) ReadGroup(ctx context.Context, r *groups.Group) (*groups.G
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Organization) ReadRole(ctx context.Context, r *roles.Role) (*roles.Role, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in ReadRole request")
|
||||
}
|
||||
if s.Id == "" {
|
||||
|
||||
// Assume the client has been configured with organization already and
|
||||
// move on
|
||||
|
||||
} else {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
|
||||
ctx = context.WithValue(ctx, "org", s.Id)
|
||||
|
||||
}
|
||||
if r.Id == "" {
|
||||
return nil, nil, fmt.Errorf("empty roles.Role ID field in ReadRole request")
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "roles", r.Id), r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating ReadRole request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during ReadRole call: %w", err)
|
||||
}
|
||||
|
||||
target := new(roles.Role)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding ReadRole repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Organization) ReadUser(ctx context.Context, r *users.User) (*users.User, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in ReadUser request")
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/groups"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
"github.com/hashicorp/watchtower/api/roles"
|
||||
"github.com/hashicorp/watchtower/api/users"
|
||||
)
|
||||
|
||||
@@ -94,6 +95,47 @@ func (s Organization) UpdateGroup(ctx context.Context, r *groups.Group) (*groups
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Organization) UpdateRole(ctx context.Context, r *roles.Role) (*roles.Role, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateRole request")
|
||||
}
|
||||
if s.Id == "" {
|
||||
|
||||
// Assume the client has been configured with organization already and
|
||||
// move on
|
||||
|
||||
} else {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
|
||||
ctx = context.WithValue(ctx, "org", s.Id)
|
||||
|
||||
}
|
||||
|
||||
id := r.Id
|
||||
r.Id = ""
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("%s/%s", "roles", id), r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateRole request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during UpdateRole call: %w", err)
|
||||
}
|
||||
|
||||
target := new(roles.Role)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding UpdateRole repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Organization) UpdateUser(ctx context.Context, r *users.User) (*users.User, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateUser request")
|
||||
|
||||
Reference in new issue
Block a user