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_database - support for importing from bacpac #972

Merged
merged 10 commits into from
Jun 19, 2018

Conversation

hbuckle
Copy link
Contributor

@hbuckle hbuckle commented Mar 13, 2018

Add ability to import a database from a bacpac (https://docs.microsoft.com/en-us/azure/sql-database/sql-database-import)

I can't see any way to add a test for this one because it needs a bacpac file in a storage account somewhere.

I've tested it using an AdventureWorks bacpac (https://s3-eu-west-1.amazonaws.com/resourcescrono/AdventureWorks2014FullForAzure.bacpac)

@@ -237,6 +285,27 @@ func resourceArmSqlDatabaseCreateUpdate(d *schema.ResourceData, meta interface{}
return err
}

if _, ok := d.GetOk("database_import"); ok {
if !strings.EqualFold(createMode, "default") {
return fmt.Errorf("database_import can only be used when create_mode is Default")
Copy link
Contributor

Choose a reason for hiding this comment

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

@hbuckle Should this error be a validation on the database_import property instead? Otherwise, you could be running your scripts and end up with the error during a deployment instead of when generating the plan? Or possibly use ConflictsWith: ["create_mode"] on the database_import since the default for create_mode is default (and its required to be default).

Copy link
Contributor

Choose a reason for hiding this comment

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

@vlouwagie ideally yes, however unfortunately the validation API's only expose a single property at this point; as such unfortunately I don't think that's feasible at this time

"SQL", "ADPassword",
}, true),
},
"operation_mode": {
Copy link
Contributor

Choose a reason for hiding this comment

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

@hbuckle If there is only one value, does this need to be required?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 - we should be able to make this Optional with a Default value instead :)

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

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

hey @hbuckle

Thanks for this PR :) apologies for the delayed review here!

I've taken a look through and left some comments in-line, but the main thing missing here is an acceptance test covering this functionality. I believe it should be possible to spin up a DB, take a backup and them import that into a new DB all via the API to create the test rather than caching a file somewhere perminantly - what do you think?

Thanks!

"SQL", "ADPassword",
}, true),
},
"operation_mode": {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 - we should be able to make this Optional with a Default value instead :)

