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

Add import for google_sql_database_instance #11

Merged

Conversation

gh-mlfowler
Copy link
Contributor

Previously raised as hashicorp/terraform#15235.

$ make testacc TESTARGS='-run=TestAccGoogleSqlDatabaseInstance_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v -run=TestAccGoogleSqlDatabaseInstance_ -timeout 120m
?       github.com/terraform-providers/terraform-provider-google        [no test files]
=== RUN   TestAccGoogleSqlDatabaseInstance_importBasic
--- PASS: TestAccGoogleSqlDatabaseInstance_importBasic (62.66s)
=== RUN   TestAccGoogleSqlDatabaseInstance_importBasic3
--- PASS: TestAccGoogleSqlDatabaseInstance_importBasic3 (299.44s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic
--- PASS: TestAccGoogleSqlDatabaseInstance_basic (52.04s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic2
--- PASS: TestAccGoogleSqlDatabaseInstance_basic2 (63.26s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic3
--- PASS: TestAccGoogleSqlDatabaseInstance_basic3 (300.31s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_basic
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_basic (42.03s)
=== RUN   TestAccGoogleSqlDatabaseInstance_slave
--- PASS: TestAccGoogleSqlDatabaseInstance_slave (702.61s)
=== RUN   TestAccGoogleSqlDatabaseInstance_diskspecs
--- PASS: TestAccGoogleSqlDatabaseInstance_diskspecs (301.20s)
=== RUN   TestAccGoogleSqlDatabaseInstance_maintenance
--- PASS: TestAccGoogleSqlDatabaseInstance_maintenance (436.09s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_upgrade
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_upgrade (59.18s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_downgrade
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_downgrade (48.40s)
=== RUN   TestAccGoogleSqlDatabaseInstance_authNets
--- PASS: TestAccGoogleSqlDatabaseInstance_authNets (75.18s)
=== RUN   TestAccGoogleSqlDatabaseInstance_multipleOperations
--- PASS: TestAccGoogleSqlDatabaseInstance_multipleOperations (79.44s)
PASS
ok      github.com/terraform-providers/terraform-provider-google/google 2521.860s```

Copy link
Collaborator

@rileykarson rileykarson left a comment

Choose a reason for hiding this comment

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

Nice work! This is a big/old file, thanks for tackling it. I left a few comments for you to look over.

@@ -630,6 +634,11 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
return err
}

// In the import case name won't be set yet
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of setting name here, let's use d.Id() in the Get below, and d.Set name using the Name property of instance later. That way, both refresh and import run through the same code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've had to add a d.setId to the Create as the resource relied on it being set at the end of the first Read.

} else {
_settingsList = make([]interface{}, 1)
_settings = make(map[string]interface{})
d.Set("region", instance.Region)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should perform this d.Set in every Read instead of only during imports; this file was written as if Terraform was a non-authoritative tool, but our current standard is to make it one.

if len(_settingsList) > 0 {
_settings = _settingsList[0].(map[string]interface{})
} else {
_settingsList = make([]interface{}, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we read _settingsList anywhere after this? I don't think we need this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like when we use the _settingsList slice down there, we are just overwriting it's contents with the _settings object. We can introduce a new slice to wrap _settings in there, and better semantically show that the two slices are not linked. That way, we can eliminate this line and break an implicit dependency between these parts of the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have made the amendments, please let me know if I understood correctly.

Database instances can be imported using the `name`, e.g.

```
$ terraform import google_sql_database_instance.instance instance_name
Copy link
Collaborator

Choose a reason for hiding this comment

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

Replace with this to match the import statement with the resource in the example:

$ terraform import google_sql_database_instance.master master-instance

@gh-mlfowler
Copy link
Contributor Author

Thanks for the review @rileykarson, I have made your suggested amendments.

==> Checking that code complies with gofmt requirements...                                                                                       
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v -run=TestAccGoogleSqlDatabaseInstance_ -timeout 120m                                      
?       github.com/terraform-providers/terraform-provider-google        [no test files]                                                          
=== RUN   TestAccGoogleSqlDatabaseInstance_importBasic                                                                                           
--- PASS: TestAccGoogleSqlDatabaseInstance_importBasic (61.91s)                                                                                  
=== RUN   TestAccGoogleSqlDatabaseInstance_importBasic3                                                                                          
--- PASS: TestAccGoogleSqlDatabaseInstance_importBasic3 (382.40s)                                                                                
=== RUN   TestAccGoogleSqlDatabaseInstance_basic                                                                                                 
--- PASS: TestAccGoogleSqlDatabaseInstance_basic (51.22s)                                                                                        
=== RUN   TestAccGoogleSqlDatabaseInstance_basic2                                                                                                
--- PASS: TestAccGoogleSqlDatabaseInstance_basic2 (51.78s)                                                                                       
=== RUN   TestAccGoogleSqlDatabaseInstance_basic3                                                                                                
--- PASS: TestAccGoogleSqlDatabaseInstance_basic3 (434.49s)                                                                                      
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_basic                                                                                        
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_basic (41.80s)                                                                               
=== RUN   TestAccGoogleSqlDatabaseInstance_slave                                                                                                 
--- PASS: TestAccGoogleSqlDatabaseInstance_slave (744.54s)                                                                                       
=== RUN   TestAccGoogleSqlDatabaseInstance_diskspecs                                                                                             
--- PASS: TestAccGoogleSqlDatabaseInstance_diskspecs (429.58s)                                                                                   
=== RUN   TestAccGoogleSqlDatabaseInstance_maintenance                                                                                           
--- PASS: TestAccGoogleSqlDatabaseInstance_maintenance (396.00s)                                                                                 
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_upgrade                                                                                      
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_upgrade (73.19s)                                                                             
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_downgrade                                                                                    
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_downgrade (62.82s)                                                                           
=== RUN   TestAccGoogleSqlDatabaseInstance_authNets                                                                                              
--- PASS: TestAccGoogleSqlDatabaseInstance_authNets (88.65s)                                                                                     
=== RUN   TestAccGoogleSqlDatabaseInstance_multipleOperations                                                                                    
--- PASS: TestAccGoogleSqlDatabaseInstance_multipleOperations (94.58s)                                                                           
PASS                                                                                                                                             
ok      github.com/terraform-providers/terraform-provider-google/google 2912.964s   ```

@gh-mlfowler
Copy link
Contributor Author

==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v -run=TestAccGoogleSqlDatabaseInstance_ -timeout 120m
?       github.com/terraform-providers/terraform-provider-google        [no test files]
=== RUN   TestAccGoogleSqlDatabaseInstance_importBasic
--- PASS: TestAccGoogleSqlDatabaseInstance_importBasic (50.59s)
=== RUN   TestAccGoogleSqlDatabaseInstance_importBasic3
--- PASS: TestAccGoogleSqlDatabaseInstance_importBasic3 (389.04s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic
--- PASS: TestAccGoogleSqlDatabaseInstance_basic (50.72s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic2
--- PASS: TestAccGoogleSqlDatabaseInstance_basic2 (49.15s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic3
--- PASS: TestAccGoogleSqlDatabaseInstance_basic3 (368.82s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_basic
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_basic (40.48s)
=== RUN   TestAccGoogleSqlDatabaseInstance_slave
--- PASS: TestAccGoogleSqlDatabaseInstance_slave (758.15s)
=== RUN   TestAccGoogleSqlDatabaseInstance_diskspecs
--- PASS: TestAccGoogleSqlDatabaseInstance_diskspecs (377.73s)
=== RUN   TestAccGoogleSqlDatabaseInstance_maintenance
--- PASS: TestAccGoogleSqlDatabaseInstance_maintenance (469.70s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_upgrade
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_upgrade (68.62s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_downgrade
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_downgrade (47.40s)
=== RUN   TestAccGoogleSqlDatabaseInstance_authNets
--- PASS: TestAccGoogleSqlDatabaseInstance_authNets (74.59s)
=== RUN   TestAccGoogleSqlDatabaseInstance_multipleOperations
--- PASS: TestAccGoogleSqlDatabaseInstance_multipleOperations (87.62s)
PASS
ok      github.com/terraform-providers/terraform-provider-google/google 2832.630s```

@rileykarson
Copy link
Collaborator

LGTM - thanks for the great work on this @gh-mlfowler!

TF_ACC=1 go test ./google -v -run=TestAccGoogleSqlDatabaseInstance_ -timeout 120m
=== RUN   TestAccGoogleSqlDatabaseInstance_importBasic
--- PASS: TestAccGoogleSqlDatabaseInstance_importBasic (59.14s)
=== RUN   TestAccGoogleSqlDatabaseInstance_importBasic3
--- PASS: TestAccGoogleSqlDatabaseInstance_importBasic3 (454.68s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic
--- PASS: TestAccGoogleSqlDatabaseInstance_basic (59.49s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic2
--- PASS: TestAccGoogleSqlDatabaseInstance_basic2 (57.87s)
=== RUN   TestAccGoogleSqlDatabaseInstance_basic3
--- PASS: TestAccGoogleSqlDatabaseInstance_basic3 (375.44s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_basic
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_basic (37.98s)
=== RUN   TestAccGoogleSqlDatabaseInstance_slave
--- PASS: TestAccGoogleSqlDatabaseInstance_slave (831.64s)
=== RUN   TestAccGoogleSqlDatabaseInstance_diskspecs
--- PASS: TestAccGoogleSqlDatabaseInstance_diskspecs (352.84s)
=== RUN   TestAccGoogleSqlDatabaseInstance_maintenance
--- PASS: TestAccGoogleSqlDatabaseInstance_maintenance (393.30s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_upgrade
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_upgrade (55.26s)
=== RUN   TestAccGoogleSqlDatabaseInstance_settings_downgrade
--- PASS: TestAccGoogleSqlDatabaseInstance_settings_downgrade (45.28s)
=== RUN   TestAccGoogleSqlDatabaseInstance_authNets
--- PASS: TestAccGoogleSqlDatabaseInstance_authNets (60.85s)
=== RUN   TestAccGoogleSqlDatabaseInstance_multipleOperations
--- PASS: TestAccGoogleSqlDatabaseInstance_multipleOperations (83.13s)
PASS
ok  	github.com/terraform-providers/terraform-provider-google/google	2867.086s

@rileykarson rileykarson merged commit 58a6b3f into hashicorp:master Jun 22, 2017
rileykarson added a commit to rileykarson/terraform that referenced this pull request Jun 22, 2017
Add `google_sql_database` and `google_sql_database_instance` to Importability page.

@danawillow
hashicorp/terraform-provider-google#11
hashicorp/terraform-provider-google#12
radeksimko pushed a commit to hashicorp/terraform that referenced this pull request Jun 23, 2017
pdecat pushed a commit to pdecat/terraform-provider-google that referenced this pull request Jun 29, 2017
* Add import for `google_sql_database_instance`

* Alterations suggested by review

* Move declaration of array for clarification
chrisst referenced this pull request in chrisst/terraform-provider-google Nov 9, 2018
* Add import for `google_sql_database_instance`

* Alterations suggested by review

* Move declaration of array for clarification
@ghost
Copy link

ghost commented Mar 31, 2020

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 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants