Adding Groups API endpoint and SDK (#122)

* feat: Add group grpc handler.

* feat: Add group sdk hooks.

* fix:  Adding forgotten generated group sdk file.

* fix:  Fix a copy/paste error in test msg.

* fix:  Correct assert error msg.

* fix: Remove unused proto imports.
This commit is contained in:
Todd Knight authored and GitHub committed 2020-06-15 15:22:41 -07:00
1 parent ffddf24f86
commit 6ea448a7f3
23 files changed
+3834 -2

No files matched your search

+39
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/groups"
"github.com/hashicorp/watchtower/api/hosts"
"github.com/hashicorp/watchtower/api/users"
)
@@ -49,6 +50,44 @@ func (s Organization) CreateProject(ctx context.Context, r *Project) (*Project,
return target, apiErr, nil
}
func (s Organization) CreateGroup(ctx context.Context, r *groups.Group) (*groups.Group, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Creategroups.Group 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", "groups", r)
if err != nil {
return nil, nil, fmt.Errorf("error creating Creategroups.Group request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Creategroups.Group call: %w", err)
}
target := new(groups.Group)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Creategroups.Group 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
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/groups"
"github.com/hashicorp/watchtower/api/hosts"
"github.com/hashicorp/watchtower/api/users"
)
@@ -54,6 +55,50 @@ func (s Organization) DeleteProject(ctx context.Context, r *Project) (bool, *api
return target.Existed, apiErr, nil
}
// DeleteGroup returns true iff the groups.Group existed when the delete attempt was made.
func (s Organization) DeleteGroup(ctx context.Context, r *groups.Group) (bool, *api.Error, error) {
if s.Client == nil {
return false, nil, fmt.Errorf("nil client in DeleteGroup 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 groups.Group ID field in DeleteGroup request")
}
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("%s/%s", "groups", r.Id), nil)
if err != nil {
return false, nil, fmt.Errorf("error creating DeleteGroup request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during DeleteGroup 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 DeleteGroup 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
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/groups"
"github.com/hashicorp/watchtower/api/hosts"
"github.com/hashicorp/watchtower/api/users"
)
@@ -52,6 +53,47 @@ func (s Organization) ReadProject(ctx context.Context, r *Project) (*Project, *a
return target, apiErr, nil
}
func (s Organization) ReadGroup(ctx context.Context, r *groups.Group) (*groups.Group, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in ReadGroup 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 groups.Group ID field in ReadGroup request")
}
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "groups", r.Id), r)
if err != nil {
return nil, nil, fmt.Errorf("error creating ReadGroup request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during ReadGroup call: %w", err)
}
target := new(groups.Group)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding ReadGroup 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
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/groups"
"github.com/hashicorp/watchtower/api/hosts"
"github.com/hashicorp/watchtower/api/users"
)
@@ -52,6 +53,47 @@ func (s Organization) UpdateProject(ctx context.Context, r *Project) (*Project,
return target, apiErr, nil
}
func (s Organization) UpdateGroup(ctx context.Context, r *groups.Group) (*groups.Group, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in CreateGroup 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", "groups", id), r)
if err != nil {
return nil, nil, fmt.Errorf("error creating CreateGroup request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during UpdateGroup call: %w", err)
}
target := new(groups.Group)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding UpdateGroup 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")