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 force_import support to gaussdb_mysql #654

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions docs/resources/gaussdb_mysql_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ The following arguments are supported:

* `backup_strategy` - (Optional) Specifies the advanced backup policy. Structure is documented below.

* `force_import` - (Optional) If specified, try to import the instance instead of creating if the name already existed.

The `datastore` block supports:

* `engine` - (Optional) Specifies the database engine. Only "gauss-mysql" is supported now.
Expand Down
28 changes: 28 additions & 0 deletions huaweicloud/resource_huaweicloud_gaussdb_mysql_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ func resourceGaussDBInstance() *schema.Resource {
},
},
},
"force_import": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -244,6 +249,29 @@ func resourceGaussDBInstanceCreate(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("Error creating HuaweiCloud GaussDB client: %s ", err)
}

// If force_import set, try to import it instead of creating
if hasFilledOpt(d, "force_import") {
log.Printf("[DEBUG] Gaussdb mysql instance force_import is set, try to import it instead of creating")
listOpts := instances.ListTaurusDBInstanceOpts{
Name: d.Get("name").(string),
}
pages, err := instances.List(client, listOpts).AllPages()
if err != nil {
return err
}

allInstances, err := instances.ExtractTaurusDBInstances(pages)
if err != nil {
return fmt.Errorf("Unable to retrieve instances: %s ", err)
}
if allInstances.TotalCount > 0 {
instance := allInstances.Instances[0]
log.Printf("[DEBUG] Found existing mysql instance %s with name %s", instance.Id, instance.Name)
d.SetId(instance.Id)
return resourceGaussDBInstanceRead(d, meta)
}
}

createOpts := instances.CreateTaurusDBOpts{
Name: d.Get("name").(string),
Flavor: d.Get("flavor").(string),
Expand Down