From 0c7f6da9361e2c98c6f07b1192704c397c49b75f Mon Sep 17 00:00:00 2001 From: Michael Gaffney Date: Wed, 27 May 2020 18:16:43 -0400 Subject: [PATCH] Static Host Catalog (#51) * database: add wt_public_id domain type * static-hosts: add migrations * Rename migrations * migrations: add description column and fix foreign keys * Add protobuf for static host * Add HostCatalog and HostSet * Add Options * Add foreign key constraint * Add package doc * Add copied scanner code * Add NewHostCatalog with tests * Override default table name for static host catalog * WIP: save static host catalog to database * WIP: Print db url for debugging * Add iam test helper function * Make gorm default to null for names and descriptions * Add foreign key constraint from host catalog to scope * Names are not needed for the test org and project * Avoid an ordering dependency in down migrations * Use wt_timestamp * Add check constraint on host address column * Add Host * Add HostSet * Add HostSetMember * Test a HostSetMember can be written to the database * Move the getters and setters for table name to a new file The TableName methods are needed by gorm and the SetTableName methods are for oplog. Moving them to a new file so related functionality is in the same file. * Add repository * Add NewRepository * Close connection before cleanup of database * Add comments to protobuf file and regenerate * Define repository methods for HostCatalog * Assign PublicId when resource is inserted into the database * Wrap a nil parameter error * Add oplog tickets for static host types * WIP: first cut at CreateCatalog * CreateHostCatalog: more tests * Detect unique constraint violation * Use const for PublicId prefixes * Use PostgreSQL error name instead of error code * Add TODO comments * Fix comments * Use PublicId not PublicID * Refactor: move error variables from host/static to db The error variables defined in host/static are generic and can be reused by other packages. * Create a new repository for each test * Validate CreateCatalog returns a new HostCatalog * Do not modify the HostCatalog passed in * Implement UpdateCatalog * Minor cleanup to make folds work * Implement LookupCatalog * Refactor: extract allocCatalog function * Add comment * Refactor: extract newCatalogMetadata function * Make error strings more consistent * Move database specific error check to internal/db package * Change signature of DeleteCatalog and update docs * Implement DeleteCatalog * Make error strings more consistent * Add examples * Update docs * Add package level doc and example for repository * No need for using reflect.DeepEqual * Remove redundant comment * Clarify ErrMultipleRecords results in the transaction being rolled back * Remove redundant error checks * Remove unnecessary check * Return early instead of using nested 'else' statements * Update and Delete must return a row count * catch err on defers (#82) Co-authored-by: Jim Lambert * Remove 'if assert' pattern * Use assert package in test helper function * Add test for updating a non existent catalog * Use a single assert when a specific error is expected * Use assert package when it clarifies the check * Catch errors in cleanup functions * Update comments in protobuf file * Update doc for UpdateCatalog to reflect the new behavior * Add common error values for field mask errors * Require a field mask when calling update methods * Add TODO for removing direct call to gorm * Export Public ID prefixes * Add db timestamp guards * Remove unused option Co-authored-by: Jim Co-authored-by: Jim Lambert --- Makefile | 1 + internal/db/error.go | 36 +- internal/db/error_test.go | 45 + internal/db/migrations/postgres.gen.go | 138 +++ .../postgres/10_static_host.down.sql | 8 + .../migrations/postgres/10_static_host.up.sql | 118 +++ internal/host/static/doc.go | 34 + internal/host/static/example_test.go | 39 + internal/host/static/gorm.go | 61 ++ internal/host/static/options.go | 40 + internal/host/static/options_test.go | 24 + internal/host/static/repository.go | 250 ++++++ internal/host/static/repository_test.go | 827 ++++++++++++++++++ internal/host/static/static.go | 151 ++++ internal/host/static/static_test.go | 507 +++++++++++ internal/host/static/store/scanners.go | 33 + internal/host/static/store/scanners_test.go | 73 ++ internal/host/static/store/static.pb.go | 663 ++++++++++++++ internal/iam/testing.go | 34 + internal/iam/testing_test.go | 30 + .../storage/host/static/store/v1/static.proto | 112 +++ 21 files changed, 3223 insertions(+), 1 deletion(-) create mode 100644 internal/db/error_test.go create mode 100644 internal/db/migrations/postgres/10_static_host.down.sql create mode 100644 internal/db/migrations/postgres/10_static_host.up.sql create mode 100644 internal/host/static/doc.go create mode 100644 internal/host/static/example_test.go create mode 100644 internal/host/static/gorm.go create mode 100644 internal/host/static/options.go create mode 100644 internal/host/static/options_test.go create mode 100644 internal/host/static/repository.go create mode 100644 internal/host/static/repository_test.go create mode 100644 internal/host/static/static.go create mode 100644 internal/host/static/static_test.go create mode 100644 internal/host/static/store/scanners.go create mode 100644 internal/host/static/store/scanners_test.go create mode 100644 internal/host/static/store/static.pb.go create mode 100644 internal/iam/testing.go create mode 100644 internal/iam/testing_test.go create mode 100644 internal/proto/local/controller/storage/host/static/store/v1/static.proto diff --git a/Makefile b/Makefile index 67489656f..0b4de93ab 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ protobuild: @protoc-go-inject-tag -input=./internal/oplog/oplog_test/oplog_test.pb.go @protoc-go-inject-tag -input=./internal/iam/store/scope.pb.go @protoc-go-inject-tag -input=./internal/db/db_test/db_test.pb.go + @protoc-go-inject-tag -input=./internal/host/static/store/static.pb.go @rm -R ${TMP_DIR} protolint: diff --git a/internal/db/error.go b/internal/db/error.go index d1af2af6b..ce54b023e 100644 --- a/internal/db/error.go +++ b/internal/db/error.go @@ -1,6 +1,10 @@ package db -import "errors" +import ( + "errors" + + "github.com/lib/pq" +) // Errors returned from this package may be tested against these errors // with errors.Is. @@ -12,6 +16,14 @@ var ( // an attribute on a struct contains illegal or invalid values. ErrInvalidParameter = errors.New("invalid parameter") + // ErrInvalidFieldMask is returned by update methods if the field mask + // contains unknown fields or fields that cannot be updated. + ErrInvalidFieldMask = errors.New("invalid field mask") + + // ErrEmptyFieldMask is returned by update methods if the field mask is + // empty. + ErrEmptyFieldMask = errors.New("empty field mask") + // ErrNotUnique is returned by create and update methods when a write // to the repository resulted in a unique constraint violation. ErrNotUnique = errors.New("unique constraint violation") @@ -23,4 +35,26 @@ var ( // when attempting to read from the database into struct. // When reading into a slice it won't return this error. ErrRecordNotFound = errors.New("record not found") + + // ErrMultipleRecords is returned by update and delete methods when a + // write to the repository would result in more than one record being + // changed resulting in the transaction being rolled back. + ErrMultipleRecords = errors.New("multiple records") ) + +// IsUnique returns a boolean indicating whether the error is known to +// report a unique constraint violation. +func IsUnique(err error) bool { + if err == nil { + return false + } + + var pqError *pq.Error + if errors.As(err, &pqError) { + if pqError.Code.Name() == "unique_violation" { + return true + } + } + + return false +} diff --git a/internal/db/error_test.go b/internal/db/error_test.go new file mode 100644 index 000000000..128065a50 --- /dev/null +++ b/internal/db/error_test.go @@ -0,0 +1,45 @@ +package db + +import ( + "testing" + + "github.com/lib/pq" + "github.com/stretchr/testify/assert" +) + +func TestError_IsUnique(t *testing.T) { + var tests = []struct { + name string + in error + want bool + }{ + { + name: "nil-error", + in: nil, + want: false, + }, + { + name: "postgres-not-unique", + in: &pq.Error{ + Code: pq.ErrorCode("23503"), + }, + want: false, + }, + { + name: "postgres-is-unique2", + in: &pq.Error{ + Code: pq.ErrorCode("23505"), + }, + want: true, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + err := tt.in + got := IsUnique(err) + assert.Equal(tt.want, got) + }) + } +} diff --git a/internal/db/migrations/postgres.gen.go b/internal/db/migrations/postgres.gen.go index e41af1d1d..b04ec7855 100644 --- a/internal/db/migrations/postgres.gen.go +++ b/internal/db/migrations/postgres.gen.go @@ -480,6 +480,144 @@ insert on iam_scope commit; +`), + }, + "migrations/10_static_host.down.sql": { + name: "10_static_host.down.sql", + bytes: []byte(` +begin; + + drop table static_host_set_member cascade; + drop table static_host_set cascade; + drop table static_host cascade; + drop table static_host_catalog cascade; + +commit; + +`), + }, + "migrations/10_static_host.up.sql": { + name: "10_static_host.up.sql", + bytes: []byte(` +begin; + + create table static_host_catalog ( + public_id wt_public_id primary key, + scope_id wt_public_id not null + references iam_scope (public_id) + on delete cascade + on update cascade, + name text, + description text, + create_time wt_timestamp, + update_time wt_timestamp, + unique(scope_id, name) + ); + + create trigger + update_time_column + before update on static_host_catalog + for each row execute procedure update_time_column(); + + create trigger + immutable_create_time + before + update on static_host_catalog + for each row execute procedure immutable_create_time_func(); + + create trigger + default_create_time_column + before + insert on static_host_catalog + for each row execute procedure default_create_time(); + + create table static_host ( + public_id wt_public_id primary key, + static_host_catalog_id wt_public_id not null + references static_host_catalog (public_id) + on delete cascade + on update cascade, + name text, + description text, + address text not null + check( + length(trim(address)) > 7 + and + length(trim(address)) < 256 + ), + create_time wt_timestamp, + update_time wt_timestamp, + unique(static_host_catalog_id, name) + ); + + create trigger + update_time_column + before update on static_host + for each row execute procedure update_time_column(); + + create trigger + immutable_create_time + before + update on static_host + for each row execute procedure immutable_create_time_func(); + + create trigger + default_create_time_column + before + insert on static_host + for each row execute procedure default_create_time(); + + create table static_host_set ( + public_id wt_public_id primary key, + static_host_catalog_id wt_public_id not null + references static_host_catalog (public_id) + on delete cascade + on update cascade, + name text, + description text, + create_time wt_timestamp, + update_time wt_timestamp, + unique(static_host_catalog_id, name) + ); + + create trigger + update_time_column + before update on static_host_set + for each row execute procedure update_time_column(); + + create trigger + immutable_create_time + before + update on static_host_set + for each row execute procedure immutable_create_time_func(); + + create trigger + default_create_time_column + before + insert on static_host_set + for each row execute procedure default_create_time(); + + create table static_host_set_member ( + static_host_set_id wt_public_id + references static_host_set (public_id) + on delete cascade + on update cascade, + static_host_id wt_public_id + references static_host (public_id) + on delete cascade + on update cascade, + primary key(static_host_set_id, static_host_id) + ); + + insert into oplog_ticket (name, version) + values + ('static_host_catalog', 1), + ('static_host', 1), + ('static_host_set', 1), + ('static_host_set_member', 1); + +commit; + `), }, } diff --git a/internal/db/migrations/postgres/10_static_host.down.sql b/internal/db/migrations/postgres/10_static_host.down.sql new file mode 100644 index 000000000..ef1db5af4 --- /dev/null +++ b/internal/db/migrations/postgres/10_static_host.down.sql @@ -0,0 +1,8 @@ +begin; + + drop table static_host_set_member cascade; + drop table static_host_set cascade; + drop table static_host cascade; + drop table static_host_catalog cascade; + +commit; diff --git a/internal/db/migrations/postgres/10_static_host.up.sql b/internal/db/migrations/postgres/10_static_host.up.sql new file mode 100644 index 000000000..5d111d5e5 --- /dev/null +++ b/internal/db/migrations/postgres/10_static_host.up.sql @@ -0,0 +1,118 @@ +begin; + + create table static_host_catalog ( + public_id wt_public_id primary key, + scope_id wt_public_id not null + references iam_scope (public_id) + on delete cascade + on update cascade, + name text, + description text, + create_time wt_timestamp, + update_time wt_timestamp, + unique(scope_id, name) + ); + + create trigger + update_time_column + before update on static_host_catalog + for each row execute procedure update_time_column(); + + create trigger + immutable_create_time + before + update on static_host_catalog + for each row execute procedure immutable_create_time_func(); + + create trigger + default_create_time_column + before + insert on static_host_catalog + for each row execute procedure default_create_time(); + + create table static_host ( + public_id wt_public_id primary key, + static_host_catalog_id wt_public_id not null + references static_host_catalog (public_id) + on delete cascade + on update cascade, + name text, + description text, + address text not null + check( + length(trim(address)) > 7 + and + length(trim(address)) < 256 + ), + create_time wt_timestamp, + update_time wt_timestamp, + unique(static_host_catalog_id, name) + ); + + create trigger + update_time_column + before update on static_host + for each row execute procedure update_time_column(); + + create trigger + immutable_create_time + before + update on static_host + for each row execute procedure immutable_create_time_func(); + + create trigger + default_create_time_column + before + insert on static_host + for each row execute procedure default_create_time(); + + create table static_host_set ( + public_id wt_public_id primary key, + static_host_catalog_id wt_public_id not null + references static_host_catalog (public_id) + on delete cascade + on update cascade, + name text, + description text, + create_time wt_timestamp, + update_time wt_timestamp, + unique(static_host_catalog_id, name) + ); + + create trigger + update_time_column + before update on static_host_set + for each row execute procedure update_time_column(); + + create trigger + immutable_create_time + before + update on static_host_set + for each row execute procedure immutable_create_time_func(); + + create trigger + default_create_time_column + before + insert on static_host_set + for each row execute procedure default_create_time(); + + create table static_host_set_member ( + static_host_set_id wt_public_id + references static_host_set (public_id) + on delete cascade + on update cascade, + static_host_id wt_public_id + references static_host (public_id) + on delete cascade + on update cascade, + primary key(static_host_set_id, static_host_id) + ); + + insert into oplog_ticket (name, version) + values + ('static_host_catalog', 1), + ('static_host', 1), + ('static_host_set', 1), + ('static_host_set_member', 1); + +commit; diff --git a/internal/host/static/doc.go b/internal/host/static/doc.go new file mode 100644 index 000000000..9a405a9b5 --- /dev/null +++ b/internal/host/static/doc.go @@ -0,0 +1,34 @@ +// Package static provides a host, a host catalog, and a host set suitable +// for hosts with a static address. +// +// A host catalog contains a collection of hosts with static addresses. +// These hosts can be grouped into host sets allowing them to be used in a +// target. Hosts and host sets are owned by a single host catalog. If a +// host catalog is deleted, all hosts and host sets owned by it are also +// deleted. A host set contains references to zero or more hosts but does +// not own them. Deleting a host set does not effect any of the hosts it +// referenced. A host set can only reference hosts from the host catalog +// that owns it. Host addresses must be unique within a host catalog. +// +// Repository +// +// A repository provides methods for creating, updating, retrieving, and +// deleting host catalogs, host sets, and hosts. A new repository should be +// created for each transaction. For example: +// +// var wrapper wrapping.Wrapper +// ... init wrapper... +// +// // db implements both the reader and writer interfaces. +// db, _ := db.Open(db.Postgres, url) +// +// var repo *static.Repository +// +// repo, _ = static.NewRepository(db, db, wrapper) +// catalog, _ := repo.LookupCatalog(ctx, catalogId) +// +// catalog.Name = "new name" +// +// repo, _ = static.NewRepository(db, db, wrapper) +// catalog, _ := repo.UpdateCatalog(ctx, catalog, []string{"Name"}) +package static diff --git a/internal/host/static/example_test.go b/internal/host/static/example_test.go new file mode 100644 index 000000000..c52533686 --- /dev/null +++ b/internal/host/static/example_test.go @@ -0,0 +1,39 @@ +package static_test + +import ( + "fmt" + + "github.com/hashicorp/watchtower/internal/host/static" +) + +func ExampleNewHostCatalog() { + projectPublicId := "p_1234" + catalog, _ := static.NewHostCatalog(projectPublicId, static.WithName("my catalog")) + fmt.Println(catalog.Name) + // Output: + // my catalog +} + +func ExampleNewHost() { + catalogPublicId := "sthc_1234" + host, _ := static.NewHost(catalogPublicId, "127.0.0.1") + fmt.Println(host.Address) + // Output: + // 127.0.0.1 +} + +func ExampleNewHostSet() { + catalogPublicId := "sthc_1234" + set, _ := static.NewHostSet(catalogPublicId, static.WithName("my host set")) + fmt.Println(set.Name) + // Output: + // my host set +} + +func ExampleNewHostSetMember() { + setPublicId := "sths_11111" + hostPublicId := "sth_22222" + member, _ := static.NewHostSetMember(setPublicId, hostPublicId) + fmt.Println(member.StaticHostSetId) + fmt.Println(member.StaticHostId) +} diff --git a/internal/host/static/gorm.go b/internal/host/static/gorm.go new file mode 100644 index 000000000..c4ba61738 --- /dev/null +++ b/internal/host/static/gorm.go @@ -0,0 +1,61 @@ +package static + +// TableName returns the table name for the host catalog. +func (c *HostCatalog) TableName() string { + if c.tableName != "" { + return c.tableName + } + return "static_host_catalog" +} + +// SetTableName sets the table name. +func (c *HostCatalog) SetTableName(n string) { + if n != "" { + c.tableName = n + } +} + +// TableName returns the table name for the host. +func (h *Host) TableName() string { + if h.tableName != "" { + return h.tableName + } + return "static_host" +} + +// SetTableName sets the table name. +func (h *Host) SetTableName(n string) { + if n != "" { + h.tableName = n + } +} + +// TableName returns the table name for the host set. +func (s *HostSet) TableName() string { + if s.tableName != "" { + return s.tableName + } + return "static_host_set" +} + +// SetTableName sets the table name. +func (s *HostSet) SetTableName(n string) { + if n != "" { + s.tableName = n + } +} + +// TableName returns the table name for the host set. +func (m *HostSetMember) TableName() string { + if m.tableName != "" { + return m.tableName + } + return "static_host_set_member" +} + +// SetTableName sets the table name. +func (m *HostSetMember) SetTableName(n string) { + if n != "" { + m.tableName = n + } +} diff --git a/internal/host/static/options.go b/internal/host/static/options.go new file mode 100644 index 000000000..938a2d08a --- /dev/null +++ b/internal/host/static/options.go @@ -0,0 +1,40 @@ +package static + +// getOpts - iterate the inbound Options and return a struct +func getOpts(opt ...Option) options { + opts := getDefaultOptions() + for _, o := range opt { + o(&opts) + } + return opts +} + +// Option - how Options are passed as arguments. +type Option func(*options) + +// options = how options are represented +type options struct { + withName string + withDescription string +} + +func getDefaultOptions() options { + return options{ + withDescription: "", + withName: "", + } +} + +// WithDescription provides an optional description. +func WithDescription(desc string) Option { + return func(o *options) { + o.withDescription = desc + } +} + +// WithName provides an optional name. +func WithName(name string) Option { + return func(o *options) { + o.withName = name + } +} diff --git a/internal/host/static/options_test.go b/internal/host/static/options_test.go new file mode 100644 index 000000000..6cf73ac69 --- /dev/null +++ b/internal/host/static/options_test.go @@ -0,0 +1,24 @@ +package static + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_GetOpts(t *testing.T) { + t.Parallel() + assert := assert.New(t) + t.Run("WithName", func(t *testing.T) { + opts := getOpts(WithName("test")) + testOpts := getDefaultOptions() + testOpts.withName = "test" + assert.Equal(opts, testOpts) + }) + t.Run("WithDescription", func(t *testing.T) { + opts := getOpts(WithDescription("test desc")) + testOpts := getDefaultOptions() + testOpts.withDescription = "test desc" + assert.Equal(opts, testOpts) + }) +} diff --git a/internal/host/static/repository.go b/internal/host/static/repository.go new file mode 100644 index 000000000..3a0e5aa25 --- /dev/null +++ b/internal/host/static/repository.go @@ -0,0 +1,250 @@ +package static + +import ( + "context" + "fmt" + "strings" + + wrapping "github.com/hashicorp/go-kms-wrapping" + + "github.com/hashicorp/watchtower/internal/db" + "github.com/hashicorp/watchtower/internal/host/static/store" + "github.com/hashicorp/watchtower/internal/oplog" +) + +// A Repository stores and retrieves the persistent types in the static +// package. It is not safe to use a repository concurrently. +type Repository struct { + reader db.Reader + writer db.Writer + wrapper wrapping.Wrapper +} + +// NewRepository creates a new Repository. The returned repository should +// only be used for one transaction and it is not safe for concurrent go +// routines to access it. +func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper) (*Repository, error) { + switch { + case r == nil: + return nil, fmt.Errorf("db.Reader: %w", db.ErrNilParameter) + case w == nil: + return nil, fmt.Errorf("db.Writer: %w", db.ErrNilParameter) + case wrapper == nil: + return nil, fmt.Errorf("wrapping.Wrapper: %w", db.ErrNilParameter) + } + + return &Repository{ + reader: r, + writer: w, + wrapper: wrapper, + }, nil +} + +// CreateCatalog inserts c into the repository and returns a new +// HostCatalog containing the catalog's PublicId. c is not changed. c must +// contain a valid ScopeID. c must not contain a PublicId. The PublicId is +// generated and assigned by the this method. opt is ignored. +// +// Both c.Name and c.Description are optional. If c.Name is set, it must be +// unique within c.ScopeID. +// +// Both c.CreateTime and c.UpdateTime are ignored. +func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...Option) (*HostCatalog, error) { + if c == nil { + return nil, fmt.Errorf("create: static host catalog: %w", db.ErrNilParameter) + } + if c.HostCatalog.ScopeId == "" { + return nil, fmt.Errorf("create: static host catalog: no scope id: %w", db.ErrInvalidParameter) + } + if c.PublicId != "" { + return nil, fmt.Errorf("create: static host catalog: public id not empty: %w", db.ErrInvalidParameter) + } + c = c.clone() + + id, err := newHostCatalogId() + if err != nil { + return nil, fmt.Errorf("create: static host catalog: %w", err) + } + c.PublicId = id + + metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_CREATE) + + var newHostCatalog *HostCatalog + _, err = r.writer.DoTx( + ctx, + db.StdRetryCnt, + db.ExpBackoff{}, + func(w db.Writer) error { + newHostCatalog = c.clone() + return w.Create( + ctx, + newHostCatalog, + db.WithOplog(r.wrapper, metadata), + ) + }, + ) + + if err != nil { + if db.IsUnique(err) { + return nil, fmt.Errorf("create: static host catalog: in scope: %s: name %s already exists: %w", + c.ScopeId, c.Name, db.ErrNotUnique) + } + return nil, fmt.Errorf("create: static host catalog: in scope: %s: %w", c.ScopeId, err) + } + return newHostCatalog, nil +} + +// UpdateCatalog updates the repository entry for c.PublicId with the +// values in c for the fields listed in fieldMask. It returns a new +// HostCatalog containing the updated values and a count of the number of +// records updated. c is not changed. +// +// c must contain a valid PublicId. Only c.Name and c.Description can be +// updated. If c.Name is set to a non-empty string, it must be unique +// within c.ScopeID. +// +// An attribute of c will be set to NULL in the database if the attribute +// in c is the zero value and it is included in fieldMask. +func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, fieldMask []string, opt ...Option) (*HostCatalog, int, error) { + if c == nil { + return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %w", db.ErrNilParameter) + } + if c.PublicId == "" { + return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: missing public id: %w", db.ErrInvalidParameter) + } + if len(fieldMask) == 0 { + return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %w", db.ErrEmptyFieldMask) + } + + for _, f := range fieldMask { + switch { + case strings.EqualFold("name", f): + case strings.EqualFold("description", f): + default: + return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: field: %s: %w", f, db.ErrInvalidFieldMask) + } + } + c = c.clone() + + metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_UPDATE) + + // TODO(mgaffney,jimlambrt) 05/2020: uncomment the nullFields line + // below once support for setting columns to nil is added to db.Update. + var rowsUpdated int + var returnedCatalog *HostCatalog + _, err := r.writer.DoTx( + ctx, + db.StdRetryCnt, + db.ExpBackoff{}, + func(w db.Writer) error { + returnedCatalog = c.clone() + var err error + rowsUpdated, err = w.Update( + ctx, + returnedCatalog, + fieldMask, + // nullFields, + db.WithOplog(r.wrapper, metadata), + ) + if err == nil && rowsUpdated > 1 { + return db.ErrMultipleRecords + } + return err + }, + ) + + if err != nil { + if db.IsUnique(err) { + return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %s: name %s already exists: %w", + c.PublicId, c.Name, db.ErrNotUnique) + } + return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %s: %w", c.PublicId, err) + } + + return returnedCatalog, rowsUpdated, nil +} + +// LookupCatalog returns the HostCatalog for id. Returns nil, nil if no +// HostCatalog is found for id. +func (r *Repository) LookupCatalog(ctx context.Context, id string, opt ...Option) (*HostCatalog, error) { + if id == "" { + return nil, fmt.Errorf("lookup: static host catalog: missing public id: %w", db.ErrInvalidParameter) + } + c := allocCatalog() + c.PublicId = id + if err := r.reader.LookupByPublicId(ctx, c); err != nil { + if err == db.ErrRecordNotFound { + return nil, nil + } + return nil, fmt.Errorf("lookup: static host catalog: %s: %w", id, err) + } + return c, nil +} + +// DeleteCatalog deletes id from the repository returning a count of the +// number of records deleted. +func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option) (int, error) { + if id == "" { + return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: missing public id: %w", db.ErrInvalidParameter) + } + + c := allocCatalog() + c.PublicId = id + + metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_DELETE) + + var rowsDeleted int + var deleteCatalog *HostCatalog + _, err := r.writer.DoTx( + ctx, + db.StdRetryCnt, + db.ExpBackoff{}, + func(w db.Writer) error { + deleteCatalog = c.clone() + var err error + rowsDeleted, err = w.Delete( + ctx, + deleteCatalog, + db.WithOplog(r.wrapper, metadata), + ) + if err == nil && rowsDeleted > 1 { + return db.ErrMultipleRecords + } + return err + }, + ) + + if err != nil { + return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: %s: %w", c.PublicId, err) + } + + return rowsDeleted, nil +} + +func contains(ss []string, t string) bool { + for _, s := range ss { + if strings.EqualFold(s, t) { + return true + } + } + return false +} + +func allocCatalog() *HostCatalog { + fresh := &HostCatalog{ + HostCatalog: &store.HostCatalog{}, + } + return fresh +} + +func newCatalogMetadata(c *HostCatalog, op oplog.OpType) oplog.Metadata { + metadata := oplog.Metadata{ + "resource-public-id": []string{c.GetPublicId()}, + "resource-type": []string{"static host catalog"}, + "op-type": []string{op.String()}, + } + if c.ScopeId != "" { + metadata["scope-id"] = []string{c.ScopeId} + } + return metadata +} diff --git a/internal/host/static/repository_test.go b/internal/host/static/repository_test.go new file mode 100644 index 000000000..3008c3f85 --- /dev/null +++ b/internal/host/static/repository_test.go @@ -0,0 +1,827 @@ +package static + +import ( + "context" + "errors" + "fmt" + "strings" + "testing" + + wrapping "github.com/hashicorp/go-kms-wrapping" + "github.com/jinzhu/gorm" + "github.com/stretchr/testify/assert" + + "github.com/hashicorp/watchtower/internal/db" + "github.com/hashicorp/watchtower/internal/host/static/store" + "github.com/hashicorp/watchtower/internal/iam" +) + +func TestRepository_New(t *testing.T) { + + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := conn.Close(); err != nil { + t.Error(err) + } + if err := cleanup(); err != nil { + t.Error(err) + } + }() + + rw := db.New(conn) + wrapper := db.TestWrapper(t) + + type args struct { + r db.Reader + w db.Writer + wrapper wrapping.Wrapper + } + + var tests = []struct { + name string + args args + want *Repository + wantIsErr error + }{ + { + name: "valid", + args: args{ + r: rw, + w: rw, + wrapper: wrapper, + }, + want: &Repository{ + reader: rw, + writer: rw, + wrapper: wrapper, + }, + }, + { + name: "nil-reader", + args: args{ + r: nil, + w: rw, + wrapper: wrapper, + }, + want: nil, + wantIsErr: db.ErrNilParameter, + }, + { + name: "nil-writer", + args: args{ + r: rw, + w: nil, + wrapper: wrapper, + }, + want: nil, + wantIsErr: db.ErrNilParameter, + }, + { + name: "nil-wrapper", + args: args{ + r: rw, + w: rw, + wrapper: nil, + }, + want: nil, + wantIsErr: db.ErrNilParameter, + }, + { + name: "all-nils", + args: args{ + r: nil, + w: nil, + wrapper: nil, + }, + want: nil, + wantIsErr: db.ErrNilParameter, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + got, err := NewRepository(tt.args.r, tt.args.w, tt.args.wrapper) + if tt.wantIsErr != nil { + assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err) + assert.Nil(got) + return + } + assert.NoError(err) + assert.NotNil(got) + assert.Equal(tt.want, got) + }) + } +} + +func TestRepository_CreateCatalog(t *testing.T) { + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + rw := db.New(conn) + wrapper := db.TestWrapper(t) + + var tests = []struct { + name string + in *HostCatalog + opts []Option + want *HostCatalog + wantIsErr error + }{ + { + name: "nil-catalog", + wantIsErr: db.ErrNilParameter, + }, + { + name: "valid-no-options", + in: &HostCatalog{ + HostCatalog: &store.HostCatalog{}, + }, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{}, + }, + }, + { + name: "valid-with-name", + in: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + }, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + }, + }, + { + name: "valid-with-description", + in: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Description: ("test-description-repo"), + }, + }, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Description: ("test-description-repo"), + }, + }, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + _, prj := iam.TestScopes(t, conn) + if tt.in != nil { + tt.in.ScopeId = prj.GetPublicId() + assert.Empty(tt.in.PublicId) + } + got, err := repo.CreateCatalog(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 + } + assert.NoError(err) + assert.Empty(tt.in.PublicId) + assert.NotNil(got) + assertPublicId(t, "sthc", 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) + }) + } + + t.Run("invalid-duplicate-names", func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + + _, prj := iam.TestScopes(t, conn) + in := &HostCatalog{ + HostCatalog: &store.HostCatalog{ + ScopeId: prj.GetPublicId(), + Name: "test-name-repo", + }, + } + + got, err := repo.CreateCatalog(context.Background(), in) + assert.NoError(err) + assert.NotNil(got) + assertPublicId(t, "sthc", 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.CreateCatalog(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-scopes", func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + + org, prj := iam.TestScopes(t, conn) + in := &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + } + in2 := in.clone() + + in.ScopeId = prj.GetPublicId() + got, err := repo.CreateCatalog(context.Background(), in) + assert.NoError(err) + assert.NotNil(got) + assertPublicId(t, "sthc", 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.ScopeId = org.GetPublicId() + got2, err := repo.CreateCatalog(context.Background(), in2) + assert.NoError(err) + assert.NotNil(got2) + assertPublicId(t, "sthc", got2.PublicId) + assert.NotSame(in2, got2) + assert.Equal(in2.Name, got2.Name) + assert.Equal(in2.Description, got2.Description) + assert.Equal(got2.CreateTime, got2.UpdateTime) + }) +} + +func assertPublicId(t *testing.T, prefix, actual string) { + t.Helper() + assert.NotEmpty(t, actual) + parts := strings.Split(actual, "_") + assert.Equalf(t, 2, len(parts), "want one '_' in PublicId, got multiple in %q", actual) + assert.Equalf(t, prefix, parts[0], "PublicId want prefix: %q, got: %q in %q", prefix, parts[0], actual) +} + +func TestRepository_UpdateCatalog(t *testing.T) { + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + + rw := db.New(conn) + wrapper := db.TestWrapper(t) + + changeName := func(s string) func(*HostCatalog) *HostCatalog { + return func(c *HostCatalog) *HostCatalog { + c.Name = s + return c + } + } + + changeDescription := func(s string) func(*HostCatalog) *HostCatalog { + return func(c *HostCatalog) *HostCatalog { + c.Description = s + return c + } + } + + makeNil := func() func(*HostCatalog) *HostCatalog { + return func(c *HostCatalog) *HostCatalog { + return nil + } + } + + deletePublicId := func() func(*HostCatalog) *HostCatalog { + return func(c *HostCatalog) *HostCatalog { + c.PublicId = "" + return c + } + } + + nonExistentPublicId := func() func(*HostCatalog) *HostCatalog { + return func(c *HostCatalog) *HostCatalog { + c.PublicId = "sthc_OOOOOOOOOO" + return c + } + } + + combine := func(fns ...func(c *HostCatalog) *HostCatalog) func(*HostCatalog) *HostCatalog { + return func(c *HostCatalog) *HostCatalog { + for _, fn := range fns { + c = fn(c) + } + return c + } + } + + var tests = []struct { + name string + orig *HostCatalog + chgFn func(*HostCatalog) *HostCatalog + masks []string + want *HostCatalog + wantCount int + wantIsErr error + }{ + { + name: "nil-catalog", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{}, + }, + chgFn: makeNil(), + masks: []string{"Name", "Description"}, + wantIsErr: db.ErrNilParameter, + }, + { + name: "no-public-id", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{}, + }, + chgFn: deletePublicId(), + masks: []string{"Name", "Description"}, + wantIsErr: db.ErrInvalidParameter, + }, + { + name: "updating-non-existent-catalog", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + }, + chgFn: combine(nonExistentPublicId(), changeName("test-update-name-repo")), + masks: []string{"Name"}, + wantIsErr: db.ErrRecordNotFound, + }, + { + name: "empty-field-mask", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + }, + chgFn: changeName("test-update-name-repo"), + wantIsErr: db.ErrEmptyFieldMask, + }, + { + name: "read-only-fields-in-field-mask", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + }, + chgFn: changeName("test-update-name-repo"), + masks: []string{"PublicId", "CreateTime", "UpdateTime", "ScopeId"}, + wantIsErr: db.ErrInvalidFieldMask, + }, + { + name: "unknown-field-in-field-mask", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + }, + chgFn: changeName("test-update-name-repo"), + masks: []string{"Bilbo"}, + wantIsErr: db.ErrInvalidFieldMask, + }, + { + name: "change-name", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + }, + chgFn: changeName("test-update-name-repo"), + masks: []string{"Name"}, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-update-name-repo", + }, + }, + wantCount: 1, + }, + { + name: "change-description", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Description: "test-description-repo", + }, + }, + chgFn: changeDescription("test-update-description-repo"), + masks: []string{"Description"}, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Description: "test-update-description-repo", + }, + }, + wantCount: 1, + }, + { + name: "change-name-and-description", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + Description: "test-description-repo", + }, + }, + chgFn: combine(changeDescription("test-update-description-repo"), changeName("test-update-name-repo")), + masks: []string{"Name", "Description"}, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-update-name-repo", + Description: "test-update-description-repo", + }, + }, + wantCount: 1, + }, + // TODO(mgaffney,jimlambrt) 05/2020: enable these tests once + // support for setting columns to nil is added to db.Update. + /* + { + name: "delete-name", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + Description: "test-description-repo", + }, + }, + masks: []string{"Name"}, + chgFn: combine(changeDescription("test-update-description-repo"), changeName("")), + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Description: "test-description-repo", + }, + }, + }, + { + name: "delete-description", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + Description: "test-description-repo", + }, + }, + masks: []string{"Description"}, + chgFn: combine(changeDescription(""), changeName("test-update-name-repo")), + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + }, + }, + { + name: "do-not-delete-name", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + Description: "test-description-repo", + }, + }, + chgFn: combine(changeDescription("test-update-description-repo"), changeName("")), + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + Description: "test-update-description-repo", + }, + }, + }, + { + name: "do-not-delete-description", + orig: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + Description: "test-description-repo", + }, + }, + chgFn: combine(changeDescription(""), changeName("test-update-name-repo")), + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-update-name-repo", + Description: "test-description-repo", + }, + }, + }, + */ + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + _, prj := iam.TestScopes(t, conn) + tt.orig.ScopeId = prj.GetPublicId() + orig, err := repo.CreateCatalog(context.Background(), tt.orig) + assert.NoError(err) + assert.NotNil(orig) + + if tt.chgFn != nil { + orig = tt.chgFn(orig) + } + got, gotCount, err := repo.UpdateCatalog(context.Background(), orig, tt.masks) + if tt.wantIsErr != nil { + assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err) + assert.Equal(tt.wantCount, gotCount, "row count") + assert.Nil(got) + return + } + assert.NoError(err) + assert.Empty(tt.orig.PublicId) + assert.NotNil(got) + assertPublicId(t, "sthc", got.PublicId) + assert.Equal(tt.wantCount, gotCount, "row count") + assert.NotSame(tt.orig, got) + assert.Equal(tt.orig.ScopeId, got.ScopeId) + if tt.want.Name == "" { + assertColumnIsNull(t, conn, got, "name") + return + } + assert.Equal(tt.want.Name, got.Name) + if tt.want.Description == "" { + assertColumnIsNull(t, conn, got, "description") + return + } + assert.Equal(tt.want.Description, got.Description) + }) + } + + t.Run("invalid-duplicate-names", func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + + name := "test-dup-name" + cats := testCatalogs(t, conn, 2) + c1 := cats[0] + c1.Name = name + got1, gotCount1, err := repo.UpdateCatalog(context.Background(), c1, []string{"name"}) + assert.NoError(err) + assert.NotNil(got1) + assert.Equal(name, got1.Name) + assert.Equal(1, gotCount1, "row count") + + c2 := cats[1] + c2.Name = name + got2, gotCount2, err := repo.UpdateCatalog(context.Background(), c2, []string{"name"}) + assert.Truef(errors.Is(err, db.ErrNotUnique), "want err: %v got: %v", db.ErrNotUnique, err) + assert.Nil(got2) + assert.Equal(db.NoRowsAffected, gotCount2, "row count") + }) + + t.Run("valid-duplicate-names-diff-scopes", func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + + org, prj := iam.TestScopes(t, conn) + in := &HostCatalog{ + HostCatalog: &store.HostCatalog{ + Name: "test-name-repo", + }, + } + in2 := in.clone() + + in.ScopeId = prj.GetPublicId() + got, err := repo.CreateCatalog(context.Background(), in) + assert.NoError(err) + assert.NotNil(got) + assertPublicId(t, "sthc", got.PublicId) + assert.NotSame(in, got) + assert.Equal(in.Name, got.Name) + assert.Equal(in.Description, got.Description) + + in2.ScopeId = org.GetPublicId() + in2.Name = "first-name" + got2, err := repo.CreateCatalog(context.Background(), in2) + assert.NoError(err) + assert.NotNil(got2) + got2.Name = got.Name + got3, gotCount3, err := repo.UpdateCatalog(context.Background(), got2, []string{"name"}) + assert.NoError(err) + assert.NotNil(got3) + assert.NotSame(got2, got3) + assert.Equal(got.Name, got3.Name) + assert.Equal(got2.Description, got3.Description) + assert.Equal(1, gotCount3, "row count") + }) + + t.Run("change-scope-id", func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + + c1, c2 := testCatalog(t, conn), testCatalog(t, conn) + assert.NotEqual(c1.ScopeId, c2.ScopeId) + orig := c1.clone() + + c1.ScopeId = c2.ScopeId + assert.Equal(c1.ScopeId, c2.ScopeId) + + got1, gotCount1, err := repo.UpdateCatalog(context.Background(), c1, []string{"name"}) + + assert.NoError(err) + assert.NotNil(got1) + assert.Equal(orig.ScopeId, got1.ScopeId) + assert.Equal(1, gotCount1, "row count") + }) +} + +// TODO(mgaffney,jimlambrt) 05/2020: delete after support for setting +// columns to nil is added to db.Update. +func TestNullAssert(t *testing.T) { + /* + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + + isNotNull := &HostCatalog{HostCatalog: &store.HostCatalog{PublicId: "sthc_JSH52G07wI"}} + m := isNotNull + if err := conn.Model(m).Where("public_id = ?", m.GetPublicId()).UpdateColumn("name", gorm.Expr("NULL")).Error; err != nil { + t.Fatalf("could not set to null: %v", err) + } + + assertColumnIsNull(t, conn, isNotNull, "name") + */ +} + +type resource interface { + GetPublicId() string + TableName() string +} + +func assertColumnIsNull(t *testing.T, db *gorm.DB, m resource, column string) { + + query := fmt.Sprintf("public_id = ? AND %s is null", column) + var count int + + // TODO(mgaffney) 05/2020: There is a good chance this test method will be + // needed in other packages. If and when that happens, this method + // should be moved to the db package. If the method is not needed, then + // the method should be refactored to eliminate the direct call on + // gorm. + if err := db.Model(m).Where(query, m.GetPublicId()).Count(&count).Error; err != nil { + t.Fatalf("could not query: table: %s, column: %s, public_id: %s err: %v", m.TableName(), column, m.GetPublicId(), err) + } + assert.Equalf(t, 1, count, "want NULL, got NOT NULL - table: %s, column: %s, public_id: %s", m.TableName(), column, m.GetPublicId()) +} + +func TestRepository_LookupCatalog(t *testing.T) { + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + + cat := testCatalog(t, conn) + badId, err := newHostCatalogId() + assert.NoError(t, err) + assert.NotNil(t, badId) + + rw := db.New(conn) + wrapper := db.TestWrapper(t) + + var tests = []struct { + name string + id string + want *HostCatalog + wantErr error + }{ + { + name: "found", + id: cat.GetPublicId(), + want: cat, + }, + { + name: "not-found", + id: badId, + want: nil, + }, + { + name: "bad-public-id", + id: "", + want: nil, + wantErr: db.ErrInvalidParameter, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + + got, err := repo.LookupCatalog(context.Background(), tt.id) + if tt.wantErr != nil { + assert.Truef(errors.Is(err, tt.wantErr), "want err: %q got: %q", tt.wantErr, err) + return + } + assert.NoError(err) + + switch { + case tt.want == nil: + assert.Nil(got) + case tt.want != nil: + assert.NotNil(got) + assert.Equal(got, tt.want) + } + }) + } +} + +func TestRepository_DeleteCatalog(t *testing.T) { + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + + cat := testCatalog(t, conn) + badId, err := newHostCatalogId() + assert.NoError(t, err) + assert.NotNil(t, badId) + + rw := db.New(conn) + wrapper := db.TestWrapper(t) + + var tests = []struct { + name string + id string + want int + wantErr error + }{ + { + name: "found", + id: cat.GetPublicId(), + want: 1, + }, + { + name: "not-found", + id: badId, + want: 0, + }, + { + name: "bad-public-id", + id: "", + want: 0, + wantErr: db.ErrInvalidParameter, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + assert.NotNil(repo) + + got, err := repo.DeleteCatalog(context.Background(), tt.id) + if tt.wantErr != nil { + assert.Truef(errors.Is(err, tt.wantErr), "want err: %q got: %q", tt.wantErr, err) + return + } + assert.NoError(err) + assert.Equal(tt.want, got, "row count") + }) + } +} diff --git a/internal/host/static/static.go b/internal/host/static/static.go new file mode 100644 index 000000000..4ce075029 --- /dev/null +++ b/internal/host/static/static.go @@ -0,0 +1,151 @@ +package static + +import ( + "fmt" + + "github.com/hashicorp/watchtower/internal/db" + "github.com/hashicorp/watchtower/internal/host/static/store" + "google.golang.org/protobuf/proto" +) + +// A HostCatalog contains static hosts and static host sets. It is owned by +// a scope. +type HostCatalog struct { + *store.HostCatalog + tableName string `gorm:"-"` +} + +// NewHostCatalog creates a new in memory HostCatalog assigned to scopeId. +// Name and description are the only valid options. All other options are +// ignored. +func NewHostCatalog(scopeId string, opt ...Option) (*HostCatalog, error) { + if scopeId == "" { + return nil, fmt.Errorf("new: static host catalog: no scope id: %w", db.ErrInvalidParameter) + } + + opts := getOpts(opt...) + hc := &HostCatalog{ + HostCatalog: &store.HostCatalog{ + ScopeId: scopeId, + Name: opts.withName, + Description: opts.withDescription, + }, + } + return hc, nil +} + +func (c *HostCatalog) clone() *HostCatalog { + cp := proto.Clone(c.HostCatalog) + return &HostCatalog{ + HostCatalog: cp.(*store.HostCatalog), + } +} + +// A Host contains a static address. +type Host struct { + *store.Host + tableName string `gorm:"-"` +} + +// NewHost creates a new in memory Host for address assigned to catalogId. +// Name and description are the only valid options. All other options are +// ignored. +func NewHost(catalogId, address string, opt ...Option) (*Host, error) { + if catalogId == "" { + return nil, fmt.Errorf("new: static host: no catalog id: %w", db.ErrInvalidParameter) + } + if address == "" { + return nil, fmt.Errorf("new: static host: no address: %w", db.ErrInvalidParameter) + } + + opts := getOpts(opt...) + host := &Host{ + Host: &store.Host{ + StaticHostCatalogId: catalogId, + Address: address, + Name: opts.withName, + Description: opts.withDescription, + }, + } + return host, nil +} + +// A HostSet is a collection of hosts from the set's catalog. +type HostSet struct { + *store.HostSet + tableName string `gorm:"-"` +} + +// NewHostSet creates a new in memory HostSet assigned to catalogId. +// Name and description are the only valid options. All other options are +// ignored. +func NewHostSet(catalogId string, opt ...Option) (*HostSet, error) { + if catalogId == "" { + return nil, fmt.Errorf("new: static host set: no catalog id: %w", db.ErrInvalidParameter) + } + + opts := getOpts(opt...) + set := &HostSet{ + HostSet: &store.HostSet{ + StaticHostCatalogId: catalogId, + Name: opts.withName, + Description: opts.withDescription, + }, + } + return set, nil +} + +// A HostSetMember represents the membership of a host in a host set. +type HostSetMember struct { + *store.HostSetMember + tableName string `gorm:"-"` +} + +// NewHostSetMember creates a new in memory HostSetMember representing the +// membership of hostId in hostSetId. +func NewHostSetMember(hostSetId, hostId string, opt ...Option) (*HostSetMember, error) { + if hostSetId == "" { + return nil, fmt.Errorf("new: static host set member: no host set id: %w", db.ErrInvalidParameter) + } + if hostId == "" { + return nil, fmt.Errorf("new: static host set member: no host id: %w", db.ErrInvalidParameter) + } + member := &HostSetMember{ + HostSetMember: &store.HostSetMember{ + StaticHostSetId: hostSetId, + StaticHostId: hostId, + }, + } + return member, nil +} + +// PublicId prefixes for the resources in the static package. +const ( + HostCatalogPrefix = "sthc" + HostSetPrefix = "sths" + HostPrefix = "sth" +) + +func newHostCatalogId() (string, error) { + id, err := db.NewPublicId(HostCatalogPrefix) + if err != nil { + return "", fmt.Errorf("new host catalog id: %w", err) + } + return id, err +} + +func newHostId() (string, error) { + id, err := db.NewPublicId(HostPrefix) + if err != nil { + return "", fmt.Errorf("new host id: %w", err) + } + return id, err +} + +func newHostSetId() (string, error) { + id, err := db.NewPublicId(HostSetPrefix) + if err != nil { + return "", fmt.Errorf("new host set id: %w", err) + } + return id, err +} diff --git a/internal/host/static/static_test.go b/internal/host/static/static_test.go new file mode 100644 index 000000000..ea6d06f2e --- /dev/null +++ b/internal/host/static/static_test.go @@ -0,0 +1,507 @@ +package static + +import ( + "context" + "fmt" + "testing" + + "github.com/hashicorp/watchtower/internal/db" + "github.com/hashicorp/watchtower/internal/host/static/store" + "github.com/hashicorp/watchtower/internal/iam" + "github.com/jinzhu/gorm" + "github.com/stretchr/testify/assert" +) + +func TestHostCatalog_New(t *testing.T) { + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + + _, prj := iam.TestScopes(t, conn) + + type args struct { + scopeId string + opts []Option + } + + var tests = []struct { + name string + args args + want *HostCatalog + wantErr bool + }{ + { + name: "blank-scopeId", + args: args{ + scopeId: "", + }, + want: nil, + wantErr: true, + }, + { + name: "valid-no-options", + args: args{ + scopeId: prj.GetPublicId(), + }, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + ScopeId: prj.GetPublicId(), + }, + }, + }, + { + name: "valid-with-name", + args: args{ + scopeId: prj.GetPublicId(), + opts: []Option{ + WithName("test-name"), + }, + }, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + ScopeId: prj.GetPublicId(), + Name: "test-name", + }, + }, + }, + { + name: "valid-with-description", + args: args{ + scopeId: prj.GetPublicId(), + opts: []Option{ + WithDescription("test-description"), + }, + }, + want: &HostCatalog{ + HostCatalog: &store.HostCatalog{ + ScopeId: prj.GetPublicId(), + Description: "test-description", + }, + }, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + got, err := NewHostCatalog(tt.args.scopeId, tt.args.opts...) + if tt.wantErr { + assert.Error(err) + assert.Nil(got) + } else { + assert.NoError(err) + if assert.NotNil(got) { + assert.Emptyf(got.PublicId, "PublicId set") + assert.Equal(tt.want, got) + + id, err := newHostCatalogId() + assert.NoError(err) + + tt.want.PublicId = id + got.PublicId = id + + w := db.New(conn) + err2 := w.Create(context.Background(), got) + assert.NoError(err2) + } + } + }) + } +} + +func testCatalogs(t *testing.T, conn *gorm.DB, count int) []*HostCatalog { + t.Helper() + assert := assert.New(t) + _, prj := iam.TestScopes(t, conn) + var cats []*HostCatalog + for i := 0; i < count; i++ { + cat, err := NewHostCatalog(prj.GetPublicId()) + assert.NoError(err) + assert.NotNil(cat) + id, err := newHostCatalogId() + assert.NoError(err) + assert.NotEmpty(id) + cat.PublicId = id + + w := db.New(conn) + err2 := w.Create(context.Background(), cat) + assert.NoError(err2) + cats = append(cats, cat) + } + return cats +} + +func testCatalog(t *testing.T, conn *gorm.DB) *HostCatalog { + t.Helper() + cats := testCatalogs(t, conn, 1) + return cats[0] +} + +func TestHost_New(t *testing.T) { + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + cat := testCatalog(t, conn) + + conn.LogMode(false) + type args struct { + catalogId string + address string + opts []Option + } + + var tests = []struct { + name string + args args + want *Host + wantCreateErr bool + wantWriteErr bool + }{ + { + name: "blank-catalogId", + args: args{ + catalogId: "", + address: "127.0.0.1", + }, + want: nil, + wantCreateErr: true, + }, + { + name: "blank-address", + args: args{ + catalogId: cat.GetPublicId(), + address: "", + }, + want: nil, + wantCreateErr: true, + }, + { + name: "address-to-short", + args: args{ + catalogId: cat.GetPublicId(), + address: "1234567", + }, + want: &Host{ + Host: &store.Host{ + StaticHostCatalogId: cat.GetPublicId(), + Address: "1234567", + }, + }, + wantWriteErr: true, + }, + { + name: "minimum-address", + args: args{ + catalogId: cat.GetPublicId(), + address: "12345678", + }, + want: &Host{ + Host: &store.Host{ + StaticHostCatalogId: cat.GetPublicId(), + Address: "12345678", + }, + }, + }, + { + name: "valid-no-options", + args: args{ + catalogId: cat.GetPublicId(), + address: "127.0.0.1", + }, + want: &Host{ + Host: &store.Host{ + StaticHostCatalogId: cat.GetPublicId(), + Address: "127.0.0.1", + }, + }, + }, + { + name: "valid-with-name", + args: args{ + catalogId: cat.GetPublicId(), + address: "127.0.0.1", + opts: []Option{ + WithName("test-name"), + }, + }, + want: &Host{ + Host: &store.Host{ + StaticHostCatalogId: cat.GetPublicId(), + Address: "127.0.0.1", + Name: "test-name", + }, + }, + }, + { + name: "valid-with-description", + args: args{ + catalogId: cat.GetPublicId(), + address: "127.0.0.1", + opts: []Option{ + WithDescription("test-description"), + }, + }, + want: &Host{ + Host: &store.Host{ + StaticHostCatalogId: cat.GetPublicId(), + Address: "127.0.0.1", + Description: "test-description", + }, + }, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + got, err := NewHost(tt.args.catalogId, tt.args.address, tt.args.opts...) + if tt.wantCreateErr { + assert.Error(err) + assert.Nil(got) + } else { + assert.NoError(err) + if assert.NotNil(got) { + assert.Emptyf(got.PublicId, "PublicId set") + assert.Equal(tt.want, got) + + id, err := newHostId() + assert.NoError(err) + + tt.want.PublicId = id + got.PublicId = id + + w := db.New(conn) + err2 := w.Create(context.Background(), got) + if tt.wantWriteErr { + assert.Error(err2) + } + } + } + }) + } +} + +func TestHostSet_New(t *testing.T) { + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + cat := testCatalog(t, conn) + + conn.LogMode(false) + type args struct { + catalogId string + opts []Option + } + + var tests = []struct { + name string + args args + want *HostSet + wantErr bool + }{ + { + name: "blank-catalogId", + args: args{ + catalogId: "", + }, + want: nil, + wantErr: true, + }, + { + name: "valid-no-options", + args: args{ + catalogId: cat.GetPublicId(), + }, + want: &HostSet{ + HostSet: &store.HostSet{ + StaticHostCatalogId: cat.GetPublicId(), + }, + }, + }, + { + name: "valid-with-name", + args: args{ + catalogId: cat.GetPublicId(), + opts: []Option{ + WithName("test-name"), + }, + }, + want: &HostSet{ + HostSet: &store.HostSet{ + StaticHostCatalogId: cat.GetPublicId(), + Name: "test-name", + }, + }, + }, + { + name: "valid-with-description", + args: args{ + catalogId: cat.GetPublicId(), + opts: []Option{ + WithDescription("test-description"), + }, + }, + want: &HostSet{ + HostSet: &store.HostSet{ + StaticHostCatalogId: cat.GetPublicId(), + Description: "test-description", + }, + }, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + got, err := NewHostSet(tt.args.catalogId, tt.args.opts...) + if tt.wantErr { + assert.Error(err) + assert.Nil(got) + } else { + assert.NoError(err) + if assert.NotNil(got) { + assert.Emptyf(got.PublicId, "PublicId set") + assert.Equal(tt.want, got) + + id, err := newHostSetId() + assert.NoError(err) + + tt.want.PublicId = id + got.PublicId = id + + w := db.New(conn) + err2 := w.Create(context.Background(), got) + assert.NoError(err2) + } + } + }) + } +} + +func testHosts(t *testing.T, conn *gorm.DB, catalogId string, count int) []*Host { + t.Helper() + assert := assert.New(t) + var hosts []*Host + + for i := 0; i < count; i++ { + host, err := NewHost(catalogId, fmt.Sprintf("%s-%d", catalogId, i)) + assert.NoError(err) + assert.NotNil(host) + + id, err := newHostCatalogId() + assert.NoError(err) + assert.NotEmpty(id) + host.PublicId = id + + w := db.New(conn) + err2 := w.Create(context.Background(), host) + assert.NoError(err2) + hosts = append(hosts, host) + } + return hosts +} + +func testSets(t *testing.T, conn *gorm.DB, catalogId string, count int) []*HostSet { + t.Helper() + assert := assert.New(t) + var sets []*HostSet + + for i := 0; i < count; i++ { + set, err := NewHostSet(catalogId) + assert.NoError(err) + assert.NotNil(set) + id, err := newHostSetId() + assert.NoError(err) + assert.NotEmpty(id) + set.PublicId = id + + w := db.New(conn) + err2 := w.Create(context.Background(), set) + assert.NoError(err2) + sets = append(sets, set) + } + return sets +} + +func TestHostSetMember_New(t *testing.T) { + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + conn.LogMode(false) + + cats := testCatalogs(t, conn, 2) + + blueCat := cats[0] + blueSets := testSets(t, conn, blueCat.GetPublicId(), 2) + blueHosts := testHosts(t, conn, blueCat.GetPublicId(), 2) + + // TODO(mgaffney) 05/2020: + // these will be needed when the repository code is done + // greenCat := cats[1] + // greenSets := testSets(t, conn, greenCat.GetPublicId(), 2) + // greenHosts := testHosts(t, conn, greenCat.GetPublicId(), 2) + + var tests = []struct { + name string + set *HostSet + host *Host + wantErr bool + }{ + { + name: "valid-host-in-set", + set: blueSets[0], + host: blueHosts[0], + }, + // { + // name: "invalid-diff-catalogs", + // set: greenSets[0], + // host: blueHosts[0], + // wantErr: true, + // }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + got, err := NewHostSetMember(tt.set.PublicId, tt.host.PublicId) + if tt.wantErr { + assert.Error(err) + assert.Nil(got) + } else { + assert.NoError(err) + if assert.NotNil(got) { + w := db.New(conn) + err2 := w.Create(context.Background(), got) + assert.NoError(err2) + } + } + }) + } +} diff --git a/internal/host/static/store/scanners.go b/internal/host/static/store/scanners.go new file mode 100644 index 000000000..f438b1e6e --- /dev/null +++ b/internal/host/static/store/scanners.go @@ -0,0 +1,33 @@ +package store + +import ( + "database/sql/driver" + "errors" + "fmt" + "time" + + "github.com/golang/protobuf/ptypes" +) + +// Scan supports Timestamps for oplogs +func (ts *Timestamp) Scan(value interface{}) error { + switch t := value.(type) { + case time.Time: + var err error + ts.Timestamp, err = ptypes.TimestampProto(t) // google proto version + if err != nil { + return fmt.Errorf("error converting the timestamp: %w", err) + } + default: + return errors.New("Not a protobuf Timestamp") + } + return nil +} + +// Value supports Timestamps for oplogs +func (ts *Timestamp) Value() (driver.Value, error) { + if ts == nil { + return nil, nil + } + return ptypes.Timestamp(ts.Timestamp) +} diff --git a/internal/host/static/store/scanners_test.go b/internal/host/static/store/scanners_test.go new file mode 100644 index 000000000..3eb4c05cb --- /dev/null +++ b/internal/host/static/store/scanners_test.go @@ -0,0 +1,73 @@ +package store + +import ( + "reflect" + "testing" + "time" + + "github.com/golang/protobuf/ptypes/timestamp" + "github.com/stretchr/testify/assert" +) + +func Test_TimestampValue(t *testing.T) { + t.Parallel() + assert := assert.New(t) + t.Run("valid", func(t *testing.T) { + ts := Timestamp{Timestamp: ×tamp.Timestamp{Seconds: 0, Nanos: 0}} + v, err := ts.Value() + assert.Nil(err) + assert.Equal(v, utcDate(1970, 1, 1)) + }) + t.Run("valid nil ts", func(t *testing.T) { + var ts *Timestamp + v, err := ts.Value() + assert.Nil(err) + assert.Equal(v, nil) + }) + t.Run("invalid ts", func(t *testing.T) { + ts := Timestamp{Timestamp: ×tamp.Timestamp{Seconds: maxValidSeconds, Nanos: 0}} + v, err := ts.Value() + assert.True(err != nil) + assert.Equal(err.Error(), "timestamp: seconds:253402300800 after 10000-01-01") + assert.Equal(v, utcDate(10000, 1, 1)) + }) +} + +func Test_TimestampScan(t *testing.T) { + t.Parallel() + assert := assert.New(t) + t.Run("valid", func(t *testing.T) { + v := time.Unix(0, 0) + ts := Timestamp{} + err := ts.Scan(v) + assert.Nil(err) + assert.True(reflect.DeepEqual(ts.Timestamp, ×tamp.Timestamp{Seconds: 0, Nanos: 0})) + }) + t.Run("valid default time", func(t *testing.T) { + var v time.Time + ts := Timestamp{} + err := ts.Scan(v) + assert.Nil(err) + assert.True(reflect.DeepEqual(ts.Timestamp, ×tamp.Timestamp{Seconds: -62135596800, Nanos: 0})) + }) + t.Run("invalid type", func(t *testing.T) { + v := 1 + ts := Timestamp{} + err := ts.Scan(v) + assert.True(err != nil) + assert.Equal(err.Error(), "Not a protobuf Timestamp") + }) + t.Run("invalid time", func(t *testing.T) { + v := time.Unix(maxValidSeconds, 0) + ts := Timestamp{} + err := ts.Scan(v) + assert.True(err != nil) + assert.Equal(err.Error(), "error converting the timestamp: timestamp: seconds:253402300800 after 10000-01-01") + }) +} + +const maxValidSeconds = 253402300800 + +func utcDate(year, month, day int) time.Time { + return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC) +} diff --git a/internal/host/static/store/static.pb.go b/internal/host/static/store/static.pb.go new file mode 100644 index 000000000..8d03ac82e --- /dev/null +++ b/internal/host/static/store/static.pb.go @@ -0,0 +1,663 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.23.0 +// protoc v3.11.4 +// source: controller/storage/host/static/store/v1/static.proto + +// Package store provides protobufs for storing types in the static host +// package. + +package store + +import ( + proto "github.com/golang/protobuf/proto" + timestamp "github.com/golang/protobuf/ptypes/timestamp" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +// Timestamp for static host messages. We've defined a new local type wrapper +// of google.protobuf.Timestamp so we can implement sql.Scanner and sql.Valuer +// interfaces. See: +// https://golang.org/pkg/database/sql/#Scanner +// https://golang.org/pkg/database/sql/driver/#Valuer +type Timestamp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Timestamp *timestamp.Timestamp `protobuf:"bytes,1,opt,name=timestamp,proto3" json:"timestamp,omitempty"` +} + +func (x *Timestamp) Reset() { + *x = Timestamp{} + if protoimpl.UnsafeEnabled { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Timestamp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Timestamp) ProtoMessage() {} + +func (x *Timestamp) ProtoReflect() protoreflect.Message { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Timestamp.ProtoReflect.Descriptor instead. +func (*Timestamp) Descriptor() ([]byte, []int) { + return file_controller_storage_host_static_store_v1_static_proto_rawDescGZIP(), []int{0} +} + +func (x *Timestamp) GetTimestamp() *timestamp.Timestamp { + if x != nil { + return x.Timestamp + } + return nil +} + +type HostCatalog struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // public_is is a surrogate key suitable for use in a public API. + // @inject_tag: gorm:"primary_key" + PublicId string `protobuf:"bytes,1,opt,name=public_id,json=publicId,proto3" json:"public_id,omitempty" gorm:"primary_key"` + // The create_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"default:current_timestamp"` + // The update_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + UpdateTime *Timestamp `protobuf:"bytes,3,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty" gorm:"default:current_timestamp"` + // name is optional. If set, it must be unique within scope_id. + // @inject_tag: `gorm:"default:null"` + Name string `protobuf:"bytes,4,opt,name=name,proto3" json:"name,omitempty" gorm:"default:null"` + // description is optional. + // @inject_tag: `gorm:"default:null"` + Description string `protobuf:"bytes,5,opt,name=description,proto3" json:"description,omitempty" gorm:"default:null"` + // The scope_id of the owning scope and must be set. + // @inject_tag: gorm:"not_null" + ScopeId string `protobuf:"bytes,6,opt,name=scope_id,json=scopeId,proto3" json:"scope_id,omitempty" gorm:"not_null"` +} + +func (x *HostCatalog) Reset() { + *x = HostCatalog{} + if protoimpl.UnsafeEnabled { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HostCatalog) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HostCatalog) ProtoMessage() {} + +func (x *HostCatalog) ProtoReflect() protoreflect.Message { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HostCatalog.ProtoReflect.Descriptor instead. +func (*HostCatalog) Descriptor() ([]byte, []int) { + return file_controller_storage_host_static_store_v1_static_proto_rawDescGZIP(), []int{1} +} + +func (x *HostCatalog) GetPublicId() string { + if x != nil { + return x.PublicId + } + return "" +} + +func (x *HostCatalog) GetCreateTime() *Timestamp { + if x != nil { + return x.CreateTime + } + return nil +} + +func (x *HostCatalog) GetUpdateTime() *Timestamp { + if x != nil { + return x.UpdateTime + } + return nil +} + +func (x *HostCatalog) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *HostCatalog) GetDescription() string { + if x != nil { + return x.Description + } + return "" +} + +func (x *HostCatalog) GetScopeId() string { + if x != nil { + return x.ScopeId + } + return "" +} + +type Host struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // public_is is a surrogate key suitable for use in a public API. + // @inject_tag: gorm:"primary_key" + PublicId string `protobuf:"bytes,1,opt,name=public_id,json=publicId,proto3" json:"public_id,omitempty" gorm:"primary_key"` + // The create_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"default:current_timestamp"` + // The update_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + UpdateTime *Timestamp `protobuf:"bytes,3,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty" gorm:"default:current_timestamp"` + // name is optional. If set, it must be unique within + // static_host_catalog_id. + // @inject_tag: `gorm:"default:null"` + Name string `protobuf:"bytes,4,opt,name=name,proto3" json:"name,omitempty" gorm:"default:null"` + // description is optional. + // @inject_tag: `gorm:"default:null"` + Description string `protobuf:"bytes,5,opt,name=description,proto3" json:"description,omitempty" gorm:"default:null"` + // static_host_catalog_id is the public_id of the owning + // static_host_catalog and must be set. + // @inject_tag: gorm:"not_null" + StaticHostCatalogId string `protobuf:"bytes,6,opt,name=static_host_catalog_id,json=staticHostCatalogId,proto3" json:"static_host_catalog_id,omitempty" gorm:"not_null"` + // address is the IP Address or DNS name of the host. It must be set and + // it must be unique within static_host_catalog_id. + // @inject_tag: gorm:"not_null" + Address string `protobuf:"bytes,7,opt,name=address,proto3" json:"address,omitempty" gorm:"not_null"` +} + +func (x *Host) Reset() { + *x = Host{} + if protoimpl.UnsafeEnabled { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Host) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Host) ProtoMessage() {} + +func (x *Host) ProtoReflect() protoreflect.Message { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Host.ProtoReflect.Descriptor instead. +func (*Host) Descriptor() ([]byte, []int) { + return file_controller_storage_host_static_store_v1_static_proto_rawDescGZIP(), []int{2} +} + +func (x *Host) GetPublicId() string { + if x != nil { + return x.PublicId + } + return "" +} + +func (x *Host) GetCreateTime() *Timestamp { + if x != nil { + return x.CreateTime + } + return nil +} + +func (x *Host) GetUpdateTime() *Timestamp { + if x != nil { + return x.UpdateTime + } + return nil +} + +func (x *Host) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *Host) GetDescription() string { + if x != nil { + return x.Description + } + return "" +} + +func (x *Host) GetStaticHostCatalogId() string { + if x != nil { + return x.StaticHostCatalogId + } + return "" +} + +func (x *Host) GetAddress() string { + if x != nil { + return x.Address + } + return "" +} + +type HostSet struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // public_is is a surrogate key suitable for use in a public API. + // @inject_tag: gorm:"primary_key" + PublicId string `protobuf:"bytes,1,opt,name=public_id,json=publicId,proto3" json:"public_id,omitempty" gorm:"primary_key"` + // The create_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"default:current_timestamp"` + // The update_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + UpdateTime *Timestamp `protobuf:"bytes,3,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty" gorm:"default:current_timestamp"` + // name is optional. If set, it must be unique within + // static_host_catalog_id. + // @inject_tag: `gorm:"default:null"` + Name string `protobuf:"bytes,4,opt,name=name,proto3" json:"name,omitempty" gorm:"default:null"` + // description is optional. + // @inject_tag: `gorm:"default:null"` + Description string `protobuf:"bytes,5,opt,name=description,proto3" json:"description,omitempty" gorm:"default:null"` + // static_host_catalog_id is the public_id of the owning + // static_host_catalog and must be set. + // @inject_tag: gorm:"not_null" + StaticHostCatalogId string `protobuf:"bytes,6,opt,name=static_host_catalog_id,json=staticHostCatalogId,proto3" json:"static_host_catalog_id,omitempty" gorm:"not_null"` +} + +func (x *HostSet) Reset() { + *x = HostSet{} + if protoimpl.UnsafeEnabled { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HostSet) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HostSet) ProtoMessage() {} + +func (x *HostSet) ProtoReflect() protoreflect.Message { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HostSet.ProtoReflect.Descriptor instead. +func (*HostSet) Descriptor() ([]byte, []int) { + return file_controller_storage_host_static_store_v1_static_proto_rawDescGZIP(), []int{3} +} + +func (x *HostSet) GetPublicId() string { + if x != nil { + return x.PublicId + } + return "" +} + +func (x *HostSet) GetCreateTime() *Timestamp { + if x != nil { + return x.CreateTime + } + return nil +} + +func (x *HostSet) GetUpdateTime() *Timestamp { + if x != nil { + return x.UpdateTime + } + return nil +} + +func (x *HostSet) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *HostSet) GetDescription() string { + if x != nil { + return x.Description + } + return "" +} + +func (x *HostSet) GetStaticHostCatalogId() string { + if x != nil { + return x.StaticHostCatalogId + } + return "" +} + +type HostSetMember struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // @inject_tag: gorm:"primary_key" + StaticHostSetId string `protobuf:"bytes,1,opt,name=static_host_set_id,json=staticHostSetId,proto3" json:"static_host_set_id,omitempty" gorm:"primary_key"` + // @inject_tag: gorm:"primary_key" + StaticHostId string `protobuf:"bytes,2,opt,name=static_host_id,json=staticHostId,proto3" json:"static_host_id,omitempty" gorm:"primary_key"` +} + +func (x *HostSetMember) Reset() { + *x = HostSetMember{} + if protoimpl.UnsafeEnabled { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HostSetMember) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HostSetMember) ProtoMessage() {} + +func (x *HostSetMember) ProtoReflect() protoreflect.Message { + mi := &file_controller_storage_host_static_store_v1_static_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HostSetMember.ProtoReflect.Descriptor instead. +func (*HostSetMember) Descriptor() ([]byte, []int) { + return file_controller_storage_host_static_store_v1_static_proto_rawDescGZIP(), []int{4} +} + +func (x *HostSetMember) GetStaticHostSetId() string { + if x != nil { + return x.StaticHostSetId + } + return "" +} + +func (x *HostSetMember) GetStaticHostId() string { + if x != nil { + return x.StaticHostId + } + return "" +} + +var File_controller_storage_host_static_store_v1_static_proto protoreflect.FileDescriptor + +var file_controller_storage_host_static_store_v1_static_proto_rawDesc = []byte{ + 0x0a, 0x34, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x2f, 0x73, 0x74, 0x6f, + 0x72, 0x61, 0x67, 0x65, 0x2f, 0x68, 0x6f, 0x73, 0x74, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, + 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x27, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, + 0x65, 0x72, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x68, 0x6f, 0x73, 0x74, 0x2e, + 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x76, 0x31, 0x1a, + 0x1f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x22, 0x45, 0x0a, 0x09, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x12, 0x38, 0x0a, + 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x09, 0x74, 0x69, + 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x22, 0xa5, 0x02, 0x0a, 0x0b, 0x48, 0x6f, 0x73, 0x74, + 0x43, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x75, 0x62, 0x6c, 0x69, + 0x63, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x75, 0x62, 0x6c, + 0x69, 0x63, 0x49, 0x64, 0x12, 0x53, 0x0a, 0x0b, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x5f, 0x74, + 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x32, 0x2e, 0x63, 0x6f, 0x6e, 0x74, + 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x68, + 0x6f, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, + 0x2e, 0x76, 0x31, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0a, 0x63, + 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x53, 0x0a, 0x0b, 0x75, 0x70, 0x64, + 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x32, + 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x2e, 0x73, 0x74, 0x6f, 0x72, + 0x61, 0x67, 0x65, 0x2e, 0x68, 0x6f, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x2e, + 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, + 0x6d, 0x70, 0x52, 0x0a, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x12, + 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, + 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x5f, 0x69, 0x64, + 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x49, 0x64, 0x22, + 0xd2, 0x02, 0x0a, 0x04, 0x48, 0x6f, 0x73, 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x75, 0x62, 0x6c, + 0x69, 0x63, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x75, 0x62, + 0x6c, 0x69, 0x63, 0x49, 0x64, 0x12, 0x53, 0x0a, 0x0b, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x5f, + 0x74, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x32, 0x2e, 0x63, 0x6f, 0x6e, + 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, + 0x68, 0x6f, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x2e, 0x73, 0x74, 0x6f, 0x72, + 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0a, + 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x53, 0x0a, 0x0b, 0x75, 0x70, + 0x64, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x32, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x2e, 0x73, 0x74, 0x6f, + 0x72, 0x61, 0x67, 0x65, 0x2e, 0x68, 0x6f, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, + 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, + 0x61, 0x6d, 0x70, 0x52, 0x0a, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12, + 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, + 0x61, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x33, 0x0a, 0x16, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x5f, + 0x68, 0x6f, 0x73, 0x74, 0x5f, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x5f, 0x69, 0x64, 0x18, + 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x13, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x48, 0x6f, 0x73, + 0x74, 0x43, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, + 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x64, 0x64, + 0x72, 0x65, 0x73, 0x73, 0x22, 0xbb, 0x02, 0x0a, 0x07, 0x48, 0x6f, 0x73, 0x74, 0x53, 0x65, 0x74, + 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x49, 0x64, 0x12, 0x53, 0x0a, + 0x0b, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x32, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x2e, + 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x68, 0x6f, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x61, + 0x74, 0x69, 0x63, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x54, 0x69, 0x6d, + 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, + 0x6d, 0x65, 0x12, 0x53, 0x0a, 0x0b, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, + 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x32, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, + 0x6c, 0x6c, 0x65, 0x72, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x68, 0x6f, 0x73, + 0x74, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x76, + 0x31, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0a, 0x75, 0x70, 0x64, + 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x64, + 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x33, 0x0a, + 0x16, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x5f, 0x68, 0x6f, 0x73, 0x74, 0x5f, 0x63, 0x61, 0x74, + 0x61, 0x6c, 0x6f, 0x67, 0x5f, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x13, 0x73, + 0x74, 0x61, 0x74, 0x69, 0x63, 0x48, 0x6f, 0x73, 0x74, 0x43, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, + 0x49, 0x64, 0x22, 0x62, 0x0a, 0x0d, 0x48, 0x6f, 0x73, 0x74, 0x53, 0x65, 0x74, 0x4d, 0x65, 0x6d, + 0x62, 0x65, 0x72, 0x12, 0x2b, 0x0a, 0x12, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x5f, 0x68, 0x6f, + 0x73, 0x74, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x0f, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x48, 0x6f, 0x73, 0x74, 0x53, 0x65, 0x74, 0x49, 0x64, + 0x12, 0x24, 0x0a, 0x0e, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x5f, 0x68, 0x6f, 0x73, 0x74, 0x5f, + 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, + 0x48, 0x6f, 0x73, 0x74, 0x49, 0x64, 0x42, 0x42, 0x5a, 0x40, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f, 0x72, 0x70, 0x2f, 0x77, + 0x61, 0x74, 0x63, 0x68, 0x74, 0x6f, 0x77, 0x65, 0x72, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, + 0x61, 0x6c, 0x2f, 0x68, 0x6f, 0x73, 0x74, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x2f, 0x73, + 0x74, 0x6f, 0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_controller_storage_host_static_store_v1_static_proto_rawDescOnce sync.Once + file_controller_storage_host_static_store_v1_static_proto_rawDescData = file_controller_storage_host_static_store_v1_static_proto_rawDesc +) + +func file_controller_storage_host_static_store_v1_static_proto_rawDescGZIP() []byte { + file_controller_storage_host_static_store_v1_static_proto_rawDescOnce.Do(func() { + file_controller_storage_host_static_store_v1_static_proto_rawDescData = protoimpl.X.CompressGZIP(file_controller_storage_host_static_store_v1_static_proto_rawDescData) + }) + return file_controller_storage_host_static_store_v1_static_proto_rawDescData +} + +var file_controller_storage_host_static_store_v1_static_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_controller_storage_host_static_store_v1_static_proto_goTypes = []interface{}{ + (*Timestamp)(nil), // 0: controller.storage.host.static.store.v1.Timestamp + (*HostCatalog)(nil), // 1: controller.storage.host.static.store.v1.HostCatalog + (*Host)(nil), // 2: controller.storage.host.static.store.v1.Host + (*HostSet)(nil), // 3: controller.storage.host.static.store.v1.HostSet + (*HostSetMember)(nil), // 4: controller.storage.host.static.store.v1.HostSetMember + (*timestamp.Timestamp)(nil), // 5: google.protobuf.Timestamp +} +var file_controller_storage_host_static_store_v1_static_proto_depIdxs = []int32{ + 5, // 0: controller.storage.host.static.store.v1.Timestamp.timestamp:type_name -> google.protobuf.Timestamp + 0, // 1: controller.storage.host.static.store.v1.HostCatalog.create_time:type_name -> controller.storage.host.static.store.v1.Timestamp + 0, // 2: controller.storage.host.static.store.v1.HostCatalog.update_time:type_name -> controller.storage.host.static.store.v1.Timestamp + 0, // 3: controller.storage.host.static.store.v1.Host.create_time:type_name -> controller.storage.host.static.store.v1.Timestamp + 0, // 4: controller.storage.host.static.store.v1.Host.update_time:type_name -> controller.storage.host.static.store.v1.Timestamp + 0, // 5: controller.storage.host.static.store.v1.HostSet.create_time:type_name -> controller.storage.host.static.store.v1.Timestamp + 0, // 6: controller.storage.host.static.store.v1.HostSet.update_time:type_name -> controller.storage.host.static.store.v1.Timestamp + 7, // [7:7] is the sub-list for method output_type + 7, // [7:7] is the sub-list for method input_type + 7, // [7:7] is the sub-list for extension type_name + 7, // [7:7] is the sub-list for extension extendee + 0, // [0:7] is the sub-list for field type_name +} + +func init() { file_controller_storage_host_static_store_v1_static_proto_init() } +func file_controller_storage_host_static_store_v1_static_proto_init() { + if File_controller_storage_host_static_store_v1_static_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_controller_storage_host_static_store_v1_static_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Timestamp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_controller_storage_host_static_store_v1_static_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HostCatalog); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_controller_storage_host_static_store_v1_static_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Host); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_controller_storage_host_static_store_v1_static_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HostSet); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_controller_storage_host_static_store_v1_static_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HostSetMember); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_controller_storage_host_static_store_v1_static_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_controller_storage_host_static_store_v1_static_proto_goTypes, + DependencyIndexes: file_controller_storage_host_static_store_v1_static_proto_depIdxs, + MessageInfos: file_controller_storage_host_static_store_v1_static_proto_msgTypes, + }.Build() + File_controller_storage_host_static_store_v1_static_proto = out.File + file_controller_storage_host_static_store_v1_static_proto_rawDesc = nil + file_controller_storage_host_static_store_v1_static_proto_goTypes = nil + file_controller_storage_host_static_store_v1_static_proto_depIdxs = nil +} diff --git a/internal/iam/testing.go b/internal/iam/testing.go new file mode 100644 index 000000000..8651324fa --- /dev/null +++ b/internal/iam/testing.go @@ -0,0 +1,34 @@ +package iam + +import ( + "context" + "testing" + + "github.com/hashicorp/watchtower/internal/db" + "github.com/jinzhu/gorm" + "github.com/stretchr/testify/assert" +) + +// TestScopes creates an organization and project suitable for testing. +func TestScopes(t *testing.T, conn *gorm.DB) (org *Scope, prj *Scope) { + t.Helper() + assert := assert.New(t) + rw := db.New(conn) + wrapper := db.TestWrapper(t) + repo, err := NewRepository(rw, rw, wrapper) + assert.NoError(err) + + org, err = NewOrganization() + org, err = repo.CreateScope(context.Background(), org) + assert.NoError(err) + assert.NotNil(org) + assert.NotEmpty(org.GetPublicId()) + + prj, err = NewProject(org.GetPublicId()) + prj, err = repo.CreateScope(context.Background(), prj) + assert.NoError(err) + assert.NotNil(prj) + assert.NotEmpty(prj.GetPublicId()) + + return +} diff --git a/internal/iam/testing_test.go b/internal/iam/testing_test.go new file mode 100644 index 000000000..896207e6c --- /dev/null +++ b/internal/iam/testing_test.go @@ -0,0 +1,30 @@ +package iam + +import ( + "testing" + + "github.com/hashicorp/watchtower/internal/db" + "github.com/stretchr/testify/assert" +) + +func Test_TestScopes(t *testing.T) { + assert := assert.New(t) + + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + if err := cleanup(); err != nil { + t.Error(err) + } + if err := conn.Close(); err != nil { + t.Error(err) + } + }() + + org, prj := TestScopes(t, conn) + + assert.NotNil(org) + assert.NotEmpty(org.GetPublicId()) + + assert.NotNil(prj) + assert.NotEmpty(prj.GetPublicId()) +} diff --git a/internal/proto/local/controller/storage/host/static/store/v1/static.proto b/internal/proto/local/controller/storage/host/static/store/v1/static.proto new file mode 100644 index 000000000..607b5f75e --- /dev/null +++ b/internal/proto/local/controller/storage/host/static/store/v1/static.proto @@ -0,0 +1,112 @@ +syntax = "proto3"; + +// Package store provides protobufs for storing types in the static host +// package. +package controller.storage.host.static.store.v1; +option go_package = "github.com/hashicorp/watchtower/internal/host/static/store;store"; + +import "google/protobuf/timestamp.proto"; + +// Timestamp for static host messages. We've defined a new local type wrapper +// of google.protobuf.Timestamp so we can implement sql.Scanner and sql.Valuer +// interfaces. See: +// https://golang.org/pkg/database/sql/#Scanner +// https://golang.org/pkg/database/sql/driver/#Valuer +message Timestamp { + google.protobuf.Timestamp timestamp = 1; +} + +message HostCatalog { + // public_is is a surrogate key suitable for use in a public API. + // @inject_tag: gorm:"primary_key" + string public_id = 1; + + // The create_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + Timestamp create_time = 2; + + // The update_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + Timestamp update_time = 3; + + // name is optional. If set, it must be unique within scope_id. + // @inject_tag: `gorm:"default:null"` + string name = 4; + + // description is optional. + // @inject_tag: `gorm:"default:null"` + string description = 5; + + // The scope_id of the owning scope and must be set. + // @inject_tag: gorm:"not_null" + string scope_id = 6; +} + +message Host { + // public_is is a surrogate key suitable for use in a public API. + // @inject_tag: gorm:"primary_key" + string public_id = 1; + + // The create_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + Timestamp create_time = 2; + + // The update_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + Timestamp update_time = 3; + + // name is optional. If set, it must be unique within + // static_host_catalog_id. + // @inject_tag: `gorm:"default:null"` + string name = 4; + + // description is optional. + // @inject_tag: `gorm:"default:null"` + string description = 5; + + // static_host_catalog_id is the public_id of the owning + // static_host_catalog and must be set. + // @inject_tag: gorm:"not_null" + string static_host_catalog_id = 6; + + // address is the IP Address or DNS name of the host. It must be set and + // it must be unique within static_host_catalog_id. + // @inject_tag: gorm:"not_null" + string address = 7; +} + +message HostSet { + // public_is is a surrogate key suitable for use in a public API. + // @inject_tag: gorm:"primary_key" + string public_id = 1; + + // The create_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + Timestamp create_time = 2; + + // The update_time is set by the database. + // @inject_tag: `gorm:"default:current_timestamp"` + Timestamp update_time = 3; + + // name is optional. If set, it must be unique within + // static_host_catalog_id. + // @inject_tag: `gorm:"default:null"` + string name = 4; + + // description is optional. + // @inject_tag: `gorm:"default:null"` + string description = 5; + + // static_host_catalog_id is the public_id of the owning + // static_host_catalog and must be set. + // @inject_tag: gorm:"not_null" + string static_host_catalog_id = 6; +} + +message HostSetMember { + // @inject_tag: gorm:"primary_key" + string static_host_set_id = 1; + + // @inject_tag: gorm:"primary_key" + string static_host_id = 2; +}