|
| 1 | +/* |
| 2 | +Provides a resource to create a chdfs access_rule |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_chdfs_access_rule" "access_rule" { |
| 8 | + access_group_id = "ag-bvmzrbsm" |
| 9 | +
|
| 10 | + access_rule { |
| 11 | + access_mode = 2 |
| 12 | + address = "10.0.1.1" |
| 13 | + priority = 12 |
| 14 | + } |
| 15 | +} |
| 16 | +``` |
| 17 | +
|
| 18 | +Import |
| 19 | +
|
| 20 | +chdfs access_rule can be imported using the id, e.g. |
| 21 | +
|
| 22 | +``` |
| 23 | +terraform import tencentcloud_chdfs_access_rule.access_rule access_group_id#access_rule_id |
| 24 | +``` |
| 25 | +*/ |
| 26 | +package tencentcloud |
| 27 | + |
| 28 | +import ( |
| 29 | + "context" |
| 30 | + "fmt" |
| 31 | + "log" |
| 32 | + "strings" |
| 33 | + |
| 34 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 35 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 36 | + chdfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs/v20201112" |
| 37 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 38 | +) |
| 39 | + |
| 40 | +func resourceTencentCloudChdfsAccessRule() *schema.Resource { |
| 41 | + return &schema.Resource{ |
| 42 | + Create: resourceTencentCloudChdfsAccessRuleCreate, |
| 43 | + Read: resourceTencentCloudChdfsAccessRuleRead, |
| 44 | + Update: resourceTencentCloudChdfsAccessRuleUpdate, |
| 45 | + Delete: resourceTencentCloudChdfsAccessRuleDelete, |
| 46 | + Importer: &schema.ResourceImporter{ |
| 47 | + State: schema.ImportStatePassthrough, |
| 48 | + }, |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "access_rule": { |
| 51 | + Required: true, |
| 52 | + Type: schema.TypeList, |
| 53 | + MaxItems: 1, |
| 54 | + Description: "rule detail.", |
| 55 | + Elem: &schema.Resource{ |
| 56 | + Schema: map[string]*schema.Schema{ |
| 57 | + "access_rule_id": { |
| 58 | + Type: schema.TypeInt, |
| 59 | + Computed: true, |
| 60 | + Description: "single rule id.", |
| 61 | + }, |
| 62 | + "address": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + Description: "rule address, IP OR IP SEG.", |
| 66 | + }, |
| 67 | + "access_mode": { |
| 68 | + Type: schema.TypeInt, |
| 69 | + Optional: true, |
| 70 | + Description: "rule access mode, 1: read only, 2: read & wirte.", |
| 71 | + }, |
| 72 | + "priority": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Optional: true, |
| 75 | + Description: "rule priority, range 1 - 100, value less higher priority.", |
| 76 | + }, |
| 77 | + "create_time": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "rule create time.", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + |
| 86 | + "access_group_id": { |
| 87 | + Required: true, |
| 88 | + ForceNew: true, |
| 89 | + Type: schema.TypeString, |
| 90 | + Description: "access group id.", |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func resourceTencentCloudChdfsAccessRuleCreate(d *schema.ResourceData, meta interface{}) error { |
| 97 | + defer logElapsed("resource.tencentcloud_chdfs_access_rule.create")() |
| 98 | + defer inconsistentCheck(d, meta)() |
| 99 | + |
| 100 | + logId := getLogId(contextNil) |
| 101 | + |
| 102 | + var ( |
| 103 | + request = chdfs.NewCreateAccessRulesRequest() |
| 104 | + response = chdfs.NewCreateAccessRulesResponse() |
| 105 | + accessGroupId string |
| 106 | + accessRuleId uint64 |
| 107 | + ) |
| 108 | + if dMap, ok := helper.InterfacesHeadMap(d, "access_rule"); ok { |
| 109 | + accessRule := chdfs.AccessRule{} |
| 110 | + if v, ok := dMap["address"]; ok { |
| 111 | + accessRule.Address = helper.String(v.(string)) |
| 112 | + } |
| 113 | + if v, ok := dMap["access_mode"]; ok { |
| 114 | + accessRule.AccessMode = helper.IntUint64(v.(int)) |
| 115 | + } |
| 116 | + if v, ok := dMap["priority"]; ok { |
| 117 | + accessRule.Priority = helper.IntUint64(v.(int)) |
| 118 | + } |
| 119 | + request.AccessRules = append(request.AccessRules, &accessRule) |
| 120 | + } |
| 121 | + |
| 122 | + if v, ok := d.GetOk("access_group_id"); ok { |
| 123 | + accessGroupId = v.(string) |
| 124 | + request.AccessGroupId = helper.String(v.(string)) |
| 125 | + } |
| 126 | + |
| 127 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 128 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseChdfsClient().CreateAccessRules(request) |
| 129 | + if e != nil { |
| 130 | + return retryError(e) |
| 131 | + } else { |
| 132 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 133 | + } |
| 134 | + response = result |
| 135 | + return nil |
| 136 | + }) |
| 137 | + if err != nil { |
| 138 | + log.Printf("[CRITAL]%s create chdfs accessRule failed, reason:%+v", logId, err) |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + if len(response.Response.AccessRules) < 1 { |
| 143 | + return fmt.Errorf("create chdfs accessRules failed") |
| 144 | + } |
| 145 | + |
| 146 | + accessRuleId = *response.Response.AccessRules[0].AccessRuleId |
| 147 | + d.SetId(accessGroupId + FILED_SP + helper.UInt64ToStr(accessRuleId)) |
| 148 | + |
| 149 | + return resourceTencentCloudChdfsAccessRuleRead(d, meta) |
| 150 | +} |
| 151 | + |
| 152 | +func resourceTencentCloudChdfsAccessRuleRead(d *schema.ResourceData, meta interface{}) error { |
| 153 | + defer logElapsed("resource.tencentcloud_chdfs_access_rule.read")() |
| 154 | + defer inconsistentCheck(d, meta)() |
| 155 | + |
| 156 | + logId := getLogId(contextNil) |
| 157 | + |
| 158 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 159 | + |
| 160 | + service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 161 | + |
| 162 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 163 | + if len(idSplit) != 2 { |
| 164 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 165 | + } |
| 166 | + accessGroupId := idSplit[0] |
| 167 | + accessRuleId := idSplit[1] |
| 168 | + |
| 169 | + accessRule, err := service.DescribeChdfsAccessRulesById(ctx, accessGroupId, accessRuleId) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + if accessRule == nil { |
| 175 | + d.SetId("") |
| 176 | + log.Printf("[WARN]%s resource `ChdfsAccessRule` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 177 | + return nil |
| 178 | + } |
| 179 | + |
| 180 | + if accessRule != nil { |
| 181 | + accessRuleMap := map[string]interface{}{} |
| 182 | + |
| 183 | + if accessRule.AccessRuleId != nil { |
| 184 | + accessRuleMap["access_rule_id"] = accessRule.AccessRuleId |
| 185 | + } |
| 186 | + |
| 187 | + if accessRule.Address != nil { |
| 188 | + accessRuleMap["address"] = accessRule.Address |
| 189 | + } |
| 190 | + |
| 191 | + if accessRule.AccessMode != nil { |
| 192 | + accessRuleMap["access_mode"] = accessRule.AccessMode |
| 193 | + } |
| 194 | + |
| 195 | + if accessRule.Priority != nil { |
| 196 | + accessRuleMap["priority"] = accessRule.Priority |
| 197 | + } |
| 198 | + |
| 199 | + if accessRule.CreateTime != nil { |
| 200 | + accessRuleMap["create_time"] = accessRule.CreateTime |
| 201 | + } |
| 202 | + |
| 203 | + _ = d.Set("access_rule", []interface{}{accessRuleMap}) |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + _ = d.Set("access_group_id", accessGroupId) |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +func resourceTencentCloudChdfsAccessRuleUpdate(d *schema.ResourceData, meta interface{}) error { |
| 212 | + defer logElapsed("resource.tencentcloud_chdfs_access_rule.update")() |
| 213 | + defer inconsistentCheck(d, meta)() |
| 214 | + |
| 215 | + logId := getLogId(contextNil) |
| 216 | + |
| 217 | + request := chdfs.NewModifyAccessRulesRequest() |
| 218 | + |
| 219 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 220 | + if len(idSplit) != 2 { |
| 221 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 222 | + } |
| 223 | + |
| 224 | + accessRuleId := idSplit[1] |
| 225 | + |
| 226 | + if d.HasChange("access_rule") { |
| 227 | + if dMap, ok := helper.InterfacesHeadMap(d, "access_rule"); ok { |
| 228 | + accessRule := chdfs.AccessRule{} |
| 229 | + |
| 230 | + accessRule.AccessRuleId = helper.StrToUint64Point(accessRuleId) |
| 231 | + |
| 232 | + if v, ok := dMap["address"]; ok { |
| 233 | + accessRule.Address = helper.String(v.(string)) |
| 234 | + } |
| 235 | + if v, ok := dMap["access_mode"]; ok { |
| 236 | + accessRule.AccessMode = helper.IntUint64(v.(int)) |
| 237 | + } |
| 238 | + if v, ok := dMap["priority"]; ok { |
| 239 | + accessRule.Priority = helper.IntUint64(v.(int)) |
| 240 | + } |
| 241 | + request.AccessRules = append(request.AccessRules, &accessRule) |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 246 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseChdfsClient().ModifyAccessRules(request) |
| 247 | + if e != nil { |
| 248 | + return retryError(e) |
| 249 | + } else { |
| 250 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 251 | + } |
| 252 | + return nil |
| 253 | + }) |
| 254 | + if err != nil { |
| 255 | + log.Printf("[CRITAL]%s update chdfs accessRule failed, reason:%+v", logId, err) |
| 256 | + return err |
| 257 | + } |
| 258 | + |
| 259 | + return resourceTencentCloudChdfsAccessRuleRead(d, meta) |
| 260 | +} |
| 261 | + |
| 262 | +func resourceTencentCloudChdfsAccessRuleDelete(d *schema.ResourceData, meta interface{}) error { |
| 263 | + defer logElapsed("resource.tencentcloud_chdfs_access_rule.delete")() |
| 264 | + defer inconsistentCheck(d, meta)() |
| 265 | + |
| 266 | + logId := getLogId(contextNil) |
| 267 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 268 | + |
| 269 | + service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 270 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 271 | + if len(idSplit) != 2 { |
| 272 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 273 | + } |
| 274 | + accessRuleId := idSplit[1] |
| 275 | + |
| 276 | + if err := service.DeleteChdfsAccessRulesById(ctx, accessRuleId); err != nil { |
| 277 | + return err |
| 278 | + } |
| 279 | + |
| 280 | + return nil |
| 281 | +} |
0 commit comments