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:
Todd Knight authored and GitHub committed 2020-06-22 06:56:27 -07:00
1 parent 1023c90729
commit 5b1e4a680d
20 files changed
+3928

No files matched your search

+39
View File
@@ -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")
+45
View File
@@ -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 {
+42
View File
@@ -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")
+42
View File
@@ -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")