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

New data source azurerm_mssql_server and new block restorable_dropped_databases in azuerrm_mssql_server #7917

Merged
merged 13 commits into from Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions azurerm/internal/services/mssql/client/client.go
Expand Up @@ -12,6 +12,7 @@ type Client struct {
DatabaseThreatDetectionPoliciesClient *sql.DatabaseThreatDetectionPoliciesClient
ElasticPoolsClient *sql.ElasticPoolsClient
DatabaseVulnerabilityAssessmentRuleBaselinesClient *sql.DatabaseVulnerabilityAssessmentRuleBaselinesClient
RestorableDroppedDatabasesClient *sql.RestorableDroppedDatabasesClient
ServerAzureADAdministratorsClient *sql.ServerAzureADAdministratorsClient
ServersClient *sql.ServersClient
ServerExtendedBlobAuditingPoliciesClient *sql.ExtendedServerBlobAuditingPoliciesClient
Expand All @@ -37,6 +38,9 @@ func NewClient(o *common.ClientOptions) *Client {
elasticPoolsClient := sql.NewElasticPoolsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&elasticPoolsClient.Client, o.ResourceManagerAuthorizer)

restorableDroppedDatabasesClient := sql.NewRestorableDroppedDatabasesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&restorableDroppedDatabasesClient.Client, o.ResourceManagerAuthorizer)

