Skip to content

Commit

Permalink
Support multi-tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
GraysonWu committed Nov 15, 2023
1 parent 850ffbb commit 38fe3a3
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 94 deletions.
27 changes: 27 additions & 0 deletions api/api_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,30 @@
supported_method:
- New
- List
- api_packages:
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra
model: github.com/vmware/vsphere-automation-sdk-go/runtime/data
list_result_model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model
type: Local
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/global_infra
model: github.com/vmware/vsphere-automation-sdk-go/runtime/data
list_result_model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/model
type: Global
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra
model: github.com/vmware/vsphere-automation-sdk-go/runtime/data
list_result_model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model
type: Multitenancy
model_name: StructValue
obj_name: FloodProtectionProfiles
client_name: FloodProtectionProfilesClient
list_result_name: FloodProtectionProfileListResult
model_prefix: vapiData_
model_pass_ptr: true
file_name: FloodProtectionProfilesClient
supported_method:
- New
- Get
- Delete
- Patch
- Update
- List
174 changes: 174 additions & 0 deletions api/infra/flood_protection_profiles_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//nolint:revive
package infra

// The following file has been autogenerated. Please avoid any changes!
import (
"errors"

model0 "github.com/vmware/vsphere-automation-sdk-go/runtime/data"
vapiProtocolClient_ "github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
client1 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/global_infra"
lrmodel1 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/model"
client0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
lrmodel0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
client2 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra"

utl "github.com/vmware/terraform-provider-nsxt/api/utl"
)

type StructValueClientContext utl.ClientContext

func NewFloodProtectionProfilesClient(sessionContext utl.SessionContext, connector vapiProtocolClient_.Connector) *StructValueClientContext {
var client interface{}

switch sessionContext.ClientType {

case utl.Local:
client = client0.NewFloodProtectionProfilesClient(connector)

case utl.Global:
client = client1.NewFloodProtectionProfilesClient(connector)

case utl.Multitenancy:
client = client2.NewFloodProtectionProfilesClient(connector)

default:
return nil
}
return &StructValueClientContext{Client: client, ClientType: sessionContext.ClientType, ProjectID: sessionContext.ProjectID}
}

func (c StructValueClientContext) Get(floodProtectionProfileIdParam string) (*model0.StructValue, error) {
var obj *model0.StructValue
var err error

switch c.ClientType {

case utl.Local:
client := c.Client.(client0.FloodProtectionProfilesClient)
obj, err = client.Get(floodProtectionProfileIdParam)
if err != nil {
return obj, err
}

case utl.Global:
client := c.Client.(client1.FloodProtectionProfilesClient)
obj, err = client.Get(floodProtectionProfileIdParam)
if err != nil {
return obj, err
}

case utl.Multitenancy:
client := c.Client.(client2.FloodProtectionProfilesClient)
obj, err = client.Get(utl.DefaultOrgID, c.ProjectID, floodProtectionProfileIdParam)
if err != nil {
return obj, err
}

default:
return obj, errors.New("invalid infrastructure for model")
}
return obj, err
}

func (c StructValueClientContext) Delete(floodProtectionProfileIdParam string, overrideParam *bool) error {
var err error

switch c.ClientType {

case utl.Local:
client := c.Client.(client0.FloodProtectionProfilesClient)
err = client.Delete(floodProtectionProfileIdParam, overrideParam)

case utl.Global:
client := c.Client.(client1.FloodProtectionProfilesClient)
err = client.Delete(floodProtectionProfileIdParam, overrideParam)

case utl.Multitenancy:
client := c.Client.(client2.FloodProtectionProfilesClient)
err = client.Delete(utl.DefaultOrgID, c.ProjectID, floodProtectionProfileIdParam, overrideParam)

default:
err = errors.New("invalid infrastructure for model")
}
return err
}

func (c StructValueClientContext) Patch(floodProtectionProfileIdParam string, floodProtectionProfileParam *model0.StructValue, overrideParam *bool) error {
var err error

switch c.ClientType {

case utl.Local:
client := c.Client.(client0.FloodProtectionProfilesClient)
err = client.Patch(floodProtectionProfileIdParam, floodProtectionProfileParam, overrideParam)

case utl.Global:
client := c.Client.(client1.FloodProtectionProfilesClient)
err = client.Patch(floodProtectionProfileIdParam, floodProtectionProfileParam, overrideParam)

case utl.Multitenancy:
client := c.Client.(client2.FloodProtectionProfilesClient)
err = client.Patch(utl.DefaultOrgID, c.ProjectID, floodProtectionProfileIdParam, floodProtectionProfileParam, overrideParam)

default:
err = errors.New("invalid infrastructure for model")
}
return err
}

func (c StructValueClientContext) Update(floodProtectionProfileIdParam string, floodProtectionProfileParam *model0.StructValue, overrideParam *bool) (*model0.StructValue, error) {
var err error
var obj *model0.StructValue

switch c.ClientType {

case utl.Local:
client := c.Client.(client0.FloodProtectionProfilesClient)
obj, err = client.Update(floodProtectionProfileIdParam, floodProtectionProfileParam, overrideParam)

case utl.Global:
client := c.Client.(client1.FloodProtectionProfilesClient)
obj, err = client.Update(floodProtectionProfileIdParam, floodProtectionProfileParam, overrideParam)

case utl.Multitenancy:
client := c.Client.(client2.FloodProtectionProfilesClient)
obj, err = client.Update(utl.DefaultOrgID, c.ProjectID, floodProtectionProfileIdParam, floodProtectionProfileParam, overrideParam)

default:
err = errors.New("invalid infrastructure for model")
}
return obj, err
}

