|
| 1 | +/* |
| 2 | +Use this data source to query the cos region list supported by the audit. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +```hcl |
| 6 | +data "tencentcloud_audit_cos_regions" "foo" { |
| 7 | +} |
| 8 | +``` |
| 9 | +*/ |
| 10 | +package tencentcloud |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "log" |
| 15 | + |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 18 | + audit "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit/v20190319" |
| 19 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 20 | +) |
| 21 | + |
| 22 | +func dataSourceTencentCloudAuditCosRegions() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Read: dataSourceTencentCloudAuditCosRegionsRead, |
| 25 | + |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "result_output_file": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Optional: true, |
| 30 | + Description: "Used to save results.", |
| 31 | + }, |
| 32 | + "audit_cos_region_list": { |
| 33 | + Type: schema.TypeList, |
| 34 | + Computed: true, |
| 35 | + Description: "List of available regions supported by audit cos.", |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "cos_region": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "Cos region.", |
| 42 | + }, |
| 43 | + "cos_region_name": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + Description: "Cos region chinese name.", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func dataSourceTencentCloudAuditCosRegionsRead(d *schema.ResourceData, meta interface{}) error { |
| 56 | + defer logElapsed("data_source.tencentcloud_audit_cos_regions.read")() |
| 57 | + |
| 58 | + logId := getLogId(contextNil) |
| 59 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 60 | + auditService := AuditService{ |
| 61 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 62 | + } |
| 63 | + |
| 64 | + var regions []*audit.CosRegionInfo |
| 65 | + var errRet error |
| 66 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 67 | + regions, errRet = auditService.DescribeAuditCosRegions(ctx) |
| 68 | + if errRet != nil { |
| 69 | + return retryError(errRet, InternalError) |
| 70 | + } |
| 71 | + return nil |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + regionList := make([]map[string]interface{}, 0, len(regions)) |
| 78 | + ids := make([]string, 0, len(regions)) |
| 79 | + for _, region := range regions { |
| 80 | + mapping := map[string]interface{}{ |
| 81 | + "cos_region": region.CosRegion, |
| 82 | + "cos_region_name": region.CosRegionName, |
| 83 | + } |
| 84 | + regionList = append(regionList, mapping) |
| 85 | + ids = append(ids, *region.CosRegion) |
| 86 | + } |
| 87 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 88 | + err = d.Set("audit_cos_region_list", regionList) |
| 89 | + if err != nil { |
| 90 | + log.Printf("[CRITAL]%s audit cos read regions list fail, reason:%s\n ", logId, err.Error()) |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + output, ok := d.GetOk("result_output_file") |
| 95 | + if ok && output.(string) != "" { |
| 96 | + if e := writeToFile(output.(string), regionList); e != nil { |
| 97 | + return e |
| 98 | + } |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
0 commit comments