Handler hostcatalogs sdk (#103)
* Created host catalog service.
* Fixing uri generation for create.
* Use newly submitted static host catalog.
* Copying over tests from projects.
* Fix UpdateHostCatalogRequest merging proto.
* Properly use static host catalog prefixes in tests.
* Correct test to verify the expected uri and id prefix is set.
* Add test for unsetting descriptions.
* Correct bad spelling in comment.
* Tweaking variable names.
* Adding Stringer to the catalog type and setting the type on the response.
* Setting up the catalogType to contain more info regarding its type.
* Validate the type field in requests.
* Remove unused method.
* Comment describing invariants around types.
* Wiring in the repo into the static catalog handler.
* feature: Add CRUD operations for host catalogs to the go api library.
* Updated comments to the exported methods.
* fix: return the result of handlers.NotFoundError instead of just calling it.
* Fixing typos referencing just hosts instead of host catlogs.
* Update NewService returns an error and a non pointer type.
* Pull "org_id" and "project_id" into constants and the validation of them into the ValidateAncestors.
* Check errors for all Register*HandlerServer calls.
* fix: Remove left over reference that could cause a panic.
* fix: Changing "x := []string{}" to "var x []string"
* fix: Log errors in test cleanup.
* Changing the code to use google.golang.org/protobuf instead of the older github based protobuf library.
* Changing all t.Fatal checks to require.NoError calls.
* Changing unrecognized prefixes to Invalid Argument instead of Not Found.
* fix: toDbUpdateMask now returns error if it was unable to find any valid paths.
* Define CreatedTime, UpdatedTime, and Type at initialization time in toProto.
* Adding sdk test for unsetting a value.
* Require that project is created correctly before testing the catalog.
* Remove unused host catalog from test.
* Change a t.Fatal to a require.NoError that I missed earlier.
* Change shutdown error logging to t.Errorfs.
* Shorten validateX function names.
* Inline API responses.
* Add todo to make a new repo per request.
* Inline regex check for invalid id.
* Change stored repos into repo creating functions which satisfies ICU-223.
* Wrap proto comments to be under 80 characters long.
* Remove the exception on the read only fields that can be set in an Update request.
* Removing out of date comment.
* Use iam.TestScopes instead of making the project myself.
* Renaming files to be consistant with go test standards and resource naming standards.
* Remove TODO related to a task completed in an earlier commit.
* Fix the previous commit which removed the wrong TODO.
This commit is contained in:
1 parent
7f6e78e66e
commit
d374ac6d4c
11 files changed
+413
-111
No files matched your search
@@ -6,9 +6,10 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
)
|
||||
|
||||
func (s Organization) CreateProject(ctx context.Context, project *Project) (*Project, *api.Error, error) {
|
||||
func (s Organization) CreateProject(ctx context.Context, r *Project) (*Project, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateProject request")
|
||||
}
|
||||
@@ -25,7 +26,7 @@ func (s Organization) CreateProject(ctx context.Context, project *Project) (*Pro
|
||||
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", "projects", project)
|
||||
req, err := s.Client.NewRequest(ctx, "POST", "projects", r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateProject request: %w", err)
|
||||
}
|
||||
@@ -46,3 +47,41 @@ func (s Organization) CreateProject(ctx context.Context, project *Project) (*Pro
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Project) CreateHostCatalog(ctx context.Context, r *hosts.HostCatalog) (*hosts.HostCatalog, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in Createhosts.HostCatalog request")
|
||||
}
|
||||
if s.Id == "" {
|
||||
|
||||
// Assume the client has been configured with project already and move
|
||||
// on
|
||||
|
||||
} else {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
|
||||
ctx = context.WithValue(ctx, "project", s.Id)
|
||||
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", "host-catalogs", r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Createhosts.HostCatalog request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Createhosts.HostCatalog call: %w", err)
|
||||
}
|
||||
|
||||
target := new(hosts.HostCatalog)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Createhosts.HostCatalog repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
@@ -6,10 +6,11 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
)
|
||||
|
||||
// DeleteProject returns true iff the project existed when the delete attempt was made.
|
||||
func (s Organization) DeleteProject(ctx context.Context, project *Project) (bool, *api.Error, error) {
|
||||
// DeleteProject returns true iff the Project existed when the delete attempt was made.
|
||||
func (s Organization) DeleteProject(ctx context.Context, r *Project) (bool, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return false, nil, fmt.Errorf("nil client in DeleteProject request")
|
||||
}
|
||||
@@ -25,11 +26,11 @@ func (s Organization) DeleteProject(ctx context.Context, project *Project) (bool
|
||||
ctx = context.WithValue(ctx, "org", s.Id)
|
||||
|
||||
}
|
||||
if project.Id == "" {
|
||||
return false, nil, fmt.Errorf("empty project ID field in DeleteProject request")
|
||||
if r.Id == "" {
|
||||
return false, nil, fmt.Errorf("empty Project ID field in DeleteProject request")
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("projects/%s", project.Id), nil)
|
||||
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("%s/%s", "projects", r.Id), nil)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error creating DeleteProject request: %w", err)
|
||||
}
|
||||
@@ -51,3 +52,47 @@ func (s Organization) DeleteProject(ctx context.Context, project *Project) (bool
|
||||
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
|
||||
// DeleteHostCatalog returns true iff the hosts.HostCatalog existed when the delete attempt was made.
|
||||
func (s Project) DeleteHostCatalog(ctx context.Context, r *hosts.HostCatalog) (bool, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return false, nil, fmt.Errorf("nil client in DeleteHostCatalog request")
|
||||
}
|
||||
if s.Id == "" {
|
||||
|
||||
// Assume the client has been configured with project already and move
|
||||
// on
|
||||
|
||||
} else {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
|
||||
ctx = context.WithValue(ctx, "project", s.Id)
|
||||
|
||||
}
|
||||
if r.Id == "" {
|
||||
return false, nil, fmt.Errorf("empty hosts.HostCatalog ID field in DeleteHostCatalog request")
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("%s/%s", "host-catalogs", r.Id), nil)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error creating DeleteHostCatalog request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error performing client request during DeleteHostCatalog 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 DeleteHostCatalog repsonse: %w", err)
|
||||
}
|
||||
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
+21
-11
@@ -1,10 +1,11 @@
|
||||
package scopes
|
||||
package scopes_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/scopes"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -14,29 +15,38 @@ func TestProjects_Crud(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := &Organization{
|
||||
org := &scopes.Organization{
|
||||
Client: client,
|
||||
}
|
||||
|
||||
checkProject := func(step string, p *Project, apiErr *api.Error, err error, wantedName string) {
|
||||
checkProject := func(step string, p *scopes.Project, apiErr *api.Error, err error, wantedName string) {
|
||||
assert := assert.New(t)
|
||||
assert.NoError(err, step)
|
||||
assert.Nil(apiErr, step)
|
||||
assert.NotNil(p, "returned project", step)
|
||||
assert.Equal(wantedName, *p.Name, step)
|
||||
gotName := ""
|
||||
if p.Name != nil {
|
||||
gotName = *p.Name
|
||||
}
|
||||
assert.Equal(wantedName, gotName, step)
|
||||
}
|
||||
|
||||
p, apiErr, err := org.CreateProject(tc.Context(), &Project{Name: api.String("foo")})
|
||||
p, apiErr, err := org.CreateProject(tc.Context(), &scopes.Project{Name: api.String("foo")})
|
||||
checkProject("create", p, apiErr, err, "foo")
|
||||
|
||||
p, apiErr, err = org.ReadProject(tc.Context(), &Project{Id: p.Id})
|
||||
p, apiErr, err = org.ReadProject(tc.Context(), &scopes.Project{Id: p.Id})
|
||||
checkProject("read", p, apiErr, err, "foo")
|
||||
|
||||
p = &Project{Id: p.Id}
|
||||
p = &scopes.Project{Id: p.Id}
|
||||
p.Name = api.String("bar")
|
||||
p, apiErr, err = org.UpdateProject(tc.Context(), p)
|
||||
checkProject("update", p, apiErr, err, "bar")
|
||||
|
||||
p = &scopes.Project{Id: p.Id}
|
||||
p.SetDefault("name")
|
||||
p, apiErr, err = org.UpdateProject(tc.Context(), p)
|
||||
checkProject("update, unset name", p, apiErr, err, "")
|
||||
|
||||
existed, apiErr, err := org.DeleteProject(tc.Context(), p)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, existed, "Expected existing project when deleted, but it wasn't.")
|
||||
@@ -54,21 +64,21 @@ func TestProject_Errors(t *testing.T) {
|
||||
ctx := tc.Context()
|
||||
|
||||
client := tc.Client()
|
||||
org := &Organization{
|
||||
org := &scopes.Organization{
|
||||
Client: client,
|
||||
}
|
||||
createdProj, apiErr, err := org.CreateProject(ctx, &Project{})
|
||||
createdProj, apiErr, err := org.CreateProject(ctx, &scopes.Project{})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(createdProj)
|
||||
assert.Nil(apiErr)
|
||||
|
||||
_, apiErr, err = org.ReadProject(ctx, &Project{Id: "p_doesntexis"})
|
||||
_, apiErr, err = org.ReadProject(ctx, &scopes.Project{Id: "p_doesntexis"})
|
||||
assert.NoError(err)
|
||||
// TODO: Should this be nil instead of just a Project that has no values set
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusNotFound)
|
||||
|
||||
_, apiErr, err = org.ReadProject(ctx, &Project{Id: "invalid id"})
|
||||
_, apiErr, err = org.ReadProject(ctx, &scopes.Project{Id: "invalid id"})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
||||
|
||||
+46
-4
@@ -6,9 +6,10 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
)
|
||||
|
||||
func (s Organization) ReadProject(ctx context.Context, project *Project) (*Project, *api.Error, error) {
|
||||
func (s Organization) ReadProject(ctx context.Context, r *Project) (*Project, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in ReadProject request")
|
||||
}
|
||||
@@ -24,11 +25,11 @@ func (s Organization) ReadProject(ctx context.Context, project *Project) (*Proje
|
||||
ctx = context.WithValue(ctx, "org", s.Id)
|
||||
|
||||
}
|
||||
if project.Id == "" {
|
||||
return nil, nil, fmt.Errorf("empty project ID field in ReadProject request")
|
||||
if r.Id == "" {
|
||||
return nil, nil, fmt.Errorf("empty Project ID field in ReadProject request")
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("projects/%s", project.Id), project)
|
||||
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "projects", r.Id), r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating ReadProject request: %w", err)
|
||||
}
|
||||
@@ -49,3 +50,44 @@ func (s Organization) ReadProject(ctx context.Context, project *Project) (*Proje
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Project) ReadHostCatalog(ctx context.Context, r *hosts.HostCatalog) (*hosts.HostCatalog, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in ReadHostCatalog request")
|
||||
}
|
||||
if s.Id == "" {
|
||||
|
||||
// Assume the client has been configured with project already and move
|
||||
// on
|
||||
|
||||
} else {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
|
||||
ctx = context.WithValue(ctx, "project", s.Id)
|
||||
|
||||
}
|
||||
if r.Id == "" {
|
||||
return nil, nil, fmt.Errorf("empty hosts.HostCatalog ID field in ReadHostCatalog request")
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "host-catalogs", r.Id), r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating ReadHostCatalog request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during ReadHostCatalog call: %w", err)
|
||||
}
|
||||
|
||||
target := new(hosts.HostCatalog)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding ReadHostCatalog repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
@@ -6,9 +6,10 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
)
|
||||
|
||||
func (s Organization) UpdateProject(ctx context.Context, project *Project) (*Project, *api.Error, error) {
|
||||
func (s Organization) UpdateProject(ctx context.Context, r *Project) (*Project, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateProject request")
|
||||
}
|
||||
@@ -25,10 +26,10 @@ func (s Organization) UpdateProject(ctx context.Context, project *Project) (*Pro
|
||||
|
||||
}
|
||||
|
||||
id := project.Id
|
||||
project.Id = ""
|
||||
id := r.Id
|
||||
r.Id = ""
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("projects/%s", id), project)
|
||||
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("%s/%s", "projects", id), r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateProject request: %w", err)
|
||||
}
|
||||
@@ -49,3 +50,44 @@ func (s Organization) UpdateProject(ctx context.Context, project *Project) (*Pro
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Project) UpdateHostCatalog(ctx context.Context, r *hosts.HostCatalog) (*hosts.HostCatalog, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateHostCatalog request")
|
||||
}
|
||||
if s.Id == "" {
|
||||
|
||||
// Assume the client has been configured with project already and move
|
||||
// on
|
||||
|
||||
} else {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
|
||||
ctx = context.WithValue(ctx, "project", s.Id)
|
||||
|
||||
}
|
||||
|
||||
id := r.Id
|
||||
r.Id = ""
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("%s/%s", "host-catalogs", id), r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateHostCatalog request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during UpdateHostCatalog call: %w", err)
|
||||
}
|
||||
|
||||
target := new(hosts.HostCatalog)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding UpdateHostCatalog repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
Reference in new issue
Block a user