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
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s HostCatalog) CreateHost(ctx context.Context, host *Host) (*Host, *api.Error, error) {
|
||||
func (s HostCatalog) CreateHost(ctx context.Context, r *Host) (*Host, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateHost request")
|
||||
}
|
||||
@@ -22,7 +22,7 @@ func (s HostCatalog) CreateHost(ctx context.Context, host *Host) (*Host, *api.Er
|
||||
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/hosts", s.Id), host)
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/hosts", s.Id), r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateHost request: %w", err)
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func (s HostCatalog) CreateHost(ctx context.Context, host *Host) (*Host, *api.Er
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s HostCatalog) CreateHostSet(ctx context.Context, hostset *HostSet) (*HostSet, *api.Error, error) {
|
||||
func (s HostCatalog) CreateHostSet(ctx context.Context, r *HostSet) (*HostSet, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateHostSet request")
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func (s HostCatalog) CreateHostSet(ctx context.Context, hostset *HostSet) (*Host
|
||||
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/host-sets", s.Id), hostset)
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/host-sets", s.Id), r)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateHostSet request: %w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package hosts_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
"github.com/hashicorp/watchtower/api/scopes"
|
||||
"github.com/hashicorp/watchtower/internal/host/static"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCatalogs_Crud(t *testing.T) {
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := &scopes.Organization{
|
||||
Client: client,
|
||||
}
|
||||
p, apiErr, err := org.CreateProject(tc.Context(), &scopes.Project{})
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, apiErr)
|
||||
require.NotNil(t, p)
|
||||
|
||||
checkCatalog := func(step string, hc *hosts.HostCatalog, 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(hc, "returned project", step)
|
||||
gotName := ""
|
||||
if hc.Name != nil {
|
||||
gotName = *hc.Name
|
||||
}
|
||||
assert.Equal(wantedName, gotName, step)
|
||||
}
|
||||
|
||||
hc, apiErr, err := p.CreateHostCatalog(tc.Context(), &hosts.HostCatalog{Name: api.String("foo"), Type: api.String("Static")})
|
||||
checkCatalog("create", hc, apiErr, err, "foo")
|
||||
|
||||
hc, apiErr, err = p.ReadHostCatalog(tc.Context(), &hosts.HostCatalog{Id: hc.Id})
|
||||
checkCatalog("read", hc, apiErr, err, "foo")
|
||||
|
||||
hc = &hosts.HostCatalog{Id: hc.Id}
|
||||
hc.Name = api.String("bar")
|
||||
hc, apiErr, err = p.UpdateHostCatalog(tc.Context(), hc)
|
||||
checkCatalog("update", hc, apiErr, err, "bar")
|
||||
|
||||
hc = &hosts.HostCatalog{Id: hc.Id}
|
||||
hc.SetDefault("name")
|
||||
hc, apiErr, err = p.UpdateHostCatalog(tc.Context(), hc)
|
||||
checkCatalog("update", hc, apiErr, err, "")
|
||||
|
||||
existed, apiErr, err := p.DeleteHostCatalog(tc.Context(), hc)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, existed, "Expected existing catalog when deleted, but it wasn't.")
|
||||
|
||||
existed, apiErr, err = p.DeleteHostCatalog(tc.Context(), hc)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, existed, "Expected catalog to not exist when deleted, but it did.")
|
||||
}
|
||||
|
||||
// TODO: Get better coverage for expected errors and error formats.
|
||||
func TestProject_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,
|
||||
}
|
||||
p, apiErr, err := org.CreateProject(ctx, &scopes.Project{})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(p)
|
||||
assert.Nil(apiErr)
|
||||
|
||||
hc, apiErr, err := p.CreateHostCatalog(ctx, &hosts.HostCatalog{Type: api.String("Static")})
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.NotNil(hc)
|
||||
|
||||
_, apiErr, err = p.CreateHostCatalog(ctx, &hosts.HostCatalog{})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
|
||||
_, apiErr, err = p.ReadHostCatalog(ctx, &hosts.HostCatalog{Id: static.HostCatalogPrefix + "_doesntexis"})
|
||||
assert.NoError(err)
|
||||
// TODO: Should this be nil instead of just a catalog that has no values set
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusNotFound)
|
||||
|
||||
_, apiErr, err = p.ReadHostCatalog(ctx, &hosts.HostCatalog{Id: "invalid id"})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
||||
|
||||
_, apiErr, err = p.UpdateHostCatalog(ctx, &hosts.HostCatalog{Id: hc.Id, Type: api.String("Cant Update")})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
||||
}
|
||||
Reference in new issue
Block a user