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:
1 parent
ffddf24f86
commit
6ea448a7f3
23 files changed
+3834
-2
No files matched your search
@@ -0,0 +1,53 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package groups
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/internal/strutil"
|
||||
)
|
||||
|
||||
type Group struct {
|
||||
Client *api.Client `json:"-"`
|
||||
|
||||
defaultFields []string
|
||||
|
||||
// The ID of the Project
|
||||
// Output only.
|
||||
Id string `json:"id,omitempty"`
|
||||
// Optional name for identification purposes
|
||||
Name *string `json:"name,omitempty"`
|
||||
// Optional user-set descripton 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 *Group) SetDefault(key string) {
|
||||
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, key)
|
||||
}
|
||||
|
||||
func (s *Group) UnsetDefault(key string) {
|
||||
s.defaultFields = strutil.StrListDelete(s.defaultFields, key)
|
||||
}
|
||||
|
||||
func (s Group) 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 groups_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/groups"
|
||||
"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 TestGroup_Crud(t *testing.T) {
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := &scopes.Organization{
|
||||
Client: client,
|
||||
}
|
||||
|
||||
checkGroup := func(step string, g *groups.Group, 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.CreateGroup(tc.Context(), &groups.Group{Name: api.String("foo")})
|
||||
checkGroup("create", g, apiErr, err, "foo")
|
||||
|
||||
g, apiErr, err = org.ReadGroup(tc.Context(), &groups.Group{Id: g.Id})
|
||||
checkGroup("read", g, apiErr, err, "foo")
|
||||
|
||||
g = &groups.Group{Id: g.Id}
|
||||
g.Name = api.String("bar")
|
||||
g, apiErr, err = org.UpdateGroup(tc.Context(), g)
|
||||
checkGroup("update", g, apiErr, err, "bar")
|
||||
|
||||
g = &groups.Group{Id: g.Id}
|
||||
g.SetDefault("name")
|
||||
g, apiErr, err = org.UpdateGroup(tc.Context(), g)
|
||||
checkGroup("update", g, apiErr, err, "")
|
||||
|
||||
existed, apiErr, err := org.DeleteGroup(tc.Context(), g)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, existed, "Expected existing user when deleted, but it wasn't.")
|
||||
|
||||
existed, apiErr, err = org.DeleteGroup(tc.Context(), g)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, existed, "Expected user to not exist when deleted, but it did.")
|
||||
}
|
||||
|
||||
func TestGroup_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.CreateGroup(ctx, &groups.Group{Name: api.String("first")})
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.NotNil(u)
|
||||
|
||||
// Create another resource with the same name.
|
||||
_, apiErr, err = org.CreateGroup(ctx, &groups.Group{Name: api.String("first")})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
|
||||
_, apiErr, err = org.ReadGroup(ctx, &groups.Group{Id: iam.GroupPrefix + "_doesntexis"})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusNotFound)
|
||||
|
||||
_, apiErr, err = org.ReadGroup(ctx, &groups.Group{Id: "invalid id"})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
||||
|
||||
_, apiErr, err = org.UpdateGroup(ctx, &groups.Group{Id: u.Id})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func TestCatalogs_Crud(t *testing.T) {
|
||||
if !assert.Nil(apiErr, step) && apiErr.Message != nil {
|
||||
t.Errorf("ApiError message: %q", *apiErr.Message)
|
||||
}
|
||||
assert.NotNil(hc, "returned project", step)
|
||||
assert.NotNil(hc, "returned no resource", step)
|
||||
gotName := ""
|
||||
if hc.Name != nil {
|
||||
gotName = *hc.Name
|
||||
|
||||
@@ -34,6 +34,11 @@ var createFuncs = map[string][]*createInfo{
|
||||
targetType: "Project",
|
||||
path: `"projects"`,
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "groups.Group",
|
||||
path: `"groups"`,
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "users.User",
|
||||
|
||||
@@ -22,6 +22,11 @@ var deleteFuncs = map[string][]*deleteInfo{
|
||||
"Project",
|
||||
"projects",
|
||||
},
|
||||
{
|
||||
"Organization",
|
||||
"groups.Group",
|
||||
"groups",
|
||||
},
|
||||
{
|
||||
"Organization",
|
||||
"users.User",
|
||||
|
||||
@@ -114,6 +114,17 @@ var inputStructs = []*structInfo{
|
||||
"",
|
||||
templateTypeResource,
|
||||
},
|
||||
{
|
||||
os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/groups/group.pb.go",
|
||||
"Group",
|
||||
os.Getenv("GEN_BASEPATH") + "/api/groups/group.gen.go",
|
||||
"Group",
|
||||
"groups",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
templateTypeResource,
|
||||
},
|
||||
{
|
||||
os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host_catalog.pb.go",
|
||||
"StaticHostCatalogDetails",
|
||||
|
||||
@@ -22,6 +22,11 @@ var readFuncs = map[string][]*readInfo{
|
||||
targetType: "Project",
|
||||
path: "projects",
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "groups.Group",
|
||||
path: "groups",
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "users.User",
|
||||
|
||||
@@ -22,6 +22,11 @@ var updateFuncs = map[string][]*updateInfo{
|
||||
targetType: "Project",
|
||||
path: "projects",
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "groups.Group",
|
||||
path: "groups",
|
||||
},
|
||||
{
|
||||
baseType: "Organization",
|
||||
targetType: "users.User",
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestUser_Crud(t *testing.T) {
|
||||
if !assert.Nil(apiErr, step) && apiErr.Message != nil {
|
||||
t.Errorf("ApiError message: %q", *apiErr.Message)
|
||||
}
|
||||
assert.NotNil(u, "returned project", step)
|
||||
assert.NotNil(u, "returned no resource", step)
|
||||
gotName := ""
|
||||
if u.Name != nil {
|
||||
gotName = *u.Name
|
||||
|
||||
Reference in new issue
Block a user