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

Update azurerm_mysql_server - Add identity support #8059

Merged
merged 7 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
69 changes: 69 additions & 0 deletions azurerm/internal/services/mysql/mysql_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@ func resourceArmMySqlServer() *schema.Resource {
}, false),
},

"identity": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(mysql.SystemAssigned),
}, false),
},

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

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

"ssl_enforcement": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -491,6 +518,7 @@ func resourceArmMySqlServerCreate(d *schema.ResourceData, meta interface{}) erro
}

server := mysql.ServerForCreate{
Identity: expandServerIdentity(d.Get("identity").([]interface{})),
Location: &location,
Properties: props,
Sku: sku,
Expand Down Expand Up @@ -567,6 +595,7 @@ func resourceArmMySqlServerUpdate(d *schema.ResourceData, meta interface{}) erro
storageProfile := expandMySQLStorageProfile(d)

properties := mysql.ServerUpdateParameters{
Identity: expandServerIdentity(d.Get("identity").([]interface{})),
ServerUpdateParametersProperties: &mysql.ServerUpdateParametersProperties{
AdministratorLoginPassword: utils.String(d.Get("administrator_login_password").(string)),
PublicNetworkAccess: publicAccess,
Expand Down Expand Up @@ -651,6 +680,10 @@ func resourceArmMySqlServerRead(d *schema.ResourceData, meta interface{}) error
tier = sku.Tier
}

if err := d.Set("identity", flattenServerIdentity(resp.Identity)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

if props := resp.ServerProperties; props != nil {
d.Set("administrator_login", props.AdministratorLogin)
d.Set("infrastructure_encryption_enabled", props.InfrastructureEncryption == mysql.InfrastructureEncryptionEnabled)
Expand Down Expand Up @@ -892,3 +925,39 @@ func flattenSecurityAlertPolicy(props *mysql.SecurityAlertPolicyProperties, acce

return []interface{}{block}
}

func expandServerIdentity(input []interface{}) *mysql.ResourceIdentity {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})

return &mysql.ResourceIdentity{
Type: mysql.IdentityType(v["type"].(string)),
}
}

func flattenServerIdentity(input *mysql.ResourceIdentity) []interface{} {
if input == nil {
return []interface{}{}
}

principalID := ""
if input.PrincipalID != nil {
principalID = input.PrincipalID.String()
}

tenantID := ""
if input.TenantID != nil {
tenantID = input.TenantID.String()
}

return []interface{}{
map[string]interface{}{
"type": string(input.Type),
"principal_id": principalID,
"tenant_id": tenantID,
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ func TestAccAzureRMMySQLServer_basicFiveSix(t *testing.T) {
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMMySQLServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMMySQLServer_basicWithIdentity(data, "5.6"),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMySQLServerExists(data.ResourceName),
),
},
data.ImportStep("administrator_login_password"), // not returned as sensitive
},
})
}

func TestAccAzureRMMySQLServer_basicFiveSixDeprecated(t *testing.T) { // remove in v3.0
data := acceptance.BuildTestData(t, "azurerm_mysql_server", "test")

Expand Down Expand Up @@ -451,6 +470,36 @@ resource "azurerm_mysql_server" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, version)
}

func testAccAzureRMMySQLServer_basicWithIdentity(data acceptance.TestData, version string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_mysql_server" "test" {
name = "acctestmysqlsvr-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_name = "GP_Gen5_2"
administrator_login = "acctestun"
administrator_login_password = "H@Sh1CoR3!"
ssl_enforcement_enabled = true
ssl_minimal_tls_version_enforced = "TLS1_1"
storage_mb = 51200
version = "%s"

identity {
type = "SystemAssigned"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, version)
}

func testAccAzureRMMySQLServer_complete(data acceptance.TestData, version string) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
20 changes: 16 additions & 4 deletions website/docs/r/mysql_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ Manages a MySQL Server.
## Example Usage

```hcl
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
Expand Down Expand Up @@ -73,6 +69,8 @@ The following arguments are supported:

* `geo_redundant_backup_enabled` - (Optional) Turn Geo-redundant server backups on/off. This allows you to choose between locally redundant or geo-redundant backup storage in the General Purpose and Memory Optimized tiers. When the backups are stored in geo-redundant backup storage, they are not only stored within the region in which your server is hosted, but are also replicated to a paired data center. This provides better protection and ability to restore your server in a different region in the event of a disaster. This is not supported for the Basic tier.

* `identity` - (Optional) An `identity` block as defined below.

* `infrastructure_encryption_enabled` - (Optional) Whether or not infrastructure is encrypted for this server. Defaults to `false`. Changing this forces a new resource to be created.

* `public_network_access_enabled` - (Optional) Whether or not public network access is allowed for this server. Defaults to `true`.
Expand All @@ -91,6 +89,12 @@ The following arguments are supported:

---

A `identity` block supports the following:

* `type` - (Required) The Type of Identity which should be used for this MySQL Server. At this time the only possible value is `SystemAssigned`.

---

a `threat_detection_policy` block supports the following:

* `enabled` - (Required) Is the policy enabled?
Expand All @@ -116,6 +120,14 @@ The following attributes are exported:

* `fqdn` - The FQDN of the MySQL Server.

---

A `identity` block exports the following:

* `principal_id` - The Client ID of the Service Principal assigned to this MySQL Server.

* `tenant_id` - The ID of the Tenant the Service Principal is assigned in.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:
Expand Down