|
| 1 | +package tencentcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 8 | + "log" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceTencentCloudVpcAclAttachment() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceTencentCloudVpcAclAttachmentCreate, |
| 16 | + Read: resourceTencentCloudVpcAclAttachmentRead, |
| 17 | + Delete: resourceTencentCloudVpcAclAttachmentDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + State: schema.ImportStatePassthrough, |
| 20 | + }, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "id": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + ForceNew: true, |
| 27 | + Description: "Id of the attached ACL.", |
| 28 | + }, |
| 29 | + "subnet_ids": { |
| 30 | + Type: schema.TypeList, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + Elem: &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + Description: "ID list of the Subnet instance ID.", |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func resourceTencentCloudVpcAclAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 43 | + defer logElapsed("resource.tencentcloud_acl_attachment.create")() |
| 44 | + var ( |
| 45 | + logId = getLogId(contextNil) |
| 46 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 47 | + service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 48 | + aclId string |
| 49 | + subnetIds []string |
| 50 | + sub_id string |
| 51 | + ) |
| 52 | + |
| 53 | + if temp, ok := d.GetOk("id"); ok { |
| 54 | + aclId = temp.(string) |
| 55 | + if len(aclId) < 1 { |
| 56 | + return fmt.Errorf("acl_id should be not empty string") |
| 57 | + } |
| 58 | + } |
| 59 | + if temp, ok := d.GetOk("subnet_ids"); ok { |
| 60 | + subnetIds = temp.([]string) |
| 61 | + } |
| 62 | + |
| 63 | + err := service.AssociateAclSubnets(ctx, aclId, subnetIds) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + for _, temp_id := range subnetIds { |
| 69 | + sub_id = sub_id + "#" + temp_id |
| 70 | + } |
| 71 | + d.SetId(aclId + "#" + sub_id) |
| 72 | + |
| 73 | + aclAttachmentId := d.Id() |
| 74 | + err = resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 75 | + e := service.DescribeByAclId(ctx, aclAttachmentId) |
| 76 | + if e != nil { |
| 77 | + return retryError(e) |
| 78 | + } |
| 79 | + return nil |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + log.Printf("[CRITAL]%s read acl attachment failed, reason:%s\n", logId, err.Error()) |
| 83 | + return err |
| 84 | + } |
| 85 | + time.Sleep(10 * time.Second) |
| 86 | + |
| 87 | + return resourceTencentCloudVpcAclAttachmentRead(d, meta) |
| 88 | +} |
| 89 | + |
| 90 | +func resourceTencentCloudVpcAclAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 91 | + defer logElapsed("resource.tencentcloud_acl_attachment.read")() |
| 92 | + defer inconsistentCheck(d, meta)() |
| 93 | + |
| 94 | + var ( |
| 95 | + logId = getLogId(contextNil) |
| 96 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 97 | + service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 98 | + attachmentId = d.Id() |
| 99 | + aclId string |
| 100 | + ) |
| 101 | + |
| 102 | + if attachmentId == "" { |
| 103 | + return fmt.Errorf("attachmentId does not exist") |
| 104 | + } |
| 105 | + |
| 106 | + aclId = strings.Split(attachmentId, "#")[0] |
| 107 | + |
| 108 | + results, err := service.DescribeNetWorkAcls(ctx, aclId, "", "") |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + if len(results) < 1 && len(results[0].SubnetSet) < 1 { |
| 113 | + return fmt.Errorf("[TECENT_TERRAFORM_CHECK][ACL attachment][Exists] check: CAM group policy attachment id is not set") |
| 114 | + } |
| 115 | + return nil |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +func resourceTencentCloudVpcAclAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 120 | + defer logElapsed("resource.tencentcloud_acl_attachment.delete")() |
| 121 | + |
| 122 | + logId := getLogId(contextNil) |
| 123 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 124 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 125 | + attachmentAcl := d.Id() |
| 126 | + |
| 127 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 128 | + e := service.DeleteAclAttachment(ctx, attachmentAcl) |
| 129 | + if e != nil { |
| 130 | + log.Printf("[CRITAL]%s reason[%s]\n", logId, e.Error()) |
| 131 | + return retryError(e) |
| 132 | + } |
| 133 | + return nil |
| 134 | + }) |
| 135 | + if err != nil { |
| 136 | + log.Printf("[CRITAL]%s delete acl attachment failed, reason:%s\n", logId, err.Error()) |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + return nil |
| 141 | + |
| 142 | +} |
0 commit comments