Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RDE related methods #615

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading