Skip to content

Commit

Permalink
Improve DefinedEntityType.SetBehaviorAccessControls method to avoid N…
Browse files Browse the repository at this point in the history
…ullPointerException errors (#615)

Signed-off-by: abarreiro <abarreiro@vmware.com>
  • Loading branch information
adambarreiro authored Oct 5, 2023
1 parent 4475e45 commit f7ceaa7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .changes/v2.22.0/615-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Makes `DefinedEntityType` method `SetBehaviorAccessControls` more robust to avoid NullPointerException errors in VCD
when the input is a nil slice [GH-615]
8 changes: 7 additions & 1 deletion govcd/defined_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func (rdeType *DefinedEntityType) DeleteBehaviorOverride(behaviorId string) erro
}

// SetBehaviorAccessControls sets the given slice of BehaviorAccess to the receiver Defined Entity Type.
// If the input is nil, it removes all access controls from the receiver Defined Entity Type.
func (det *DefinedEntityType) SetBehaviorAccessControls(acls []*types.BehaviorAccess) error {
if det.DefinedEntityType.ID == "" {
return fmt.Errorf("ID of the receiver Defined Entity Type is empty")
Expand All @@ -322,8 +323,13 @@ func (det *DefinedEntityType) SetBehaviorAccessControls(acls []*types.BehaviorAc
return err
}

sanitizedAcls := acls
if acls == nil {
sanitizedAcls = []*types.BehaviorAccess{}
}

// Wrap it in OpenAPI pages, this endpoint requires it
rawMessage, err := json.Marshal(acls)
rawMessage, err := json.Marshal(sanitizedAcls)
if err != nil {
return fmt.Errorf("error setting Access controls in payload: %s", err)
}
Expand Down
11 changes: 11 additions & 0 deletions govcd/defined_entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,17 @@ func testRdeTypeAccessControls(check *C, rdeType *DefinedEntityType, behavior *t
err = rdeType.SetBehaviorAccessControls([]*types.BehaviorAccess{})
check.Assert(err, IsNil)

var payload []*types.BehaviorAccess
// This one simulates a filtering that goes wrong and leaves "payload" nil
for _, acl := range allAccCtrl {
if acl.BehaviorId == "notExist" {
payload = append(payload, acl)
}
}

err = rdeType.SetBehaviorAccessControls(payload) // payload is nil, it should be equivalent to set an empty slice of access controls
check.Assert(err, IsNil)

allAccCtrl, err = rdeType.GetAllBehaviorsAccessControls(nil)
check.Assert(err, IsNil)
check.Assert(len(allAccCtrl), Equals, 0)
Expand Down

0 comments on commit f7ceaa7

Please sign in to comment.