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

azurerm_mssql_database - add support for secondary_type #25360

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 17 additions & 1 deletion internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ func resourceMsSqlDatabaseCreate(d *pluginsdk.ResourceData, meta interface{}) er
ZoneRedundant: pointer.To(d.Get("zone_redundant").(bool)),
IsLedgerOn: pointer.To(ledgerEnabled),
EncryptionProtectorAutoRotation: pointer.To(d.Get("transparent_data_encryption_key_automatic_rotation_enabled").(bool)),
SecondaryType: pointer.To(databases.SecondaryType(d.Get("secondary_type").(string))),
},

Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
Expand Down Expand Up @@ -951,7 +952,6 @@ func resourceMsSqlDatabaseUpdate(d *pluginsdk.ResourceData, meta interface{}) er
return nil
}
}

}

if d.HasChange("import") {
Expand Down Expand Up @@ -1091,12 +1091,19 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro
requestedBackupStorageRedundancy = string(*props.RequestedBackupStorageRedundancy)
}

// A named replica doesn't return props.RequestedBackupStorageRedundancy from the api but it is Geo in the portal regardless of what the parent database has
// so we'll copy that here to get around a perpetual diff
if props.SecondaryType != nil && *props.SecondaryType == "Named" {
requestedBackupStorageRedundancy = string(databases.BackupStorageRedundancyGeo)
}

d.Set("auto_pause_delay_in_minutes", pointer.From(props.AutoPauseDelay))
d.Set("collation", pointer.From(props.Collation))
d.Set("read_replica_count", pointer.From(props.HighAvailabilityReplicaCount))
d.Set("storage_account_type", requestedBackupStorageRedundancy)
d.Set("zone_redundant", pointer.From(props.ZoneRedundant))
d.Set("read_scale", pointer.From(props.ReadScale) == databases.DatabaseReadScaleEnabled)
d.Set("secondary_type", pointer.From(props.SecondaryType))

if props.ElasticPoolId != nil {
elasticPoolId = pointer.From(props.ElasticPoolId)
Expand Down Expand Up @@ -1729,6 +1736,15 @@ func resourceMsSqlDatabaseSchema() map[string]*pluginsdk.Schema {
RequiredWith: []string{"transparent_data_encryption_key_vault_key_id"},
},

"secondary_type": {
Type: pluginsdk.TypeString,
Optional: true,
// This must be Computed as it has defaulted to Geo for replicas but not all databases are replicas.
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(databases.PossibleValuesForSecondaryType(), false),
},

"tags": commonschema.Tags(),
}
}
38 changes: 38 additions & 0 deletions internal/services/mssql/mssql_database_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,30 @@ func TestAccMsSqlDatabase_transparentDataEncryptionKey(t *testing.T) {
})
}

func TestAccMsSqlDatabase_namedReplication(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_database", "test")
r := MsSqlDatabaseResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.hs(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enclave_type").IsEmpty(),
),
},
data.ImportStep(),
{
Config: r.namedReplication(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enclave_type").IsEmpty(),
),
},
data.ImportStep(),
})
}

func (MsSqlDatabaseResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := commonids.ParseSqlDatabaseID(state.ID)
if err != nil {
Expand Down Expand Up @@ -2079,3 +2103,17 @@ resource "azurerm_mssql_database" "test" {
}
`, r.template(data), data.RandomInteger, data.RandomString)
}

func (r MsSqlDatabaseResource) namedReplication(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_mssql_database" "secondary" {
name = "acctest-dbs2-%[2]d"
server_id = azurerm_mssql_server.test.id
create_mode = "Secondary"
secondary_type = "Named"
creation_source_database_id = azurerm_mssql_database.test.id
}
`, r.hs(data), data.RandomInteger)
}
2 changes: 2 additions & 0 deletions website/docs/r/mssql_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ The following arguments are supported:

* `zone_redundant` - (Optional) Whether or not this database is zone redundant, which means the replicas of this database will be spread across multiple availability zones. This property is only settable for Premium and Business Critical databases.

* `secondary_type` - (Optional) How do you want your replica to be made? Valid values include `Geo` and `Named`. Defaults to `Geo`. Changing this forces a new resource to be created.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand Down
Loading