func (c StructValueClientContext) List(cursorParam *string, includeMarkForDeleteObjectsParam *bool, includedFieldsParam *string, pageSizeParam *int64, sortAscendingParam *bool, sortByParam *string) (lrmodel0.FloodProtectionProfileListResult, error) {
var err error
var obj lrmodel0.FloodProtectionProfileListResult

switch c.ClientType {

case utl.Local:
client := c.Client.(client0.FloodProtectionProfilesClient)
obj, err = client.List(cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, pageSizeParam, sortAscendingParam, sortByParam)

case utl.Global:
client := c.Client.(client1.FloodProtectionProfilesClient)
gmObj, err := client.List(cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, pageSizeParam, sortAscendingParam, sortByParam)
if err != nil {
return obj, err
}
obj1, err1 := utl.ConvertModelBindingType(gmObj, lrmodel1.FloodProtectionProfileListResultBindingType(), lrmodel0.FloodProtectionProfileListResultBindingType())
if err1 != nil {
return obj, err1
}
obj = obj1.(lrmodel0.FloodProtectionProfileListResult)

case utl.Multitenancy:
client := c.Client.(client2.FloodProtectionProfilesClient)
obj, err = client.List(utl.DefaultOrgID, c.ProjectID, cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, pageSizeParam, sortAscendingParam, sortByParam)

default:
err = errors.New("invalid infrastructure for model")
}
return obj, err
}
53 changes: 15 additions & 38 deletions nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
"github.com/vmware/vsphere-automation-sdk-go/runtime/data"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
gm_infra "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/global_infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"

"github.com/vmware/terraform-provider-nsxt/api/infra"
utl "github.com/vmware/terraform-provider-nsxt/api/utl"
)

func resourceNsxtPolicyDistributedFloodProtectionProfile() *schema.Resource {
Expand All @@ -38,6 +39,7 @@ func getFloodProtectionProfile() map[string]*schema.Schema {
"description": getDescriptionSchema(),
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"context": getContextSchema(),
"icmp_active_flow_limit": {
Type: schema.TypeInt,
Description: "Active ICMP connections limit",
Expand Down Expand Up @@ -82,15 +84,10 @@ func getDistributedFloodProtectionProfile() map[string]*schema.Schema {
return baseProfile
}

func resourceNsxtPolicyFloodProtectionProfileExists(id string, connector client.Connector, isGlobalManager bool) (bool, error) {
var err error
if isGlobalManager {
client := gm_infra.NewFloodProtectionProfilesClient(connector)
_, err = client.Get(id)
} else {
client := infra.NewFloodProtectionProfilesClient(connector)
_, err = client.Get(id)
}
func resourceNsxtPolicyFloodProtectionProfileExists(sessionContext utl.SessionContext, id string, connector client.Connector) (bool, error) {

client := infra.NewFloodProtectionProfilesClient(sessionContext, connector)
_, err := client.Get(id)
if err == nil {
return true, nil
}
Expand Down Expand Up @@ -144,18 +141,14 @@ func resourceNsxtPolicyDistributedFloodProtectionProfilePatch(d *schema.Resource
profileStruct := profileValue.(*data.StructValue)

log.Printf("[INFO] Patching DistributedFloodProtectionProfile with ID %s", id)
if isPolicyGlobalManager(m) {
client := gm_infra.NewFloodProtectionProfilesClient(connector)
return client.Patch(id, profileStruct, nil)
}
client := infra.NewFloodProtectionProfilesClient(connector)
client := infra.NewFloodProtectionProfilesClient(getSessionContext(d, m), connector)
return client.Patch(id, profileStruct, nil)
}

func resourceNsxtPolicyDistributedFloodProtectionProfileCreate(d *schema.ResourceData, m interface{}) error {

// Initialize resource Id and verify this ID is not yet used
id, err := getOrGenerateID(d, m, resourceNsxtPolicyFloodProtectionProfileExists)
id, err := getOrGenerateID2(d, m, resourceNsxtPolicyFloodProtectionProfileExists)
if err != nil {
return err
}
Expand All @@ -180,21 +173,13 @@ func resourceNsxtPolicyDistributedFloodProtectionProfileRead(d *schema.ResourceD
return fmt.Errorf("Error obtaining FloodProtectionProfile ID")
}

var profileStruct *data.StructValue
var err error
if isPolicyGlobalManager(m) {
client := gm_infra.NewFloodProtectionProfilesClient(connector)
profileStruct, err = client.Get(id)
} else {
client := infra.NewFloodProtectionProfilesClient(connector)
profileStruct, err = client.Get(id)
}

client := infra.NewFloodProtectionProfilesClient(getSessionContext(d, m), connector)
dpffData, err := client.Get(id)
if err != nil {
return handleReadError(d, "FloodProtectionProfile", id, err)
}

dfppInterface, errs := converter.ConvertToGolang(profileStruct, model.DistributedFloodProtectionProfileBindingType())
dfppInterface, errs := converter.ConvertToGolang(dpffData, model.DistributedFloodProtectionProfileBindingType())
if errs != nil {
return errs[0]
}
Expand Down Expand Up @@ -239,18 +224,10 @@ func resourceNsxtPolicyFloodProtectionProfileDelete(d *schema.ResourceData, m in
}

connector := getPolicyConnector(m)
var err error
if isPolicyGlobalManager(m) {
client := gm_infra.NewFloodProtectionProfilesClient(connector)
err = client.Delete(id, nil)
} else {
client := infra.NewFloodProtectionProfilesClient(connector)
err = client.Delete(id, nil)
}

client := infra.NewFloodProtectionProfilesClient(getSessionContext(d, m), connector)
err := client.Delete(id, nil)
if err != nil {
return handleDeleteError("FloodProtectionProfile", id, err)
}

return nil
}
Loading

0 comments on commit 38fe3a3

Please sign in to comment.