Skip to content

Commit

Permalink
Use constants for entity labels
Browse files Browse the repository at this point in the history
Signed-off-by: Dainius Serplis <[email protected]>
  • Loading branch information
Didainius committed Jan 25, 2024
1 parent d08ff0d commit 2df30b0
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 67 deletions.
15 changes: 12 additions & 3 deletions CODING_GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,10 @@ When the tenant context is not needed (system administration calls), we just pas

## Generic CRUD functions for OpenAPI entity implementation

Generic CRUD functions are used to minimize boilerplate for entity implementation in the SDK.
Generic CRUD functions are used to minimize boilerplate for entity implementation in the SDK. They
might not always be the way to go when there are very specific operation needs as it is not worth
having a generic function for single use case. In such cases, low level API client function set,
that is located in `openapi.go` can help to perform such operations.

### Terminology

Expand Down Expand Up @@ -551,11 +554,13 @@ The entities that match below criteria are usually going to use `inner` crud fun
* Read only entities (e.g. NSX-T Segment Profiles `VCDClient.GetAllIpDiscoveryProfiles`)

Inner types are more simple as they can be directly used without any additional overhead. There are
5 functions that can be used:
7 functions that can be used:

* `createInnerEntity`
* `updateInnerEntity`
* `updateInnerEntityWithHeaders`
* `getInnerEntity`
* `getInnerEntityWithHeaders`
* `deleteEntityById`
* `getAllInnerEntities`

