From d2f187623a1c9c65b10768c164d23e8fbba1eb3a Mon Sep 17 00:00:00 2001 From: tamperMonkeyZQ Date: Thu, 22 Feb 2024 20:08:02 +0800 Subject: [PATCH] BCC tag logic enhance to prevent tag binding failure --- baiducloud/resource_baiducloud_instance.go | 63 ++++++++++++++++++---- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/baiducloud/resource_baiducloud_instance.go b/baiducloud/resource_baiducloud_instance.go index a28335b2..6991a283 100644 --- a/baiducloud/resource_baiducloud_instance.go +++ b/baiducloud/resource_baiducloud_instance.go @@ -5,19 +5,21 @@ Use this resource to create a BCC instance. Example Usage ```hcl -resource "baiducloud_instance" "my-server" { - image_id = "m-A4jJpFzi" - name = "my-instance" - availability_zone = "cn-bj-a" - cpu_count = "2" - memory_capacity_in_gb = "8" - billing = { - payment_timing = "Postpaid" - } -} + + resource "baiducloud_instance" "my-server" { + image_id = "m-A4jJpFzi" + name = "my-instance" + availability_zone = "cn-bj-a" + cpu_count = "2" + memory_capacity_in_gb = "8" + billing = { + payment_timing = "Postpaid" + } + } + ``` -Import +# Import BCC instance can be imported, e.g. @@ -520,6 +522,11 @@ func resourceBaiduCloudInstanceCreate(d *schema.ResourceData, meta interface{}) return err } + // check tag bind + if err := checkTagBind(d, meta); err != nil { + return err + } + // stop the instance if the action field is stop. if d.Get("action").(string) == INSTANCE_ACTION_STOP { stopWithNoCharge := d.Get("stop_with_no_charge").(bool) @@ -531,6 +538,40 @@ func resourceBaiduCloudInstanceCreate(d *schema.ResourceData, meta interface{}) return resourceBaiduCloudInstanceRead(d, meta) } +func checkTagBind(d *schema.ResourceData, meta interface{}) error { + if v, ok := d.GetOk("tags"); ok { + client := meta.(*connectivity.BaiduClient) + instanceID := d.Id() + action := "Retry BCC Instance tags bind " + instanceID + raw, err := client.WithBccClient(func(bccClient *bcc.Client) (interface{}, error) { + return bccClient.GetInstanceDetail(instanceID) + }) + addDebug(action, raw) + + if err != nil { + if NotFoundError(err) { + d.SetId("") + return nil + } + return WrapErrorf(err, DefaultErrorMsg, "baiducloud_instance", action, BCESDKGoERROR) + } + response, _ := raw.(*api.GetInstanceDetailResult) + if response.Instance.Tags == nil || len(response.Instance.Tags) == 0 { + // bind tags failed, retry + _, err := client.WithBccClient(func(bccClient *bcc.Client) (interface{}, error) { + tagArgs := &api.BindTagsRequest{ + ChangeTags: tranceTagMapToModel(v.(map[string]interface{})), + } + return nil, bccClient.BindInstanceToTags(instanceID, tagArgs) + }) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, "baiducloud_instance", action, BCESDKGoERROR) + } + } + } + return nil +} + func resourceBaiduCloudInstanceRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.BaiduClient) bccService := BccService{client}