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_managed_instance fix identity bug and updating subnet_id #28319

Merged
merged 7 commits into from
Jan 3, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Support Identity Types, changing Subnets
  • Loading branch information
wyattfry committed Dec 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 9580e5de6096cdf7475c9db926e466e445ad3efe
52 changes: 46 additions & 6 deletions examples/mssql/managed-instance/main.tf
Original file line number Diff line number Diff line change
@@ -5,6 +5,12 @@ terraform {
# version = "3.108.0"
# }
# }
required_providers {
random = {
source = "hashicorp/random"
version = "3.6.2"
}
}
}

provider "azurerm" {
@@ -13,7 +19,7 @@ provider "azurerm" {
}

resource "azurerm_resource_group" "example" {
name = "acctest-mssql-managed-instance"
name = "acctest-mssql-managed-instance" # TODO add "Wyatt"
location = "eastus"
}

@@ -164,11 +170,37 @@ resource "azurerm_subnet" "example" {
}
}

resource "azurerm_subnet" "another_example" {
name = "${random_pet.example.id}-subnet-mi"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
private_endpoint_network_policies = "Enabled"
address_prefixes = ["10.0.1.0/24"]

delegation {
name = "managedinstancedelegation"

service_delegation {
name = "Microsoft.Sql/managedInstances"
actions = [
"Microsoft.Network/virtualNetworks/subnets/join/action",
"Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action",
"Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action"
]
}
}
}

resource "azurerm_subnet_network_security_group_association" "example" {
subnet_id = azurerm_subnet.example.id
network_security_group_id = azurerm_network_security_group.example.id
}

resource "azurerm_subnet_network_security_group_association" "another_example" {
subnet_id = azurerm_subnet.another_example.id
network_security_group_id = azurerm_network_security_group.example.id
}

resource "azurerm_route_table" "example" {
name = "routetable-mi"
location = azurerm_resource_group.example.location
@@ -184,15 +216,21 @@ resource "azurerm_subnet_route_table_association" "example" {
route_table_id = azurerm_route_table.example.id
}

resource "azurerm_subnet_route_table_association" "another_example" {
subnet_id = azurerm_subnet.another_example.id
route_table_id = azurerm_route_table.example.id
}

resource "random_pet" "example" {}

resource "azurerm_mssql_managed_instance" "example" {
name = "managedsqlinstance"
name = "${random_pet.example.id}-managedsqlinstance"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location

license_type = "BasePrice"
sku_name = "GP_Gen5"
storage_size_in_gb = 32
subnet_id = azurerm_subnet.example.id
subnet_id = azurerm_subnet.another_example.id
vcores = 4

identity {
@@ -203,11 +241,13 @@ resource "azurerm_mssql_managed_instance" "example" {
}

administrator_login = "mradministrator"
administrator_login_password = ";ljkhsdf98HSND*UFBNojkfn42p9if23hngklf"
administrator_login_password = "UPPERlower123!SixteenCharacters"

depends_on = [
azurerm_subnet_network_security_group_association.example,
azurerm_subnet_route_table_association.example,
azurerm_subnet_network_security_group_association.another_example,
azurerm_subnet_route_table_association.another_example,
]
}

@@ -218,6 +258,6 @@ resource "azurerm_user_assigned_identity" "example" {
}

import {
id = "/subscriptions/1a6092a6-137e-4025-9a7c-ef77f76f2c02/resourceGroups/acctest-mssql-managed-instance/providers/Microsoft.Sql/managedInstances/managedsqlinstance"
id = "/subscriptions/1a6092a6-137e-4025-9a7c-ef77f76f2c02/resourceGroups/acctest-mssql-managed-instance/providers/Microsoft.Sql/managedInstances/${random_pet.example.id}-managedsqlinstance"
to = azurerm_mssql_managed_instance.example
}
Original file line number Diff line number Diff line change
@@ -140,7 +140,6 @@ func (r MsSqlManagedInstanceResource) Arguments() map[string]*pluginsdk.Schema {
"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
wyattfry marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: commonids.ValidateSubnetID,
},

@@ -418,6 +417,9 @@ func (r MsSqlManagedInstanceResource) Update() sdk.ResourceFunc {
RequestedBackupStorageRedundancy: pointer.To(storageAccTypeToBackupStorageRedundancy(state.StorageAccountType)),
VCores: pointer.To(state.VCores),
ZoneRedundant: pointer.To(state.ZoneRedundantEnabled),
AdministratorLogin: pointer.To(state.AdministratorLogin),
AdministratorLoginPassword: pointer.To(state.AdministratorLoginPassword),
SubnetId: pointer.To(state.SubnetId),
Comment on lines +420 to +422
Copy link
Collaborator Author

@wyattfry wyattfry Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because subnet_id has since become mutable (idk about the admin creds, but the op fails without them), it must now be included in resource updates.

},
Tags: pointer.To(state.Tags),
}
Original file line number Diff line number Diff line change
@@ -146,6 +146,24 @@ func TestAccMsSqlManagedInstance_identity(t *testing.T) {
})
}

