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

Support dhcp relay resource on Global Manager #883

Merged
merged 1 commit into from
May 9, 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
77 changes: 63 additions & 14 deletions nsxt/resource_nsxt_policy_dhcp_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
gm_infra "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/global_infra"
gm_model "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)
Expand Down Expand Up @@ -43,9 +45,15 @@ func resourceNsxtPolicyDhcpRelayConfig() *schema.Resource {
}

func resourceNsxtPolicyDhcpRelayConfigExists(id string, connector client.Connector, isGlobalManager bool) (bool, error) {
client := infra.NewDhcpRelayConfigsClient(connector)
var err error
if isGlobalManager {
client := gm_infra.NewDhcpRelayConfigsClient(connector)
_, err = client.Get(id)
} else {
client := infra.NewDhcpRelayConfigsClient(connector)
_, err = client.Get(id)
}

_, err := client.Get(id)
if err == nil {
return true, nil
}
Expand All @@ -59,11 +67,6 @@ func resourceNsxtPolicyDhcpRelayConfigExists(id string, connector client.Connect

func resourceNsxtPolicyDhcpRelayConfigCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := infra.NewDhcpRelayConfigsClient(connector)

if client == nil {
return policyResourceNotSupportedError()
}

// Initialize resource Id and verify this ID is not yet used
id, err := getOrGenerateID(d, m, resourceNsxtPolicyDhcpRelayConfigExists)
Expand All @@ -85,7 +88,18 @@ func resourceNsxtPolicyDhcpRelayConfigCreate(d *schema.ResourceData, m interface

// Create the resource using PATCH
log.Printf("[INFO] Creating DhcpRelayConfig with ID %s", id)
err = client.Patch(id, obj)
if isPolicyGlobalManager(m) {
gmObj, err1 := convertModelBindingType(obj, model.DhcpRelayConfigBindingType(), gm_model.DhcpRelayConfigBindingType())
if err1 != nil {
return err1
}

client := gm_infra.NewDhcpRelayConfigsClient(connector)
err = client.Patch(id, gmObj.(gm_model.DhcpRelayConfig))
} else {
client := infra.NewDhcpRelayConfigsClient(connector)
err = client.Patch(id, obj)
}
if err != nil {
return handleCreateError("DhcpRelayConfig", id, err)
}
Expand All @@ -108,10 +122,25 @@ func resourceNsxtPolicyDhcpRelayConfigRead(d *schema.ResourceData, m interface{}
if id == "" {
return fmt.Errorf("Error obtaining DhcpRelayConfig ID")
}

obj, err := client.Get(id)
if err != nil {
return handleReadError(d, "DhcpRelayConfig", id, err)
var obj model.DhcpRelayConfig
if isPolicyGlobalManager(m) {
client := gm_infra.NewDhcpRelayConfigsClient(connector)
gmObj, err1 := client.Get(id)
if err1 != nil {
return handleReadError(d, "DhcpRelayConfig", id, err1)
}
rawObj, err := convertModelBindingType(gmObj, gm_model.DhcpRelayConfigBindingType(), model.DhcpRelayConfigBindingType())
if err != nil {
return err
}
obj = rawObj.(model.DhcpRelayConfig)
} else {
var err error
client := infra.NewDhcpRelayConfigsClient(connector)
obj, err = client.Get(id)
if err != nil {
return handleReadError(d, "DhcpRelayConfig", id, err)
}
}

d.Set("display_name", obj.DisplayName)
Expand Down Expand Up @@ -154,7 +183,19 @@ func resourceNsxtPolicyDhcpRelayConfigUpdate(d *schema.ResourceData, m interface
Revision: &revision,
}

_, err := client.Update(id, obj)
var err error
if isPolicyGlobalManager(m) {
gmObj, err1 := convertModelBindingType(obj, model.DhcpRelayConfigBindingType(), gm_model.DhcpRelayConfigBindingType())
if err1 != nil {
return err1
}

client := gm_infra.NewDhcpRelayConfigsClient(connector)
_, err = client.Update(id, gmObj.(gm_model.DhcpRelayConfig))
} else {
client := infra.NewDhcpRelayConfigsClient(connector)
_, err = client.Update(id, obj)
}
if err != nil {
return handleUpdateError("DhcpRelayConfig", id, err)
}
Expand All @@ -174,7 +215,15 @@ func resourceNsxtPolicyDhcpRelayConfigDelete(d *schema.ResourceData, m interface
return policyResourceNotSupportedError()
}

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

if err != nil {
return handleDeleteError("DhcpRelayConfig", id, err)
}
Expand Down
20 changes: 12 additions & 8 deletions nsxt/resource_nsxt_policy_dhcp_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
)

var accTestPolicyDhcpRelayConfigCreateAttributes = map[string]string{
Expand All @@ -29,7 +28,7 @@ func TestAccResourceNsxtPolicyDhcpRelayConfig_basic(t *testing.T) {
testResourceName := "nsxt_policy_dhcp_relay.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccOnlyLocalManager(t); testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyDhcpRelayConfigCheckDestroy(state, accTestPolicyDhcpRelayConfigUpdateAttributes["display_name"])
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestAccResourceNsxtPolicyDhcpRelayConfig_importBasic(t *testing.T) {
testResourceName := "nsxt_policy_dhcp_relay.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccOnlyLocalManager(t); testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyDhcpRelayConfigCheckDestroy(state, name)
Expand All @@ -105,7 +104,6 @@ func testAccNsxtPolicyDhcpRelayConfigExists(resourceName string) resource.TestCh
return func(state *terraform.State) error {

connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
nsxClient := infra.NewDhcpRelayConfigsClient(connector)

rs, ok := state.RootModule().Resources[resourceName]
if !ok {
Expand All @@ -117,9 +115,12 @@ func testAccNsxtPolicyDhcpRelayConfigExists(resourceName string) resource.TestCh
return fmt.Errorf("Policy DhcpRelayConfig resource ID not set in resources")
}

_, err := nsxClient.Get(resourceID)
exists, err := resourceNsxtPolicyDhcpRelayConfigExists(resourceID, connector, testAccIsGlobalManager())
if err != nil {
return fmt.Errorf("Error while retrieving policy DhcpRelayConfig ID %s. Error: %v", resourceID, err)
return err
}
if !exists {
return fmt.Errorf("Error while retrieving policy DhcpServer ID %s. Error: %v", resourceID, err)
}

return nil
Expand All @@ -128,16 +129,19 @@ func testAccNsxtPolicyDhcpRelayConfigExists(resourceName string) resource.TestCh

func testAccNsxtPolicyDhcpRelayConfigCheckDestroy(state *terraform.State, displayName string) error {
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
nsxClient := infra.NewDhcpRelayConfigsClient(connector)
for _, rs := range state.RootModule().Resources {

if rs.Type != "nsxt_policy_dhcp_relay" {
continue
}

resourceID := rs.Primary.Attributes["id"]
_, err := nsxClient.Get(resourceID)
exists, err := resourceNsxtPolicyDhcpRelayConfigExists(resourceID, connector, testAccIsGlobalManager())
if err == nil {
return err
}

if exists {
return fmt.Errorf("Policy DhcpRelayConfig %s still exists", displayName)
}
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/policy_dhcp_relay.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: A resource to configure a Dhcp Relay.

This resource provides a method for the management of a Dhcp Relay.

This resource is applicable to NSX Policy Manager.
This resource is applicable to NSX Policy Manager and NSX Global Manager.

## Example Usage

Expand Down