@@ -57,6 +58,53 @@ func resourceArmSqlDatabase() *schema.Resource {
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"database_import": {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be worth renaming this to import - what do you think?

},
"administrator_login_password": {
Type: schema.TypeString,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add Sensitive: true here?

},
"storage_key": {
Type: schema.TypeString,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add Sensitive: true here?

@@ -237,6 +285,27 @@ func resourceArmSqlDatabaseCreateUpdate(d *schema.ResourceData, meta interface{}
return err
}

if _, ok := d.GetOk("database_import"); ok {
if !strings.EqualFold(createMode, "default") {
return fmt.Errorf("database_import can only be used when create_mode is Default")
Copy link
Contributor

Choose a reason for hiding this comment

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

@vlouwagie ideally yes, however unfortunately the validation API's only expose a single property at this point; as such unfortunately I don't think that's feasible at this time

// this is set in config.go, but something sets
// it back to 15 minutes, which isn't long enough
// for most imports
client.Client.PollingDuration = 60 * time.Minute
Copy link
Contributor

Choose a reason for hiding this comment

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

from what I recall the 15 minute reset is pulled from the API's retry interval recommendation * the number of retries allowed - I've been meaning to investigate this further

@hbuckle
Copy link
Contributor Author

hbuckle commented Mar 27, 2018

@tombuildsstuff RE the acceptance test, using the Azure portal I created a new blank database, exported it to a bacpac in a storage account and then imported it into a second new database. However the import fails with the error "Archive file cannot be size 0" so I think we need a way to get some data into the database. Do you know a way to do this using the API ?

@tombuildsstuff
Copy link
Contributor

@hbuckle sorry I'd missed your comment about the AdventureWorks bacpac, I guess we can place that somewhere and restore it? Let me reach out internally to see where's best to do that, but we should be able to upload it to blob storage from wherever it's hosted and then restore it as needed :)

@tombuildsstuff tombuildsstuff modified the milestones: 1.3.1, 1.3.2 Mar 28, 2018
@tombuildsstuff tombuildsstuff added the upstream/terraform This issue is blocked on an upstream issue within Terraform (Terraform Core/CLI, The Plugin SDK etc) label Mar 30, 2018
@tombuildsstuff tombuildsstuff modified the milestones: 1.3.2, 1.3.3 Apr 4, 2018
@hbuckle
Copy link
Contributor Author

hbuckle commented Apr 10, 2018

@tombuildsstuff Yes I think we basically just need a long lived storage account to do the restore from. Let me know if you have one available.

@hbuckle
Copy link
Contributor Author

hbuckle commented Apr 11, 2018

Actually I have just seen the azurerm_storage_blob resource can upload a local file to storage, so I can include a bacpac in the testdata folder. I'll go down that route

@tombuildsstuff
Copy link
Contributor

@hbuckle rather than storing a large binary file in the repository, it may be better to use a SQL Server Driver such as this one to create a table and insert some random test data (so we have this in code) - what do you think?

@tombuildsstuff tombuildsstuff added waiting-response and removed upstream/terraform This issue is blocked on an upstream issue within Terraform (Terraform Core/CLI, The Plugin SDK etc) labels Apr 12, 2018
@hbuckle
Copy link
Contributor Author

hbuckle commented Apr 17, 2018

@tombuildsstuff my only concern is that requires connecting directly to the DB, which probably won't work if you are behind a corporate firewall. I can make a bacpac that is much smaller than adventureworks, only a few kb and include that?

@tombuildsstuff tombuildsstuff modified the milestones: 1.3.3, 1.4.0 Apr 17, 2018
@tombuildsstuff
Copy link
Contributor

@hbuckle after chatting internally as long as it's a small file (e.g. a couple hundred KB) this should be fine - we were mostly concerned about large binary files :)

@tombuildsstuff tombuildsstuff removed this from the 1.4.0 milestone Apr 25, 2018
@tombuildsstuff tombuildsstuff added this to the 1.5.0 milestone Apr 25, 2018
@hbuckle
Copy link
Contributor Author

hbuckle commented May 5, 2018

@tombuildsstuff Acceptance test added

go test github.com/terraform-providers/terraform-provider-azurerm/azurerm -v -timeout 600s -run TestAccAzureRMSqlDatabase_bacpac
=== RUN   TestAccAzureRMSqlDatabase_bacpac
--- PASS: TestAccAzureRMSqlDatabase_bacpac (312.25s)
PASS
ok      github.com/terraform-providers/terraform-provider-azurerm/azurerm       312.288s

@tombuildsstuff tombuildsstuff modified the milestones: 1.5.0, 1.6.0 May 8, 2018
@hbuckle
Copy link
Contributor Author

hbuckle commented May 24, 2018

@tombuildsstuff anything more you need from me on this one?

@tombuildsstuff tombuildsstuff modified the milestones: 1.6.0, 1.7.0 May 24, 2018
@katbyte katbyte modified the milestones: 1.7.0, Soon Jun 12, 2018
@tombuildsstuff
Copy link
Contributor

@hbuckle apologies for the delay, I've been out for a few weeks - I'm taking a look at this now :)

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

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

hey @hbuckle

Thanks for pushing those updates - apologies on the delayed re-review of this. I've taken a look through and left a couple of minor comments in-line for which I'll push a commit to fix, but this otherwise LGTM - thanks for this!

Thanks!

Required: true,
ValidateFunc: validation.StringInSlice([]string{
"StorageAccessKey", "SharedAccessKey",
}, true),
Copy link
Contributor

Choose a reason for hiding this comment

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

this true represents the strings being case-insensitive; as such can we also add DiffSuppressFunc: ignoreCaseDiffSuppressFunc here to handle the casing being different?

Default: "Import",
ValidateFunc: validation.StringInSlice([]string{
"Import",
}, true),
Copy link
Contributor

Choose a reason for hiding this comment

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

(same here)

@tombuildsstuff tombuildsstuff changed the title Import SQL database from bacpac azurerm_sql_database - support for importing from bacpac Jun 19, 2018
@tombuildsstuff tombuildsstuff changed the title Import SQL database from bacpac azurerm_sql_database - support for importing from bacpac Jun 19, 2018
@tombuildsstuff
Copy link
Contributor

Tests pass:

screen shot 2018-06-19 at 12 55 32

@tombuildsstuff tombuildsstuff merged commit a454b5c into hashicorp:master Jun 19, 2018
tombuildsstuff added a commit that referenced this pull request Jun 19, 2018
tombuildsstuff added a commit that referenced this pull request Jun 19, 2018
tombuildsstuff added a commit that referenced this pull request Jun 19, 2018
@tombuildsstuff tombuildsstuff modified the milestones: Soon, Being Sorted Oct 25, 2018
@ghost
Copy link

ghost commented Mar 6, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement service/mssql Microsoft SQL Server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants