Handler for Host CRUDL actions (#287)

This commit is contained in:
Todd Knight authored and GitHub committed 2020-08-20 08:20:26 -07:00
1 parent 8dad85fef4
commit d5678c4f80
33 files changed
+1944 -372

No files matched your search

+11 -9
View File
@@ -14,15 +14,17 @@ import (
)
type Host struct {
Id string `json:"id,omitempty"`
Scope *scopes.ScopeInfo `json:"scope,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CreatedTime time.Time `json:"created_time,omitempty"`
UpdatedTime time.Time `json:"updated_time,omitempty"`
Version uint32 `json:"version,omitempty"`
Type string `json:"type,omitempty"`
Address string `json:"address,omitempty"`
Id string `json:"id,omitempty"`
Scope *scopes.ScopeInfo `json:"scope,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CreatedTime time.Time `json:"created_time,omitempty"`
UpdatedTime time.Time `json:"updated_time,omitempty"`
Version uint32 `json:"version,omitempty"`
Type string `json:"type,omitempty"`
HostCatalogId string `json:"host_catalog_id,omitempty"`
HostSetIds []string `json:"host_set_ids,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
type HostsClient struct {
+116
View File
@@ -0,0 +1,116 @@
package hosts_test
import (
"net/http"
"testing"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/hosts"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/servers/controller"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHost_Crud(t *testing.T) {
assert, require := assert.New(t), require.New(t)
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultAuthMethodId: amId,
DefaultLoginName: "user",
DefaultPassword: "passpass",
})
defer tc.Shutdown()
client := tc.Client()
org, proj := iam.TestScopes(t, tc.IamRepo())
client.SetScopeId(org.GetPublicId())
projClient := client.Clone()
projClient.SetScopeId(proj.GetPublicId())
hc, apiErr, err := hosts.NewHostCatalogsClient(projClient).Create(tc.Context(), "static")
require.NoError(err)
require.Nil(apiErr)
require.NotNil(hc)
checkHost := func(step string, h *hosts.Host, apiErr *api.Error, err error, wantedName string, wantVersion uint32) {
require.NoError(err, step)
if !assert.Nil(apiErr, step) && apiErr.Message != "" {
t.Errorf("ApiError message: %q", apiErr.Message)
}
assert.NotNil(h, "returned no resource", step)
gotName := ""
if h.Name != "" {
gotName = h.Name
}
assert.Equal(wantedName, gotName, step)
assert.Equal(wantVersion, h.Version)
}
hClient := hosts.NewHostsClient(projClient)
h, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
checkHost("create", h, apiErr, err, "foo", 1)
h, apiErr, err = hClient.Read(tc.Context(), hc.Id, h.Id)
checkHost("read", h, apiErr, err, "foo", 1)
h, apiErr, err = hClient.Update(tc.Context(), hc.Id, h.Id, h.Version, hosts.WithName("bar"))
checkHost("update", h, apiErr, err, "bar", 2)
h, apiErr, err = hClient.Update(tc.Context(), hc.Id, h.Id, h.Version, hosts.DefaultName())
checkHost("update", h, apiErr, err, "", 3)
existed, apiErr, err := hClient.Delete(tc.Context(), hc.Id, h.Id)
assert.NoError(err)
assert.True(existed, "Expected existing catalog when deleted, but it wasn't.")
existed, apiErr, err = hClient.Delete(tc.Context(), hc.Id, h.Id)
assert.NoError(err)
assert.False(existed, "Expected catalog to not exist when deleted, but it did.")
}
// TODO: Get better coverage for expected errors and error formats.
func TestHost_Errors(t *testing.T) {
assert, require := assert.New(t), require.New(t)
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultAuthMethodId: amId,
DefaultLoginName: "user",
DefaultPassword: "passpass",
})
defer tc.Shutdown()
client := tc.Client()
_, proj := iam.TestScopes(t, tc.IamRepo())
client.SetScopeId(proj.GetPublicId())
hc, apiErr, err := hosts.NewHostCatalogsClient(client).Create(tc.Context(), "static")
require.NoError(err)
require.Nil(apiErr)
require.NotNil(hc)
hClient := hosts.NewHostsClient(client)
h, apiErr, err := hClient.Create(tc.Context(), hc.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
require.Nil(apiErr)
assert.NotNil(h)
_, apiErr, err = hClient.Create(tc.Context(), hc.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
assert.NotNil(apiErr)
_, apiErr, err = hClient.Read(tc.Context(), hc.Id, static.HostPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(apiErr.Status, http.StatusNotFound)
_, apiErr, err = hClient.Read(tc.Context(), hc.Id, "invalid id")
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
}
+16 -4
View File
@@ -53,15 +53,27 @@ func WithAutomaticVersioning() Option {
}
}
func WithAddress(inAddress string) Option {
func WithStaticHostAddress(inAddress string) Option {
return func(o *options) {
o.valueMap["address"] = inAddress
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["address"] = inAddress
o.valueMap["attributes"] = val
}
}
func DefaultAddress() Option {
func DefaultStaticHostAddress() Option {
return func(o *options) {
o.valueMap["address"] = nil
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
val := raw.(map[string]interface{})
val["address"] = nil
o.valueMap["attributes"] = val
}
}
+6
View File
@@ -0,0 +1,6 @@
// Code generated by "make api"; DO NOT EDIT.
package hosts
type StaticHostAttributes struct {
Address string `json:"address,omitempty"`
}
+5
View File
@@ -258,6 +258,11 @@ var inputStructs = []*structInfo{
pathArgs: []string{"host-catalog", "host"},
versionEnabled: true,
},
{
inProto: &hosts.StaticHostAttributes{},
outFile: "hosts/static_host_attributes.gen.go",
subtypeName: "StaticHost",
},
{
inProto: &hosts.HostSet{},
outFile: "hosts/host_set.gen.go",