diff --git a/service/integration/registered_resources_test.go b/service/integration/registered_resources_test.go index 4627190fbc..bd41b797bb 100644 --- a/service/integration/registered_resources_test.go +++ b/service/integration/registered_resources_test.go @@ -1055,6 +1055,16 @@ func (s *RegisteredResourcesSuite) Test_UpdateRegisteredResourceValue_Succeeds() created, err := s.db.PolicyClient.CreateRegisteredResourceValue(s.ctx, ®isteredresources.CreateRegisteredResourceValueRequest{ ResourceId: res.GetId(), Value: "value", + ActionAttributeValues: []*registeredresources.ActionAttributeValue{ + { + ActionIdentifier: ®isteredresources.ActionAttributeValue_ActionName{ + ActionName: actions.ActionNameRead, + }, + AttributeValueIdentifier: ®isteredresources.ActionAttributeValue_AttributeValueFqn{ + AttributeValueFqn: "https://example.com/attr/attr1/value/value1", + }, + }, + }, Metadata: &common.MetadataMutable{ Labels: labels, }, @@ -1079,7 +1089,7 @@ func (s *RegisteredResourcesSuite) Test_UpdateRegisteredResourceValue_Succeeds() s.Require().NotNil(got) s.Equal(created.GetValue(), got.GetValue()) s.Equal(labels, got.GetMetadata().GetLabels()) - s.Empty(got.GetActionAttributeValues()) + s.Require().Len(got.GetActionAttributeValues(), 1) // update with changes updated, err = s.db.PolicyClient.UpdateRegisteredResourceValue(s.ctx, ®isteredresources.UpdateRegisteredResourceValueRequest{ @@ -1092,12 +1102,20 @@ func (s *RegisteredResourcesSuite) Test_UpdateRegisteredResourceValue_Succeeds() ActionAttributeValues: []*registeredresources.ActionAttributeValue{ { ActionIdentifier: ®isteredresources.ActionAttributeValue_ActionName{ - ActionName: actions.ActionNameCreate, + ActionName: actions.ActionNameDelete, }, AttributeValueIdentifier: ®isteredresources.ActionAttributeValue_AttributeValueFqn{ AttributeValueFqn: "https://example.com/attr/attr1/value/value1", }, }, + { + ActionIdentifier: ®isteredresources.ActionAttributeValue_ActionName{ + ActionName: "custom_action_1", + }, + AttributeValueIdentifier: ®isteredresources.ActionAttributeValue_AttributeValueFqn{ + AttributeValueFqn: "https://example.com/attr/attr2/value/value2", + }, + }, }, }) s.Require().NoError(err) @@ -1120,11 +1138,15 @@ func (s *RegisteredResourcesSuite) Test_UpdateRegisteredResourceValue_Succeeds() s.False(updatedAt.AsTime().IsZero()) s.True(updatedAt.AsTime().After(createdAt.AsTime())) actionAttrValues := got.GetActionAttributeValues() - s.Require().Len(actionAttrValues, 1) - s.Equal(actions.ActionNameCreate, actionAttrValues[0].GetAction().GetName()) - attrValue := actionAttrValues[0].GetAttributeValue() - s.Equal("https://example.com/attr/attr1/value/value1", attrValue.GetFqn()) - s.Equal("value1", attrValue.GetValue()) + s.Require().Len(actionAttrValues, 2) + s.Equal(actions.ActionNameDelete, actionAttrValues[0].GetAction().GetName()) + attrValue1 := actionAttrValues[0].GetAttributeValue() + s.Equal("https://example.com/attr/attr1/value/value1", attrValue1.GetFqn()) + s.Equal("value1", attrValue1.GetValue()) + s.Equal("custom_action_1", actionAttrValues[1].GetAction().GetName()) + attrValue2 := actionAttrValues[1].GetAttributeValue() + s.Equal("https://example.com/attr/attr2/value/value2", attrValue2.GetFqn()) + s.Equal("value2", attrValue2.GetValue()) } func (s *RegisteredResourcesSuite) Test_UpdateRegisteredResourceValue_NormalizedName_Succeeds() { diff --git a/service/policy/db/registered_resources.go b/service/policy/db/registered_resources.go index 64aeeb9879..b681771261 100644 --- a/service/policy/db/registered_resources.go +++ b/service/policy/db/registered_resources.go @@ -430,15 +430,18 @@ func (c PolicyDBClient) UpdateRegisteredResourceValue(ctx context.Context, r *re return nil, db.ErrNotFound } - // update overwrites all action attribute values with those provided in the request, so clear all existing ones first - _, err = c.Queries.deleteRegisteredResourceActionAttributeValues(ctx, id) - if err != nil { - return nil, db.WrapIfKnownInvalidQueryErr(err) - } + actionAttrValues := r.GetActionAttributeValues() + if len(actionAttrValues) > 0 { + // update overwrites all action attribute values with those provided in the request, so clear all existing ones first + _, err = c.Queries.deleteRegisteredResourceActionAttributeValues(ctx, id) + if err != nil { + return nil, db.WrapIfKnownInvalidQueryErr(err) + } - err = c.createRegisteredResourceActionAttributeValues(ctx, id, r.GetActionAttributeValues()) - if err != nil { - return nil, db.WrapIfKnownInvalidQueryErr(err) + err = c.createRegisteredResourceActionAttributeValues(ctx, id, actionAttrValues) + if err != nil { + return nil, db.WrapIfKnownInvalidQueryErr(err) + } } return c.GetRegisteredResourceValue(ctx, ®isteredresources.GetRegisteredResourceValueRequest{