|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of kms get_parameters_for_import |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_kms_get_parameters_for_import" "example" { |
| 8 | + key_id = "786aea8c-4aec-11ee-b601-525400281a45" |
| 9 | + wrapping_algorithm = "RSAES_OAEP_SHA_1" |
| 10 | + wrapping_key_spec = "RSA_2048" |
| 11 | +} |
| 12 | +``` |
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 21 | + kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudKmsGetParametersForImport() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudKmsGetParametersForImportRead, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "key_id": { |
| 30 | + Required: true, |
| 31 | + Type: schema.TypeString, |
| 32 | + Description: "CMK unique identifier.", |
| 33 | + }, |
| 34 | + "wrapping_algorithm": { |
| 35 | + Required: true, |
| 36 | + Type: schema.TypeString, |
| 37 | + Description: "Specifies the algorithm for encrypting key material, currently supports RSAES_PKCS1_V1_5, RSAES_OAEP_SHA_1, RSAES_OAEP_SHA_256.", |
| 38 | + }, |
| 39 | + "wrapping_key_spec": { |
| 40 | + Required: true, |
| 41 | + Type: schema.TypeString, |
| 42 | + Description: "Specifies the type of encryption key material, currently only supports RSA_2048.", |
| 43 | + }, |
| 44 | + "public_key": { |
| 45 | + Computed: true, |
| 46 | + Type: schema.TypeString, |
| 47 | + Description: "Base64-encoded public key content.", |
| 48 | + }, |
| 49 | + "import_token": { |
| 50 | + Computed: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "The token required for importing key material is used as the parameter of ImportKeyMaterial.", |
| 53 | + }, |
| 54 | + "parameters_valid_to": { |
| 55 | + Computed: true, |
| 56 | + Type: schema.TypeInt, |
| 57 | + Description: "The validity period of the exported token and public key cannot be imported after this period, and you need to call GetParametersForImport again to obtain it.", |
| 58 | + }, |
| 59 | + "result_output_file": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Optional: true, |
| 62 | + Description: "Used to save results.", |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func dataSourceTencentCloudKmsGetParametersForImportRead(d *schema.ResourceData, meta interface{}) error { |
| 69 | + defer logElapsed("data_source.tencentcloud_kms_get_parameters_for_import.read")() |
| 70 | + defer inconsistentCheck(d, meta)() |
| 71 | + |
| 72 | + var ( |
| 73 | + logId = getLogId(contextNil) |
| 74 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 75 | + service = KmsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 76 | + getParametersForImport *kms.GetParametersForImportResponseParams |
| 77 | + keyId string |
| 78 | + ) |
| 79 | + |
| 80 | + paramMap := make(map[string]interface{}) |
| 81 | + if v, ok := d.GetOk("key_id"); ok { |
| 82 | + paramMap["KeyId"] = helper.String(v.(string)) |
| 83 | + keyId = v.(string) |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := d.GetOk("wrapping_algorithm"); ok { |
| 87 | + paramMap["WrappingAlgorithm"] = helper.String(v.(string)) |
| 88 | + } |
| 89 | + |
| 90 | + if v, ok := d.GetOk("wrapping_key_spec"); ok { |
| 91 | + paramMap["WrappingKeySpec"] = helper.String(v.(string)) |
| 92 | + } |
| 93 | + |
| 94 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 95 | + result, e := service.DescribeKmsGetParametersForImportByFilter(ctx, paramMap) |
| 96 | + if e != nil { |
| 97 | + return retryError(e) |
| 98 | + } |
| 99 | + |
| 100 | + getParametersForImport = result |
| 101 | + return nil |
| 102 | + }) |
| 103 | + |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + if getParametersForImport.KeyId != nil { |
| 109 | + _ = d.Set("key_id", getParametersForImport.KeyId) |
| 110 | + } |
| 111 | + |
| 112 | + if getParametersForImport.PublicKey != nil { |
| 113 | + _ = d.Set("public_key", getParametersForImport.PublicKey) |
| 114 | + } |
| 115 | + |
| 116 | + if getParametersForImport.ImportToken != nil { |
| 117 | + _ = d.Set("import_token", getParametersForImport.ImportToken) |
| 118 | + } |
| 119 | + |
| 120 | + if getParametersForImport.ParametersValidTo != nil { |
| 121 | + _ = d.Set("parameters_valid_to", getParametersForImport.ParametersValidTo) |
| 122 | + } |
| 123 | + |
| 124 | + d.SetId(keyId) |
| 125 | + output, ok := d.GetOk("result_output_file") |
| 126 | + if ok && output.(string) != "" { |
| 127 | + if e := writeToFile(output.(string), d); e != nil { |
| 128 | + return e |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments