diff --git a/azurerm/helpers/validate/virtual_network_rule_name.go b/azurerm/helpers/validate/virtual_network_rule_name.go new file mode 100644 index 000000000000..16dcdc33cd49 --- /dev/null +++ b/azurerm/helpers/validate/virtual_network_rule_name.go @@ -0,0 +1,43 @@ +package validate + +import ( + "fmt" + "regexp" +) + +func VirtualNetworkRuleName(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + + // Cannot be empty + if len(value) == 0 { + errors = append(errors, fmt.Errorf( + "%q cannot be an empty string: %q", k, value)) + } + + // Cannot be more than 128 characters + if len(value) > 128 { + errors = append(errors, fmt.Errorf( + "%q cannot be longer than 128 characters: %q", k, value)) + } + + // Must only contain alphanumeric characters or hyphens + if !regexp.MustCompile(`^[A-Za-z0-9-]*$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "%q can only contain alphanumeric characters and hyphens: %q", + k, value)) + } + + // Cannot end in a hyphen + if regexp.MustCompile(`-$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "%q cannot end with a hyphen: %q", k, value)) + } + + // Cannot start with a number or hyphen + if regexp.MustCompile(`^[0-9-]`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "%q cannot start with a number or hyphen: %q", k, value)) + } + + return +} diff --git a/azurerm/helpers/validate/virtual_network_rule_name_test.go b/azurerm/helpers/validate/virtual_network_rule_name_test.go new file mode 100644 index 000000000000..3f92c155712f --- /dev/null +++ b/azurerm/helpers/validate/virtual_network_rule_name_test.go @@ -0,0 +1,126 @@ +package validate + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" +) + +func TestVirtualNetworkRule_invalidNameValidation(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + // Must only contain alphanumeric characters or hyphens (4 cases) + { + Value: "test!Rule", + ErrCount: 1, + }, + { + Value: "test_Rule", + ErrCount: 1, + }, + { + Value: "test:Rule", + ErrCount: 1, + }, + { + Value: "test'Rule", + ErrCount: 1, + }, + // Cannot be more than 128 characters (1 case - ensure starts with a letter) + { + Value: fmt.Sprintf("v%s", acctest.RandString(128)), + ErrCount: 1, + }, + // Cannot be empty (1 case) + { + Value: "", + ErrCount: 1, + }, + // Cannot end in a hyphen (1 case) + { + Value: "testRule-", + ErrCount: 1, + }, + // Cannot start with a number or hyphen (2 cases) + { + Value: "7testRule", + ErrCount: 1, + }, + { + Value: "-testRule", + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := VirtualNetworkRuleName(tc.Value, "azurerm_postgresql_virtual_network_rule") + + if len(errors) != tc.ErrCount { + t.Fatalf("Bad: Expected the Azure RM PostgreSQL Virtual Network Rule Name to trigger a validation error.") + } + } +} + +func TestResourceAzureRMPostgreSQLVirtualNetworkRule_validNameValidation(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + // Test all lowercase + { + Value: "thisisarule", + ErrCount: 0, + }, + // Test all uppercase + { + Value: "THISISARULE", + ErrCount: 0, + }, + // Test alternating cases + { + Value: "tHiSiSaRuLe", + ErrCount: 0, + }, + // Test hyphens + { + Value: "this-is-a-rule", + ErrCount: 0, + }, + // Test multiple hyphens in a row + { + Value: "this----1s----a----ru1e", + ErrCount: 0, + }, + // Test numbers (except for first character) + { + Value: "v1108501298509850810258091285091820-5", + ErrCount: 0, + }, + // Test a lot of hyphens and numbers + { + Value: "x-5-4-1-2-5-2-6-1-5-2-5-1-2-5-6-2-2", + ErrCount: 0, + }, + // Test exactly 128 characters + { + Value: fmt.Sprintf("v%s", acctest.RandString(127)), + ErrCount: 0, + }, + // Test short, 1-letter name + { + Value: "V", + ErrCount: 0, + }, + } + + for _, tc := range cases { + _, errors := VirtualNetworkRuleName(tc.Value, "azurerm_postgresql_virtual_network_rule") + + if len(errors) != tc.ErrCount { + t.Fatalf("Bad: Expected the Virtual Network Rule Name pass name validation successfully but triggered a validation error.") + } + } +} diff --git a/azurerm/resource_arm_postgresql_virtual_network_rule.go b/azurerm/resource_arm_postgresql_virtual_network_rule.go index c0d0ecc3fa96..72b882dc1ac9 100644 --- a/azurerm/resource_arm_postgresql_virtual_network_rule.go +++ b/azurerm/resource_arm_postgresql_virtual_network_rule.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "regexp" "strings" "time" @@ -14,6 +13,7 @@ import ( "github.com/hashicorp/terraform/helper/validation" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -32,7 +32,7 @@ func resourceArmPostgreSQLVirtualNetworkRule() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validatePostgreSQLVirtualNetworkRuleName, + ValidateFunc: validate.VirtualNetworkRuleName, }, "resource_group_name": resourceGroupNameSchema(), @@ -111,7 +111,7 @@ func resourceArmPostgreSQLVirtualNetworkRuleCreateUpdate(d *schema.ResourceData, return fmt.Errorf("Error creating PostgreSQL Virtual Network Rule %q (PostgreSQL Server: %q, Resource Group: %q): %+v", name, serverName, resourceGroup, err) } - //Wait for the provisioning state to become ready + // Wait for the provisioning state to become ready log.Printf("[DEBUG] Waiting for PostgreSQL Virtual Network Rule %q (PostgreSQL Server: %q, Resource Group: %q) to become ready: %+v", name, serverName, resourceGroup, err) stateConf := &resource.StateChangeConf{ Pending: []string{"Initializing", "InProgress", "Unknown", "ResponseNotFound"}, @@ -199,66 +199,12 @@ func resourceArmPostgreSQLVirtualNetworkRuleDelete(d *schema.ResourceData, meta return nil } - return fmt.Errorf("Error deleting PostgreSQL Virtual Network Rule %q (PostgreSQL Server: %q, Resource Group: %q): %+v", name, serverName, resourceGroup, err) + return fmt.Errorf("Error waiting for deletion of PostgreSQL Virtual Network Rule %q (PostgreSQL Server: %q, Resource Group: %q): %+v", name, serverName, resourceGroup, err) } return nil } -/* - This function checks the format of the PostgreSQL Virtual Network Rule Name to make sure that - it does not contain any potentially invalid values. -*/ -func validatePostgreSQLVirtualNetworkRuleName(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - - // Cannot be empty - if len(value) == 0 { - errors = append(errors, fmt.Errorf( - "%q cannot be an empty string: %q", k, value)) - } - - // Cannot be more than 128 characters - if len(value) > 128 { - errors = append(errors, fmt.Errorf( - "%q cannot be longer than 128 characters: %q", k, value)) - } - - // Must only contain alphanumeric characters or hyphens - if !regexp.MustCompile(`^[A-Za-z0-9-]*$`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "%q can only contain alphanumeric characters and hyphens: %q", - k, value)) - } - - // Cannot end in a hyphen - if regexp.MustCompile(`-$`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "%q cannot end with a hyphen: %q", k, value)) - } - - // Cannot start with a number or hyphen - if regexp.MustCompile(`^[0-9-]`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "%q cannot start with a number or hyphen: %q", k, value)) - } - - // There are multiple returns in the case that there is more than one invalid - // case applied to the name. - return -} - -/* - This function refreshes and checks the state of the PostgreSQL Virtual Network Rule. - - Response will contain a VirtualNetworkRuleProperties struct with a State property. The state property contain one of the following states (except ResponseNotFound). - * Deleting - * Initializing - * InProgress - * Unknown - * Ready - * ResponseNotFound (Custom state in case of 404) -*/ func postgreSQLVirtualNetworkStateStatusCodeRefreshFunc(ctx context.Context, client postgresql.VirtualNetworkRulesClient, resourceGroup string, serverName string, name string) resource.StateRefreshFunc { return func() (interface{}, string, error) { resp, err := client.Get(ctx, resourceGroup, serverName, name) diff --git a/azurerm/resource_arm_postgresql_virtual_network_rule_test.go b/azurerm/resource_arm_postgresql_virtual_network_rule_test.go index c050eed97d29..d860cb74c1cf 100644 --- a/azurerm/resource_arm_postgresql_virtual_network_rule_test.go +++ b/azurerm/resource_arm_postgresql_virtual_network_rule_test.go @@ -12,10 +12,6 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) -/* - ---Testing for Success--- - Test a basic PostgreSQL virtual network rule configuration setup scenario. -*/ func TestAccAzureRMPostgreSQLVirtualNetworkRule_basic(t *testing.T) { resourceName := "azurerm_postgresql_virtual_network_rule.test" ri := acctest.RandInt() @@ -35,11 +31,6 @@ func TestAccAzureRMPostgreSQLVirtualNetworkRule_basic(t *testing.T) { }) } -/* - ---Testing for Success--- - Test an update to the PostgreSQL Virtual Network Rule to connect to a different subnet, and - validate that new subnet is set correctly. -*/ func TestAccAzureRMPostgreSQLVirtualNetworkRule_switchSubnets(t *testing.T) { resourceName := "azurerm_postgresql_virtual_network_rule.test" ri := acctest.RandInt() @@ -74,9 +65,6 @@ func TestAccAzureRMPostgreSQLVirtualNetworkRule_switchSubnets(t *testing.T) { }) } -/* - ---Testing for Success--- -*/ func TestAccAzureRMPostgreSQLVirtualNetworkRule_disappears(t *testing.T) { resourceName := "azurerm_postgresql_virtual_network_rule.test" ri := acctest.RandInt() @@ -99,11 +87,6 @@ func TestAccAzureRMPostgreSQLVirtualNetworkRule_disappears(t *testing.T) { }) } -/* - --Testing for Success-- - Test if we are able to create multiple subnets and connect multiple subnets to the - PostgreSQL server. -*/ func TestAccAzureRMPostgreSQLVirtualNetworkRule_multipleSubnets(t *testing.T) { resourceName1 := "azurerm_postgresql_virtual_network_rule.rule1" resourceName2 := "azurerm_postgresql_virtual_network_rule.rule2" @@ -128,135 +111,6 @@ func TestAccAzureRMPostgreSQLVirtualNetworkRule_multipleSubnets(t *testing.T) { }) } -/* - --Testing for Failure-- - Validation Function Tests - Invalid Name Validations -*/ -func TestResourceAzureRMPostgreSQLVirtualNetworkRule_invalidNameValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - // Must only contain alphanumeric characters or hyphens (4 cases) - { - Value: "test!Rule", - ErrCount: 1, - }, - { - Value: "test_Rule", - ErrCount: 1, - }, - { - Value: "test:Rule", - ErrCount: 1, - }, - { - Value: "test'Rule", - ErrCount: 1, - }, - // Cannot be more than 128 characters (1 case - ensure starts with a letter) - { - Value: fmt.Sprintf("v%s", acctest.RandString(128)), - ErrCount: 1, - }, - // Cannot be empty (1 case) - { - Value: "", - ErrCount: 1, - }, - // Cannot end in a hyphen (1 case) - { - Value: "testRule-", - ErrCount: 1, - }, - // Cannot start with a number or hyphen (2 cases) - { - Value: "7testRule", - ErrCount: 1, - }, - { - Value: "-testRule", - ErrCount: 1, - }, - } - - for _, tc := range cases { - _, errors := validatePostgreSQLVirtualNetworkRuleName(tc.Value, "azurerm_postgresql_virtual_network_rule") - - if len(errors) != tc.ErrCount { - t.Fatalf("Bad: Expected the Azure RM PostgreSQL Virtual Network Rule Name to trigger a validation error.") - } - } -} - -/* - --Testing for Success-- - Validation Function Tests - (Barely) Valid Name Validations -*/ -func TestResourceAzureRMPostgreSQLVirtualNetworkRule_validNameValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - // Test all lowercase - { - Value: "thisisarule", - ErrCount: 0, - }, - // Test all uppercase - { - Value: "THISISARULE", - ErrCount: 0, - }, - // Test alternating cases - { - Value: "tHiSiSaRuLe", - ErrCount: 0, - }, - // Test hyphens - { - Value: "this-is-a-rule", - ErrCount: 0, - }, - // Test multiple hyphens in a row - { - Value: "this----1s----a----ru1e", - ErrCount: 0, - }, - // Test numbers (except for first character) - { - Value: "v1108501298509850810258091285091820-5", - ErrCount: 0, - }, - // Test a lot of hyphens and numbers - { - Value: "x-5-4-1-2-5-2-6-1-5-2-5-1-2-5-6-2-2", - ErrCount: 0, - }, - // Test exactly 128 characters - { - Value: fmt.Sprintf("v%s", acctest.RandString(127)), - ErrCount: 0, - }, - // Test short, 1-letter name - { - Value: "V", - ErrCount: 0, - }, - } - - for _, tc := range cases { - _, errors := validatePostgreSQLVirtualNetworkRuleName(tc.Value, "azurerm_postgresql_virtual_network_rule") - - if len(errors) != tc.ErrCount { - t.Fatalf("Bad: Expected the Azure RM PostgreSQL Virtual Network Rule Name pass name validation successfully but triggered a validation error.") - } - } -} - -/* - Test Check function to assert if a rule exists or not. -*/ func testCheckAzureRMPostgreSQLVirtualNetworkRuleExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] @@ -284,9 +138,6 @@ func testCheckAzureRMPostgreSQLVirtualNetworkRuleExists(name string) resource.Te } } -/* - Test Check function to delete a rule. -*/ func testCheckAzureRMPostgreSQLVirtualNetworkRuleDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_postgresql_virtual_network_rule" { @@ -315,9 +166,6 @@ func testCheckAzureRMPostgreSQLVirtualNetworkRuleDestroy(s *terraform.State) err return nil } -/* - Test Check function to assert if that a rule gets deleted. -*/ func testCheckAzureRMPostgreSQLVirtualNetworkRuleDisappears(name string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API @@ -358,260 +206,272 @@ func testCheckAzureRMPostgreSQLVirtualNetworkRuleDisappears(name string) resourc } } -/* - (This test configuration is intended to succeed.) - Basic Provisioning Configuration -*/ func testAccAzureRMPostgreSQLVirtualNetworkRule_basic(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" + name = "acctestRG-%d" location = "%s" } + resource "azurerm_virtual_network" "test" { name = "acctestvnet%d" address_space = ["10.7.29.0/29"] location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" } + resource "azurerm_subnet" "test" { - name = "acctestsubnet%d" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctestsubnet%d" + resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.test.name}" - address_prefix = "10.7.29.0/29" - service_endpoints = ["Microsoft.Sql"] + address_prefix = "10.7.29.0/29" + service_endpoints = ["Microsoft.Sql"] } + resource "azurerm_postgresql_server" "test" { - name = "acctestpostgresqlsvr-%d" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctestpostgresqlsvr-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + administrator_login = "acctestun" + administrator_login_password = "H@Sh1CoR3!" + version = "9.5" + ssl_enforcement = "Enabled" + sku { name = "GP_Gen5_2" capacity = 2 tier = "GeneralPurpose" family = "Gen5" } + storage_profile { storage_mb = 51200 backup_retention_days = 7 geo_redundant_backup = "Disabled" } - administrator_login = "acctestun" - administrator_login_password = "H@Sh1CoR3!" - version = "9.5" - ssl_enforcement = "Enabled" } + resource "azurerm_postgresql_virtual_network_rule" "test" { - name = "acctestpostgresqlvnetrule%d" + name = "acctestpostgresqlvnetrule%d" resource_group_name = "${azurerm_resource_group.test.name}" - server_name = "${azurerm_postgresql_server.test.name}" - subnet_id = "${azurerm_subnet.test.id}" + server_name = "${azurerm_postgresql_server.test.name}" + subnet_id = "${azurerm_subnet.test.id}" } + `, rInt, location, rInt, rInt, rInt, rInt) } -/* - (This test configuration is intended to succeed.) - This test is designed to set up a scenario where a user would want to update the subnet - on a given PostgreSQL virtual network rule. This configuration sets up the resources initially. -*/ func testAccAzureRMPostgreSQLVirtualNetworkRule_subnetSwitchPre(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" + name = "acctestRG-%d" location = "%s" } + resource "azurerm_virtual_network" "test" { name = "acctestvnet%d" address_space = ["10.7.29.0/24"] location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" } + resource "azurerm_subnet" "test1" { - name = "subnet1%d" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "subnet1%d" + resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.test.name}" - address_prefix = "10.7.29.0/25" - service_endpoints = ["Microsoft.Sql"] + address_prefix = "10.7.29.0/25" + service_endpoints = ["Microsoft.Sql"] } + resource "azurerm_subnet" "test2" { - name = "subnet2%d" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "subnet2%d" + resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.test.name}" - address_prefix = "10.7.29.128/25" - service_endpoints = ["Microsoft.Sql"] + address_prefix = "10.7.29.128/25" + service_endpoints = ["Microsoft.Sql"] } + resource "azurerm_postgresql_server" "test" { - name = "acctestpostgresqlsvr-%d" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctestpostgresqlsvr-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + administrator_login = "acctestun" + administrator_login_password = "H@Sh1CoR3!" + version = "9.5" + ssl_enforcement = "Enabled" + sku { name = "GP_Gen5_2" capacity = 2 tier = "GeneralPurpose" family = "Gen5" } + storage_profile { storage_mb = 51200 backup_retention_days = 7 geo_redundant_backup = "Disabled" } - administrator_login = "acctestun" - administrator_login_password = "H@Sh1CoR3!" - version = "9.5" - ssl_enforcement = "Enabled" } + resource "azurerm_postgresql_virtual_network_rule" "test" { - name = "acctestpostgresqlvnetrule%d" + name = "acctestpostgresqlvnetrule%d" resource_group_name = "${azurerm_resource_group.test.name}" - server_name = "${azurerm_postgresql_server.test.name}" - subnet_id = "${azurerm_subnet.test1.id}" + server_name = "${azurerm_postgresql_server.test.name}" + subnet_id = "${azurerm_subnet.test1.id}" } `, rInt, location, rInt, rInt, rInt, rInt, rInt) } -/* - (This test configuration is intended to succeed.) - This test is designed to set up a scenario where a user would want to update the subnet - on a given PostgreSQL virtual network rule. This configuration contains the update from - azurerm_subnet.test1 to azurerm_subnet.test2. -*/ func testAccAzureRMPostgreSQLVirtualNetworkRule_subnetSwitchPost(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" + name = "acctestRG-%d" location = "%s" } + resource "azurerm_virtual_network" "test" { name = "acctestvnet%d" address_space = ["10.7.29.0/24"] location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" } + resource "azurerm_subnet" "test1" { - name = "subnet1%d" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "subnet1%d" + resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.test.name}" - address_prefix = "10.7.29.0/25" - service_endpoints = ["Microsoft.Sql"] + address_prefix = "10.7.29.0/25" + service_endpoints = ["Microsoft.Sql"] } + resource "azurerm_subnet" "test2" { - name = "subnet2%d" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "subnet2%d" + resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.test.name}" - address_prefix = "10.7.29.128/25" - service_endpoints = ["Microsoft.Sql"] + address_prefix = "10.7.29.128/25" + service_endpoints = ["Microsoft.Sql"] } + resource "azurerm_postgresql_server" "test" { - name = "acctestpostgresqlsvr-%d" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctestpostgresqlsvr-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + administrator_login = "acctestun" + administrator_login_password = "H@Sh1CoR3!" + version = "9.5" + ssl_enforcement = "Enabled" + sku { name = "GP_Gen5_2" capacity = 2 tier = "GeneralPurpose" family = "Gen5" } + storage_profile { storage_mb = 51200 backup_retention_days = 7 geo_redundant_backup = "Disabled" } - administrator_login = "acctestun" - administrator_login_password = "H@Sh1CoR3!" - version = "9.5" - ssl_enforcement = "Enabled" } + resource "azurerm_postgresql_virtual_network_rule" "test" { - name = "acctestpostgresqlvnetrule%d" + name = "acctestpostgresqlvnetrule%d" resource_group_name = "${azurerm_resource_group.test.name}" - server_name = "${azurerm_postgresql_server.test.name}" - subnet_id = "${azurerm_subnet.test2.id}" + server_name = "${azurerm_postgresql_server.test.name}" + subnet_id = "${azurerm_subnet.test2.id}" } `, rInt, location, rInt, rInt, rInt, rInt, rInt) } -/* - (This test configuration is intended to succeed.) - This configuration sets up 3 subnets in 2 different virtual networks, and adds - PostgreSQL virtual network rules for all 3 subnets to the SQL server. -*/ func testAccAzureRMPostgreSQLVirtualNetworkRule_multipleSubnets(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" + name = "acctestRG-%d" location = "%s" } + resource "azurerm_virtual_network" "vnet1" { name = "acctestvnet1%d" address_space = ["10.7.29.0/24"] location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" } + resource "azurerm_virtual_network" "vnet2" { name = "acctestvnet2%d" address_space = ["10.1.29.0/29"] location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" } + resource "azurerm_subnet" "vnet1_subnet1" { - name = "acctestsubnet1%d" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctestsubnet1%d" + resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.vnet1.name}" - address_prefix = "10.7.29.0/29" - service_endpoints = ["Microsoft.Sql"] + address_prefix = "10.7.29.0/29" + service_endpoints = ["Microsoft.Sql"] } + resource "azurerm_subnet" "vnet1_subnet2" { - name = "acctestsubnet2%d" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctestsubnet2%d" + resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.vnet1.name}" - address_prefix = "10.7.29.128/29" - service_endpoints = ["Microsoft.Sql"] + address_prefix = "10.7.29.128/29" + service_endpoints = ["Microsoft.Sql"] } + resource "azurerm_subnet" "vnet2_subnet1" { - name = "acctestsubnet3%d" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctestsubnet3%d" + resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.vnet2.name}" - address_prefix = "10.1.29.0/29" - service_endpoints = ["Microsoft.Sql"] + address_prefix = "10.1.29.0/29" + service_endpoints = ["Microsoft.Sql"] } + resource "azurerm_postgresql_server" "test" { - name = "acctestpostgresqlsvr-%d" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctestpostgresqlsvr-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + administrator_login = "acctestun" + administrator_login_password = "H@Sh1CoR3!" + version = "9.5" + ssl_enforcement = "Enabled" + sku { name = "GP_Gen5_2" capacity = 2 tier = "GeneralPurpose" family = "Gen5" } + storage_profile { storage_mb = 51200 backup_retention_days = 7 geo_redundant_backup = "Disabled" } - administrator_login = "acctestun" - administrator_login_password = "H@Sh1CoR3!" - version = "9.5" - ssl_enforcement = "Enabled" } + resource "azurerm_postgresql_virtual_network_rule" "rule1" { - name = "acctestpostgresqlvnetrule1%d" + name = "acctestpostgresqlvnetrule1%d" resource_group_name = "${azurerm_resource_group.test.name}" - server_name = "${azurerm_postgresql_server.test.name}" - subnet_id = "${azurerm_subnet.vnet1_subnet1.id}" + server_name = "${azurerm_postgresql_server.test.name}" + subnet_id = "${azurerm_subnet.vnet1_subnet1.id}" } + resource "azurerm_postgresql_virtual_network_rule" "rule2" { - name = "acctestpostgresqlvnetrule2%d" + name = "acctestpostgresqlvnetrule2%d" resource_group_name = "${azurerm_resource_group.test.name}" - server_name = "${azurerm_postgresql_server.test.name}" - subnet_id = "${azurerm_subnet.vnet1_subnet2.id}" + server_name = "${azurerm_postgresql_server.test.name}" + subnet_id = "${azurerm_subnet.vnet1_subnet2.id}" } + resource "azurerm_postgresql_virtual_network_rule" "rule3" { - name = "acctestpostgresqlvnetrule3%d" + name = "acctestpostgresqlvnetrule3%d" resource_group_name = "${azurerm_resource_group.test.name}" - server_name = "${azurerm_postgresql_server.test.name}" - subnet_id = "${azurerm_subnet.vnet2_subnet1.id}" + server_name = "${azurerm_postgresql_server.test.name}" + subnet_id = "${azurerm_subnet.vnet2_subnet1.id}" } `, rInt, location, rInt, rInt, rInt, rInt, rInt, rInt, rInt, rInt, rInt) } diff --git a/website/docs/r/postgresql_virtual_network_rule.html.markdown b/website/docs/r/postgresql_virtual_network_rule.html.markdown index 18147e37d4e6..79de06acdd9b 100644 --- a/website/docs/r/postgresql_virtual_network_rule.html.markdown +++ b/website/docs/r/postgresql_virtual_network_rule.html.markdown @@ -1,39 +1,39 @@ --- layout: "azurerm" page_title: "Azure Resource Manager: azurerm_postgresql_virtual_network_rule" -sidebar_current: "docs-azurerm-resource-database-sql-virtual-network-rule" +sidebar_current: "docs-azurerm-resource-database-postgresql-virtual-network-rule" description: |- Manages a PostgreSQL Virtual Network Rule. --- # azurerm_postgresql_virtual_network_rule -Allows you to add, update, or remove an Azure PostgreSQL server to a subnet of a virtual network. +Manages a PostgreSQL Virtual Network Rule. ## Example Usage ```hcl -resource "azurerm_resource_group" "example" { - name = "example-postgresql-server-vnet-rule" +resource "azurerm_resource_group" "test" { + name = "example-resources" location = "West US" } -resource "azurerm_virtual_network" "vnet" { +resource "azurerm_virtual_network" "test" { name = "example-vnet" address_space = ["10.7.29.0/29"] - location = "${azurerm_resource_group.example.location}" - resource_group_name = "${azurerm_resource_group.example.name}" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" } -resource "azurerm_subnet" "subnet" { - name = "example-subnet" +resource "azurerm_subnet" "internal" { + name = "internal" resource_group_name = "${azurerm_resource_group.example.name}" virtual_network_name = "${azurerm_virtual_network.vnet.name}" address_prefix = "10.7.29.0/29" service_endpoints = ["Microsoft.Sql"] } -resource "azurerm_postgresql_server" "postgresql_server" { +resource "azurerm_postgresql_server" "test" { name = "postgresql-server-1" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" @@ -57,11 +57,11 @@ resource "azurerm_postgresql_server" "postgresql_server" { ssl_enforcement = "Enabled" } -resource "azurerm_postgresql_virtual_network_rule" "postgresqlsql_virtual_network_rule" { +resource "azurerm_postgresql_virtual_network_rule" "test" { name = "postgresql-vnet-rule" - resource_group_name = "${azurerm_resource_group.example.name}" - server_name = "${azurerm_postgresql_server.postgresql_server.name}" - subnet_id = "${azurerm_subnet.subnet.id}" + resource_group_name = "${azurerm_resource_group.test.name}" + server_name = "${azurerm_postgresql_server.test.name}" + subnet_id = "${azurerm_subnet.internal.id}" } ``` @@ -69,7 +69,7 @@ resource "azurerm_postgresql_virtual_network_rule" "postgresqlsql_virtual_networ The following arguments are supported: -* `name` - (Required) The name of the SQL virtual network rule. Cannot be empty and must only contain alphanumeric characters and hyphens. Cannot start with a number, and cannot start or end with a hyphen. Changing this forces a new resource to be created. +* `name` - (Required) The name of the PostgreSQL virtual network rule. Cannot be empty and must only contain alphanumeric characters and hyphens. Cannot start with a number, and cannot start or end with a hyphen. Changing this forces a new resource to be created. ~> **NOTE:** `name` must be between 1-128 characters long and must satisfy all of the requirements below: 1. Contains only alphanumeric and hyphen characters