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_sql_managed_instance: Support for storage_account_type #14123

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions internal/services/sql/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
33 changes: 22 additions & 11 deletions internal/services/sql/sql_managed_instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -174,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(),
},

Expand All @@ -182,11 +195,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"
}),
),
}
}
Expand All @@ -202,7 +210,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)
Expand Down Expand Up @@ -236,10 +244,11 @@ 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)),
},
}

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)
}
Expand Down Expand Up @@ -273,7 +282,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())
Expand Down Expand Up @@ -311,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))
}
Expand Down Expand Up @@ -359,13 +369,14 @@ 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
}

if config.Type == identity.Type("None") {
// Workaround for issue https://github.com/Azure/azure-rest-api-specs/issues/16838
if config.Type == identity.Type("None") && isNewResource {
return nil, nil
}

Expand Down
55 changes: 54 additions & 1 deletion internal/services/sql/sql_managed_instance_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -53,6 +68,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"),
})
}

Expand Down Expand Up @@ -150,7 +173,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
Expand Down Expand Up @@ -189,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
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/sql_managed_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---
Expand Down