|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of KMS key |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_kms_keys" "foo" { |
| 8 | + search_key_alias = "test" |
| 9 | + key_state = 0 |
| 10 | + origin = "TENCENT_KMS" |
| 11 | + key_usage = "ALL" |
| 12 | +} |
| 13 | +``` |
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "log" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 23 | + kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | +) |
| 26 | + |
| 27 | +func dataSourceTencentCloudKmsKeys() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Read: dataSourceTencentCloudKmsKeysRead, |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "role": { |
| 32 | + Type: schema.TypeInt, |
| 33 | + Optional: true, |
| 34 | + Default: 0, |
| 35 | + Description: "Filter by role of the CMK creator. `0` - created by user, `1` - created by cloud product. Default value is `0`.", |
| 36 | + }, |
| 37 | + "order_type": { |
| 38 | + Type: schema.TypeInt, |
| 39 | + Optional: true, |
| 40 | + Default: 0, |
| 41 | + Description: "Order to sort the CMK create time. `0` - desc, `1` - asc. Default value is `0`.", |
| 42 | + }, |
| 43 | + "key_state": { |
| 44 | + Type: schema.TypeInt, |
| 45 | + Optional: true, |
| 46 | + Default: 0, |
| 47 | + Description: "Filter by state of CMK. `0` - all CMKs are queried, `1` - only Enabled CMKs are queried, `2` - only Disabled CMKs are queried, `3` - only PendingDelete CMKs are queried, `4` - only PendingImport CMKs are queried, `5` - only Archived CMKs are queried.", |
| 48 | + }, |
| 49 | + "search_key_alias": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + Description: "Words used to match the results, and the words can be: key_id and alias.", |
| 53 | + }, |
| 54 | + "origin": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + Default: KMS_ORIGIN_ALL, |
| 58 | + Description: "Filter by origin of CMK. `TENCENT_KMS` - CMK created by KMS, `EXTERNAL` - CMK imported by user, `ALL` - all CMKs. Default value is `ALL`.", |
| 59 | + }, |
| 60 | + "key_usage": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Optional: true, |
| 63 | + Default: KMS_KEY_USAGE_ENCRYPT_DECRYPT, |
| 64 | + Description: "Filter by usage of CMK. Available values include `ALL`, `ENCRYPT_DECRYPT`, `ASYMMETRIC_DECRYPT_RSA_2048`, `ASYMMETRIC_DECRYPT_SM2`, `ASYMMETRIC_SIGN_VERIFY_SM2`, `ASYMMETRIC_SIGN_VERIFY_RSA_2048`, `ASYMMETRIC_SIGN_VERIFY_ECC`. Default value is `ENCRYPT_DECRYPT`.", |
| 65 | + }, |
| 66 | + "tags": { |
| 67 | + Type: schema.TypeMap, |
| 68 | + Optional: true, |
| 69 | + Description: "Tags to filter CMK.", |
| 70 | + }, |
| 71 | + "result_output_file": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Optional: true, |
| 74 | + Description: "Used to save results.", |
| 75 | + }, |
| 76 | + "key_list": { |
| 77 | + Type: schema.TypeList, |
| 78 | + Computed: true, |
| 79 | + Description: "A list of KMS keys.", |
| 80 | + Elem: &schema.Resource{ |
| 81 | + Schema: map[string]*schema.Schema{ |
| 82 | + "key_id": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "ID of CMK.", |
| 86 | + }, |
| 87 | + "alias": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "Name of CMK.", |
| 91 | + }, |
| 92 | + "create_time": { |
| 93 | + Type: schema.TypeInt, |
| 94 | + Computed: true, |
| 95 | + Description: "Create time of CMK.", |
| 96 | + }, |
| 97 | + "description": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "Description of CMK.", |
| 101 | + }, |
| 102 | + "key_state": { |
| 103 | + Type: schema.TypeString, |
| 104 | + Computed: true, |
| 105 | + Description: "State of CMK.", |
| 106 | + }, |
| 107 | + "key_usage": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + Description: "Usage of CMK.", |
| 111 | + }, |
| 112 | + "creator_uin": { |
| 113 | + Type: schema.TypeInt, |
| 114 | + Computed: true, |
| 115 | + Description: "Uin of CMK Creator.", |
| 116 | + }, |
| 117 | + "key_rotation_enabled": { |
| 118 | + Type: schema.TypeBool, |
| 119 | + Computed: true, |
| 120 | + Description: "Specify whether to enable key rotation.", |
| 121 | + }, |
| 122 | + "owner": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Computed: true, |
| 125 | + Description: "Creator of CMK.", |
| 126 | + }, |
| 127 | + "next_rotate_time": { |
| 128 | + Type: schema.TypeInt, |
| 129 | + Computed: true, |
| 130 | + Description: "Next rotate time of CMK when key_rotation_enabled is true.", |
| 131 | + }, |
| 132 | + "deletion_date": { |
| 133 | + Type: schema.TypeInt, |
| 134 | + Computed: true, |
| 135 | + Description: "Delete time of CMK.", |
| 136 | + }, |
| 137 | + "origin": { |
| 138 | + Type: schema.TypeString, |
| 139 | + Computed: true, |
| 140 | + Description: "Origin of CMK. `TENCENT_KMS` - CMK created by KMS, `EXTERNAL` - CMK imported by user.", |
| 141 | + }, |
| 142 | + "valid_to": { |
| 143 | + Type: schema.TypeInt, |
| 144 | + Computed: true, |
| 145 | + Description: "Valid when origin is `EXTERNAL`, it means the effective date of the key material.", |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func dataSourceTencentCloudKmsKeysRead(d *schema.ResourceData, meta interface{}) error { |
| 155 | + defer logElapsed("data_source.tencentcloud_kms_keys.read")() |
| 156 | + |
| 157 | + logId := getLogId(contextNil) |
| 158 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 159 | + |
| 160 | + param := make(map[string]interface{}) |
| 161 | + if v, ok := d.GetOk("role"); ok { |
| 162 | + param["role"] = v.(int) |
| 163 | + } |
| 164 | + if v, ok := d.GetOk("order_type"); ok { |
| 165 | + param["order_type"] = v.(int) |
| 166 | + } |
| 167 | + if v, ok := d.GetOk("key_state"); ok { |
| 168 | + keyState := v.(int) |
| 169 | + param["key_state"] = uint64(keyState) |
| 170 | + } |
| 171 | + if v, ok := d.GetOk("search_key_alias"); ok { |
| 172 | + param["search_key_alias"] = v.(string) |
| 173 | + } |
| 174 | + if v, ok := d.GetOk("origin"); ok { |
| 175 | + param["origin"] = v.(string) |
| 176 | + } |
| 177 | + if v, ok := d.GetOk("key_usage"); ok { |
| 178 | + param["key_usage"] = v.(string) |
| 179 | + } |
| 180 | + if tags := helper.GetTags(d, "tags"); len(tags) > 0 { |
| 181 | + param["tag_filter"] = tags |
| 182 | + } |
| 183 | + |
| 184 | + kmsService := KmsService{ |
| 185 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 186 | + } |
| 187 | + var keys []*kms.KeyMetadata |
| 188 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 189 | + results, e := kmsService.DescribeKeysByFilter(ctx, param) |
| 190 | + if e != nil { |
| 191 | + return retryError(e) |
| 192 | + } |
| 193 | + keys = results |
| 194 | + return nil |
| 195 | + }) |
| 196 | + if err != nil { |
| 197 | + log.Printf("[CRITAL]%s read KMS keys failed, reason:%+v", logId, err) |
| 198 | + return err |
| 199 | + } |
| 200 | + keyList := make([]map[string]interface{}, 0, len(keys)) |
| 201 | + ids := make([]string, 0, len(keys)) |
| 202 | + for _, key := range keys { |
| 203 | + mapping := map[string]interface{}{ |
| 204 | + "key_id": key.KeyId, |
| 205 | + "alias": key.Alias, |
| 206 | + "create_time": key.CreateTime, |
| 207 | + "description": key.Description, |
| 208 | + "key_state": key.KeyState, |
| 209 | + "key_usage": key.KeyUsage, |
| 210 | + "creator_uin": key.CreatorUin, |
| 211 | + "key_rotation_enabled": key.KeyRotationEnabled, |
| 212 | + "owner": key.Owner, |
| 213 | + "next_rotate_time": key.NextRotateTime, |
| 214 | + "deletion_date": key.DeletionDate, |
| 215 | + "origin": key.Origin, |
| 216 | + "valid_to": key.ValidTo, |
| 217 | + } |
| 218 | + |
| 219 | + keyList = append(keyList, mapping) |
| 220 | + ids = append(ids, *key.KeyId) |
| 221 | + } |
| 222 | + |
| 223 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 224 | + if e := d.Set("key_list", keyList); e != nil { |
| 225 | + log.Printf("[CRITAL]%s provider set KMS key list fail, reason:%+v", logId, e) |
| 226 | + return e |
| 227 | + } |
| 228 | + if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" { |
| 229 | + return writeToFile(output.(string), keyList) |
| 230 | + } |
| 231 | + return nil |
| 232 | +} |
0 commit comments