|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of kms key_lists |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_kms_describe_keys" "example" { |
| 8 | + key_ids = [ |
| 9 | + "9ffacc8b-6461-11ee-a54e-525400dd8a7d", |
| 10 | + "bffae4ed-6465-11ee-90b2-5254000ef00e" |
| 11 | + ] |
| 12 | +} |
| 13 | +``` |
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 22 | + kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118" |
| 23 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 24 | +) |
| 25 | + |
| 26 | +func dataSourceTencentCloudKmsDescribeKeys() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Read: dataSourceTencentCloudKmsDescribeKeysRead, |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "key_ids": { |
| 31 | + Required: true, |
| 32 | + Type: schema.TypeSet, |
| 33 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 34 | + Description: "Query the ID list of CMK, batch query supports up to 100 KeyIds at a time.", |
| 35 | + }, |
| 36 | + "key_list": { |
| 37 | + Type: schema.TypeList, |
| 38 | + Computed: true, |
| 39 | + Description: "A list of KMS keys.", |
| 40 | + Elem: &schema.Resource{ |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "key_id": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: "ID of CMK.", |
| 46 | + }, |
| 47 | + "alias": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Name of CMK.", |
| 51 | + }, |
| 52 | + "create_time": { |
| 53 | + Type: schema.TypeInt, |
| 54 | + Computed: true, |
| 55 | + Description: "Create time of CMK.", |
| 56 | + }, |
| 57 | + "description": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Description of CMK.", |
| 61 | + }, |
| 62 | + "key_state": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "State of CMK.", |
| 66 | + }, |
| 67 | + "key_usage": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "Usage of CMK.", |
| 71 | + }, |
| 72 | + "creator_uin": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Computed: true, |
| 75 | + Description: "Uin of CMK Creator.", |
| 76 | + }, |
| 77 | + "key_rotation_enabled": { |
| 78 | + Type: schema.TypeBool, |
| 79 | + Computed: true, |
| 80 | + Description: "Specify whether to enable key rotation.", |
| 81 | + }, |
| 82 | + "owner": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "Creator of CMK.", |
| 86 | + }, |
| 87 | + "next_rotate_time": { |
| 88 | + Type: schema.TypeInt, |
| 89 | + Computed: true, |
| 90 | + Description: "Next rotate time of CMK when key_rotation_enabled is true.", |
| 91 | + }, |
| 92 | + "deletion_date": { |
| 93 | + Type: schema.TypeInt, |
| 94 | + Computed: true, |
| 95 | + Description: "Delete time of CMK.", |
| 96 | + }, |
| 97 | + "origin": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "Origin of CMK. `TENCENT_KMS` - CMK created by KMS, `EXTERNAL` - CMK imported by user.", |
| 101 | + }, |
| 102 | + "valid_to": { |
| 103 | + Type: schema.TypeInt, |
| 104 | + Computed: true, |
| 105 | + Description: "Valid when origin is `EXTERNAL`, it means the effective date of the key material.", |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + "result_output_file": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Optional: true, |
| 113 | + Description: "Used to save results.", |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func dataSourceTencentCloudKmsDescribeKeysRead(d *schema.ResourceData, meta interface{}) error { |
| 120 | + defer logElapsed("data_source.tencentcloud_kms_describe_keys.read")() |
| 121 | + defer inconsistentCheck(d, meta)() |
| 122 | + |
| 123 | + var ( |
| 124 | + logId = getLogId(contextNil) |
| 125 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 126 | + service = KmsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 127 | + keyMetadata []*kms.KeyMetadata |
| 128 | + ) |
| 129 | + |
| 130 | + paramMap := make(map[string]interface{}) |
| 131 | + if v, ok := d.GetOk("key_ids"); ok { |
| 132 | + keyIdsSet := v.(*schema.Set).List() |
| 133 | + paramMap["KeyIds"] = helper.InterfacesStringsPoint(keyIdsSet) |
| 134 | + } |
| 135 | + |
| 136 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 137 | + result, e := service.DescribeKmsKeyListsByFilter(ctx, paramMap) |
| 138 | + if e != nil { |
| 139 | + return retryError(e) |
| 140 | + } |
| 141 | + |
| 142 | + keyMetadata = result |
| 143 | + return nil |
| 144 | + }) |
| 145 | + |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + ids := make([]string, 0, len(keyMetadata)) |
| 151 | + tmpList := make([]map[string]interface{}, 0, len(keyMetadata)) |
| 152 | + |
| 153 | + if keyMetadata != nil { |
| 154 | + for _, key := range keyMetadata { |
| 155 | + mapping := map[string]interface{}{ |
| 156 | + "key_id": key.KeyId, |
| 157 | + "alias": key.Alias, |
| 158 | + "create_time": key.CreateTime, |
| 159 | + "description": key.Description, |
| 160 | + "key_state": key.KeyState, |
| 161 | + "key_usage": key.KeyUsage, |
| 162 | + "creator_uin": key.CreatorUin, |
| 163 | + "key_rotation_enabled": key.KeyRotationEnabled, |
| 164 | + "owner": key.Owner, |
| 165 | + "next_rotate_time": key.NextRotateTime, |
| 166 | + "deletion_date": key.DeletionDate, |
| 167 | + "origin": key.Origin, |
| 168 | + "valid_to": key.ValidTo, |
| 169 | + } |
| 170 | + |
| 171 | + tmpList = append(tmpList, mapping) |
| 172 | + ids = append(ids, *key.KeyId) |
| 173 | + } |
| 174 | + |
| 175 | + _ = d.Set("key_list", tmpList) |
| 176 | + } |
| 177 | + |
| 178 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 179 | + output, ok := d.GetOk("result_output_file") |
| 180 | + if ok && output.(string) != "" { |
| 181 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 182 | + return e |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return nil |
| 187 | +} |
0 commit comments