func TestAccMsSqlManagedInstance_systemAssignedUserAssignedIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_managed_instance", "test")
r := MsSqlManagedInstanceResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.systemAndUserIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.#").HasValue("1"),
check.That(data.ResourceName).Key("identity.0.identity_ids.#").HasValue("1"),
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned, UserAssigned"),
),
},
data.ImportStep("administrator_login_password"),
})
}

func TestAccMsSqlManagedInstance_multiple(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_managed_instance", "test")
r := MsSqlManagedInstanceResource{}
@@ -294,6 +312,28 @@ func TestAccMsSqlManagedInstance_backupRedundancyUpdated(t *testing.T) {
})
}

func TestAccMsSqlManagedInstance_subnetUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_managed_instance", "test")
r := MsSqlManagedInstanceResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("administrator_login_password"),
{
Config: r.subnetUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("administrator_login_password"),
})
}

func (r MsSqlManagedInstanceResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := commonids.ParseSqlManagedInstanceID(state.ID)
if err != nil {
@@ -531,6 +571,62 @@ resource "azurerm_mssql_managed_instance" "test" {
`, r.template(data, data.Locations.Primary), data.RandomInteger)
}

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

provider "azurerm" {
features {
resource_group {
/* Due to the creation of unmanaged Microsoft.Network/networkIntentPolicies in this service,
prevent_deletion_if_contains_resources has been added here to allow the test resources to be
deleted until this can be properly investigated
*/
prevent_deletion_if_contains_resources = false
}
}
}

resource "azurerm_user_assigned_identity" "test" {
location = azurerm_resource_group.test.location
name = "acctestsqlserver%[2]d"
resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_mssql_managed_instance" "test" {
name = "acctestsqlserver%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location

license_type = "BasePrice"
sku_name = "GP_Gen5"
storage_size_in_gb = 32
subnet_id = azurerm_subnet.test.id
vcores = 4

administrator_login = "missadministrator"
administrator_login_password = "NCC-1701-D"

depends_on = [
azurerm_subnet_network_security_group_association.test,
azurerm_subnet_route_table_association.test,
]

identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.test.id
]
}

tags = {
environment = "staging"
database = "test"
}
}
`, r.template(data, data.Locations.Primary), data.RandomInteger)
}

func (r MsSqlManagedInstanceResource) update(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
@@ -1685,3 +1781,75 @@ resource "azurerm_mssql_managed_instance" "test" {
}
`, r.template(data, data.Locations.Primary), data.RandomInteger)
}

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

provider "azurerm" {
features {
resource_group {
/* Due to the creation of unmanaged Microsoft.Network/networkIntentPolicies in this service,
prevent_deletion_if_contains_resources has been added here to allow the test resources to be
deleted until this can be properly investigated
*/
prevent_deletion_if_contains_resources = false
}
}
}


resource "azurerm_subnet" "test2" {
name = "subnet2-%[2]d"
wyattfry marked this conversation as resolved.
Show resolved Hide resolved
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]

delegation {
name = "managedinstancedelegation"

service_delegation {
name = "Microsoft.Sql/managedInstances"
actions = ["Microsoft.Network/virtualNetworks/subnets/join/action", "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action", "Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action"]
}
}
}

resource "azurerm_subnet_route_table_association" "test2" {
subnet_id = azurerm_subnet.test2.id
route_table_id = azurerm_route_table.test.id
}

resource "azurerm_subnet_network_security_group_association" "test2" {
subnet_id = azurerm_subnet.test2.id
network_security_group_id = azurerm_network_security_group.test.id
}

resource "azurerm_mssql_managed_instance" "test" {
name = "acctestsqlserver%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location

license_type = "BasePrice"
sku_name = "GP_Gen5"
storage_size_in_gb = 32
subnet_id = azurerm_subnet.test.id
vcores = 4

administrator_login = "missadministrator"
administrator_login_password = "NCC-1701-D"

depends_on = [
azurerm_subnet_network_security_group_association.test,
azurerm_subnet_route_table_association.test,
azurerm_subnet_network_security_group_association.test2,
azurerm_subnet_route_table_association.test2,
]

tags = {
environment = "staging"
database = "test"
}
}
`, r.template(data, data.Locations.Primary), data.RandomInteger)
}
Loading