|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of kms list_algorithms |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_kms_list_algorithms" "example" {} |
| 8 | +``` |
| 9 | +*/ |
| 10 | +package tencentcloud |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "strconv" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 19 | + kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118" |
| 20 | +) |
| 21 | + |
| 22 | +func dataSourceTencentCloudKmsListAlgorithms() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Read: dataSourceTencentCloudKmsListAlgorithmsRead, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "symmetric_algorithms": { |
| 27 | + Computed: true, |
| 28 | + Type: schema.TypeList, |
| 29 | + Description: "Symmetric encryption algorithms supported in this region.", |
| 30 | + Elem: &schema.Resource{ |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "key_usage": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + Description: "Key usage.", |
| 36 | + }, |
| 37 | + "algorithm": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + Description: "Algorithm.", |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + "asymmetric_algorithms": { |
| 46 | + Computed: true, |
| 47 | + Type: schema.TypeList, |
| 48 | + Description: "Asymmetric encryption algorithms supported in this region.", |
| 49 | + Elem: &schema.Resource{ |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "key_usage": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "Key usage.", |
| 55 | + }, |
| 56 | + "algorithm": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "Algorithm.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + "asymmetric_sign_verify_algorithms": { |
| 65 | + Computed: true, |
| 66 | + Type: schema.TypeList, |
| 67 | + Description: "Asymmetric signature verification algorithms supported in this region.", |
| 68 | + Elem: &schema.Resource{ |
| 69 | + Schema: map[string]*schema.Schema{ |
| 70 | + "key_usage": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "Key usage.", |
| 74 | + }, |
| 75 | + "algorithm": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "Algorithm.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + "result_output_file": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Optional: true, |
| 86 | + Description: "Used to save results.", |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func dataSourceTencentCloudKmsListAlgorithmsRead(d *schema.ResourceData, meta interface{}) error { |
| 93 | + defer logElapsed("data_source.tencentcloud_kms_list_algorithms.read")() |
| 94 | + defer inconsistentCheck(d, meta)() |
| 95 | + |
| 96 | + var ( |
| 97 | + logId = getLogId(contextNil) |
| 98 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 99 | + service = KmsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 100 | + listAlgorithms *kms.ListAlgorithmsResponseParams |
| 101 | + ) |
| 102 | + |
| 103 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 104 | + result, e := service.DescribeKmsListAlgorithmsByFilter(ctx) |
| 105 | + if e != nil { |
| 106 | + return retryError(e) |
| 107 | + } |
| 108 | + |
| 109 | + listAlgorithms = result |
| 110 | + return nil |
| 111 | + }) |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + if listAlgorithms.SymmetricAlgorithms != nil { |
| 118 | + tmpList := make([]map[string]interface{}, 0, len(listAlgorithms.SymmetricAlgorithms)) |
| 119 | + for _, item := range listAlgorithms.SymmetricAlgorithms { |
| 120 | + itemMap := map[string]interface{}{} |
| 121 | + if item.KeyUsage != nil { |
| 122 | + itemMap["key_usage"] = item.KeyUsage |
| 123 | + } |
| 124 | + |
| 125 | + if item.Algorithm != nil { |
| 126 | + itemMap["algorithm"] = item.Algorithm |
| 127 | + } |
| 128 | + |
| 129 | + tmpList = append(tmpList, itemMap) |
| 130 | + } |
| 131 | + |
| 132 | + _ = d.Set("symmetric_algorithms", tmpList) |
| 133 | + } |
| 134 | + |
| 135 | + if listAlgorithms.AsymmetricAlgorithms != nil { |
| 136 | + tmpList := make([]map[string]interface{}, 0, len(listAlgorithms.AsymmetricAlgorithms)) |
| 137 | + for _, item := range listAlgorithms.AsymmetricAlgorithms { |
| 138 | + itemMap := map[string]interface{}{} |
| 139 | + if item.KeyUsage != nil { |
| 140 | + itemMap["key_usage"] = item.KeyUsage |
| 141 | + } |
| 142 | + |
| 143 | + if item.Algorithm != nil { |
| 144 | + itemMap["algorithm"] = item.Algorithm |
| 145 | + } |
| 146 | + |
| 147 | + tmpList = append(tmpList, itemMap) |
| 148 | + } |
| 149 | + |
| 150 | + _ = d.Set("asymmetric_algorithms", tmpList) |
| 151 | + } |
| 152 | + |
| 153 | + if listAlgorithms.AsymmetricSignVerifyAlgorithms != nil { |
| 154 | + tmpList := make([]map[string]interface{}, 0, len(listAlgorithms.AsymmetricSignVerifyAlgorithms)) |
| 155 | + for _, item := range listAlgorithms.AsymmetricSignVerifyAlgorithms { |
| 156 | + itemMap := map[string]interface{}{} |
| 157 | + if item.KeyUsage != nil { |
| 158 | + itemMap["key_usage"] = item.KeyUsage |
| 159 | + } |
| 160 | + |
| 161 | + if item.Algorithm != nil { |
| 162 | + itemMap["algorithm"] = item.Algorithm |
| 163 | + } |
| 164 | + |
| 165 | + tmpList = append(tmpList, itemMap) |
| 166 | + } |
| 167 | + |
| 168 | + _ = d.Set("asymmetric_sign_verify_algorithms", tmpList) |
| 169 | + } |
| 170 | + |
| 171 | + d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) |
| 172 | + output, ok := d.GetOk("result_output_file") |
| 173 | + if ok && output.(string) != "" { |
| 174 | + if e := writeToFile(output.(string), d); e != nil { |
| 175 | + return e |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
0 commit comments