-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
420 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/mariadb/mgmt/2018-06-01/mariadb" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmMariaDBFirewallRule() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmMariaDBFirewallRuleCreateUpdate, | ||
Read: resourceArmMariaDBFirewallRuleRead, | ||
Update: resourceArmMariaDBFirewallRuleCreateUpdate, | ||
Delete: resourceArmMariaDBFirewallRuleDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"server_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"start_ip_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"end_ip_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmMariaDBFirewallRuleCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).mariadbFirewallRulesClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
log.Printf("[INFO] preparing arguments for AzureRM MariaDB Firewall Rule creation.") | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
serverName := d.Get("server_name").(string) | ||
startIPAddress := d.Get("start_ip_address").(string) | ||
endIPAddress := d.Get("end_ip_address").(string) | ||
|
||
if requireResourcesToBeImported && d.IsNewResource() { | ||
existing, err := client.Get(ctx, resourceGroup, serverName, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("Error checking for presence of existing MariaDB Firewall Rule %q (resource group %q, server name %q): %v", name, resourceGroup, serverName, err) | ||
} | ||
} | ||
|
||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_mariadb_firewall_rule", *existing.ID) | ||
} | ||
} | ||
|
||
properties := mariadb.FirewallRule{ | ||
FirewallRuleProperties: &mariadb.FirewallRuleProperties{ | ||
StartIPAddress: utils.String(startIPAddress), | ||
EndIPAddress: utils.String(endIPAddress), | ||
}, | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, resourceGroup, serverName, name, properties) | ||
if err != nil { | ||
return fmt.Errorf("Error issuing create/update request for MariaDB Firewall Rule %q (resource group %q, server name %q): %v", name, resourceGroup, serverName, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting onf create/update future for MariaDB Firewall Rule %q (resource group %q, server name %q): %v", name, resourceGroup, serverName, err) | ||
} | ||
|
||
read, err := client.Get(ctx, resourceGroup, serverName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error issuing get request for MariaDB Firewall Rule %q (resource group %q, server name %q): %v", name, resourceGroup, serverName, err) | ||
} | ||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read MariaDB Firewall Rule %q (Gesource Group %q) ID", name, resourceGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmMariaDBFirewallRuleRead(d, meta) | ||
} | ||
|
||
func resourceArmMariaDBFirewallRuleRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).mariadbFirewallRulesClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
serverName := id.Path["servers"] | ||
name := id.Path["firewallRules"] | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serverName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on Azure MariaDB Firewall Rule %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("resource_group_name", resourceGroup) | ||
d.Set("server_name", serverName) | ||
d.Set("start_ip_address", resp.StartIPAddress) | ||
d.Set("end_ip_address", resp.EndIPAddress) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmMariaDBFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).mariadbFirewallRulesClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
serverName := id.Path["servers"] | ||
name := id.Path["firewallRules"] | ||
|
||
future, err := client.Delete(ctx, resourceGroup, serverName, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return future.WaitForCompletionRef(ctx, client.Client) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func TestAccAzureRMMariaDBFirewallRule_basic(t *testing.T) { | ||
resourceName := "azurerm_mariadb_firewall_rule.test" | ||
ri := tf.AccRandTimeInt() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMMariaDBFirewallRuleDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureRMMariaDBFirewallRule_basic(ri, testLocation()), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMMariaDBFirewallRuleExists(resourceName), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMMariaDBFirewallRule_requiresImport(t *testing.T) { | ||
if !requireResourcesToBeImported { | ||
t.Skip("Skipping since resources aren't required to be imported") | ||
return | ||
} | ||
|
||
resourceName := "azurerm_mariadb_firewall_rule.test" | ||
ri := tf.AccRandTimeInt() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMMariaDBFirewallRuleDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureRMMariaDBFirewallRule_basic(ri, testLocation()), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMMariaDBFirewallRuleExists(resourceName), | ||
), | ||
}, | ||
{ | ||
Config: testAccAzureRMMariaDBFirewallRule_requiresImport(ri, testLocation()), | ||
ExpectError: testRequiresImportError("azurerm_mariadb_firewall_rule"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMMariaDBFirewallRuleExists(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// Ensure we have enough information in state to look up in API | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", resourceName) | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
serverName := rs.Primary.Attributes["server_name"] | ||
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
if !hasResourceGroup { | ||
return fmt.Errorf("Bad: no resource group found in state for MariaDB Firewall Rule: %s", name) | ||
} | ||
|
||
client := testAccProvider.Meta().(*ArmClient).mariadbFirewallRulesClient | ||
ctx := testAccProvider.Meta().(*ArmClient).StopContext | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serverName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Bad: MariaDB Firewall Rule %q (server %q resource group: %q) does not exist", name, serverName, resourceGroup) | ||
} | ||
return fmt.Errorf("Bad: Get on mariadbFirewallRulesClient: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testCheckAzureRMMariaDBFirewallRuleDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*ArmClient).mariadbDatabasesClient | ||
ctx := testAccProvider.Meta().(*ArmClient).StopContext | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_mariadb_firewall_rule" { | ||
continue | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
serverName := rs.Primary.Attributes["server_name"] | ||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serverName, name) | ||
|
||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("MariaDB Firewall Rule still exists:\n%#v", resp) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccAzureRMMariaDBFirewallRule_basic(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_mariadb_server" "test" { | ||
name = "acctestmariadbsvr-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
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 = "10.2" | ||
ssl_enforcement = "Enabled" | ||
} | ||
resource "azurerm_mariadb_firewall_rule" "test" { | ||
name = "acctestfwrule-%d" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
server_name = "${azurerm_mariadb_server.test.name}" | ||
start_ip_address = "0.0.0.0" | ||
end_ip_address = "255.255.255.255" | ||
} | ||
`, rInt, location, rInt, rInt) | ||
} | ||
|
||
func testAccAzureRMMariaDBFirewallRule_requiresImport(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_mariadb_firewall_rule" "import" { | ||
name = "${azurerm_mariadb_firewall_rule.test.name}" | ||
resource_group_name = "${azurerm_mariadb_firewall_rule.test.resource_group_name}" | ||
server_name = "${azurerm_mariadb_firewall_rule.test.server_name}" | ||
start_ip_address = "${azurerm_mariadb_firewall_rule.test.start_ip_address}" | ||
end_ip_address = "${azurerm_mariadb_firewall_rule.test.end_ip_address}" | ||
} | ||
`, testAccAzureRMMariaDBFirewallRule_basic(rInt, location)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.