Expand All @@ -579,10 +584,11 @@ func (o OuterEntity) wrap(inner *InnerEntity) *OuterEntity {
return &o
}
```
There are 4 functions for handling CRU(D).
There are 5 functions for handling CRU(D).
* `createOuterEntity`
* `updateOuterEntity`
* `getOuterEntity`
* `getOuterEntityWithHeaders`
* `getAllOuterEntities`

*Note*: `D` (deletion) in `CRUD` is a simple operation that does not additionally handle data and
Expand All @@ -594,6 +600,9 @@ Existing examples of the implementation are:
* `DistributedFirewall`
* `DistributedFirewallRule`
* `NsxtSegmentProfileTemplate`
* `DefinedEntityType`
* `DefinedInterface`
* `DefinedEntity`

## Testing

Expand Down
36 changes: 22 additions & 14 deletions govcd/defined_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (
"github.com/vmware/go-vcloud-director/v2/types/v56"
)

const (
labelDefinedEntity = "Defined Entity"
labelDefinedEntityType = "Defined Entity Type"
labelRdeBehavior = "RDE Behavior"
labelRdeBehaviorOverride = "RDE Behavior Override"
labelRdeBehaviorAccessControls = "RDE Behavior Access Controls"
)

// DefinedEntityType is a type for handling Runtime Defined Entity (RDE) Type definitions.
// Note. Running a few of these operations in parallel may corrupt database in VCD (at least <= 10.4.2)
type DefinedEntityType struct {
Expand Down Expand Up @@ -48,7 +56,7 @@ func (d DefinedEntity) wrap(inner *types.DefinedEntity) *DefinedEntity {
func (vcdClient *VCDClient) CreateRdeType(rde *types.DefinedEntityType) (*DefinedEntityType, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntityTypes,
entityLabel: "RDE Type",
entityLabel: labelDefinedEntityType,
}
outerType := DefinedEntityType{client: &vcdClient.Client}
return createOuterEntity(&vcdClient.Client, outerType, c, rde)
Expand All @@ -58,7 +66,7 @@ func (vcdClient *VCDClient) CreateRdeType(rde *types.DefinedEntityType) (*Define
func (vcdClient *VCDClient) GetAllRdeTypes(queryParameters url.Values) ([]*DefinedEntityType, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntityTypes,
entityLabel: "RDE Type",
entityLabel: labelDefinedEntityType,
queryParameters: queryParameters,
}

Expand Down Expand Up @@ -89,7 +97,7 @@ func (vcdClient *VCDClient) GetRdeType(vendor, nss, version string) (*DefinedEnt
// GetRdeTypeById gets a Runtime Defined Entity Type by its ID.
func (vcdClient *VCDClient) GetRdeTypeById(id string) (*DefinedEntityType, error) {
c := crudConfig{
entityLabel: "RDE Type",
entityLabel: labelDefinedEntityType,
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntityTypes,
endpointParams: []string{id},
}
Expand Down Expand Up @@ -124,7 +132,7 @@ func (rdeType *DefinedEntityType) Update(rdeTypeToUpdate types.DefinedEntityType
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntityTypes,
endpointParams: []string{rdeType.DefinedEntityType.ID},
entityLabel: "Runtime Defined Entity Type",
entityLabel: labelDefinedEntityType,
}

resultDefinedEntityType, err := updateInnerEntity(rdeType.client, c, &rdeTypeToUpdate)
Expand All @@ -144,7 +152,7 @@ func (rdeType *DefinedEntityType) Delete() error {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntityTypes,
endpointParams: []string{rdeType.DefinedEntityType.ID},
entityLabel: "RDE Type",
entityLabel: labelDefinedEntityType,
}

if err := deleteEntityById(rdeType.client, c); err != nil {
Expand All @@ -169,7 +177,7 @@ func (rdeType *DefinedEntityType) GetBehaviorById(id string) (*types.Behavior, e
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeTypeBehaviors,
endpointParams: []string{rdeType.DefinedEntityType.ID, id},
entityLabel: "RDE Behavior",
entityLabel: labelRdeBehavior,
}
return getInnerEntity[types.Behavior](rdeType.client, c)
}
Expand Down Expand Up @@ -198,7 +206,7 @@ func (rdeType *DefinedEntityType) UpdateBehaviorOverride(behavior types.Behavior
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeTypeBehaviors,
endpointParams: []string{rdeType.DefinedEntityType.ID, behavior.ID},
entityLabel: "Behavior override",
entityLabel: labelRdeBehaviorOverride,
}
return updateInnerEntity(rdeType.client, c, &behavior)
}
Expand All @@ -213,7 +221,7 @@ func (rdeType *DefinedEntityType) DeleteBehaviorOverride(behaviorId string) erro
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeTypeBehaviors,
endpointParams: []string{rdeType.DefinedEntityType.ID, behaviorId},
entityLabel: "Behavior override",
entityLabel: labelRdeBehaviorOverride,
}
return deleteEntityById(rdeType.client, c)
}
Expand Down Expand Up @@ -242,7 +250,7 @@ func (det *DefinedEntityType) SetBehaviorAccessControls(acls []*types.BehaviorAc
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeTypeBehaviorAccessControls,
endpointParams: []string{det.DefinedEntityType.ID},
entityLabel: "RDE Behavior Access Controls",
entityLabel: labelRdeBehaviorAccessControls,
}
_, err = updateInnerEntity(det.client, c, &payload)
if err != nil {
Expand All @@ -259,7 +267,7 @@ func (det *DefinedEntityType) GetAllBehaviorsAccessControls(queryParameters url.
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeTypeBehaviorAccessControls,
queryParameters: queryParameters,
endpointParams: []string{det.DefinedEntityType.ID},
entityLabel: "Behavior Access Controls",
entityLabel: labelRdeBehaviorAccessControls,
}
return getAllInnerEntities[types.BehaviorAccess](det.client, c)
}
Expand All @@ -279,7 +287,7 @@ func (rdeType *DefinedEntityType) GetAllRdes(queryParameters url.Values) ([]*Def
func getAllRdes(client *Client, vendor, nss, version string, queryParameters url.Values) ([]*DefinedEntity, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntitiesTypes,
entityLabel: "RDE",
entityLabel: labelDefinedEntityType,
queryParameters: queryParameters,
endpointParams: []string{vendor, "/", nss, "/", version},
}
Expand Down Expand Up @@ -333,7 +341,7 @@ func (vcdClient *VCDClient) GetRdeById(id string) (*DefinedEntity, error) {
// Getting a RDE by ID populates the ETag field in the returned object.
func getRdeById(client *Client, id string) (*DefinedEntity, error) {
c := crudConfig{
entityLabel: "RDE",
entityLabel: labelDefinedEntityType,
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntities,
endpointParams: []string{id},
}
Expand Down Expand Up @@ -481,7 +489,7 @@ func (rde *DefinedEntity) Update(rdeToUpdate types.DefinedEntity) error {
}

c := crudConfig{
entityLabel: "RDE",
entityLabel: labelDefinedEntity,
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntities,
endpointParams: []string{rde.DefinedEntity.ID},
additionalHeader: map[string]string{"If-Match": rde.Etag},
Expand All @@ -504,7 +512,7 @@ func (rde *DefinedEntity) Delete() error {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntities,
endpointParams: []string{rde.DefinedEntity.ID},
entityLabel: "RDE",
entityLabel: labelDefinedEntity,
}

if err := deleteEntityById(rde.client, c); err != nil {
Expand Down
25 changes: 15 additions & 10 deletions govcd/defined_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"github.com/vmware/go-vcloud-director/v2/types/v56"
)

const (
labelDefinedInterface = "Defined Interface"
labelDefinedInterfaceBehavior = "Defined Interface Behavior"
)

// DefinedInterface is a type for handling Defined Interfaces, from the Runtime Defined Entities framework, in VCD.
// This is often referred as Runtime Defined Entity Interface or RDE Interface in documentation.
type DefinedInterface struct {
Expand All @@ -32,7 +37,7 @@ func (d DefinedInterface) wrap(inner *types.DefinedInterface) *DefinedInterface
func (vcdClient *VCDClient) CreateDefinedInterface(definedInterface *types.DefinedInterface) (*DefinedInterface, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaces,
entityLabel: "Defined Interface",
entityLabel: labelDefinedInterface,
}
outerType := DefinedInterface{client: &vcdClient.Client}
return createOuterEntity(&vcdClient.Client, outerType, c, definedInterface)
Expand All @@ -42,7 +47,7 @@ func (vcdClient *VCDClient) CreateDefinedInterface(definedInterface *types.Defin
func (vcdClient *VCDClient) GetAllDefinedInterfaces(queryParameters url.Values) ([]*DefinedInterface, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaces,
entityLabel: "Defined Interface",
entityLabel: labelDefinedInterface,
queryParameters: queryParameters,
}

Expand Down Expand Up @@ -73,7 +78,7 @@ func (vcdClient *VCDClient) GetDefinedInterface(vendor, nss, version string) (*D
// GetDefinedInterfaceById gets a Defined Interface identified by its unique URN.
func (vcdClient *VCDClient) GetDefinedInterfaceById(id string) (*DefinedInterface, error) {
c := crudConfig{
entityLabel: "Define Interface",
entityLabel: labelDefinedInterface,
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaces,
endpointParams: []string{id},
}
Expand Down Expand Up @@ -101,7 +106,7 @@ func (di *DefinedInterface) Update(definedInterface types.DefinedInterface) erro
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaces,
endpointParams: []string{di.DefinedInterface.ID},
entityLabel: "Defined Interface",
entityLabel: labelDefinedInterface,
}
resultDefinedInterface, err := updateInnerEntity(di.client, c, &definedInterface)
if err != nil {
Expand All @@ -123,7 +128,7 @@ func (di *DefinedInterface) Delete() error {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaces,
endpointParams: []string{di.DefinedInterface.ID},
entityLabel: "Defined Interface",
entityLabel: labelDefinedInterface,
}

err := deleteEntityById(di.client, c)
Expand All @@ -145,7 +150,7 @@ func (di *DefinedInterface) AddBehavior(behavior types.Behavior) (*types.Behavio
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaceBehaviors,
endpointParams: []string{di.DefinedInterface.ID},
entityLabel: "Defined Interface Behavior",
entityLabel: labelDefinedInterfaceBehavior,
}
return createInnerEntity(di.client, c, &behavior)
}
Expand All @@ -162,7 +167,7 @@ func (di *DefinedInterface) GetAllBehaviors(queryParameters url.Values) ([]*type
func getAllBehaviors(client *Client, objectId, openApiEndpoint string, queryParameters url.Values) ([]*types.Behavior, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + openApiEndpoint,
entityLabel: "Defined Entity Behavior",
entityLabel: labelDefinedInterfaceBehavior,
endpointParams: []string{objectId},
queryParameters: queryParameters,
}
Expand All @@ -175,7 +180,7 @@ func (di *DefinedInterface) GetBehaviorById(id string) (*types.Behavior, error)
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaceBehaviors,
endpointParams: []string{di.DefinedInterface.ID, id},
entityLabel: "Defined Interface Behavior",
entityLabel: labelDefinedInterfaceBehavior,
}
return getInnerEntity[types.Behavior](di.client, c)
}
Expand Down Expand Up @@ -203,7 +208,7 @@ func (di *DefinedInterface) UpdateBehavior(behavior types.Behavior) (*types.Beha
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaceBehaviors,
endpointParams: []string{di.DefinedInterface.ID, behavior.ID},
entityLabel: "Defined Interface Behavior",
entityLabel: labelDefinedInterfaceBehavior,
}
return updateInnerEntity(di.client, c, &behavior)
}
Expand All @@ -217,7 +222,7 @@ func (di *DefinedInterface) DeleteBehavior(behaviorId string) error {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeInterfaceBehaviors,
endpointParams: []string{di.DefinedInterface.ID, behaviorId},
entityLabel: "Defined Interface Behavior",
entityLabel: labelDefinedInterfaceBehavior,
}
return deleteEntityById(di.client, c)
}
Expand Down
12 changes: 7 additions & 5 deletions govcd/ip_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/vmware/go-vcloud-director/v2/types/v56"
)

const labelIpSpace = "IP Space"

// IpSpace provides structured approach to allocating public and private IP addresses by preventing
// the use of overlapping IP addresses across organizations and organization VDCs.
//
Expand Down Expand Up @@ -39,7 +41,7 @@ func (g IpSpace) wrap(inner *types.IpSpace) *IpSpace {
func (vcdClient *VCDClient) CreateIpSpace(ipSpaceConfig *types.IpSpace) (*IpSpace, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaces,
entityLabel: "IP Space",
entityLabel: labelIpSpace,
}
outerType := IpSpace{vcdClient: vcdClient}
return createOuterEntity(&vcdClient.Client, outerType, c, ipSpaceConfig)
Expand All @@ -52,7 +54,7 @@ func (vcdClient *VCDClient) CreateIpSpace(ipSpaceConfig *types.IpSpace) (*IpSpac
func (vcdClient *VCDClient) GetAllIpSpaceSummaries(queryParameters url.Values) ([]*IpSpace, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceSummaries,
entityLabel: "IP Space",
entityLabel: labelIpSpace,
queryParameters: queryParameters,
}

Expand Down Expand Up @@ -85,7 +87,7 @@ func (vcdClient *VCDClient) GetIpSpaceByName(name string) (*IpSpace, error) {

func (vcdClient *VCDClient) GetIpSpaceById(id string) (*IpSpace, error) {
c := crudConfig{
entityLabel: "IP Space",
entityLabel: labelIpSpace,
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaces,
endpointParams: []string{id},
}
Expand Down Expand Up @@ -123,7 +125,7 @@ func (ipSpace *IpSpace) Update(ipSpaceConfig *types.IpSpace) (*IpSpace, error) {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaces,
endpointParams: []string{ipSpace.IpSpace.ID},
entityLabel: "IP Space",
entityLabel: labelIpSpace,
}
outerType := IpSpace{vcdClient: ipSpace.vcdClient}
return updateOuterEntity(&ipSpace.vcdClient.Client, outerType, c, ipSpaceConfig)
Expand All @@ -134,7 +136,7 @@ func (ipSpace *IpSpace) Delete() error {
c := crudConfig{
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaces,
endpointParams: []string{ipSpace.IpSpace.ID},
entityLabel: "IP Space",
entityLabel: labelIpSpace,
}
return deleteEntityById(&ipSpace.vcdClient.Client, c)
}
Loading

0 comments on commit 2df30b0

Please sign in to comment.