diff --git a/docs/resources/gaussdb_mysql_instance.md b/docs/resources/gaussdb_mysql_instance.md index 4ffe4b69b5..81e879867d 100644 --- a/docs/resources/gaussdb_mysql_instance.md +++ b/docs/resources/gaussdb_mysql_instance.md @@ -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. diff --git a/huaweicloud/resource_huaweicloud_gaussdb_mysql_instance.go b/huaweicloud/resource_huaweicloud_gaussdb_mysql_instance.go index 0b09f0b855..c575b0be6d 100644 --- a/huaweicloud/resource_huaweicloud_gaussdb_mysql_instance.go +++ b/huaweicloud/resource_huaweicloud_gaussdb_mysql_instance.go @@ -145,6 +145,11 @@ func resourceGaussDBInstance() *schema.Resource { }, }, }, + "force_import": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, "status": { Type: schema.TypeString, Computed: true, @@ -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),