diff --git a/azurerm/internal/services/mariadb/data_source_mariadb_server.go b/azurerm/internal/services/mariadb/data_source_mariadb_server.go new file mode 100644 index 000000000000..dc15e44d49bc --- /dev/null +++ b/azurerm/internal/services/mariadb/data_source_mariadb_server.go @@ -0,0 +1,142 @@ +package mariadb + +import ( + "fmt" + "regexp" + "time" + + "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/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 dataSourceMariaDbServer() *schema.Resource { + return &schema.Resource{ + Read: dataSourceMariaDbServerRead, + + 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, + }, + + "administrator_login": { + Type: schema.TypeString, + Computed: true, + }, + + "administrator_login_password": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "version": { + Type: schema.TypeString, + Computed: true, + }, + + "storage_profile": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "storage_mb": { + Type: schema.TypeInt, + Computed: true, + }, + + "backup_retention_days": { + Type: schema.TypeInt, + Computed: true, + }, + + "geo_redundant_backup": { + Type: schema.TypeString, + Computed: true, + }, + + "auto_grow": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + + "ssl_enforcement": { + Type: schema.TypeString, + Computed: true, + }, + + "fqdn": { + Type: schema.TypeString, + Computed: true, + }, + + "tags": tags.SchemaDataSource(), + }, + } +} + +func dataSourceMariaDbServerRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).MariaDB.ServersClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + resp, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Azure MariaDB Server %q (Resource Group %q) was not found", name, resourceGroup) + } + + return fmt.Errorf("Error making Read request on Azure MariaDB Server %s: %+v", name, 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)) + 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) + } + } + return tags.FlattenAndSet(d, resp.Tags) +} diff --git a/azurerm/internal/services/mariadb/registration.go b/azurerm/internal/services/mariadb/registration.go index 56fcaf38d2fc..0e32545c8668 100644 --- a/azurerm/internal/services/mariadb/registration.go +++ b/azurerm/internal/services/mariadb/registration.go @@ -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": dataSourceMariaDbServer(), + } } // SupportedResources returns the supported Resources supported by this Service diff --git a/azurerm/internal/services/mariadb/tests/data_source_mariadb_server_test.go b/azurerm/internal/services/mariadb/tests/data_source_mariadb_server_test.go new file mode 100644 index 000000000000..3a8b51b90eae --- /dev/null +++ b/azurerm/internal/services/mariadb/tests/data_source_mariadb_server_test.go @@ -0,0 +1,63 @@ +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-maria-%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" + + 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_server.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) +} diff --git a/website/docs/d/mariadb_server.html.markdown b/website/docs/d/mariadb_server.html.markdown new file mode 100644 index 000000000000..f310747a3890 --- /dev/null +++ b/website/docs/d/mariadb_server.html.markdown @@ -0,0 +1,66 @@ +--- +subcategory: "Database" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_mariadb_server" +description: |- + Gets information about a MariaDB Server. +--- + +# Data Source: azurerm_mariadb_server + +Use this data source to access information about a MariaDB Server. + +## Example Usage + +```hcl +data "azurerm_mariadb_server" "db_server" { + name = "mariadb-server" + resource_group_name = "${azurerm_mariadb_server.example.resource_group_name}" +} +output "mariadb_server_id" { + value = "${data.azurerm_mariadb_server.example.id}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the MariaDB Server to retrieve information about. + +* `resource_group_name` - (Required) The name of the resource group where the MariaDB Server exists. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the MariaDB Server. + +* `fqdn` - The FQDN of the MariaDB Server. + +* `location` - The Azure location where the resource exists. + +* `sku_name` - The SKU Name for this MariaDB Server. + +* `storage_profile` - A `storage_profile` block as defined below. + +* `administrator_login` - The Administrator Login for the MariaDB Server. + +* `administrator_login_password` - The password associated with the `administrator_login` for the MariaDB Server. + +* `version` - The version of MariaDB being used. + +* `ssl_enforcement` - The SSL being enforced on connections. + +* `tags` - A mapping of tags assigned to the resource. +--- + +A `storage_profile` block exports the following: + +* `storage_mb` - The max storage allowed for a server. + +* `backup_retention_days` - Backup retention days for the server. + +* `geo_redundant_backup` - Whether Geo-redundant is enabled or not for server backup. + +* `auto_grow` - Whether autogrow is enabled or disabled for the storage.