|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of kms public_key |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_kms_public_key" "example" { |
| 8 | + key_id = tencentcloud_kms_key.example.id |
| 9 | +} |
| 10 | +
|
| 11 | +resource "tencentcloud_kms_key" "example" { |
| 12 | + alias = "tf-example-kms-key" |
| 13 | + description = "example of kms key" |
| 14 | + key_usage = "ASYMMETRIC_DECRYPT_RSA_2048" |
| 15 | + is_enabled = true |
| 16 | + pending_delete_window_in_days = 7 |
| 17 | +} |
| 18 | +``` |
| 19 | +*/ |
| 20 | +package tencentcloud |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + |
| 25 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 27 | + kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118" |
| 28 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 29 | +) |
| 30 | + |
| 31 | +func dataSourceTencentCloudKmsPublicKey() *schema.Resource { |
| 32 | + return &schema.Resource{ |
| 33 | + Read: dataSourceTencentCloudKmsPublicKeyRead, |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "key_id": { |
| 36 | + Required: true, |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "CMK unique identifier.", |
| 39 | + }, |
| 40 | + "public_key": { |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "Base64-encoded public key content.", |
| 44 | + }, |
| 45 | + "public_key_pem": { |
| 46 | + Computed: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "Public key content in PEM format.", |
| 49 | + }, |
| 50 | + "result_output_file": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Description: "Used to save results.", |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func dataSourceTencentCloudKmsPublicKeyRead(d *schema.ResourceData, meta interface{}) error { |
| 60 | + defer logElapsed("data_source.tencentcloud_kms_public_key.read")() |
| 61 | + defer inconsistentCheck(d, meta)() |
| 62 | + |
| 63 | + var ( |
| 64 | + logId = getLogId(contextNil) |
| 65 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 66 | + service = KmsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 67 | + publicKey *kms.GetPublicKeyResponseParams |
| 68 | + keyId string |
| 69 | + ) |
| 70 | + |
| 71 | + paramMap := make(map[string]interface{}) |
| 72 | + if v, ok := d.GetOk("key_id"); ok { |
| 73 | + paramMap["KeyId"] = helper.String(v.(string)) |
| 74 | + keyId = v.(string) |
| 75 | + } |
| 76 | + |
| 77 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 78 | + result, e := service.DescribeKmsPublicKeyByFilter(ctx, paramMap) |
| 79 | + if e != nil { |
| 80 | + return retryError(e) |
| 81 | + } |
| 82 | + |
| 83 | + publicKey = result |
| 84 | + return nil |
| 85 | + }) |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + if publicKey.KeyId != nil { |
| 92 | + _ = d.Set("key_id", publicKey.KeyId) |
| 93 | + } |
| 94 | + |
| 95 | + if publicKey.PublicKey != nil { |
| 96 | + _ = d.Set("public_key", publicKey.PublicKey) |
| 97 | + } |
| 98 | + |
| 99 | + if publicKey.PublicKeyPem != nil { |
| 100 | + _ = d.Set("public_key_pem", publicKey.PublicKeyPem) |
| 101 | + } |
| 102 | + |
| 103 | + d.SetId(keyId) |
| 104 | + output, ok := d.GetOk("result_output_file") |
| 105 | + if ok && output.(string) != "" { |
| 106 | + if e := writeToFile(output.(string), d); e != nil { |
| 107 | + return e |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
0 commit comments