2020-08-21 10:57:30 -07:00
package host_sets
import (
"context"
"fmt"
2021-05-04 19:57:35 -05:00
"github.com/hashicorp/boundary/globals"
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
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"
2021-01-20 10:53:01 -05:00
"github.com/hashicorp/boundary/internal/perms"
2021-05-04 19:57:35 -05:00
"github.com/hashicorp/boundary/internal/requests"
2021-07-27 09:59:52 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/auth"
2020-08-21 10:57:30 -07:00
"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"
2021-08-26 10:44:41 -04:00
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostsets"
2021-07-14 17:37:40 -07:00
"github.com/hashicorp/go-secure-stdlib/strutil"
2020-08-21 10:57:30 -07:00
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/wrapperspb"
)
2021-01-20 10:53:01 -05:00
var (
maskManager handlers . MaskManager
// IdActions contains the set of actions that can be performed on
// individual resources
IdActions = action . ActionSet {
2021-04-23 09:13:37 -05:00
action . NoOp ,
2021-01-20 10:53:01 -05:00
action . Read ,
action . Update ,
action . Delete ,
action . AddHosts ,
action . SetHosts ,
action . RemoveHosts ,
}
// CollectionActions contains the set of actions that can be performed on
// this collection
CollectionActions = action . ActionSet {
action . Create ,
action . List ,
}
)
2020-08-21 10:57:30 -07:00
func init () {
var err error
2021-06-22 22:26:26 -04:00
if maskManager , err = handlers . NewMaskManager ( handlers . MaskDestination { & store . HostSet {}}, handlers . MaskSource { & 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 ) {
2021-04-16 12:06:03 -05:00
const op = "host_sets.NewService"
2020-08-21 10:57:30 -07:00
if repoFn == nil {
2021-07-29 18:52:56 -06:00
return Service {}, errors . NewDeprecated ( errors . InvalidParameter , op , "missing static repository" )
2020-08-21 10:57:30 -07:00
}
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
}
2021-05-04 19:57:35 -05:00
if len ( hl ) == 0 {
return & pbs . ListHostSetsResponse {}, nil
}
2021-02-22 10:09:25 -07:00
filter , err := handlers . NewFilter ( req . GetFilter ())
if err != nil {
return nil , err
}
2021-01-20 10:53:01 -05:00
finalItems := make ([] * pb . HostSet , 0 , len ( hl ))
2021-05-04 19:57:35 -05:00
res := perms . Resource {
2021-01-20 10:53:01 -05:00
ScopeId : authResults . Scope . Id ,
Type : resource . HostSet ,
Pin : req . GetHostCatalogId (),
}
2020-08-21 10:57:30 -07:00
for _ , item := range hl {
2021-05-04 19:57:35 -05:00
res . Id = item . GetPublicId ()
authorizedActions := authResults . FetchActionSetForId ( ctx , item . GetPublicId (), IdActions , auth . WithResource ( & res )). Strings ()
if len ( authorizedActions ) == 0 {
2021-02-22 10:09:25 -07:00
continue
}
2021-05-04 19:57:35 -05:00
outputFields := authResults . FetchOutputFields ( res , action . List ). SelfOrDefaults ( authResults . UserId )
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authorizedActions ))
}
item , err := toProto ( ctx , item , nil , outputOpts ... )
if err != nil {
return nil , err
}
2021-02-22 10:09:25 -07:00
if filter . Match ( item ) {
2021-01-20 10:53:01 -05:00
finalItems = append ( finalItems , item )
}
2020-08-21 10:57:30 -07:00
}
2021-01-20 10:53:01 -05:00
return & pbs . ListHostSetsResponse { Items : finalItems }, nil
2020-08-21 10:57:30 -07:00
}
// GetHostSet implements the interface pbs.HostSetServiceServer.
func ( s Service ) GetHostSet ( ctx context . Context , req * pbs . GetHostSetRequest ) ( * pbs . GetHostSetResponse , error ) {
2021-05-04 19:57:35 -05:00
const op = "host_sets.(Service).GetHostSet"
2020-08-21 10:57:30 -07:00
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
}
2021-05-04 19:57:35 -05:00
hs , hosts , err := s . getFromRepo ( ctx , req . GetId ())
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . Internal , op , "no request context found" )
2021-05-04 19:57:35 -05:00
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , hs . GetPublicId (), IdActions ). Strings ()))
}
item , err := toProto ( ctx , hs , hosts , outputOpts ... )
if err != nil {
return nil , err
}
return & pbs . GetHostSetResponse { Item : item }, nil
2020-08-21 10:57:30 -07:00
}
// CreateHostSet implements the interface pbs.HostSetServiceServer.
func ( s Service ) CreateHostSet ( ctx context . Context , req * pbs . CreateHostSetRequest ) ( * pbs . CreateHostSetResponse , error ) {
2021-05-04 19:57:35 -05:00
const op = "host_sets.(Service).CreateHostSet"
2020-08-21 10:57:30 -07:00
if err := validateCreateRequest ( req ); err != nil {
return nil , err
}
2021-06-22 22:26:26 -04:00
_ , 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
}
2021-06-22 22:26:26 -04:00
hs , err := s . createInRepo ( ctx , authResults . Scope . GetId (), req . GetItem (). GetHostCatalogId (), req . GetItem ())
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . Internal , op , "no request context found" )
2021-05-04 19:57:35 -05:00
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , hs . GetPublicId (), IdActions ). Strings ()))
}
item , err := toProto ( ctx , hs , nil , outputOpts ... )
if err != nil {
return nil , err
}
2020-08-21 10:57:30 -07:00
return & pbs . CreateHostSetResponse {
2021-05-04 19:57:35 -05:00
Item : item ,
Uri : fmt . Sprintf ( "host-sets/%s" , item . 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 ) {
2021-05-04 19:57:35 -05:00
const op = "host_sets.(Service).UpdateHostSet"
2020-08-21 10:57:30 -07:00
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
}
2021-05-04 19:57:35 -05:00
hs , hosts , err := s . updateInRepo ( ctx , authResults . Scope . GetId (), cat . GetPublicId (), req . GetId (), req . GetUpdateMask (). GetPaths (), req . GetItem ())
if err != nil {
return nil , err
}
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . Internal , op , "no request context found" )
2021-05-04 19:57:35 -05:00
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , hs . GetPublicId (), IdActions ). Strings ()))
}
item , err := toProto ( ctx , hs , hosts , outputOpts ... )
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
return & pbs . UpdateHostSetResponse { Item : item }, nil
2020-08-21 10:57:30 -07:00
}
// 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
}
2021-04-26 19:38:55 -05:00
return nil , 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 ) {
2021-05-04 19:57:35 -05:00
const op = "host_sets.(Service).AddHostSetHosts"
2020-08-21 10:57:30 -07:00
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
}
2021-05-04 19:57:35 -05:00
hs , hosts , err := s . addInRepo ( ctx , authResults . Scope . GetId (), req . GetId (), req . GetHostIds (), req . GetVersion ())
if err != nil {
return nil , err
}
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . Internal , op , "no request context found" )
2021-05-04 19:57:35 -05:00
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , hs . GetPublicId (), IdActions ). Strings ()))
}
item , err := toProto ( ctx , hs , hosts , outputOpts ... )
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
return & pbs . AddHostSetHostsResponse { Item : item }, nil
2020-08-21 10:57:30 -07:00
}
// SetHostSetHosts implements the interface pbs.HostSetServiceServer.
func ( s Service ) SetHostSetHosts ( ctx context . Context , req * pbs . SetHostSetHostsRequest ) ( * pbs . SetHostSetHostsResponse , error ) {
2021-05-04 19:57:35 -05:00
const op = "host_sets.(Service).SetHostSetHosts"
2020-08-21 10:57:30 -07:00
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
}
2021-05-04 19:57:35 -05:00
hs , hosts , err := s . setInRepo ( ctx , authResults . Scope . GetId (), req . GetId (), req . GetHostIds (), req . GetVersion ())
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . Internal , op , "no request context found" )
2021-05-04 19:57:35 -05:00
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , hs . GetPublicId (), IdActions ). Strings ()))
}
item , err := toProto ( ctx , hs , hosts , outputOpts ... )
if err != nil {
return nil , err
}
return & pbs . SetHostSetHostsResponse { Item : item }, nil
2020-08-21 10:57:30 -07:00
}
// RemoveHostSetHosts implements the interface pbs.HostSetServiceServer.
func ( s Service ) RemoveHostSetHosts ( ctx context . Context , req * pbs . RemoveHostSetHostsRequest ) ( * pbs . RemoveHostSetHostsResponse , error ) {
2021-05-04 19:57:35 -05:00
const op = "host_sets.(Service).RemoveHostSetHosts"
2020-08-21 10:57:30 -07:00
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
}
2021-05-04 19:57:35 -05:00
hs , hosts , err := s . removeInRepo ( ctx , authResults . Scope . GetId (), req . GetId (), req . GetHostIds (), req . GetVersion ())
2020-08-21 10:57:30 -07:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . Internal , op , "no request context found" )
2021-05-04 19:57:35 -05:00
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , hs . GetPublicId (), IdActions ). Strings ()))
}
item , err := toProto ( ctx , hs , hosts , outputOpts ... )
if err != nil {
return nil , err
}
return & pbs . RemoveHostSetHostsResponse { Item : item }, nil
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) getFromRepo ( ctx context . Context , id string ) ( * static . HostSet , [] * static . Host , error ) {
2020-08-21 10:57:30 -07:00
repo , err := s . staticRepoFn ()
if err != nil {
2021-05-04 19:57:35 -05:00
return nil , nil , err
2020-08-21 10:57:30 -07:00
}
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 {
2021-05-04 19:57:35 -05:00
return nil , nil , err
2020-08-21 10:57:30 -07:00
}
if h == nil {
2021-05-04 19:57:35 -05:00
return nil , nil , handlers . NotFoundErrorf ( "Host Set %q doesn't exist." , id )
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
return h , m , nil
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) createInRepo ( ctx context . Context , scopeId , catalogId string , item * pb . HostSet ) ( * static . HostSet , error ) {
2021-04-16 12:06:03 -05:00
const op = "host_sets.(Service).createInRepo"
2020-08-21 10:57:30 -07:00
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 {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "Unable to build host set for creation" ))
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 {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to create host set" ))
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
}
2021-05-04 19:57:35 -05:00
return out , nil
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) updateInRepo ( ctx context . Context , scopeId , catalogId , id string , mask [] string , item * pb . HostSet ) ( * static . HostSet , [] * static . Host , error ) {
2021-04-16 12:06:03 -05:00
const op = "host_sets.(Service).updateInRepo"
2020-08-21 10:57:30 -07:00
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 {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "Unable to build host set for update" ))
2020-08-21 10:57:30 -07:00
}
h . PublicId = id
dbMask := maskManager . Translate ( mask )
if len ( dbMask ) == 0 {
2021-05-04 19:57:35 -05:00
return nil , 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 {
2021-05-04 19:57:35 -05:00
return nil , nil , err
2020-08-21 10:57:30 -07:00
}
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 {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to update host set" ))
2020-08-21 10:57:30 -07:00
}
if rowsUpdated == 0 {
2021-05-04 19:57:35 -05:00
return nil , nil , handlers . NotFoundErrorf ( "Host Set %q doesn't exist or incorrect version provided." , id )
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
return out , m , nil
2020-08-21 10:57:30 -07:00
}
func ( s Service ) deleteFromRepo ( ctx context . Context , scopeId , id string ) ( bool , error ) {
2021-04-16 12:06:03 -05:00
const op = "host_sets.(Service).deleteFromRepo"
2020-08-21 10:57:30 -07:00
repo , err := s . staticRepoFn ()
if err != nil {
return false , err
}
rows , err := repo . DeleteSet ( ctx , scopeId , id )
if err != nil {
2021-07-29 18:52:56 -06:00
return false , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to delete host" ))
2020-08-21 10:57:30 -07:00
}
return rows > 0 , nil
}
2021-05-04 19:57:35 -05:00
func ( s Service ) listFromRepo ( ctx context . Context , catalogId string ) ([] * static . HostSet , error ) {
2020-08-21 10:57:30 -07:00
repo , err := s . staticRepoFn ()
if err != nil {
return nil , err
}
hl , err := repo . ListSets ( ctx , catalogId )
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
return hl , nil
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) addInRepo ( ctx context . Context , scopeId , setId string , hostIds [] string , version uint32 ) ( * static . HostSet , [] * static . Host , error ) {
2021-04-16 12:06:03 -05:00
const op = "host_sets.(Service).addInRepo"
2020-08-21 10:57:30 -07:00
repo , err := s . staticRepoFn ()
if err != nil {
2021-05-04 19:57:35 -05:00
return nil , nil , err
2020-08-21 10:57:30 -07:00
}
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 {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "Unable to add hosts to host set" ))
2020-08-26 11:04:26 -07:00
}
out , m , err := repo . LookupSet ( ctx , setId )
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to look up host set after adding hosts" ))
2020-08-21 10:57:30 -07:00
}
if out == nil {
2021-05-04 19:57:35 -05:00
return nil , nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup host set after adding hosts to it." )
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
return out , m , nil
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) setInRepo ( ctx context . Context , scopeId , setId string , hostIds [] string , version uint32 ) ( * static . HostSet , [] * static . Host , error ) {
2021-04-16 12:06:03 -05:00
const op = "host_sets.(Service).setInRepo"
2020-08-21 10:57:30 -07:00
repo , err := s . staticRepoFn ()
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op )
2020-08-21 10:57:30 -07:00
}
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 {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "Unable to set hosts in host set" ))
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 {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to look up host set after setting hosts" ))
2020-08-21 10:57:30 -07:00
}
if out == nil {
2021-05-04 19:57:35 -05:00
return nil , nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup host set after setting hosts for it." )
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
return out , m , nil
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) removeInRepo ( ctx context . Context , scopeId , setId string , hostIds [] string , version uint32 ) ( * static . HostSet , [] * static . Host , error ) {
2021-04-16 12:06:03 -05:00
const op = "host_sets.(Service).removeInRepo"
2020-08-21 10:57:30 -07:00
repo , err := s . staticRepoFn ()
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op )
2020-08-21 10:57:30 -07:00
}
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 {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "Unable to remove hosts from host set" ))
2020-08-26 11:04:26 -07:00
}
out , m , err := repo . LookupSet ( ctx , setId )
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to look up host set" ))
2020-08-21 10:57:30 -07:00
}
if out == nil {
2021-05-04 19:57:35 -05:00
return nil , nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup host set after removing hosts from it." )
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
return 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 ... )
}
2021-05-04 19:57:35 -05:00
func toProto ( ctx context . Context , in * static . HostSet , hosts [] * static . Host , opt ... handlers . Option ) ( * pb . HostSet , error ) {
opts := handlers . GetOpts ( opt ... )
if opts . WithOutputFields == nil {
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "output fields not found when building hostset proto" )
}
outputFields := * opts . WithOutputFields
out := pb . HostSet {}
if outputFields . Has ( globals . IdField ) {
out . Id = in . GetPublicId ()
}
if outputFields . Has ( globals . HostCatalogIdField ) {
out . HostCatalogId = in . GetCatalogId ()
}
if outputFields . Has ( globals . TypeField ) {
2021-07-27 09:59:52 -07:00
out . Type = static . Subtype . String ()
2021-05-04 19:57:35 -05:00
}
if outputFields . Has ( globals . DescriptionField ) && in . GetDescription () != "" {
out . Description = & wrapperspb . StringValue { Value : in . GetDescription ()}
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
if outputFields . Has ( globals . NameField ) && in . GetName () != "" {
out . Name = & wrapperspb . StringValue { Value : in . GetName ()}
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
if outputFields . Has ( globals . CreatedTimeField ) {
out . CreatedTime = in . GetCreateTime (). GetTimestamp ()
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
if outputFields . Has ( globals . UpdatedTimeField ) {
out . UpdatedTime = in . GetUpdateTime (). GetTimestamp ()
}
if outputFields . Has ( globals . VersionField ) {
out . Version = in . GetVersion ()
}
if outputFields . Has ( globals . ScopeField ) {
out . Scope = opts . WithScope
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
out . AuthorizedActions = opts . WithAuthorizedActions
}
if outputFields . Has ( globals . HostIdsField ) {
for _ , h := range hosts {
out . HostIds = append ( out . HostIds , h . GetPublicId ())
}
2020-08-21 10:57:30 -07:00
}
2021-05-04 19:57:35 -05:00
return & out , nil
2020-08-21 10:57:30 -07:00
}
// 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 {
2021-04-08 20:54:32 -04:00
return handlers . ValidateGetRequest ( handlers . NoopValidatorFn , req , static . HostSetPrefix )
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 {}
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( req . GetItem (). GetHostCatalogId ()), static . HostCatalogPrefix ) {
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 ()) {
2021-07-27 09:59:52 -07:00
case static . Subtype :
if req . GetItem (). GetType () != "" && req . GetItem (). GetType () != static . Subtype . String () {
2020-08-21 10:57:30 -07:00
badFields [ "type" ] = "Doesn't match the parent resource's type."
}
}
return badFields
})
}
func validateUpdateRequest ( req * pbs . UpdateHostSetRequest ) error {
2021-04-08 20:54:32 -04:00
return handlers . ValidateUpdateRequest ( req , req . GetItem (), func () map [ string ] string {
2020-08-21 10:57:30 -07:00
badFields := map [ string ] string {}
2020-09-08 16:04:19 -07:00
switch host . SubtypeFromId ( req . GetId ()) {
2021-07-27 09:59:52 -07:00
case static . Subtype :
if req . GetItem (). GetType () != "" && req . GetItem (). GetType () != static . Subtype . String () {
2020-09-08 16:04:19 -07:00
badFields [ "type" ] = "Cannot modify the resource type."
}
2020-08-21 10:57:30 -07:00
}
return badFields
2021-04-08 20:54:32 -04:00
}, static . HostSetPrefix )
2020-08-21 10:57:30 -07:00
}
func validateDeleteRequest ( req * pbs . DeleteHostSetRequest ) error {
2021-04-08 20:54:32 -04:00
return handlers . ValidateDeleteRequest ( handlers . NoopValidatorFn , req , static . HostSetPrefix )
2020-08-21 10:57:30 -07:00
}
func validateListRequest ( req * pbs . ListHostSetsRequest ) error {
badFields := map [ string ] string {}
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( req . GetHostCatalogId ()), static . HostCatalogPrefix ) {
2020-08-21 10:57:30 -07:00
badFields [ "host_catalog_id" ] = "The field is incorrectly formatted."
}
2021-02-22 10:09:25 -07:00
if _ , err := handlers . NewFilter ( req . GetFilter ()); err != nil {
badFields [ "filter" ] = fmt . Sprintf ( "This field could not be parsed. %v" , err )
}
2020-08-21 10:57:30 -07:00
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Improperly formatted identifier." , badFields )
}
return nil
}
func validateAddRequest ( req * pbs . AddHostSetHostsRequest ) error {
badFields := map [ string ] string {}
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( req . GetId ()), static . HostSetPrefix ) {
2020-08-21 10:57:30 -07:00
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 () {
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), static . HostPrefix ) {
2020-09-01 12:31:54 -04:00
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 {}
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( req . GetId ()), static . HostSetPrefix ) {
2020-08-21 10:57:30 -07:00
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 () {
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), static . HostPrefix ) {
2020-09-01 12:31:54 -04:00
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 {}
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( req . GetId ()), static . HostSetPrefix ) {
2020-08-21 10:57:30 -07:00
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 () {
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), static . HostPrefix ) {
2020-09-01 12:31:54 -04:00
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
}