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
142 changes: 142 additions & 0 deletions azurerm/internal/services/mariadb/data_source_mariadb_server.go
Original file line number Diff line number Diff line change
@@ -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)
}
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": dataSourceMariaDbServer(),
}
}

// SupportedResources returns the supported Resources supported by this Service
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
66 changes: 66 additions & 0 deletions website/docs/d/mariadb_server.html.markdown
Original file line number Diff line number Diff line change
@@ -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.