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
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},

Expand Down Expand Up @@ -181,7 +180,7 @@ func (r MsSqlManagedInstanceResource) Arguments() map[string]*pluginsdk.Schema {
ValidateFunc: validate.ManagedInstanceID,
},

"identity": commonschema.SystemOrUserAssignedIdentityOptional(),
"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),
wyattfry marked this conversation as resolved.
Show resolved Hide resolved

"maintenance_configuration_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -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),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -532,6 +572,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
Expand Down Expand Up @@ -1686,3 +1782,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