2020-08-21 10:57:30 -07:00
package host_sets
import (
"context"
"fmt"
"github.com/hashicorp/boundary/internal/auth"
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-27 20:43:33 -04:00
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/hostsets"
2020-08-21 10:57:30 -07:00
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/host"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/host/static/store"
"github.com/hashicorp/boundary/internal/servers/controller/common"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
2020-09-01 12:31:54 -04:00
"github.com/hashicorp/boundary/internal/types/action"
2020-09-04 14:48:11 -07:00
"github.com/hashicorp/boundary/internal/types/resource"
2020-10-02 16:45:32 -07:00
"github.com/hashicorp/boundary/sdk/strutil"
2020-08-21 10:57:30 -07:00
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/wrapperspb"
)
var (
maskManager handlers . MaskManager
)
func init () {
var err error
2020-09-01 09:23:41 -07:00
if maskManager , err = handlers . NewMaskManager ( & store . HostSet {}, & pb . HostSet {}); err != nil {
2020-08-21 10:57:30 -07:00
panic ( err )
}
}
type Service struct {
2020-11-11 14:41:05 -05:00
pbs . UnimplementedHostSetServiceServer
2020-08-21 10:57:30 -07:00
staticRepoFn common . StaticRepoFactory
}
var _ pbs . HostSetServiceServer = Service {}
2020-10-08 12:36:01 -07:00
// NewService returns a host set Service which handles host set related requests to boundary and uses the provided
2020-08-21 10:57:30 -07:00
// repositories for storage and retrieval.
func NewService ( repoFn common . StaticRepoFactory ) ( Service , error ) {
if repoFn == nil {
return Service {}, fmt . Errorf ( "nil static repository provided" )
}
return Service { staticRepoFn : repoFn }, nil
}
2020-09-01 12:31:54 -04:00
func ( s Service ) ListHostSets ( ctx context . Context , req * pbs . ListHostSetsRequest ) ( * pbs . ListHostSetsResponse , error ) {
2020-08-21 10:57:30 -07:00
if err := validateListRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
_ , authResults := s . parentAndAuthResult ( ctx , req . GetHostCatalogId (), action . List )
2020-09-01 12:31:54 -04:00
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-21 10:57:30 -07:00
hl , err := s . listFromRepo ( ctx , req . GetHostCatalogId ())
if err != nil {
return nil , err
}
for _ , item := range hl {
item . Scope = authResults . Scope
}
return & pbs . ListHostSetsResponse { Items : hl }, nil
}
// GetHostSet implements the interface pbs.HostSetServiceServer.
func ( s Service ) GetHostSet ( ctx context . Context , req * pbs . GetHostSetRequest ) ( * pbs . GetHostSetResponse , error ) {
if err := validateGetRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
_ , authResults := s . parentAndAuthResult ( ctx , req . GetId (), action . Read )
2020-09-01 12:31:54 -04:00
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-21 10:57:30 -07:00
hc , err := s . getFromRepo ( ctx , req . GetId ())
if err != nil {
return nil , err
}
hc . Scope = authResults . Scope
return & pbs . GetHostSetResponse { Item : hc }, nil
}
// CreateHostSet implements the interface pbs.HostSetServiceServer.
func ( s Service ) CreateHostSet ( ctx context . Context , req * pbs . CreateHostSetRequest ) ( * pbs . CreateHostSetResponse , error ) {
if err := validateCreateRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
cat , authResults := s . parentAndAuthResult ( ctx , req . GetItem (). GetHostCatalogId (), action . Create )
2020-09-01 12:31:54 -04:00
if authResults . Error != nil {
return nil , authResults . Error
}
2020-09-04 14:48:11 -07:00
h , err := s . createInRepo ( ctx , authResults . Scope . GetId (), cat . GetPublicId (), req . GetItem ())
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
h . Scope = authResults . Scope
return & pbs . CreateHostSetResponse {
Item : h ,
2020-09-02 19:43:41 -07:00
Uri : fmt . Sprintf ( "host-sets/%s" , h . GetId ()),
2020-08-21 10:57:30 -07:00
}, nil
}
// UpdateHostSet implements the interface pbs.HostSetServiceServer.
func ( s Service ) UpdateHostSet ( ctx context . Context , req * pbs . UpdateHostSetRequest ) ( * pbs . UpdateHostSetResponse , error ) {
if err := validateUpdateRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
cat , authResults := s . parentAndAuthResult ( ctx , req . GetId (), action . Update )
2020-09-01 12:31:54 -04:00
if authResults . Error != nil {
return nil , authResults . Error
}
2020-09-04 14:48:11 -07:00
hc , err := s . updateInRepo ( ctx , authResults . Scope . GetId (), cat . GetPublicId (), req . GetId (), req . GetUpdateMask (). GetPaths (), req . GetItem ())
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
hc . Scope = authResults . Scope
return & pbs . UpdateHostSetResponse { Item : hc }, nil
}
// DeleteHostSet implements the interface pbs.HostSetServiceServer.
func ( s Service ) DeleteHostSet ( ctx context . Context , req * pbs . DeleteHostSetRequest ) ( * pbs . DeleteHostSetResponse , error ) {
if err := validateDeleteRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
_ , authResults := s . parentAndAuthResult ( ctx , req . GetId (), action . Delete )
2020-09-01 12:31:54 -04:00
if authResults . Error != nil {
return nil , authResults . Error
}
2020-09-08 23:57:26 -04:00
_ , err := s . deleteFromRepo ( ctx , authResults . Scope . GetId (), req . GetId ())
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
2020-09-21 12:09:30 -07:00
return & pbs . DeleteHostSetResponse {}, nil
2020-08-21 10:57:30 -07:00
}
// AddHostSetHosts implements the interface pbs.HostSetServiceServer.
func ( s Service ) AddHostSetHosts ( ctx context . Context , req * pbs . AddHostSetHostsRequest ) ( * pbs . AddHostSetHostsResponse , error ) {
if err := validateAddRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
_ , authResults := s . parentAndAuthResult ( ctx , req . GetId (), action . AddHostSets )
2020-09-01 12:31:54 -04:00
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-21 10:57:30 -07:00
g , err := s . addInRepo ( ctx , authResults . Scope . GetId (), req . GetId (), req . GetHostIds (), req . GetVersion ())
if err != nil {
return nil , err
}
2020-09-29 12:24:54 -04:00
g . Scope = authResults . Scope
2020-08-21 10:57:30 -07:00
return & pbs . AddHostSetHostsResponse { Item : g }, nil
}
// SetHostSetHosts implements the interface pbs.HostSetServiceServer.
func ( s Service ) SetHostSetHosts ( ctx context . Context , req * pbs . SetHostSetHostsRequest ) ( * pbs . SetHostSetHostsResponse , error ) {
if err := validateSetRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
_ , authResults := s . parentAndAuthResult ( ctx , req . GetId (), action . SetHostSets )
2020-09-01 12:31:54 -04:00
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-21 10:57:30 -07:00
g , err := s . setInRepo ( ctx , authResults . Scope . GetId (), req . GetId (), req . GetHostIds (), req . GetVersion ())
if err != nil {
return nil , err
}
2020-09-29 12:24:54 -04:00
g . Scope = authResults . Scope
2020-08-21 10:57:30 -07:00
return & pbs . SetHostSetHostsResponse { Item : g }, nil
}
// RemoveHostSetHosts implements the interface pbs.HostSetServiceServer.
func ( s Service ) RemoveHostSetHosts ( ctx context . Context , req * pbs . RemoveHostSetHostsRequest ) ( * pbs . RemoveHostSetHostsResponse , error ) {
if err := validateRemoveRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
_ , authResults := s . parentAndAuthResult ( ctx , req . GetId (), action . RemoveHostSets )
2020-09-01 12:31:54 -04:00
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-21 10:57:30 -07:00
g , err := s . removeInRepo ( ctx , authResults . Scope . GetId (), req . GetId (), req . GetHostIds (), req . GetVersion ())
if err != nil {
return nil , err
}
2020-09-29 12:24:54 -04:00
g . Scope = authResults . Scope
2020-08-21 10:57:30 -07:00
return & pbs . RemoveHostSetHostsResponse { Item : g }, nil
}
func ( s Service ) getFromRepo ( ctx context . Context , id string ) ( * pb . HostSet , error ) {
repo , err := s . staticRepoFn ()
if err != nil {
return nil , err
}
2020-08-26 11:04:26 -07:00
h , m , err := repo . LookupSet ( ctx , id )
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
if h == nil {
2020-10-08 12:36:01 -07:00
return nil , handlers . NotFoundErrorf ( "Host Set %q doesn't exist." , id )
2020-08-21 10:57:30 -07:00
}
2020-08-26 11:04:26 -07:00
return toProto ( h , m ), nil
2020-08-21 10:57:30 -07:00
}
func ( s Service ) createInRepo ( ctx context . Context , scopeId , catalogId string , item * pb . HostSet ) ( * pb . HostSet , error ) {
var opts [] static . Option
if item . GetName () != nil {
opts = append ( opts , static . WithName ( item . GetName (). GetValue ()))
}
if item . GetDescription () != nil {
opts = append ( opts , static . WithDescription ( item . GetDescription (). GetValue ()))
}
h , err := static . NewHostSet ( catalogId , opts ... )
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to build host set for creation: %v." , err )
2020-08-21 10:57:30 -07:00
}
repo , err := s . staticRepoFn ()
if err != nil {
return nil , err
}
out , err := repo . CreateSet ( ctx , scopeId , h )
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
2020-09-25 13:41:20 -07:00
}
return nil , fmt . Errorf ( "unable to create host set: %w" , err )
2020-08-21 10:57:30 -07:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to create host set but no error returned from repository." )
2020-08-21 10:57:30 -07:00
}
return toProto ( out , nil ), nil
}
func ( s Service ) updateInRepo ( ctx context . Context , scopeId , catalogId , id string , mask [] string , item * pb . HostSet ) ( * pb . HostSet , error ) {
var opts [] static . Option
if desc := item . GetDescription (); desc != nil {
opts = append ( opts , static . WithDescription ( desc . GetValue ()))
}
if name := item . GetName (); name != nil {
opts = append ( opts , static . WithName ( name . GetValue ()))
}
h , err := static . NewHostSet ( catalogId , opts ... )
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to build host set for update: %v." , err )
2020-08-21 10:57:30 -07:00
}
h . PublicId = id
dbMask := maskManager . Translate ( mask )
if len ( dbMask ) == 0 {
2020-08-28 00:17:09 -04:00
return nil , handlers . InvalidArgumentErrorf ( "No valid fields included in the update mask." , map [ string ] string { "update_mask" : "No valid fields provided in the update mask." })
2020-08-21 10:57:30 -07:00
}
repo , err := s . staticRepoFn ()
if err != nil {
return nil , err
}
2020-08-26 11:04:26 -07:00
out , m , rowsUpdated , err := repo . UpdateSet ( ctx , scopeId , h , item . GetVersion (), dbMask )
2020-08-21 10:57:30 -07:00
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to update host set: %w." , err )
2020-08-21 10:57:30 -07:00
}
if rowsUpdated == 0 {
2020-10-08 12:36:01 -07:00
return nil , handlers . NotFoundErrorf ( "Host Set %q doesn't exist or incorrect version provided." , id )
2020-08-21 10:57:30 -07:00
}
2020-08-26 11:04:26 -07:00
return toProto ( out , m ), nil
2020-08-21 10:57:30 -07:00
}
func ( s Service ) deleteFromRepo ( ctx context . Context , scopeId , id string ) ( bool , error ) {
repo , err := s . staticRepoFn ()
if err != nil {
return false , err
}
rows , err := repo . DeleteSet ( ctx , scopeId , id )
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return false , e
}
2020-09-25 13:41:20 -07:00
return false , fmt . Errorf ( "unable to delete host: %w" , err )
2020-08-21 10:57:30 -07:00
}
return rows > 0 , nil
}
func ( s Service ) listFromRepo ( ctx context . Context , catalogId string ) ([] * pb . HostSet , error ) {
repo , err := s . staticRepoFn ()
if err != nil {
return nil , err
}
hl , err := repo . ListSets ( ctx , catalogId )
if err != nil {
return nil , err
}
var outH [] * pb . HostSet
for _ , h := range hl {
outH = append ( outH , toProto ( h , nil ))
}
return outH , nil
}
func ( s Service ) addInRepo ( ctx context . Context , scopeId , setId string , hostIds [] string , version uint32 ) ( * pb . HostSet , error ) {
repo , err := s . staticRepoFn ()
if err != nil {
return nil , err
}
2020-10-02 16:45:32 -07:00
_ , err = repo . AddSetMembers ( ctx , scopeId , setId , version , strutil . RemoveDuplicates ( hostIds , false ))
2020-08-21 10:57:30 -07:00
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to add hosts to host set: %v." , err )
2020-08-26 11:04:26 -07:00
}
out , m , err := repo . LookupSet ( ctx , setId )
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to look up host set after adding hosts: %w" , err )
2020-08-21 10:57:30 -07:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup host set after adding hosts to it." )
2020-08-21 10:57:30 -07:00
}
2020-08-26 11:04:26 -07:00
return toProto ( out , m ), nil
2020-08-21 10:57:30 -07:00
}
func ( s Service ) setInRepo ( ctx context . Context , scopeId , setId string , hostIds [] string , version uint32 ) ( * pb . HostSet , error ) {
repo , err := s . staticRepoFn ()
if err != nil {
return nil , err
}
2020-10-02 16:45:32 -07:00
_ , _ , err = repo . SetSetMembers ( ctx , scopeId , setId , version , strutil . RemoveDuplicates ( hostIds , false ))
2020-08-26 11:04:26 -07:00
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to set hosts in host set: %v." , err )
2020-08-26 11:04:26 -07:00
}
2020-08-21 10:57:30 -07:00
2020-08-26 11:04:26 -07:00
out , m , err := repo . LookupSet ( ctx , setId )
2020-08-21 10:57:30 -07:00
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to look up host set after setting hosts: %w" , err )
2020-08-21 10:57:30 -07:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup host set after setting hosts for it." )
2020-08-21 10:57:30 -07:00
}
2020-08-26 11:04:26 -07:00
return toProto ( out , m ), nil
2020-08-21 10:57:30 -07:00
}
func ( s Service ) removeInRepo ( ctx context . Context , scopeId , setId string , hostIds [] string , version uint32 ) ( * pb . HostSet , error ) {
repo , err := s . staticRepoFn ()
if err != nil {
return nil , err
}
2020-10-02 16:45:32 -07:00
_ , err = repo . DeleteSetMembers ( ctx , scopeId , setId , version , strutil . RemoveDuplicates ( hostIds , false ))
2020-08-21 10:57:30 -07:00
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to remove hosts from host set: %v." , err )
2020-08-26 11:04:26 -07:00
}
out , m , err := repo . LookupSet ( ctx , setId )
if err != nil {
2020-12-09 10:11:38 -08:00
if e := errors . Convert ( err ); e != nil {
// This is a domain error, push this error through so the error interceptor can interpret it correctly.
return nil , e
}
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to look up host set: %w" , err )
2020-08-21 10:57:30 -07:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup host set after removing hosts from it." )
2020-08-21 10:57:30 -07:00
}
2020-08-26 11:04:26 -07:00
return toProto ( out , m ), nil
2020-08-21 10:57:30 -07:00
}
2020-09-04 14:48:11 -07:00
func ( s Service ) parentAndAuthResult ( ctx context . Context , id string , a action . Type ) ( * static . HostCatalog , auth . VerifyResults ) {
res := auth . VerifyResults {}
repo , err := s . staticRepoFn ()
if err != nil {
res . Error = err
return nil , res
}
var parentId string
opts := [] auth . Option { auth . WithType ( resource . HostSet ), auth . WithAction ( a )}
switch a {
case action . List , action . Create :
parentId = id
default :
set , _ , err := repo . LookupSet ( ctx , id )
if err != nil {
res . Error = err
return nil , res
}
if set == nil {
2020-09-08 23:57:26 -04:00
res . Error = handlers . NotFoundError ()
2020-09-04 14:48:11 -07:00
return nil , res
}
parentId = set . GetCatalogId ()
opts = append ( opts , auth . WithId ( id ))
}
cat , err := repo . LookupCatalog ( ctx , parentId )
if err != nil {
res . Error = err
return nil , res
}
if cat == nil {
2020-09-08 23:57:26 -04:00
res . Error = handlers . NotFoundError ()
2020-09-04 14:48:11 -07:00
return nil , res
}
opts = append ( opts , auth . WithScopeId ( cat . GetScopeId ()), auth . WithPin ( parentId ))
return cat , auth . Verify ( ctx , opts ... )
}
2020-08-26 11:04:26 -07:00
func toProto ( in * static . HostSet , hs [] * static . Host ) * pb . HostSet {
2020-08-21 10:57:30 -07:00
out := pb . HostSet {
Id : in . GetPublicId (),
HostCatalogId : in . GetCatalogId (),
Type : host . StaticSubtype . String (),
CreatedTime : in . GetCreateTime (). GetTimestamp (),
UpdatedTime : in . GetUpdateTime (). GetTimestamp (),
Version : in . GetVersion (),
}
if in . GetDescription () != "" {
out . Description = wrapperspb . String ( in . GetDescription ())
}
if in . GetName () != "" {
out . Name = wrapperspb . String ( in . GetName ())
}
2020-08-26 11:04:26 -07:00
for _ , h := range hs {
out . HostIds = append ( out . HostIds , h . GetPublicId ())
2020-08-21 10:57:30 -07:00
}
return & out
}
// A validateX method should exist for each method above. These methods do not make calls to any backing service but enforce
// requirements on the structure of the request. They verify that:
// * The path passed in is correctly formatted
// * All required parameters are set
// * There are no conflicting parameters provided
// * The type asserted by the ID and/or field is known
// * If relevant, the type derived from the id prefix matches what is claimed by the type field
func validateGetRequest ( req * pbs . GetHostSetRequest ) error {
2020-09-01 12:31:54 -04:00
return handlers . ValidateGetRequest ( static . HostSetPrefix , req , handlers . NoopValidatorFn )
2020-08-21 10:57:30 -07:00
}
func validateCreateRequest ( req * pbs . CreateHostSetRequest ) error {
return handlers . ValidateCreateRequest ( req . GetItem (), func () map [ string ] string {
badFields := map [ string ] string {}
2020-09-02 19:43:41 -07:00
if ! handlers . ValidId ( static . HostCatalogPrefix , req . GetItem (). GetHostCatalogId ()) {
2020-08-21 10:57:30 -07:00
badFields [ "host_catalog_id" ] = "The field is incorrectly formatted."
}
2020-09-02 19:43:41 -07:00
switch host . SubtypeFromId ( req . GetItem (). GetHostCatalogId ()) {
2020-08-21 10:57:30 -07:00
case host . StaticSubtype :
if req . GetItem (). GetType () != "" && req . GetItem (). GetType () != host . StaticSubtype . String () {
badFields [ "type" ] = "Doesn't match the parent resource's type."
}
}
return badFields
})
}
func validateUpdateRequest ( req * pbs . UpdateHostSetRequest ) error {
return handlers . ValidateUpdateRequest ( static . HostSetPrefix , req , req . GetItem (), func () map [ string ] string {
badFields := map [ string ] string {}
2020-09-08 16:04:19 -07:00
switch host . SubtypeFromId ( req . GetId ()) {
case host . StaticSubtype :
if req . GetItem (). GetType () != "" && req . GetItem (). GetType () != host . StaticSubtype . String () {
badFields [ "type" ] = "Cannot modify the resource type."
}
2020-08-21 10:57:30 -07:00
}
return badFields
})
}
func validateDeleteRequest ( req * pbs . DeleteHostSetRequest ) error {
2020-09-01 12:31:54 -04:00
return handlers . ValidateDeleteRequest ( static . HostSetPrefix , req , handlers . NoopValidatorFn )
2020-08-21 10:57:30 -07:00
}
func validateListRequest ( req * pbs . ListHostSetsRequest ) error {
badFields := map [ string ] string {}
if ! handlers . ValidId ( static . HostCatalogPrefix , req . GetHostCatalogId ()) {
badFields [ "host_catalog_id" ] = "The field is incorrectly formatted."
}
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Improperly formatted identifier." , badFields )
}
return nil
}
func validateAddRequest ( req * pbs . AddHostSetHostsRequest ) error {
badFields := map [ string ] string {}
if ! handlers . ValidId ( static . HostSetPrefix , req . GetId ()) {
badFields [ "id" ] = "Incorrectly formatted identifier."
}
if req . GetVersion () == 0 {
badFields [ "version" ] = "Required field."
}
if len ( req . GetHostIds ()) == 0 {
badFields [ "host_ids" ] = "Must be non-empty."
}
2020-09-01 12:31:54 -04:00
for _ , id := range req . GetHostIds () {
if ! handlers . ValidId ( static . HostPrefix , id ) {
badFields [ "host_ids" ] = "Incorrectly formatted host identifier."
break
}
}
2020-08-21 10:57:30 -07:00
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}
func validateSetRequest ( req * pbs . SetHostSetHostsRequest ) error {
badFields := map [ string ] string {}
if ! handlers . ValidId ( static . HostSetPrefix , req . GetId ()) {
badFields [ "id" ] = "Incorrectly formatted identifier."
}
if req . GetVersion () == 0 {
badFields [ "version" ] = "Required field."
}
2020-09-01 12:31:54 -04:00
for _ , id := range req . GetHostIds () {
if ! handlers . ValidId ( static . HostPrefix , id ) {
badFields [ "host_ids" ] = "Incorrectly formatted host identifier."
break
}
}
2020-08-21 10:57:30 -07:00
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}
func validateRemoveRequest ( req * pbs . RemoveHostSetHostsRequest ) error {
badFields := map [ string ] string {}
if ! handlers . ValidId ( static . HostSetPrefix , req . GetId ()) {
badFields [ "id" ] = "Incorrectly formatted identifier."
}
if req . GetVersion () == 0 {
badFields [ "version" ] = "Required field."
}
if len ( req . GetHostIds ()) == 0 {
badFields [ "host_ids" ] = "Must be non-empty."
}
2020-09-01 12:31:54 -04:00
for _ , id := range req . GetHostIds () {
if ! handlers . ValidId ( static . HostPrefix , id ) {
badFields [ "host_ids" ] = "Incorrectly formatted host identifier."
break
}
}
2020-08-21 10:57:30 -07:00
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}