|
| 1 | +/* |
| 2 | +Provide a datasource to query cluster CommonNames. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_kubernetes_cluster_common_names" "foo" { |
| 8 | + cluster_id = "cls-12345678" |
| 9 | + subaccount_uins = ["1234567890", "0987654321"] |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +
|
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 21 | + tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func datasourceTencentCloudKubernetesClusterCommonNames() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: datasourceTencentCloudKubernetesClusterCommonNamesRead, |
| 28 | + Importer: &schema.ResourceImporter{ |
| 29 | + State: schema.ImportStatePassthrough, |
| 30 | + }, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "cluster_id": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + Description: "Cluster ID.", |
| 36 | + }, |
| 37 | + "subaccount_uins": { |
| 38 | + Type: schema.TypeList, |
| 39 | + Optional: true, |
| 40 | + Description: "List of sub-account. Up to 50 sub-accounts can be passed in at a time.", |
| 41 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 42 | + }, |
| 43 | + "role_ids": { |
| 44 | + Type: schema.TypeList, |
| 45 | + Optional: true, |
| 46 | + Description: "List of Role ID. Up to 50 sub-accounts can be passed in at a time.", |
| 47 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 48 | + }, |
| 49 | + "result_output_file": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + Description: "Used for save result.", |
| 53 | + }, |
| 54 | + "list": { |
| 55 | + Type: schema.TypeList, |
| 56 | + Computed: true, |
| 57 | + Description: "List of the CommonName in the certificate of the client corresponding to the sub-account UIN.", |
| 58 | + Elem: &schema.Resource{ |
| 59 | + Schema: map[string]*schema.Schema{ |
| 60 | + "subaccount_uin": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "User UIN.", |
| 64 | + }, |
| 65 | + "common_names": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "The CommonName in the certificate of the client corresponding to the sub-account.", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func datasourceTencentCloudKubernetesClusterCommonNamesRead(d *schema.ResourceData, meta interface{}) error { |
| 78 | + defer logElapsed("datasource.tencentcloud_kubernetes_cluster_common_names.read")() |
| 79 | + defer inconsistentCheck(d, meta)() |
| 80 | + |
| 81 | + logId := getLogId(contextNil) |
| 82 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 83 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 84 | + service := TkeService{client} |
| 85 | + |
| 86 | + clusterId := d.Get("cluster_id").(string) |
| 87 | + request := tke.NewDescribeClusterCommonNamesRequest() |
| 88 | + request.ClusterId = &clusterId |
| 89 | + |
| 90 | + if v, ok := d.GetOk("subaccount_uins"); ok { |
| 91 | + request.SubaccountUins = helper.InterfacesStringsPoint(v.([]interface{})) |
| 92 | + } |
| 93 | + if v, ok := d.GetOk("role_ids"); ok { |
| 94 | + request.RoleIds = helper.InterfacesStringsPoint(v.([]interface{})) |
| 95 | + } |
| 96 | + |
| 97 | + names, err := service.DescribeClusterCommonNames(ctx, request) |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + result := make([]interface{}, 0, len(names)) |
| 104 | + cns := make([]string, 0) |
| 105 | + |
| 106 | + for i := range names { |
| 107 | + cn := names[i] |
| 108 | + result = append(result, map[string]interface{}{ |
| 109 | + "subaccount_uin": cn.SubaccountUin, |
| 110 | + "common_names": cn.CN, |
| 111 | + }) |
| 112 | + cns = append(cns, *cn.CN) |
| 113 | + } |
| 114 | + |
| 115 | + if err := d.Set("list", result); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + d.SetId(clusterId + FILED_SP + helper.DataResourceIdsHash(cns)) |
| 120 | + |
| 121 | + if output, ok := d.GetOk("result_output_file"); ok { |
| 122 | + return writeToFile(output.(string), result) |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
0 commit comments