-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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_database
create mode read during import
#11026
Conversation
8feeaa2
to
2271e51
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @aristosvo
Thanks for this PR - taking a look through this looks pretty good to me, but if we can move this to the Import function rather than the Read (since this is an import-specific fix) then this should otherwise be good to go 👍
Thanks!
@aristosvo I've tested this locally and realised there's some further changes needed here, so I hope you don't mind but I'm going to push a couple of commits to fix this, namely it appears we also need to change the Computed value out for a Default (since the API doesn't return one) and add a state migration here to ensure that's set for older resources - will confirm the upgrade path in a while. |
No problem! Thanks for taking over! |
@tombuildsstuff Just curious, the state migration is just adding the previous schema and a migration function to a migration package? Are there specific tricks to take into account? Looks a bit less complicated than it sounded 👍🏽 |
@aristosvo pretty much, the complexity there is more in terms of testing than the code itself - also de-duping references to the schema, since it's a point-in-time reference to the schema at the time the migration should occur From a testing perspective it's then confirming the upgrade path - but this is a one-way upgrade so it's a little bit of a pain to test, all in all it's not that involved it's just painful copying/making the point in time reference of the schema :) |
State migration runs clean:
|
@tombuildsstuff Although the code works fine, the initial problem is not solved. BTW, you probably know everything I write down here, but I'm writing it down for myself to completely understand what's going on 🤣 This is the setup: variable "instance_name" {
type = string
default = "test-mssql-db-2191"
}
variable "resource_group" {
type = string
default = "test-mssql-db-rg"
}
variable "db_name" {
type = string
default = "test-db"
}
variable "location" {
type = string
default = "westeurope"
}
variable "labels" {
type = map(any)
default = {}
}
variable "sku_name" {
type = string
default = "GP_Gen5_2"
}
variable "max_storage_gb" {
type = number
default = 10
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "azure-sql-fog" {
name = var.resource_group
location = var.location
tags = var.labels
}
resource "random_string" "username" {
length = 16
special = false
number = false
}
resource "random_password" "password" {
length = 64
override_special = "~_-."
min_upper = 2
min_lower = 2
min_special = 2
}
resource "azurerm_sql_server" "primary_azure_sql_db_server" {
depends_on = [azurerm_resource_group.azure-sql-fog]
name = format("%s-primary", var.instance_name)
resource_group_name = var.resource_group
location = var.location
version = "12.0"
administrator_login = random_string.username.result
administrator_login_password = random_password.password.result
tags = var.labels
}
locals {
default_pair = {
// https://docs.microsoft.com/en-us/azure/best-practices-availability-paired-regions
"eastasia" = "southeastasia"
"southeastasia" = "eastasia"
"centralus" = "eastus2"
"eastus" = "westus"
"eastus2" = "centralus"
"westus" = "eastus"
"northcentralus" = "southcentralus"
"southcentralus" = "northcentralus"
"northeurope" = "westeurope"
"westeurope" = "northeurope"
"japanwest" = "japaneast"
"japaneast" = "japanwest"
"brazilsouth" = "southcentralus"
"australiaeast" = "australiasoutheast"
"australiasoutheast" = "australiaeast"
"australiacentral" = "australiacentral2"
"australiacentral2" = "australiacentral"
"southindia" = "centralindia"
"centralindia" = "southindia"
"westindia" = "southindia"
"canadacentral" = "canadaeast"
"canadaeast" = "canadacentral"
"uksouth" = "ukwest"
"ukwest" = "uksouth"
"westcentralus" = "westus2"
"westus2" = "westcentralus"
"koreacentral" = "koreasouth"
"koreasouth" = "koreacentral"
"francecentral" = "francesouth"
"francesouth" = "francecentral"
"uaenorth" = "uaecentral"
"uaecentral" = "uaenorth"
"southafricanorth" = "southafricawest"
"southafricawest" = "southafricanorth"
"germanycentral" = "germanynortheast"
"germanynortheast" = "germanycentral"
}
}
resource "azurerm_sql_server" "secondary_sql_db_server" {
depends_on = [azurerm_resource_group.azure-sql-fog]
name = format("%s-secondary", var.instance_name)
resource_group_name = var.resource_group
location = local.default_pair[var.location]
version = "12.0"
administrator_login = random_string.username.result
administrator_login_password = random_password.password.result
tags = var.labels
}
resource "azurerm_mssql_database" "azure_sql_db" {
name = var.db_name
server_id = azurerm_sql_server.primary_azure_sql_db_server.id
sku_name = var.sku_name
max_size_gb = var.max_storage_gb
tags = var.labels
}
resource "azurerm_sql_failover_group" "failover_group" {
depends_on = [azurerm_resource_group.azure-sql-fog]
name = var.instance_name
resource_group_name = var.resource_group
server_name = azurerm_sql_server.primary_azure_sql_db_server.name
databases = [azurerm_mssql_database.azure_sql_db.id]
partner_servers {
id = azurerm_sql_server.secondary_sql_db_server.id
}
read_write_endpoint_failover_policy {
mode = "Automatic"
grace_minutes = 5
}
}
# resource "azurerm_mssql_database" "secondary_db" {
# name = var.db_name
# server_id = azurerm_sql_server.secondary_sql_db_server.id
# sku_name = var.sku_name
# max_size_gb = var.max_storage_gb
# tags = var.labels
# create_mode = "Secondary"
# }
After the From the documentation of StateFunc as found here:
This means we never can use configuration from our Possible solutions
Importer: azSchema.ValidateResourceIDPriorToImportThen(func(id string) error {
_, err := parse.DatabaseID(id)
return err
}, func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*clients.Client).MSSQL.DatabasesClient
failovergroupClient := meta.(*clients.Client).MSSQL.FailoverGroupsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.DatabaseID(d.Id())
if err != nil {
return nil, err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
return nil, fmt.Errorf("reading MsSql Database %s (MsSql Server Name %q / Resource Group %q): %s", id.Name, id.ServerName, id.ResourceGroup, err)
}
if resp.FailoverGroupID != nil && *resp.FailoverGroupID != "" {
failoverGroupId, err := parse.FailoverGroupID(*resp.FailoverGroupID)
if err != nil {
return nil, err
}
respFailoverGroup, err := failovergroupClient.Get(ctx, failoverGroupId.ResourceGroup, id.ServerName, id.ServerName, failoverGroupId.Name)
if err != nil {
return nil, err
}
if respFailoverGroup.FailoverGroupProperties != nil && *&respFailoverGroup.FailoverGroupProperties.ReplicationRole == "Secondary" {
d.Set("create_mode", "Secondary")
return []*schema.ResourceData{d}, nil
}
}
d.Set("create_mode", "Default")
return []*schema.ResourceData{d}, nil
}),
|
@tombuildsstuff Ping! Is it possible to validate the |
@aristosvo - this is similar to what you did with postgres? sounds like a good solution to me 🙂 |
@katbyte Thanks! I've submitted an improved version of my proposal, haven't tested it yet but the foundations is the same/better. Will rebase in a few minutes. |
…d to `Default` This mirrors the API behaviour, although it doesn't return it
5e92ae5
to
d50493d
Compare
Against
Against PR branch:
Checked:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @aristosvo - LGTM 👍
This has been released in version 2.57.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example: provider "azurerm" {
version = "~> 2.57.0"
}
# ... other configuration ... |
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |
Fixes #11024