Skip to content

Commit

Permalink
Add force_import support to gaussdb_mysql (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo authored Nov 12, 2020
1 parent 60466ed commit f45f3a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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

0 comments on commit f45f3a7

Please sign in to comment.