From f4fbc395ff6182226f21f1f1654c536eddfc68e8 Mon Sep 17 00:00:00 2001 From: Aris van Ommeren Date: Wed, 10 Nov 2021 09:43:53 +0100 Subject: [PATCH 1/3] Update `sql/managed_instance` to `sql/mgmt/v5.0` --- internal/services/sql/client/client.go | 14 +++++++------- .../sql/sql_managed_instance_resource.go | 16 ++++------------ .../sql/sql_managed_instance_resource_test.go | 10 +++++++++- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/internal/services/sql/client/client.go b/internal/services/sql/client/client.go index 9c8d0617b1c2..7287d78eb66b 100644 --- a/internal/services/sql/client/client.go +++ b/internal/services/sql/client/client.go @@ -3,7 +3,7 @@ package client import ( "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2017-03-01-preview/sql" msi "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2018-06-01-preview/sql" - aadAdmin "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v5.0/sql" + sqlv5 "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v5.0/sql" "github.com/hashicorp/terraform-provider-azurerm/internal/common" ) @@ -14,13 +14,13 @@ type Client struct { DatabaseExtendedBlobAuditingPoliciesClient *sql.ExtendedDatabaseBlobAuditingPoliciesClient FirewallRulesClient *sql.FirewallRulesClient FailoverGroupsClient *sql.FailoverGroupsClient - ManagedInstancesClient *msi.ManagedInstancesClient + ManagedInstancesClient *sqlv5.ManagedInstancesClient ManagedDatabasesClient *msi.ManagedDatabasesClient ServersClient *sql.ServersClient ServerExtendedBlobAuditingPoliciesClient *sql.ExtendedServerBlobAuditingPoliciesClient ServerConnectionPoliciesClient *sql.ServerConnectionPoliciesClient - ServerAzureADAdministratorsClient *aadAdmin.ServerAzureADAdministratorsClient - ServerAzureADOnlyAuthenticationsClient *aadAdmin.ServerAzureADOnlyAuthenticationsClient + ServerAzureADAdministratorsClient *sqlv5.ServerAzureADAdministratorsClient + ServerAzureADOnlyAuthenticationsClient *sqlv5.ServerAzureADOnlyAuthenticationsClient ServerSecurityAlertPoliciesClient *sql.ServerSecurityAlertPoliciesClient VirtualNetworkRulesClient *sql.VirtualNetworkRulesClient } @@ -45,7 +45,7 @@ func NewClient(o *common.ClientOptions) *Client { firewallRulesClient := sql.NewFirewallRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&firewallRulesClient.Client, o.ResourceManagerAuthorizer) - managedInstancesClient := msi.NewManagedInstancesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + managedInstancesClient := sqlv5.NewManagedInstancesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&managedInstancesClient.Client, o.ResourceManagerAuthorizer) managedDatabasesClient := msi.NewManagedDatabasesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) @@ -57,10 +57,10 @@ func NewClient(o *common.ClientOptions) *Client { serverConnectionPoliciesClient := sql.NewServerConnectionPoliciesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&serverConnectionPoliciesClient.Client, o.ResourceManagerAuthorizer) - serverAzureADAdministratorsClient := aadAdmin.NewServerAzureADAdministratorsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + serverAzureADAdministratorsClient := sqlv5.NewServerAzureADAdministratorsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&serverAzureADAdministratorsClient.Client, o.ResourceManagerAuthorizer) - serverAzureADOnlyAuthenticationsClient := aadAdmin.NewServerAzureADOnlyAuthenticationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + serverAzureADOnlyAuthenticationsClient := sqlv5.NewServerAzureADOnlyAuthenticationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&serverAzureADOnlyAuthenticationsClient.Client, o.ResourceManagerAuthorizer) virtualNetworkRulesClient := sql.NewVirtualNetworkRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) diff --git a/internal/services/sql/sql_managed_instance_resource.go b/internal/services/sql/sql_managed_instance_resource.go index 6acfb90ae2ab..be20d028b02e 100644 --- a/internal/services/sql/sql_managed_instance_resource.go +++ b/internal/services/sql/sql_managed_instance_resource.go @@ -7,7 +7,8 @@ import ( "strings" "time" - "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2018-06-01-preview/sql" + "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v5.0/sql" + "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" @@ -182,11 +183,6 @@ func resourceArmSqlMiServer() *schema.Resource { pluginsdk.ForceNewIfChange("dns_zone_partner_id", func(ctx context.Context, old, new, _ interface{}) bool { return old.(string) == "" && new.(string) != "" }), - - // identity.0.type can be set to SystemAssigned, but not removed or set to None in this SDK version - pluginsdk.ForceNewIfChange("identity.0.type", func(ctx context.Context, old, new, _ interface{}) bool { - return old.(string) == "SystemAssigned" && new.(string) != "SystemAssigned" - }), ), } } @@ -202,7 +198,7 @@ func resourceArmSqlMiServerCreateUpdate(d *schema.ResourceData, meta interface{} id := parse.NewManagedInstanceID(subscriptionId, resGroup, name) if d.IsNewResource() { - existing, err := client.Get(ctx, id.ResourceGroup, id.Name) + existing, err := client.Get(ctx, id.ResourceGroup, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { return fmt.Errorf("checking for presence of existing Managed Instance %q: %s", id.ID(), err) @@ -273,7 +269,7 @@ func resourceArmSqlMiServerRead(d *schema.ResourceData, meta interface{}) error return err } - resp, err := client.Get(ctx, id.ResourceGroup, id.Name) + resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "") if err != nil { if utils.ResponseWasNotFound(resp.Response) { log.Printf("[INFO] Error reading SQL Managed Instance %q - removing from state", d.Id()) @@ -365,10 +361,6 @@ func expandManagedInstanceIdentity(input []interface{}) (*sql.ResourceIdentity, return nil, err } - if config.Type == identity.Type("None") { - return nil, nil - } - return &sql.ResourceIdentity{ Type: sql.IdentityType(config.Type), }, nil diff --git a/internal/services/sql/sql_managed_instance_resource_test.go b/internal/services/sql/sql_managed_instance_resource_test.go index ff31dc117d8f..a67bf8622784 100644 --- a/internal/services/sql/sql_managed_instance_resource_test.go +++ b/internal/services/sql/sql_managed_instance_resource_test.go @@ -53,6 +53,14 @@ func TestAccAzureRMSqlMiServer_identity(t *testing.T) { ), }, data.ImportStep("administrator_login_password"), + { + Config: r.basic(data), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("identity.#").HasValue("0"), + ), + }, + data.ImportStep("administrator_login_password"), }) } @@ -150,7 +158,7 @@ func (r SqlManagedInstanceResource) Exists(ctx context.Context, client *clients. return nil, err } - resp, err := client.Sql.ManagedInstancesClient.Get(ctx, id.ResourceGroup, id.Name) + resp, err := client.Sql.ManagedInstancesClient.Get(ctx, id.ResourceGroup, id.Name, "") if err != nil { if utils.ResponseWasNotFound(resp.Response) { return utils.Bool(false), nil From 2bda0c0f7421761206c5c7c534b49196a5195613 Mon Sep 17 00:00:00 2001 From: Aris van Ommeren Date: Wed, 10 Nov 2021 10:28:29 +0100 Subject: [PATCH 2/3] `azurerm_sql_managed_instance`: Support for `storage_account_type` --- .../sql/sql_managed_instance_resource.go | 14 ++++++ .../sql/sql_managed_instance_resource_test.go | 45 +++++++++++++++++++ .../docs/r/sql_managed_instance.html.markdown | 2 + 3 files changed, 61 insertions(+) diff --git a/internal/services/sql/sql_managed_instance_resource.go b/internal/services/sql/sql_managed_instance_resource.go index be20d028b02e..e9d14c1e9ec2 100644 --- a/internal/services/sql/sql_managed_instance_resource.go +++ b/internal/services/sql/sql_managed_instance_resource.go @@ -175,6 +175,18 @@ func resourceArmSqlMiServer() *schema.Resource { "identity": managedInstanceIdentity{}.Schema(), + "storage_account_type": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + Default: string(sql.StorageAccountTypeGRS), + ValidateFunc: validation.StringInSlice([]string{ + string(sql.StorageAccountTypeGRS), + string(sql.StorageAccountTypeLRS), + string(sql.StorageAccountTypeZRS), + }, false), + }, + "tags": tags.Schema(), }, @@ -232,6 +244,7 @@ func resourceArmSqlMiServerCreateUpdate(d *schema.ResourceData, meta interface{} ProxyOverride: sql.ManagedInstanceProxyOverride(d.Get("proxy_override").(string)), TimezoneID: utils.String(d.Get("timezone_id").(string)), DNSZonePartner: utils.String(d.Get("dns_zone_partner_id").(string)), + StorageAccountType: sql.StorageAccountType(d.Get("storage_account_type").(string)), }, } @@ -307,6 +320,7 @@ func resourceArmSqlMiServerRead(d *schema.ResourceData, meta interface{}) error d.Set("minimum_tls_version", props.MinimalTLSVersion) d.Set("proxy_override", props.ProxyOverride) d.Set("timezone_id", props.TimezoneID) + d.Set("storage_account_type", props.StorageAccountType) // This value is not returned from the api so we'll just set whatever is in the config d.Set("administrator_login_password", d.Get("administrator_login_password").(string)) } diff --git a/internal/services/sql/sql_managed_instance_resource_test.go b/internal/services/sql/sql_managed_instance_resource_test.go index a67bf8622784..d5a78acedd97 100644 --- a/internal/services/sql/sql_managed_instance_resource_test.go +++ b/internal/services/sql/sql_managed_instance_resource_test.go @@ -31,6 +31,21 @@ func TestAccAzureRMSqlMiServer_basic(t *testing.T) { }) } +func TestAccAzureRMSqlMiServer_backupRedundancyLRS(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_sql_managed_instance", "test") + r := SqlManagedInstanceResource{} + + data.ResourceTest(t, r, []resource.TestStep{ + { + Config: r.storageType(data, "LRS"), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("administrator_login_password"), + }) +} + func TestAccAzureRMSqlMiServer_identity(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_sql_managed_instance", "test") r := SqlManagedInstanceResource{} @@ -197,6 +212,36 @@ resource "azurerm_sql_managed_instance" "test" { `, r.template(data), data.RandomInteger) } +func (r SqlManagedInstanceResource) storageType(data acceptance.TestData, storageAccountType string) string { + return fmt.Sprintf(` +%s + +resource "azurerm_sql_managed_instance" "test" { + name = "acctestsqlserver%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + administrator_login = "mradministrator" + administrator_login_password = "thisIsDog11" + license_type = "BasePrice" + subnet_id = azurerm_subnet.test.id + sku_name = "GP_Gen5" + vcores = 4 + storage_size_in_gb = 32 + storage_account_type = "%s" + + depends_on = [ + azurerm_subnet_network_security_group_association.test, + azurerm_subnet_route_table_association.test, + ] + + tags = { + environment = "staging" + database = "test" + } +} +`, r.template(data), data.RandomInteger, storageAccountType) +} + func (r SqlManagedInstanceResource) identity(data acceptance.TestData) string { return fmt.Sprintf(` %s diff --git a/website/docs/r/sql_managed_instance.html.markdown b/website/docs/r/sql_managed_instance.html.markdown index 97d8359c60f4..9342a9adb887 100644 --- a/website/docs/r/sql_managed_instance.html.markdown +++ b/website/docs/r/sql_managed_instance.html.markdown @@ -239,6 +239,8 @@ The following arguments are supported: * `identity` - (Optional) An `identity` block as defined below. +* `storage_account_type` - (Optional) Specifies the storage account type used to store backups for this database. Changing this forces a new resource to be created. Possible values are `GRS`, `LRS` and `ZRS`. The default value is `GRS`. + * `tags` - (Optional) A mapping of tags to assign to the resource. --- From e2cc3dda4601360362d1bf32349503d26486a9ab Mon Sep 17 00:00:00 2001 From: Aris van Ommeren Date: Mon, 22 Nov 2021 13:26:24 +0100 Subject: [PATCH 3/3] Add workaround for https://github.com/Azure/azure-rest-api-specs/issues/16838 --- internal/services/sql/sql_managed_instance_resource.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/services/sql/sql_managed_instance_resource.go b/internal/services/sql/sql_managed_instance_resource.go index e9d14c1e9ec2..96eae7a1ff8c 100644 --- a/internal/services/sql/sql_managed_instance_resource.go +++ b/internal/services/sql/sql_managed_instance_resource.go @@ -248,7 +248,7 @@ func resourceArmSqlMiServerCreateUpdate(d *schema.ResourceData, meta interface{} }, } - identity, err := expandManagedInstanceIdentity(d.Get("identity").([]interface{})) + identity, err := expandManagedInstanceIdentity(d.Get("identity").([]interface{}), d.IsNewResource()) if err != nil { return fmt.Errorf(`expanding "identity": %v`, err) } @@ -369,12 +369,17 @@ func expandManagedInstanceSkuName(skuName string) (*sql.Sku, error) { }, nil } -func expandManagedInstanceIdentity(input []interface{}) (*sql.ResourceIdentity, error) { +func expandManagedInstanceIdentity(input []interface{}, isNewResource bool) (*sql.ResourceIdentity, error) { config, err := managedInstanceIdentity{}.Expand(input) if err != nil { return nil, err } + // Workaround for issue https://github.com/Azure/azure-rest-api-specs/issues/16838 + if config.Type == identity.Type("None") && isNewResource { + return nil, nil + } + return &sql.ResourceIdentity{ Type: sql.IdentityType(config.Type), }, nil