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

mssql - upgrade to 2023-08-01-preview and hashicorp/go-azure-sdk #27073

Merged
merged 9 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
191 changes: 119 additions & 72 deletions internal/services/mssql/client/client.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/serverazureadonlyauthentications"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/serverazureadonlyauthentications"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/services/mssql/helper/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/databases"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/databases"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/parse"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down
42 changes: 12 additions & 30 deletions internal/services/mssql/helper/sql_retention_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/backupshorttermretentionpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/longtermretentionpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/backupshorttermretentionpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/longtermretentionpolicies"
"github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -62,12 +62,6 @@ func LongTermRetentionPolicySchema() *pluginsdk.Schema {
ValidateFunc: validation.IntBetween(0, 52),
AtLeastOneOf: atLeastOneOf,
},

"immutable_backups_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
},
},
}
Expand Down Expand Up @@ -108,19 +102,18 @@ func ShortTermRetentionPolicySchema() *pluginsdk.Schema {
}
}

func ExpandLongTermRetentionPolicy(input []interface{}) *longtermretentionpolicies.LongTermRetentionPolicyProperties {
func ExpandLongTermRetentionPolicy(input []interface{}) *longtermretentionpolicies.BaseLongTermRetentionPolicyProperties {
if len(input) == 0 || input[0] == nil {
return nil
}

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

output := longtermretentionpolicies.LongTermRetentionPolicyProperties{
WeeklyRetention: pointer.To("PT0S"),
MonthlyRetention: pointer.To("PT0S"),
YearlyRetention: pointer.To("PT0S"),
WeekOfYear: pointer.To(int64(1)),
MakeBackupsImmutable: pointer.To(false),
output := longtermretentionpolicies.BaseLongTermRetentionPolicyProperties{
WeeklyRetention: pointer.To("PT0S"),
MonthlyRetention: pointer.To("PT0S"),
YearlyRetention: pointer.To("PT0S"),
WeekOfYear: pointer.To(int64(1)),
}

if v, ok := policy["weekly_retention"].(string); ok && v != "" {
Expand All @@ -138,11 +131,6 @@ func ExpandLongTermRetentionPolicy(input []interface{}) *longtermretentionpolici
if v, ok := policy["week_of_year"].(int); ok && v != 0 {
output.WeekOfYear = pointer.To(int64(v))
}

if v, ok := policy["immutable_backups_enabled"].(bool); ok {
output.MakeBackupsImmutable = pointer.To(v)
}

return pointer.To(output)
}

Expand Down Expand Up @@ -171,18 +159,12 @@ func FlattenLongTermRetentionPolicy(input *longtermretentionpolicies.LongTermRet
yearlyRetention = *input.Properties.YearlyRetention
}

immutableBackupsEnabled := false
if input.Properties.MakeBackupsImmutable != nil {
immutableBackupsEnabled = *input.Properties.MakeBackupsImmutable
}

return []interface{}{
map[string]interface{}{
"monthly_retention": monthlyRetention,
"weekly_retention": weeklyRetention,
"week_of_year": weekOfYear,
"yearly_retention": yearlyRetention,
"immutable_backups_enabled": immutableBackupsEnabled,
"monthly_retention": monthlyRetention,
"weekly_retention": weeklyRetention,
"week_of_year": weekOfYear,
"yearly_retention": yearlyRetention,
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/services/mssql/mssql_database_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/databases"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/transparentdataencryptions"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/databases"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/transparentdataencryptions"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v5.0/sql" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/blobauditing"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/parse"
Expand Down Expand Up @@ -89,7 +90,7 @@ func resourceMsSqlDatabaseExtendedAuditingPolicy() *pluginsdk.Resource {
}

func resourceMsSqlDatabaseExtendedAuditingPolicyCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.DatabaseExtendedBlobAuditingPoliciesClient
client := meta.(*clients.Client).MSSQL.BlobAuditingPoliciesClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand All @@ -101,53 +102,53 @@ func resourceMsSqlDatabaseExtendedAuditingPolicyCreateUpdate(d *pluginsdk.Resour
}

if d.IsNewResource() {
existing, err := client.Get(ctx, dbId.ResourceGroupName, dbId.ServerName, dbId.DatabaseName)
existing, err := client.ExtendedDatabaseBlobAuditingPoliciesGet(ctx, *dbId)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for the presence of existing %s: %+v", dbId, err)
}
}

// if state is not disabled, we should import it.
if existing.ID != nil && *existing.ID != "" && existing.ExtendedDatabaseBlobAuditingPolicyProperties != nil && existing.ExtendedDatabaseBlobAuditingPolicyProperties.State != sql.BlobAuditingPolicyStateDisabled {
return tf.ImportAsExistsError("azurerm_mssql_database_extended_auditing_policy", *existing.ID)
if existing.Model != nil && existing.Model.Id != nil && *existing.Model.Id != "" && existing.Model.Properties != nil && existing.Model.Properties.State != blobauditing.BlobAuditingPolicyStateDisabled {
return tf.ImportAsExistsError("azurerm_mssql_database_extended_auditing_policy", *existing.Model.Id)
}
}

params := sql.ExtendedDatabaseBlobAuditingPolicy{
ExtendedDatabaseBlobAuditingPolicyProperties: &sql.ExtendedDatabaseBlobAuditingPolicyProperties{
params := blobauditing.ExtendedDatabaseBlobAuditingPolicy{
Properties: &blobauditing.ExtendedDatabaseBlobAuditingPolicyProperties{
StorageEndpoint: utils.String(d.Get("storage_endpoint").(string)),
IsStorageSecondaryKeyInUse: utils.Bool(d.Get("storage_account_access_key_is_secondary").(bool)),
RetentionDays: utils.Int32(int32(d.Get("retention_in_days").(int))),
RetentionDays: utils.Int64(int64(d.Get("retention_in_days").(int))),
IsAzureMonitorTargetEnabled: utils.Bool(d.Get("log_monitoring_enabled").(bool)),
},
}

if d.Get("enabled").(bool) {
params.ExtendedDatabaseBlobAuditingPolicyProperties.State = sql.BlobAuditingPolicyStateEnabled
params.Properties.State = blobauditing.BlobAuditingPolicyStateEnabled
} else {
params.ExtendedDatabaseBlobAuditingPolicyProperties.State = sql.BlobAuditingPolicyStateDisabled
params.Properties.State = blobauditing.BlobAuditingPolicyStateDisabled
}

if v, ok := d.GetOk("storage_account_access_key"); ok {
params.ExtendedDatabaseBlobAuditingPolicyProperties.StorageAccountAccessKey = utils.String(v.(string))
params.Properties.StorageAccountAccessKey = utils.String(v.(string))
}

if _, err = client.CreateOrUpdate(ctx, dbId.ResourceGroupName, dbId.ServerName, dbId.DatabaseName, params); err != nil {
if _, err = client.ExtendedDatabaseBlobAuditingPoliciesCreateOrUpdate(ctx, *dbId, params); err != nil {
return fmt.Errorf("creating extended auditing policy for %s: %+v", dbId, err)
}

read, err := client.Get(ctx, dbId.ResourceGroupName, dbId.ServerName, dbId.DatabaseName)
read, err := client.ExtendedDatabaseBlobAuditingPoliciesGet(ctx, *dbId)
if err != nil {
return fmt.Errorf("retrieving the extended auditing policy for %s: %+v", dbId, err)
}

if read.ID == nil || pointer.From(read.ID) == "" {
if read.Model == nil || read.Model.Id == nil || pointer.From(read.Model.Id) == "" {
return fmt.Errorf("the extended auditing policy ID for %s is 'nil' or 'empty'", dbId.String())
}

// TODO: update this to use the Database ID - requiring a State Migration
readId, err := parse.DatabaseExtendedAuditingPolicyID(pointer.From(read.ID))
readId, err := parse.DatabaseExtendedAuditingPolicyID(pointer.From(read.Model.Id))
if err != nil {
return err
}
Expand All @@ -158,7 +159,7 @@ func resourceMsSqlDatabaseExtendedAuditingPolicyCreateUpdate(d *pluginsdk.Resour
}

func resourceMsSqlDatabaseExtendedAuditingPolicyRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.DatabaseExtendedBlobAuditingPoliciesClient
client := meta.(*clients.Client).MSSQL.BlobAuditingPoliciesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand All @@ -167,9 +168,11 @@ func resourceMsSqlDatabaseExtendedAuditingPolicyRead(d *pluginsdk.ResourceData,
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.DatabaseName)
dbId := commonids.NewSqlDatabaseID(id.SubscriptionId, id.ResourceGroup, id.ServerName, id.DatabaseName)

resp, err := client.ExtendedDatabaseBlobAuditingPoliciesGet(ctx, dbId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}
Expand All @@ -179,19 +182,21 @@ func resourceMsSqlDatabaseExtendedAuditingPolicyRead(d *pluginsdk.ResourceData,
databaseId := commonids.NewSqlDatabaseID(id.SubscriptionId, id.ResourceGroup, id.ServerName, id.DatabaseName)
d.Set("database_id", databaseId.ID())

if props := resp.ExtendedDatabaseBlobAuditingPolicyProperties; props != nil {
d.Set("storage_endpoint", props.StorageEndpoint)
d.Set("storage_account_access_key_is_secondary", props.IsStorageSecondaryKeyInUse)
d.Set("retention_in_days", props.RetentionDays)
d.Set("log_monitoring_enabled", props.IsAzureMonitorTargetEnabled)
d.Set("enabled", props.State == sql.BlobAuditingPolicyStateEnabled)
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
d.Set("storage_endpoint", props.StorageEndpoint)
d.Set("storage_account_access_key_is_secondary", props.IsStorageSecondaryKeyInUse)
d.Set("retention_in_days", props.RetentionDays)
d.Set("log_monitoring_enabled", props.IsAzureMonitorTargetEnabled)
d.Set("enabled", props.State == blobauditing.BlobAuditingPolicyStateEnabled)
}
}

return nil
}

func resourceMsSqlDatabaseExtendedAuditingPolicyDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.DatabaseExtendedBlobAuditingPoliciesClient
client := meta.(*clients.Client).MSSQL.BlobAuditingPoliciesClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand All @@ -200,14 +205,16 @@ func resourceMsSqlDatabaseExtendedAuditingPolicyDelete(d *pluginsdk.ResourceData
return err
}

params := sql.ExtendedDatabaseBlobAuditingPolicy{
ExtendedDatabaseBlobAuditingPolicyProperties: &sql.ExtendedDatabaseBlobAuditingPolicyProperties{
State: sql.BlobAuditingPolicyStateDisabled,
dbId := commonids.NewSqlDatabaseID(id.SubscriptionId, id.ResourceGroup, id.ServerName, id.DatabaseName)

params := blobauditing.ExtendedDatabaseBlobAuditingPolicy{
Properties: &blobauditing.ExtendedDatabaseBlobAuditingPolicyProperties{
State: blobauditing.BlobAuditingPolicyStateDisabled,
},
}

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ServerName, id.DatabaseName, params); err != nil {
return fmt.Errorf("deleting MsSql Database %q Extended Auditing Policy( MsSql Server %q / Resource Group %q): %+v", id.DatabaseName, id.ServerName, id.ResourceGroup, err)
if _, err = client.ExtendedDatabaseBlobAuditingPoliciesCreateOrUpdate(ctx, dbId, params); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type MsSqlDatabaseExtendedAuditingPolicyResource struct{}
Expand Down Expand Up @@ -200,16 +202,18 @@ func (MsSqlDatabaseExtendedAuditingPolicyResource) Exists(ctx context.Context, c
return nil, err
}

resp, err := client.MSSQL.DatabaseExtendedBlobAuditingPoliciesClient.Get(ctx, id.ResourceGroup, id.ServerName, id.DatabaseName)
dbId := commonids.NewSqlDatabaseID(id.SubscriptionId, id.ResourceGroup, id.ServerName, id.DatabaseName)

resp, err := client.MSSQL.BlobAuditingPoliciesClient.ExtendedDatabaseBlobAuditingPoliciesGet(ctx, dbId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil, fmt.Errorf("SQL Virtual Machine %q (Server %q, Resource Group %q) does not exist", id.DatabaseName, id.ServerName, id.ResourceGroup)
if response.WasNotFound(resp.HttpResponse) {
return nil, fmt.Errorf("%s does not exist", *id)
}

return nil, fmt.Errorf("reading SQL Database ExtendedAuditingPolicy %q (Server %q, Resource Group %q): %v", id.DatabaseName, id.ServerName, id.ResourceGroup, err)
return nil, fmt.Errorf("reading %s: %v", *id, err)
}

return utils.Bool(resp.ID != nil), nil
return pointer.To(resp.Model != nil), nil
}

func (MsSqlDatabaseExtendedAuditingPolicyResource) template(data acceptance.TestData) string {
Expand Down
18 changes: 9 additions & 9 deletions internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/maintenance/2023-04-01/publicmaintenanceconfigurations"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/backupshorttermretentionpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/databases"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/databasesecurityalertpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/elasticpools"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/geobackuppolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/longtermretentionpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/servers"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/serversecurityalertpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/transparentdataencryptions"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/backupshorttermretentionpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/databases"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/databasesecurityalertpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/elasticpools"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/geobackuppolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/longtermretentionpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/servers"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/serversecurityalertpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/transparentdataencryptions"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
2 changes: 1 addition & 1 deletion internal/services/mssql/mssql_database_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/databases"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/databases"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Loading
Loading