feat(hostcatalogs): Classify all proto fields

This commit is contained in:
Johan Brandhorst-Satzkorn committed 2022-04-20 13:32:31 -04:00
1 parent 5440195cce
commit 8fa3e8dee0
11 files changed
+446 -219

No files matched your search

@@ -68,6 +68,8 @@ var (
}
)
const domain = "host"
func init() {
var err error
if staticMaskManager, err = handlers.NewMaskManager(handlers.MaskDestination{&store.HostCatalog{}}, handlers.MaskSource{&pb.HostCatalog{}}); err != nil {
@@ -197,7 +199,13 @@ func (s Service) ListHostCatalogs(ctx context.Context, req *pbs.ListHostCatalogs
return nil, err
}
if filter.Match(item) {
// This comes last so that we can use item fields in the filter after
// the allowed fields are populated above
filterable, err := subtypes.Filterable(item)
if err != nil {
return nil, err
}
if filter.Match(filterable) {
finalItems = append(finalItems, item)
}
}
@@ -396,7 +404,7 @@ func (s Service) DeleteHostCatalog(ctx context.Context, req *pbs.DeleteHostCatal
func (s Service) getFromRepo(ctx context.Context, id string) (host.Catalog, *plugins.PluginInfo, error) {
var plg *plugins.PluginInfo
var cat host.Catalog
switch subtypes.SubtypeFromId("host", id) {
switch subtypes.SubtypeFromId(domain, id) {
case static.Subtype:
repo, err := s.staticRepoFn()
if err != nil {
@@ -518,7 +526,7 @@ func (s Service) createPluginInRepo(ctx context.Context, projId string, req *pbs
func (s Service) createInRepo(ctx context.Context, projId string, req *pbs.CreateHostCatalogRequest) (hc host.Catalog, info *plugins.PluginInfo, err error) {
var plg *plugins.PluginInfo
switch subtypes.SubtypeFromType("host", req.GetItem().GetType()) {
switch subtypes.SubtypeFromType(domain, req.GetItem().GetType()) {
case static.Subtype:
hc, err = s.createStaticInRepo(ctx, projId, req.GetItem())
default:
@@ -582,7 +590,7 @@ func (s Service) updatePluginInRepo(ctx context.Context, projId, id string, mask
func (s Service) updateInRepo(ctx context.Context, projId string, req *pbs.UpdateHostCatalogRequest) (hc host.Catalog, plg *plugins.PluginInfo, err error) {
const op = "host_catalogs.(Service).updateInRepo"
switch subtypes.SubtypeFromId("host", req.GetId()) {
switch subtypes.SubtypeFromId(domain, req.GetId()) {
case static.Subtype:
hc, err = s.updateStaticInRepo(ctx, projId, req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
case plugin.Subtype:
@@ -594,7 +602,7 @@ func (s Service) updateInRepo(ctx context.Context, projId string, req *pbs.Updat
func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
const op = "host_catalogs.(Service).deleteFromRepo"
rows := 0
switch subtypes.SubtypeFromId("host", id) {
switch subtypes.SubtypeFromId(domain, id) {
case static.Subtype:
repo, err := s.staticRepoFn()
if err != nil {
@@ -640,7 +648,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
default:
switch subtypes.SubtypeFromId("host", id) {
switch subtypes.SubtypeFromId(domain, id) {
case static.Subtype:
repo, err := s.staticRepoFn()
if err != nil {
@@ -757,7 +765,9 @@ func toProto(ctx context.Context, in host.Catalog, opt ...handlers.Option) (*pb.
return nil, errors.Wrap(ctx, err, op)
}
if len(attrs.GetFields()) > 0 {
out.Attributes = attrs
out.Attrs = &pb.HostCatalog_Attributes{
Attributes: attrs,
}
}
}
}
@@ -822,7 +832,7 @@ func validateCreateRequest(req *pbs.CreateHostCatalogRequest) error {
if req.GetItem().GetSecretsHmac() != "" {
badFields[globals.SecretsHmacField] = "This is a read only field."
}
switch subtypes.SubtypeFromType("host", req.GetItem().GetType()) {
switch subtypes.SubtypeFromType(domain, req.GetItem().GetType()) {
case static.Subtype:
case plugin.Subtype:
if req.GetItem().GetPlugin() != nil {
@@ -849,16 +859,16 @@ func validateUpdateRequest(req *pbs.UpdateHostCatalogRequest) error {
if req.GetItem().GetSecretsHmac() != "" {
badFields[globals.SecretsHmacField] = "This is a read only field."
}
switch subtypes.SubtypeFromId("host", req.GetId()) {
switch subtypes.SubtypeFromId(domain, req.GetId()) {
case static.Subtype:
if req.GetItem().GetType() != "" && subtypes.SubtypeFromType("host", req.GetItem().GetType()) != static.Subtype {
if req.GetItem().GetType() != "" && subtypes.SubtypeFromType(domain, req.GetItem().GetType()) != static.Subtype {
badFields[globals.TypeField] = "Cannot modify resource type."
}
if req.GetItem().GetPlugin() != nil {
badFields[globals.PluginField] = "This field is unused for this type of host catalog."
}
case plugin.Subtype:
if req.GetItem().GetType() != "" && subtypes.SubtypeFromType("host", req.GetItem().GetType()) != plugin.Subtype {
if req.GetItem().GetType() != "" && subtypes.SubtypeFromType(domain, req.GetItem().GetType()) != plugin.Subtype {
badFields[globals.TypeField] = "Cannot modify resource type."
}
if req.GetItem().GetPlugin() != nil {
@@ -1388,7 +1388,9 @@ func TestUpdate_Plugin(t *testing.T) {
Name: wrapperspb.String("default"),
Description: wrapperspb.String("default"),
Type: "plugin",
Attributes: attr,
Attrs: &pb.HostCatalog_Attributes{
Attributes: attr,
},
}})
require.NoError(t, err)
id := resp.GetItem().GetId()
@@ -1422,7 +1424,9 @@ func TestUpdate_Plugin(t *testing.T) {
}
updateAttrs := func(i *structpb.Struct) updateFn {
return func(c *pb.HostCatalog) {
c.Attributes = i
c.Attrs = &pb.HostCatalog_Attributes{
Attributes: i,
}
}
}
updateSecrets := func(i *structpb.Struct) updateFn {