Skip to content

Commit

Permalink
Manual changes post-generation
Browse files Browse the repository at this point in the history
Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jul 3, 2024
1 parent 19e91c8 commit 441ff5e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 147 deletions.
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ func Provider() *schema.Provider {
"nsxt_vpc_gateway_policy": resourceNsxtVPCGatewayPolicy(),
"nsxt_policy_share": resourceNsxtPolicyShare(),
"nsxt_policy_shared_resource": resourceNsxtPolicySharedResource(),
"nsxt_vpc_subnet": resourceNsxtVpcSubnet(),
},

ConfigureFunc: providerConfigure,
Expand Down
9 changes: 5 additions & 4 deletions nsxt/resource_nsxt_vpc_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var vpcSubnetSchema = map[string]*metadata.ExtendedSchema{
"description": metadata.GetExtendedSchema(getDescriptionSchema()),
"revision": metadata.GetExtendedSchema(getRevisionSchema()),
"tag": metadata.GetExtendedSchema(getTagsSchema()),
"context": metadata.GetExtendedSchema(getContextSchema(false, false, true)),
"context": metadata.GetExtendedSchema(getContextSchema(true, false, true)),
"advanced_config": {
Schema: schema.Schema{
Type: schema.TypeList,
Expand Down Expand Up @@ -80,6 +80,7 @@ var vpcSubnetSchema = map[string]*metadata.ExtendedSchema{
Metadata: metadata.Metadata{
SchemaType: "int",
SdkFieldName: "Ipv4SubnetSize",
OmitIfEmpty: true,
},
},
"ip_addresses": {
Expand Down Expand Up @@ -204,13 +205,13 @@ func resourceNsxtVpcSubnet() *schema.Resource {
Update: resourceNsxtVpcSubnetUpdate,
Delete: resourceNsxtVpcSubnetDelete,
Importer: &schema.ResourceImporter{
State: nsxtParentPathResourceImporter,
State: nsxtVPCPathResourceImporter,
},
Schema: metadata.GetSchemaFromExtendedSchema(vpcSubnetSchema),
}
}

func resourceNsxtVpcSubnetExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) {
func resourceNsxtVpcSubnetExists(sessionContext utl.SessionContext, id string, connector client.Connector) (bool, error) {
var err error
parents := getVpcParentsFromContext(sessionContext)
client := clientLayer.NewSubnetsClient(connector)
Expand All @@ -229,7 +230,7 @@ func resourceNsxtVpcSubnetExists(sessionContext utl.SessionContext, parentPath s
func resourceNsxtVpcSubnetCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id, err := getOrGenerateIDWithParent(d, m, resourceNsxtVpcSubnetExists)
id, err := getOrGenerateID2(d, m, resourceNsxtVpcSubnetExists)
if err != nil {
return err
}
Expand Down
162 changes: 59 additions & 103 deletions nsxt/resource_nsxt_vpc_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,44 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

var accTestPolicyVpcSubnetCreateAttributes = map[string]string{
"display_name": getAccTestResourceName(),
"description": "terraform created",
"ipv4_subnet_size": "2",
"ip_addresses": "test-create",
"access_mode": "Private",
"enabled": "true",
"dhcp_relay_config_path": "test-create",
"enable_dhcp": "true",
"dns_server_ips": "test-create",
"ipv4_pool_size": "2",
var accTestVpcSubnetCreateAttributes = map[string]string{
"display_name": getAccTestResourceName(),
"description": "terraform created",
"ip_addresses": "192.168.240.10-192.168.240.14",
"access_mode": "Private",
"enabled": "true",
"enable_dhcp": "true",
}

var accTestPolicyVpcSubnetUpdateAttributes = map[string]string{
"display_name": getAccTestResourceName(),
"description": "terraform updated",
"ipv4_subnet_size": "5",
"ip_addresses": "test-update",
"access_mode": "Public",
"enabled": "false",
"dhcp_relay_config_path": "test-update",
"enable_dhcp": "false",
"dns_server_ips": "test-update",
"ipv4_pool_size": "5",
var accTestVpcSubnetUpdateAttributes = map[string]string{
"display_name": getAccTestResourceName(),
"description": "terraform updated",
"ip_addresses": "192.168.240.10-192.168.240.17",
"access_mode": "Public",
"enabled": "false",
"enable_dhcp": "false",
}

func TestAccResourceNsxtPolicyVpcSubnet_basic(t *testing.T) {
testResourceName := "nsxt_policy_subnet.test"
func TestAccResourceNsxtVpcSubnet_basic(t *testing.T) {
testResourceName := "nsxt_vpc_subnet.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyVpcSubnetCheckDestroy(state, accTestPolicyVpcSubnetUpdateAttributes["display_name"])
return testAccNsxtVpcSubnetCheckDestroy(state, accTestVpcSubnetUpdateAttributes["display_name"])
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyVpcSubnetTemplate(true),
Config: testAccNsxtVpcSubnetTemplate(true),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyVpcSubnetExists(accTestPolicyVpcSubnetCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyVpcSubnetCreateAttributes["display_name"]),
resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyVpcSubnetCreateAttributes["description"]),
resource.TestCheckResourceAttr(testResourceName, "advanced_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "ipv4_subnet_size", accTestPolicyVpcSubnetCreateAttributes["ipv4_subnet_size"]),
resource.TestCheckResourceAttr(testResourceName, "ip_addresses.0", accTestPolicyVpcSubnetCreateAttributes["ip_addresses"]),
resource.TestCheckResourceAttr(testResourceName, "access_mode", accTestPolicyVpcSubnetCreateAttributes["access_mode"]),
testAccNsxtVpcSubnetExists(accTestVpcSubnetCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestVpcSubnetCreateAttributes["display_name"]),
resource.TestCheckResourceAttr(testResourceName, "description", accTestVpcSubnetCreateAttributes["description"]),
resource.TestCheckResourceAttr(testResourceName, "ip_addresses.0", accTestVpcSubnetCreateAttributes["ip_addresses"]),
resource.TestCheckResourceAttr(testResourceName, "access_mode", accTestVpcSubnetCreateAttributes["access_mode"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "advanced_config.0.static_ip_allocation.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "static_ip_allocation.0.enabled", accTestPolicyVpcSubnetCreateAttributes["enabled"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_relay_config_path", accTestPolicyVpcSubnetCreateAttributes["dhcp_relay_config_path"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dns_client_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.enable_dhcp", accTestPolicyVpcSubnetCreateAttributes["enable_dhcp"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.static_pool_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dns_client_config.0.dns_server_ips.0", accTestPolicyVpcSubnetCreateAttributes["dns_server_ips"]),
resource.TestCheckResourceAttr(testResourceName, "static_pool_config.0.ipv4_pool_size", accTestPolicyVpcSubnetCreateAttributes["ipv4_pool_size"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.enable_dhcp", accTestVpcSubnetCreateAttributes["enable_dhcp"]),

resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
Expand All @@ -74,24 +57,15 @@ func TestAccResourceNsxtPolicyVpcSubnet_basic(t *testing.T) {
),
},
{
Config: testAccNsxtPolicyVpcSubnetTemplate(false),
Config: testAccNsxtVpcSubnetTemplate(false),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyVpcSubnetExists(accTestPolicyVpcSubnetUpdateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyVpcSubnetUpdateAttributes["display_name"]),
resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyVpcSubnetUpdateAttributes["description"]),
resource.TestCheckResourceAttr(testResourceName, "advanced_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "ipv4_subnet_size", accTestPolicyVpcSubnetUpdateAttributes["ipv4_subnet_size"]),
resource.TestCheckResourceAttr(testResourceName, "ip_addresses.0", accTestPolicyVpcSubnetUpdateAttributes["ip_addresses"]),
resource.TestCheckResourceAttr(testResourceName, "access_mode", accTestPolicyVpcSubnetUpdateAttributes["access_mode"]),
testAccNsxtVpcSubnetExists(accTestVpcSubnetUpdateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestVpcSubnetUpdateAttributes["display_name"]),
resource.TestCheckResourceAttr(testResourceName, "description", accTestVpcSubnetUpdateAttributes["description"]),
resource.TestCheckResourceAttr(testResourceName, "ip_addresses.0", accTestVpcSubnetUpdateAttributes["ip_addresses"]),
resource.TestCheckResourceAttr(testResourceName, "access_mode", accTestVpcSubnetUpdateAttributes["access_mode"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "advanced_config.0.static_ip_allocation.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "static_ip_allocation.0.enabled", accTestPolicyVpcSubnetUpdateAttributes["enabled"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_relay_config_path", accTestPolicyVpcSubnetUpdateAttributes["dhcp_relay_config_path"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dns_client_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.enable_dhcp", accTestPolicyVpcSubnetUpdateAttributes["enable_dhcp"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.static_pool_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dns_client_config.0.dns_server_ips.0", accTestPolicyVpcSubnetUpdateAttributes["dns_server_ips"]),
resource.TestCheckResourceAttr(testResourceName, "static_pool_config.0.ipv4_pool_size", accTestPolicyVpcSubnetUpdateAttributes["ipv4_pool_size"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.enable_dhcp", accTestVpcSubnetUpdateAttributes["enable_dhcp"]),

resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
Expand All @@ -100,9 +74,9 @@ func TestAccResourceNsxtPolicyVpcSubnet_basic(t *testing.T) {
),
},
{
Config: testAccNsxtPolicyVpcSubnetMinimalistic(),
Config: testAccNsxtVpcSubnetMinimalistic(),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyVpcSubnetExists(accTestPolicyVpcSubnetCreateAttributes["display_name"], testResourceName),
testAccNsxtVpcSubnetExists(accTestVpcSubnetCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "description", ""),
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
Expand All @@ -114,128 +88,110 @@ func TestAccResourceNsxtPolicyVpcSubnet_basic(t *testing.T) {
})
}

func TestAccResourceNsxtPolicyVpcSubnet_importBasic(t *testing.T) {
func TestAccResourceNsxtVpcSubnet_importBasic(t *testing.T) {
name := getAccTestResourceName()
testResourceName := "nsxt_policy_subnet.test"
testResourceName := "nsxt_vpc_subnet.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyVpcSubnetCheckDestroy(state, name)
return testAccNsxtVpcSubnetCheckDestroy(state, name)
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyVpcSubnetMinimalistic(),
Config: testAccNsxtVpcSubnetMinimalistic(),
},
{
ResourceName: testResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccResourceNsxtPolicyImportIDRetriever(testResourceName),
},
},
})
}

func testAccNsxtPolicyVpcSubnetExists(displayName string, resourceName string) resource.TestCheckFunc {
func testAccNsxtVpcSubnetExists(displayName string, resourceName string) resource.TestCheckFunc {
return func(state *terraform.State) error {

connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))

rs, ok := state.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Policy VpcSubnet resource %s not found in resources", resourceName)
return fmt.Errorf("VpcSubnet resource %s not found in resources", resourceName)
}

resourceID := rs.Primary.ID
if resourceID == "" {
return fmt.Errorf("Policy VpcSubnet resource ID not set in resources")
return fmt.Errorf("VpcSubnet resource ID not set in resources")
}

exists, err := resourceNsxtPolicyVpcSubnetExists(resourceID, connector, testAccIsGlobalManager())
exists, err := resourceNsxtVpcSubnetExists(testAccGetSessionContext(), resourceID, connector)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("Policy VpcSubnet %s does not exist", resourceID)
return fmt.Errorf("VpcSubnet %s does not exist", resourceID)
}

return nil
}
}

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

if rs.Type != "nsxt_policy_subnet" {
if rs.Type != "nsxt_vpc_subnet" {
continue
}

resourceID := rs.Primary.Attributes["id"]
exists, err := resourceNsxtPolicyVpcSubnetExists(resourceID, connector, testAccIsGlobalManager())
exists, err := resourceNsxtVpcSubnetExists(testAccGetSessionContext(), resourceID, connector)
if err == nil {
return err
}

if exists {
return fmt.Errorf("Policy VpcSubnet %s still exists", displayName)
return fmt.Errorf("VpcSubnet %s still exists", displayName)
}
}
return nil
}

func testAccNsxtPolicyVpcSubnetTemplate(createFlow bool) string {
func testAccNsxtVpcSubnetTemplate(createFlow bool) string {
var attrMap map[string]string
if createFlow {
attrMap = accTestPolicyVpcSubnetCreateAttributes
attrMap = accTestVpcSubnetCreateAttributes
} else {
attrMap = accTestPolicyVpcSubnetUpdateAttributes
attrMap = accTestVpcSubnetUpdateAttributes
}
return fmt.Sprintf(`
resource "nsxt_policy_subnet" "test" {
resource "nsxt_vpc_subnet" "test" {
%s
display_name = "%s"
description = "%s"
advanced_config {
static_ip_allocation {
enabled = %s
}
}
ipv4_subnet_size = %s
ip_addresses = [%s]
access_mode = %s
ip_addresses = ["%s"]
access_mode = "%s"
dhcp_config {
dhcp_relay_config_path = %s
dns_client_config {
dns_server_ips = [%s]
}
enable_dhcp = %s
static_pool_config {
ipv4_pool_size = %s
}
}
tag {
scope = "scope1"
tag = "tag1"
}
}`, attrMap["display_name"], attrMap["description"], attrMap["ipv4_subnet_size"], attrMap["ip_addresses"], attrMap["access_mode"], attrMap["enabled"], attrMap["dhcp_relay_config_path"], attrMap["enable_dhcp"], attrMap["dns_server_ips"], attrMap["ipv4_pool_size"])
}`, testAccNsxtPolicyMultitenancyContext(), attrMap["display_name"], attrMap["description"], attrMap["ip_addresses"], attrMap["access_mode"], attrMap["enable_dhcp"])
}

func testAccNsxtPolicyVpcSubnetMinimalistic() string {
func testAccNsxtVpcSubnetMinimalistic() string {
return fmt.Sprintf(`
resource "nsxt_policy_subnet" "test" {
resource "nsxt_vpc_subnet" "test" {
%s
display_name = "%s"
}`, accTestPolicyVpcSubnetUpdateAttributes["display_name"])
}`, testAccNsxtPolicyMultitenancyContext(), accTestVpcSubnetUpdateAttributes["display_name"])
}
Loading

0 comments on commit 441ff5e

Please sign in to comment.