feat(hostsets): Classify all proto fields
This commit is contained in:
1 parent
8fa3e8dee0
commit
d51ce41db8
11 files changed
+483
-285
No files matched your search
@@ -62,6 +62,8 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
const domain = "host"
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
if maskManager[static.Subtype], err = handlers.NewMaskManager(handlers.MaskDestination{&staticstore.HostSet{}, &staticstore.UnimplementedSetFields{}}, handlers.MaskSource{&pb.HostSet{}}); err != nil {
|
||||
@@ -127,7 +129,7 @@ func (s Service) ListHostSetsWithOptions(ctx context.Context, req *pbs.ListHostS
|
||||
}
|
||||
for _, item := range hl {
|
||||
res.Id = item.GetPublicId()
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId("host", res.Id)]
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId(domain, res.Id)]
|
||||
authorizedActions := authResults.FetchActionSetForId(ctx, item.GetPublicId(), idActions, auth.WithResource(&res)).Strings()
|
||||
if len(authorizedActions) == 0 {
|
||||
continue
|
||||
@@ -151,7 +153,13 @@ func (s Service) ListHostSetsWithOptions(ctx context.Context, req *pbs.ListHostS
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -185,7 +193,7 @@ func (s Service) GetHostSet(ctx context.Context, req *pbs.GetHostSetRequest) (*p
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId("host", req.GetId())]
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId(domain, req.GetId())]
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, hs.GetPublicId(), idActions).Strings()))
|
||||
}
|
||||
if plg != nil {
|
||||
@@ -227,7 +235,7 @@ func (s Service) CreateHostSet(ctx context.Context, req *pbs.CreateHostSetReques
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId("host", req.GetItem().GetHostCatalogId())]
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId(domain, req.GetItem().GetHostCatalogId())]
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, hs.GetPublicId(), idActions).Strings()))
|
||||
}
|
||||
if plg != nil {
|
||||
@@ -275,7 +283,7 @@ func (s Service) UpdateHostSet(ctx context.Context, req *pbs.UpdateHostSetReques
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId("host", req.GetId())]
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId(domain, req.GetId())]
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, hs.GetPublicId(), idActions).Strings()))
|
||||
}
|
||||
|
||||
@@ -330,7 +338,7 @@ func (s Service) AddHostSetHosts(ctx context.Context, req *pbs.AddHostSetHostsRe
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId("host", req.GetId())]
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId(domain, req.GetId())]
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, hs.GetPublicId(), idActions).Strings()))
|
||||
}
|
||||
|
||||
@@ -368,7 +376,7 @@ func (s Service) SetHostSetHosts(ctx context.Context, req *pbs.SetHostSetHostsRe
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId("host", req.GetId())]
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId(domain, req.GetId())]
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, hs.GetPublicId(), idActions).Strings()))
|
||||
}
|
||||
|
||||
@@ -407,7 +415,7 @@ func (s Service) RemoveHostSetHosts(ctx context.Context, req *pbs.RemoveHostSetH
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId("host", req.GetId())]
|
||||
idActions := idActionsTypeMap[subtypes.SubtypeFromId(domain, req.GetId())]
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, hs.GetPublicId(), idActions).Strings()))
|
||||
}
|
||||
|
||||
@@ -423,7 +431,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (host.Set, []host.H
|
||||
var hs host.Set
|
||||
var hl []host.Host
|
||||
var plg *plugins.PluginInfo
|
||||
switch subtypes.SubtypeFromId("host", id) {
|
||||
switch subtypes.SubtypeFromId(domain, id) {
|
||||
case static.Subtype:
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
@@ -473,7 +481,7 @@ func (s Service) createInRepo(ctx context.Context, scopeId, catalogId string, it
|
||||
}
|
||||
var hSet host.Set
|
||||
var plg *plugins.PluginInfo
|
||||
switch subtypes.SubtypeFromId("host", catalogId) {
|
||||
switch subtypes.SubtypeFromId(domain, catalogId) {
|
||||
case static.Subtype:
|
||||
h, err := toStorageStaticSet(ctx, catalogId, item)
|
||||
if err != nil {
|
||||
@@ -577,7 +585,7 @@ func (s Service) updatePluginInRepo(ctx context.Context, scopeId string, req *pb
|
||||
|
||||
func (s Service) updateInRepo(ctx context.Context, scopeId, catalogId string, req *pbs.UpdateHostSetRequest) (hs host.Set, hosts []host.Host, plg *plugins.PluginInfo, err error) {
|
||||
const op = "host_sets.(Service).updateInRepo"
|
||||
switch subtypes.SubtypeFromId("host", req.GetId()) {
|
||||
switch subtypes.SubtypeFromId(domain, req.GetId()) {
|
||||
case static.Subtype:
|
||||
hs, hosts, err = s.updateStaticInRepo(ctx, scopeId, catalogId, req)
|
||||
case plugin.Subtype:
|
||||
@@ -589,7 +597,7 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, catalogId string, re
|
||||
func (s Service) deleteFromRepo(ctx context.Context, scopeId, id string) (bool, error) {
|
||||
const op = "host_sets.(Service).deleteFromRepo"
|
||||
rows := 0
|
||||
switch subtypes.SubtypeFromId("host", id) {
|
||||
switch subtypes.SubtypeFromId(domain, id) {
|
||||
case static.Subtype:
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
@@ -616,7 +624,7 @@ func (s Service) listFromRepo(ctx context.Context, catalogId string, opt ...host
|
||||
const op = "host_sets.(Service).listFromRepo"
|
||||
var plg *plugins.PluginInfo
|
||||
var sets []host.Set
|
||||
switch subtypes.SubtypeFromId("host", catalogId) {
|
||||
switch subtypes.SubtypeFromId(domain, catalogId) {
|
||||
case static.Subtype:
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
@@ -740,7 +748,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
|
||||
parentId = id
|
||||
default:
|
||||
var set host.Set
|
||||
switch subtypes.SubtypeFromId("host", id) {
|
||||
switch subtypes.SubtypeFromId(domain, id) {
|
||||
case static.Subtype:
|
||||
ss, _, err := staticRepo.LookupSet(ctx, id)
|
||||
if err != nil {
|
||||
@@ -769,7 +777,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
|
||||
}
|
||||
|
||||
var cat host.Catalog
|
||||
switch subtypes.SubtypeFromId("host", id) {
|
||||
switch subtypes.SubtypeFromId(domain, id) {
|
||||
case static.Subtype:
|
||||
cs, err := staticRepo.LookupCatalog(ctx, parentId)
|
||||
if err != nil {
|
||||
@@ -876,7 +884,9 @@ func toProto(ctx context.Context, in host.Set, hosts []host.Host, opt ...handler
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if len(attrs.GetFields()) > 0 {
|
||||
out.Attributes = attrs
|
||||
out.Attrs = &pb.HostSet_Attributes{
|
||||
Attributes: attrs,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -948,7 +958,7 @@ func validateCreateRequest(ctx context.Context, req *pbs.CreateHostSetRequest) e
|
||||
badFields[globals.PreferredEndpointsField] = fmt.Errorf("Error parsing preferred endpoints: %w.", err).Error()
|
||||
}
|
||||
}
|
||||
switch subtypes.SubtypeFromId("host", req.GetItem().GetHostCatalogId()) {
|
||||
switch subtypes.SubtypeFromId(domain, req.GetItem().GetHostCatalogId()) {
|
||||
case static.Subtype:
|
||||
if req.GetItem().GetType() != "" && req.GetItem().GetType() != static.Subtype.String() {
|
||||
badFields[globals.TypeField] = "Doesn't match the parent resource's type."
|
||||
@@ -979,7 +989,7 @@ func validateUpdateRequest(ctx context.Context, req *pbs.UpdateHostSetRequest) e
|
||||
badFields[globals.PreferredEndpointsField] = fmt.Errorf("Error parsing preferred endpoints: %w.", err).Error()
|
||||
}
|
||||
}
|
||||
switch subtypes.SubtypeFromId("host", req.GetId()) {
|
||||
switch subtypes.SubtypeFromId(domain, req.GetId()) {
|
||||
case static.Subtype:
|
||||
if req.GetItem().GetType() != "" && req.GetItem().GetType() != static.Subtype.String() {
|
||||
badFields[globals.TypeField] = "Cannot modify the resource type."
|
||||
|
||||
@@ -903,7 +903,9 @@ func TestCreate_Plugin(t *testing.T) {
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
Type: plugin.Subtype.String(),
|
||||
SyncIntervalSeconds: &wrapperspb.Int32Value{Value: 90},
|
||||
Attributes: testInputAttrs,
|
||||
Attrs: &pb.HostSet_Attributes{
|
||||
Attributes: testInputAttrs,
|
||||
},
|
||||
}},
|
||||
res: &pbs.CreateHostSetResponse{
|
||||
Uri: fmt.Sprintf("host-sets/%s_", plugin.HostSetPrefix),
|
||||
@@ -920,7 +922,9 @@ func TestCreate_Plugin(t *testing.T) {
|
||||
Type: plugin.Subtype.String(),
|
||||
SyncIntervalSeconds: &wrapperspb.Int32Value{Value: 90},
|
||||
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
|
||||
Attributes: testOutputAttrs,
|
||||
Attrs: &pb.HostSet_Attributes{
|
||||
Attributes: testOutputAttrs,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1480,11 +1484,13 @@ func TestUpdate_Plugin(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
resp, err := tested.CreateHostSet(ctx, &pbs.CreateHostSetRequest{Item: &pb.HostSet{
|
||||
HostCatalogId: hc.GetPublicId(),
|
||||
Name: wrapperspb.String("default"),
|
||||
Description: wrapperspb.String("default"),
|
||||
Type: plugin.Subtype.String(),
|
||||
Attributes: attr,
|
||||
HostCatalogId: hc.GetPublicId(),
|
||||
Name: wrapperspb.String("default"),
|
||||
Description: wrapperspb.String("default"),
|
||||
Type: plugin.Subtype.String(),
|
||||
Attrs: &pb.HostSet_Attributes{
|
||||
Attributes: attr,
|
||||
},
|
||||
PreferredEndpoints: []string{"dns:default"},
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
@@ -1528,7 +1534,9 @@ func TestUpdate_Plugin(t *testing.T) {
|
||||
}
|
||||
updateAttrs := func(i *structpb.Struct) updateFn {
|
||||
return func(c *pb.HostSet) {
|
||||
c.Attributes = i
|
||||
c.Attrs = &pb.HostSet_Attributes{
|
||||
Attributes: i,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user