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

New Resource: MariaDB Database #2445

Merged
merged 33 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
15c6e04
Implements the MariaDB Database API
WodansSon Dec 5, 2018
9d29c44
Update Changelog and doc
WodansSon Dec 5, 2018
23d3dd6
Fix test cases
WodansSon Dec 5, 2018
7594839
Fix lint errors
WodansSon Dec 5, 2018
bac5512
Updated order
WodansSon Dec 5, 2018
5df43c9
Update test
WodansSon Dec 5, 2018
a6f22cc
Remove tests
WodansSon Dec 5, 2018
f9b0a0a
Fix lint error
WodansSon Dec 5, 2018
2a2a23a
Fixed sidebar
WodansSon Dec 5, 2018
17cc44a
Update azurerm/resource_arm_mariadb_database.go
katbyte Dec 5, 2018
4cb3f5d
Update azurerm/resource_arm_mariadb_database.go
katbyte Dec 5, 2018
3e5d708
Update azurerm/resource_arm_mariadb_database.go
katbyte Dec 5, 2018
eeba3e3
Update name to resourceName
WodansSon Dec 5, 2018
60f0692
Updates per PR comments
WodansSon Dec 5, 2018
6acb50f
Updated usage example
WodansSon Dec 5, 2018
a7ef3a3
Added missing arg
WodansSon Dec 5, 2018
6a335ce
Updated the latest Changelog
WodansSon Dec 5, 2018
db7eb3b
Removed Resource Group arg
WodansSon Dec 5, 2018
28060ef
Missed wraping one error
WodansSon Dec 5, 2018
34fc458
Correct formatting
WodansSon Dec 5, 2018
675d74d
Fixed delete logic
WodansSon Dec 5, 2018
35a0e04
Fixed error message casing
WodansSon Dec 5, 2018
d3f8bf0
Update
WodansSon Dec 5, 2018
cc2a3c1
removed punctuation from errors
WodansSon Dec 6, 2018
22ad342
added RG to error msg
WodansSon Dec 6, 2018
a2671f7
made err msg clearer
WodansSon Dec 6, 2018
7172c83
Sync changelog
WodansSon Dec 6, 2018
2da9002
Added new resource to changelog
WodansSon Dec 6, 2018
6b9cdf6
Sync changelog again
WodansSon Dec 6, 2018
f441ab4
Remove CRLF -> LF
WodansSon Dec 6, 2018
a790d36
Try again
WodansSon Dec 6, 2018
337148f
Add new resource to changelog
WodansSon Dec 6, 2018
b21fef1
Merge branch 'master' into nr-mariadb-database
katbyte Dec 6, 2018
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FEATURES:

* **New Data Source:** `azurerm_monitor_action_group` [GH-2430]
* **New Resource:** `azurerm_mariadb_database` [GH-2445]
* **New Resource:** `azurerm_mariadb_server` [GH-2406]

IMPROVEMENTS:
Expand All @@ -11,11 +12,13 @@ IMPROVEMENTS:
* authentication: requesting a token using the audience address [GH-2381]
* authentication: switching to request tokens from the Azure CLI [GH-2387]
* sdk: upgrading to version `2018-05-01` of the Policy API [GH-2386]
* Data Source: `azurerm_subscriptions` - ability to filtering by prefix/contains on the Display Name [GH-2429]
* `azurerm_app_service` - support for configuring `app_command_line` in the `site_config` block [GH-2350]
* `azurerm_app_service_plan` - deprecated the `properties` and moved `app_service_environment_id`, `per_site_scaling` and `reserved` to the top level [GH-2442]
* `azurerm_app_service_slot` - support for configuring `app_command_line` in the `site_config` block [GH-2350]
* `azurerm_application_insights` - added `Node.JS` application type [GH-2407]
* `azurerm_key_vault` - exposed `backup` and `restore` permissions made `key_permissions` and `secret_permissions` optional [GH-2363]
* `azurerm_container_registry` - supprt for geo-replication via the `georeplication_locations` property [GH-2055]
* `azurerm_key_vault` - exposed `backup` and `restore` permissions made `key_permissions` and `secret_permissions` optional [GH-2363]
* `azurerm_mssql_elasticpool` - deprecated the `elastic_pool_properties` propety and moved `max_size_bytes` and `zone_redundant` to the top level [GH-2378]

BUG FIXES:
Expand All @@ -26,6 +29,7 @@ BUG FIXES:
* `azurerm_eventhub` - now validates that the `storage_account_id` is a proper resource ID [GH-2374]
* `azurerm_mssql_elasticpool` - relaxed validation of the `name` property [GH-2398]
* `azurerm_recovery_services_protection_policy_vm` - added the `timezone` property [GH-2404]
* `azurerm_virtual_machine` - nil-checking properties prior to accessing [GH-2365]

## 1.19.0 (November 15, 2018)

