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

Added new data source azurerm_mariadb_server #5506

Merged
merged 10 commits into from
Jan 29, 2020
254 changes: 254 additions & 0 deletions azurerm/internal/services/mariadb/data_source_arm_mariadb_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package mariadb

import (
"fmt"
"log"
"regexp"
"time"

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

func dataSourceArmMariaDbServer() *schema.Resource {
return &schema.Resource{
Read: resourceArmMariaDbServerRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

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

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"sku_name": {
Type: schema.TypeString,
Computed: true, // remove in 2.0
ConflictsWith: []string{"sku"},
ValidateFunc: validation.StringInSlice([]string{
"B_Gen5_1",
"B_Gen5_2",
"GP_Gen5_2",
"GP_Gen5_4",
"GP_Gen5_8",
"GP_Gen5_16",
"GP_Gen5_32",
"MO_Gen5_2",
"MO_Gen5_4",
"MO_Gen5_8",
"MO_Gen5_16",
}, false),
},

// remove in 2.0
"sku": {
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
Computed: true,
ConflictsWith: []string{"sku_name"},
Deprecated: "This property has been deprecated in favour of the 'sku_name' property and will be removed in version 2.0 of the provider",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"B_Gen5_1",
"B_Gen5_2",
"GP_Gen5_2",
"GP_Gen5_4",
"GP_Gen5_8",
"GP_Gen5_16",
"GP_Gen5_32",
"MO_Gen5_2",
"MO_Gen5_4",
"MO_Gen5_8",
"MO_Gen5_16",
}, false),
},

"capacity": {
Type: schema.TypeInt,
Computed: true,
ValidateFunc: validate.IntInSlice([]int{
1,
2,
4,
8,
16,
32,
}),
},

"tier": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
string(mariadb.Basic),
string(mariadb.GeneralPurpose),
string(mariadb.MemoryOptimized),
}, false),
},

"family": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"Gen5",
}, false),
},
},
},
},

"administrator_login": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validate.NoEmptyStrings,
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved
},

"administrator_login_password": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},

"version": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"10.2",
"10.3",
}, false),
},

"storage_profile": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"storage_mb": {
Type: schema.TypeInt,
Computed: true,
ValidateFunc: validate.IntBetweenAndDivisibleBy(5120, 4096000, 1024),
},

"backup_retention_days": {
Type: schema.TypeInt,
Computed: true,
ValidateFunc: validation.IntBetween(7, 35),
},

"geo_redundant_backup": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
string(mariadb.Enabled),
string(mariadb.Disabled),
}, false),
},

"auto_grow": {
Type: schema.TypeString,
Computed: true,
Default: string(mariadb.StorageAutogrowEnabled),
ValidateFunc: validation.StringInSlice([]string{
string(mariadb.StorageAutogrowEnabled),
string(mariadb.StorageAutogrowDisabled),
}, false),
},
},
},
},

"ssl_enforcement": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
string(mariadb.SslEnforcementEnumDisabled),
string(mariadb.SslEnforcementEnumEnabled),
}, false),
},

"fqdn": {
Type: schema.TypeString,
Computed: true,
},

"tags": tags.SchemaDataSource(),
},
}
}

func dataSourceArmMariaDbServerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MariaDB.ServersClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] MariaDB Server %q was not found (Resource Group %q)", name, resourceGroup)
d.SetId("")
return nil
}

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

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if sku := resp.Sku; sku != nil {
d.Set("sku_name", sku.Name)
}

if properties := resp.ServerProperties; properties != nil {
d.Set("administrator_login", properties.AdministratorLogin)
d.Set("version", string(properties.Version))
d.Set("ssl_enforcement", string(properties.SslEnforcement))
// Computed
d.Set("fqdn", properties.FullyQualifiedDomainName)

if err := d.Set("storage_profile", flattenMariaDbStorageProfile(properties.StorageProfile)); err != nil {
return fmt.Errorf("Error setting `storage_profile`: %+v", err)
}
}

if err := d.Set("sku", flattenMariaDbServerSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
}

return tags.FlattenAndSet(d, resp.Tags)
}
4 changes: 3 additions & 1 deletion azurerm/internal/services/mariadb/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ func (r Registration) Name() string {

// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{}
return map[string]*schema.Resource{
"azurerm_mariadb_server": dataSourceArmMariaDbServer(),
}
}

// SupportedResources returns the supported Resources supported by this Service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package tests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

func TestAccDataSourceAzureRMMariaDbServer_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mariadb_server", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMMariaDbServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMMariaDbServer_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMariaDbServerExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "administrator_login", "acctestun"),
resource.TestCheckResourceAttr(data.ResourceName, "version", "10.2"),
resource.TestCheckResourceAttr(data.ResourceName, "ssl_enforcement", "Enabled"),
),
},
},
})
}

func testAccDataSourceAzureRMMariaDbServer_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved
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"

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

data "azurerm_mariadb_server" "test" {
name = "${azurerm_mariadb.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func TestAccDataSourceAzureRMMariaDbServer_basicOldSku(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mariadb_server", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMMariaDbServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMMariaDbServer_basicOldSku(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMariaDbServerExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "administrator_login", "acctestun"),
resource.TestCheckResourceAttr(data.ResourceName, "version", "10.2"),
resource.TestCheckResourceAttr(data.ResourceName, "ssl_enforcement", "Enabled"),
),
},
},
})
}

func testAccDataSourceAzureRMMariaDbServer_basicOldSku(data acceptance.TestData) string {
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved
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"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
Loading