mirror of https://github.com/hashicorp/boundary
Create host (#276)
* Change testHostCatalog methods to be like other resource test methods * Implement CreateHost * Make allocHost return a pointer to a Hostpull/278/head
parent
282177afc2
commit
e18cdc52b3
@ -0,0 +1,9 @@
|
||||
package static
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrInvalidAddress results from attempting to perform an operation
|
||||
// that sets an address on a host to an invalid value.
|
||||
ErrInvalidAddress = errors.New("invalid address")
|
||||
)
|
||||
@ -0,0 +1,61 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
)
|
||||
|
||||
// CreateHost inserts h into the repository and returns a new Host
|
||||
// containing the host's PublicId. h is not changed. h must contain a valid
|
||||
// CatalogId. h must not contain a PublicId. The PublicId is generated and
|
||||
// assigned by this method. opt is ignored.
|
||||
//
|
||||
// h must contain a valid Address.
|
||||
//
|
||||
// Both h.Name and h.Description are optional. If h.Name is set, it must be
|
||||
// unique within h.CatalogId.
|
||||
func (r *Repository) CreateHost(ctx context.Context, h *Host, opt ...Option) (*Host, error) {
|
||||
if h == nil {
|
||||
return nil, fmt.Errorf("create: static host: %w", db.ErrNilParameter)
|
||||
}
|
||||
if h.Host == nil {
|
||||
return nil, fmt.Errorf("create: static host: embedded Host: %w", db.ErrNilParameter)
|
||||
}
|
||||
if h.CatalogId == "" {
|
||||
return nil, fmt.Errorf("create: static host: no catalog id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if h.PublicId != "" {
|
||||
return nil, fmt.Errorf("create: static host: public id not empty: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
h = h.clone()
|
||||
|
||||
id, err := newHostId()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host: %w", err)
|
||||
}
|
||||
h.PublicId = id
|
||||
|
||||
var newHost *Host
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
newHost = h.clone()
|
||||
return w.Create(ctx, newHost, db.WithOplog(r.wrapper, h.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
if db.IsUniqueError(err) {
|
||||
return nil, fmt.Errorf("create: static host: in catalog: %s: name %s already exists: %w",
|
||||
h.CatalogId, h.Name, db.ErrNotUnique)
|
||||
}
|
||||
if db.IsCheckConstraintError(err) {
|
||||
return nil, fmt.Errorf("create: static host: in catalog: %s: %q: %w",
|
||||
h.CatalogId, h.Address, ErrInvalidAddress)
|
||||
}
|
||||
return nil, fmt.Errorf("create: static host: in catalog: %s: %w", h.CatalogId, err)
|
||||
}
|
||||
return newHost, nil
|
||||
}
|
||||
@ -0,0 +1,223 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRepository_CreateHost(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
in *Host
|
||||
opts []Option
|
||||
want *Host
|
||||
wantIsErr error
|
||||
}{
|
||||
{
|
||||
name: "nil-Host",
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-embedded-Host",
|
||||
in: &Host{},
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-no-catalog-id",
|
||||
in: &Host{
|
||||
Host: &store.Host{},
|
||||
},
|
||||
wantIsErr: db.ErrInvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-public-id-set",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
PublicId: "abcd_OOOOOOOOOO",
|
||||
},
|
||||
},
|
||||
wantIsErr: db.ErrInvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "valid-no-options",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-with-name",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Name: "test-name-repo",
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Name: "test-name-repo",
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-with-description",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Description: ("test-description-repo"),
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Description: ("test-description-repo"),
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid-no-address",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
},
|
||||
},
|
||||
wantIsErr: ErrInvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "invalid-address-to-short",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "127",
|
||||
},
|
||||
},
|
||||
wantIsErr: ErrInvalidAddress,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.CreateHost(context.Background(), tt.in, tt.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Empty(tt.in.PublicId)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, HostPrefix, got.PublicId)
|
||||
assert.NotSame(tt.in, got)
|
||||
assert.Equal(tt.want.Name, got.Name)
|
||||
assert.Equal(tt.want.Description, got.Description)
|
||||
assert.Equal(got.CreateTime, got.UpdateTime)
|
||||
assert.NoError(db.TestVerifyOplog(t, rw, got.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second)))
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("invalid-duplicate-names", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
|
||||
in := &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Name: "test-name-repo",
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
}
|
||||
|
||||
got, err := repo.CreateHost(context.Background(), in)
|
||||
require.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, HostPrefix, got.PublicId)
|
||||
assert.NotSame(in, got)
|
||||
assert.Equal(in.Name, got.Name)
|
||||
assert.Equal(in.Description, got.Description)
|
||||
assert.Equal(got.CreateTime, got.UpdateTime)
|
||||
|
||||
got2, err := repo.CreateHost(context.Background(), in)
|
||||
assert.Truef(errors.Is(err, db.ErrNotUnique), "want err: %v got: %v", db.ErrNotUnique, err)
|
||||
assert.Nil(got2)
|
||||
})
|
||||
|
||||
t.Run("valid-duplicate-names-diff-catalogs", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalogs := testCatalogs(t, conn, prj.PublicId, 2)
|
||||
|
||||
catalogA, catalogB := catalogs[0], catalogs[1]
|
||||
|
||||
in := &Host{
|
||||
Host: &store.Host{
|
||||
Name: "test-name-repo",
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
}
|
||||
in2 := in.clone()
|
||||
|
||||
in.CatalogId = catalogA.PublicId
|
||||
got, err := repo.CreateHost(context.Background(), in)
|
||||
require.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, HostPrefix, got.PublicId)
|
||||
assert.NotSame(in, got)
|
||||
assert.Equal(in.Name, got.Name)
|
||||
assert.Equal(in.Description, got.Description)
|
||||
assert.Equal(got.CreateTime, got.UpdateTime)
|
||||
|
||||
in2.CatalogId = catalogB.PublicId
|
||||
got2, err := repo.CreateHost(context.Background(), in2)
|
||||
require.NoError(err)
|
||||
require.NotNil(got2)
|
||||
assertPublicId(t, HostPrefix, got2.PublicId)
|
||||
assert.NotSame(in2, got2)
|
||||
assert.Equal(in2.Name, got2.Name)
|
||||
assert.Equal(in2.Description, got2.Description)
|
||||
assert.Equal(got2.CreateTime, got2.UpdateTime)
|
||||
})
|
||||
}
|
||||
Loading…
Reference in new issue