Expand Down
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ type ArmClient struct {
devSpaceControllerClient devspaces.ControllersClient

// Databases
mariadbDatabasesClient mariadb.DatabasesClient
mariadbServersClient mariadb.ServersClient
mysqlConfigurationsClient mysql.ConfigurationsClient
mysqlDatabasesClient mysql.DatabasesClient
Expand Down Expand Up @@ -641,6 +642,10 @@ func (c *ArmClient) registerDatabricksClients(endpoint, subscriptionId string, a
}

func (c *ArmClient) registerDatabases(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
mariadbDBClient := mariadb.NewDatabasesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&mariadbDBClient.Client, auth)
c.mariadbDatabasesClient = mariadbDBClient

mariadbServersClient := mariadb.NewServersClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&mariadbServersClient.Client, auth)
c.mariadbServersClient = mariadbServersClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_logic_app_trigger_http_request": resourceArmLogicAppTriggerHttpRequest(),
"azurerm_logic_app_trigger_recurrence": resourceArmLogicAppTriggerRecurrence(),
"azurerm_logic_app_workflow": resourceArmLogicAppWorkflow(),
"azurerm_mariadb_database": resourceArmMariaDbDatabase(),
"azurerm_mariadb_server": resourceArmMariaDbServer(),
"azurerm_managed_disk": resourceArmManagedDisk(),
"azurerm_management_lock": resourceArmManagementLock(),
Expand Down
176 changes: 176 additions & 0 deletions azurerm/resource_arm_mariadb_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package azurerm

import (
"fmt"
"log"
"regexp"

"github.com/Azure/azure-sdk-for-go/services/preview/mariadb/mgmt/2018-06-01-preview/mariadb"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmMariaDbDatabase() *schema.Resource {
return &schema.Resource{
Create: resourceArmMariaDbDatabaseCreateOrUpdate,
Read: resourceArmMariaDbDatabaseRead,
Delete: resourceArmMariaDbDatabaseDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[_a-zA-Z0-9]{1,64}$"),
"name must be 1 - 64 characters long, and contain only letters, numbers and underscores.",
),
},

"resource_group_name": resourceGroupNameSchema(),

"server_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[-a-zA-Z0-9]{3,50}$"),
"server_name must be 3 - 50 characters long, and contain only letters, numbers and hyphens.",
),
},

"charset": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[a-z0-9]{3,8}$"),
"charset must be 3 - 8 characters long, and contain only lowercase letters and numbers.",
),
},

"collation": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCollation(),
},
},
}
}

func resourceArmMariaDbDatabaseCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).mariadbDatabasesClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for AzureRM MariaDB Database creation.")

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
serverName := d.Get("server_name").(string)

charset := d.Get("charset").(string)
collation := d.Get("collation").(string)

properties := mariadb.Database{
DatabaseProperties: &mariadb.DatabaseProperties{
Charset: utils.String(charset),
Collation: utils.String(collation),
},
}

future, err := client.CreateOrUpdate(ctx, resGroup, serverName, name, properties)
if err != nil {
return fmt.Errorf("Error creating Maria DB %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Maria DB %q (Resource Group %q): %+v", name, resGroup, err)
}

read, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
return fmt.Errorf("Error retrieving Maria DB %q (Resource Group %q): %+v", name, resGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read MariaDB Database %s (resource group %s) ID", name, resGroup)
}

d.SetId(*read.ID)

return resourceArmMariaDbDatabaseRead(d, meta)
}

func resourceArmMariaDbDatabaseRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).mariadbDatabasesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
serverName := id.Path["servers"]
name := id.Path["databases"]

resp, err := client.Get(ctx, resGroup, serverName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] MariaDB Database '%s' was not found (resource group '%s')", name, resGroup)
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Azure MariaDB Database %s: %+v", name, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("server_name", serverName)

if props := resp.DatabaseProperties; props != nil {
d.Set("charset", props.Charset)

if collation := props.Collation; collation != nil {
d.Set("collation", collation)
}
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

func resourceArmMariaDbDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).mariadbDatabasesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
serverName := id.Path["servers"]
name := id.Path["databases"]

future, err := client.Delete(ctx, resGroup, serverName, name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return err
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return err
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}
133 changes: 133 additions & 0 deletions azurerm/resource_arm_mariadb_database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func TestAccAzureRMMariaDbDatabase_basic(t *testing.T) {
resourceName := "azurerm_mariadb_database.test"
ri := acctest.RandInt()
config := testAccAzureRMMariaDbDatabase_basic(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMMariaDbDatabaseDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMariaDbDatabaseExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "charset", "utf8"),
resource.TestCheckResourceAttr(resourceName, "collation", "utf8_general_ci"),
),
},
},
})
}

func testCheckAzureRMMariaDbDatabaseExists(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 Database: %s", name)
}

client := testAccProvider.Meta().(*ArmClient).mariadbDatabasesClient
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 Database %q (server %q resource group: %q) does not exist", name, serverName, resourceGroup)
}
return fmt.Errorf("Bad: Get on mariadbDatabasesClient: %+v", err)
}

return nil
}
}

func testCheckAzureRMMariaDbDatabaseDestroy(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_database" {
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 err
}

return fmt.Errorf("MariaDB Database still exists:\n%#v", resp)
}

return nil
}

func testAccAzureRMMariaDbDatabase_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 = "B_Gen5_2"
capacity = 2
tier = "Basic"
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_database" "test" {
name = "acctestmariadb_%d"
resource_group_name = "${azurerm_resource_group.test.name}"
server_name = "${azurerm_mariadb_server.test.name}"
charset = "utf8"
collation = "utf8_general_ci"
}
`, rInt, location, rInt, rInt)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@
<a href="#">Database Resources</a>
<ul class="nav nav-visible">

<li<%= sidebar_current("docs-azurerm-resource-database-mariadb-database") %>>
<a href="/docs/providers/azurerm/r/mariadb_database.html">azurerm_mariadb_database</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-database-mariadb-server") %>>
<a href="/docs/providers/azurerm/r/mariadb_server.html">azurerm_mariadb_server</a>
</li>
Expand Down
Loading