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