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: azurerm_mariadb_configuration #4060

Merged
merged 5 commits into from Aug 13, 2019
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
10 changes: 7 additions & 3 deletions azurerm/internal/services/mariadb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (
)

type Client struct {
DatabasesClient mariadb.DatabasesClient
FirewallRulesClient mariadb.FirewallRulesClient
ServersClient mariadb.ServersClient
ConfigurationsClient mariadb.ConfigurationsClient
DatabasesClient mariadb.DatabasesClient
FirewallRulesClient mariadb.FirewallRulesClient
ServersClient mariadb.ServersClient
}

func BuildClient(o *common.ClientOptions) *Client {
c := Client{}

c.ConfigurationsClient = mariadb.NewConfigurationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&c.ConfigurationsClient.Client, o.ResourceManagerAuthorizer)

c.DatabasesClient = mariadb.NewDatabasesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&c.DatabasesClient.Client, o.ResourceManagerAuthorizer)

Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_management_group": resourceArmManagementGroup(),
"azurerm_management_lock": resourceArmManagementLock(),
"azurerm_maps_account": resourceArmMapsAccount(),
"azurerm_mariadb_configuration": resourceArmMariaDbConfiguration(),
"azurerm_mariadb_database": resourceArmMariaDbDatabase(),
"azurerm_mariadb_firewall_rule": resourceArmMariaDBFirewallRule(),
"azurerm_mariadb_server": resourceArmMariaDbServer(),
Expand Down
155 changes: 155 additions & 0 deletions azurerm/resource_arm_mariadb_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package azurerm

import (
"fmt"
"log"
"regexp"

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

func resourceArmMariaDbConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceArmMariaDbConfigurationCreateUpdate,
Read: resourceArmMariaDbConfigurationRead,
Delete: resourceArmMariaDbConfigurationDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
caius marked this conversation as resolved.
Show resolved Hide resolved

"resource_group_name": azure.SchemaResourceGroupName(),

"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",
),
},

"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

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

log.Printf("[INFO] preparing arguments for AzureRM MariaDb Configuration creation.")

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
serverName := d.Get("server_name").(string)
value := d.Get("value").(string)

properties := mariadb.Configuration{
ConfigurationProperties: &mariadb.ConfigurationProperties{
Value: utils.String(value),
},
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, serverName, name, properties)
if err != nil {
return fmt.Errorf("Error issuing create/update request for MariaDb Configuration %s (resource group %s, server name %s): %v", name, resourceGroup, serverName, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for create/update of MariaDb Configuration %s (resource group %s, server name %s): %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 Configuration %s (resource group %s, server name %s): %v", name, resourceGroup, serverName, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read MariaDb Configuration %s (resource group %s, server name %s) ID", name, resourceGroup, serverName)
}

d.SetId(*read.ID)

return resourceArmMariaDbConfigurationRead(d, meta)
}

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

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

resp, err := client.Get(ctx, resourceGroup, serverName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] MariaDb Configuration %q was not found (resource group %q)", name, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Azure MariaDb Configuration %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("server_name", serverName)
d.Set("resource_group_name", resourceGroup)
d.Set("value", resp.ConfigurationProperties.Value)

return nil
}

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

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

// "delete" = resetting this to the default value
resp, err := client.Get(ctx, resourceGroup, serverName, name)
if err != nil {
return fmt.Errorf("Error retrieving MariaDb Configuration %q (Resource Group %q): %+v", name, resourceGroup, err)
}

properties := mariadb.Configuration{
ConfigurationProperties: &mariadb.ConfigurationProperties{
// we can alternatively set `source: "system-default"`
Value: resp.DefaultValue,
},
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, serverName, name, properties)
if err != nil {
return err
}

return future.WaitForCompletionRef(ctx, client.Client)
}
Loading