serverSecurityAlertPoliciesClient := sql.NewServerSecurityAlertPoliciesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&serverSecurityAlertPoliciesClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -64,6 +68,7 @@ func NewClient(o *common.ClientOptions) *Client {
DatabaseThreatDetectionPoliciesClient: &databaseThreatDetectionPoliciesClient,
DatabaseVulnerabilityAssessmentRuleBaselinesClient: &databaseVulnerabilityAssessmentRuleBaselinesClient,
ElasticPoolsClient: &elasticPoolsClient,
RestorableDroppedDatabasesClient: &restorableDroppedDatabasesClient,
ServerAzureADAdministratorsClient: &serverAzureADAdministratorsClient,
ServersClient: &serversClient,
ServerExtendedBlobAuditingPoliciesClient: &serverExtendedBlobAuditingPoliciesClient,
Expand Down
29 changes: 27 additions & 2 deletions azurerm/internal/services/mssql/mssql_database_resource.go
Expand Up @@ -67,7 +67,6 @@ func resourceArmMsSqlDatabase() *schema.Resource {
ValidateFunc: validate.MsSqlDatabaseAutoPauseDelay,
},

// recovery is not support in version 2017-10-01-preview
"create_mode": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -79,6 +78,7 @@ func resourceArmMsSqlDatabase() *schema.Resource {
string(sql.CreateModeOnlineSecondary),
string(sql.CreateModePointInTimeRestore),
string(sql.CreateModeRestore),
string(sql.CreateModeRecovery),
string(sql.CreateModeRestoreExternalBackup),
string(sql.CreateModeRestoreExternalBackupSecondary),
string(sql.CreateModeRestoreLongTermRetentionBackup),
Expand Down Expand Up @@ -134,6 +134,16 @@ func resourceArmMsSqlDatabase() *schema.Resource {
ValidateFunc: validation.IsRFC3339Time,
},

"recoverable_database_id": {
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
},

"restorable_dropped_database_id": {
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
},

"read_replica_count": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -339,9 +349,16 @@ func resourceArmMsSqlDatabaseCreateUpdate(d *schema.ResourceData, meta interface
}

createMode, ok := d.GetOk("create_mode")
if _, dbok := d.GetOk("creation_source_database_id"); ok && (createMode.(string) == string(sql.CreateModeCopy) || createMode.(string) == string(sql.CreateModePointInTimeRestore) || createMode.(string) == string(sql.CreateModeRestore) || createMode.(string) == string(sql.CreateModeSecondary)) && !dbok {
if _, dbok := d.GetOk("creation_source_database_id"); ok && (createMode.(string) == string(sql.CreateModeCopy) || createMode.(string) == string(sql.CreateModePointInTimeRestore) || createMode.(string) == string(sql.CreateModeSecondary)) && !dbok {
return fmt.Errorf("'creation_source_database_id' is required for create_mode %s", createMode.(string))
}
if _, dbok := d.GetOk("recoverable_database_id"); ok && createMode.(string) == string(sql.CreateModeRecovery) && !dbok {
return fmt.Errorf("'recoverable_database_id' is required for create_mode %s", createMode.(string))
}
if _, dbok := d.GetOk("restorable_dropped_database_id"); ok && createMode.(string) == string(sql.CreateModeRestore) && !dbok {
return fmt.Errorf("'restorable_dropped_database_id' is required for create_mode %s", createMode.(string))
}

params.DatabaseProperties.CreateMode = sql.CreateMode(createMode.(string))

auditingPolicies := d.Get("extended_auditing_policy").([]interface{})
Expand Down Expand Up @@ -377,6 +394,14 @@ func resourceArmMsSqlDatabaseCreateUpdate(d *schema.ResourceData, meta interface
params.DatabaseProperties.SourceDatabaseID = utils.String(v.(string))
}

if v, ok := d.GetOk("recoverable_database_id"); ok {
params.DatabaseProperties.RecoverableDatabaseID = utils.String(v.(string))
}

if v, ok := d.GetOk("restorable_dropped_database_id"); ok {
params.DatabaseProperties.RestorableDroppedDatabaseID = utils.String(v.(string))
}

future, err := client.CreateOrUpdate(ctx, serverId.ResourceGroup, serverId.Name, name, params)
if err != nil {
return fmt.Errorf("creating MsSql Database %q (Sql Server %q / Resource Group %q): %+v", name, serverId.Name, serverId.ResourceGroup, err)
Expand Down
126 changes: 126 additions & 0 deletions azurerm/internal/services/mssql/mssql_server_data_source.go
@@ -0,0 +1,126 @@
package mssql

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"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/location"
"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 dataSourceMsSqlServer() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmMsSqlServerRead,

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

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

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

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

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

"identity": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"restorable_dropped_databases": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

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

func dataSourceArmMsSqlServerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.ServersClient
restorableDroppedDatabasesClient := meta.(*clients.Client).MSSQL.RestorableDroppedDatabasesClient
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("sql Server %q was not found in Resource Group %q", name, resourceGroup)
}

return fmt.Errorf("retrieving Sql Server %q (Resource Group %q): %s", name, resourceGroup, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("reading Ms Sql Server %q (Resource Group %q) ID is empty or nil", name, resourceGroup)
}
d.SetId(*resp.ID)
d.Set("location", location.NormalizeNilable(resp.Location))

if props := resp.ServerProperties; props != nil {
d.Set("version", props.Version)
d.Set("administrator_login", props.AdministratorLogin)
d.Set("fully_qualified_domain_name", props.FullyQualifiedDomainName)
}

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

restorableResp, err := restorableDroppedDatabasesClient.ListByServer(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("listing SQL Server %s Restorable Dropped Databases: %v", name, err)
}
if err := d.Set("restorable_dropped_databases", flattenAzureRmSqlServerRestorableDatabases(restorableResp)); err != nil {
return fmt.Errorf("setting `restorable_dropped_databases`: %+v", err)
}

return tags.FlattenAndSet(d, resp.Tags)
}
32 changes: 32 additions & 0 deletions azurerm/internal/services/mssql/mssql_server_resource.go
Expand Up @@ -150,6 +150,14 @@ func resourceArmMsSqlServer() *schema.Resource {
Computed: true,
},

"restorable_dropped_databases": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -281,6 +289,7 @@ func resourceArmMsSqlServerRead(d *schema.ResourceData, meta interface{}) error
auditingClient := meta.(*clients.Client).MSSQL.ServerExtendedBlobAuditingPoliciesClient
connectionClient := meta.(*clients.Client).MSSQL.ServerConnectionPoliciesClient
adminClient := meta.(*clients.Client).MSSQL.ServerAzureADAdministratorsClient
restorableDroppedDatabasesClient := meta.(*clients.Client).MSSQL.RestorableDroppedDatabasesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down Expand Up @@ -349,6 +358,14 @@ func resourceArmMsSqlServerRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error setting `extended_auditing_policy`: %+v", err)
}

restorableResp, err := restorableDroppedDatabasesClient.ListByServer(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("listing SQL Server %s Restorable Dropped Databases: %v", name, err)
}
if err := d.Set("restorable_dropped_databases", flattenAzureRmSqlServerRestorableDatabases(restorableResp)); err != nil {
return fmt.Errorf("setting `restorable_dropped_databases`: %+v", err)
}

return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down Expand Up @@ -446,3 +463,18 @@ func flatternAzureRmMsSqlServerAdministrator(admin sql.ServerAzureADAdministrato
},
}
}

func flattenAzureRmSqlServerRestorableDatabases(resp sql.RestorableDroppedDatabaseListResult) []string {
if resp.Value == nil || len(*resp.Value) == 0 {
return []string{}
}
res := make([]string, 0)
for _, r := range *resp.Value {
var id string
if r.ID != nil {
id = *r.ID
}
res = append(res, id)
}
return res
}
1 change: 1 addition & 0 deletions azurerm/internal/services/mssql/registration.go
Expand Up @@ -23,6 +23,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_mssql_database": dataSourceArmMsSqlDatabase(),
"azurerm_mssql_elasticpool": dataSourceArmMsSqlElasticpool(),
"azurerm_mssql_server": dataSourceMsSqlServer(),
}
}

Expand Down