|
| 1 | +/* |
| 2 | +Use this data source to query elastic kubernetes cluster resource. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +``` |
| 7 | +data "tencentcloud_eks_clusters" "foo" { |
| 8 | + cluster_id = "cls-xxxxxxxx" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 18 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 19 | + "log" |
| 20 | +) |
| 21 | + |
| 22 | +func dataSourceTencentCloudEKSClusters() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Read: dataSourceTencentCloudEKSClustersRead, |
| 25 | + |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "cluster_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + ConflictsWith: []string{"cluster_name"}, |
| 30 | + Description: "ID of the cluster. Conflict with cluster_name, can not be set at the same time.", |
| 31 | + Optional: true, |
| 32 | + }, |
| 33 | + "cluster_name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + ConflictsWith: []string{"cluster_id"}, |
| 36 | + Optional: true, |
| 37 | + Description: "Name of the cluster. Conflict with cluster_id, can not be set at the same time.", |
| 38 | + }, |
| 39 | + "result_output_file": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Description: "Used to save results.", |
| 43 | + }, |
| 44 | + "list": { |
| 45 | + Type: schema.TypeList, |
| 46 | + Computed: true, |
| 47 | + Description: "EKS cluster list.", |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "cluster_id": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "ID of the cluster.", |
| 54 | + }, |
| 55 | + "cluster_name": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "Name of the cluster.", |
| 59 | + }, |
| 60 | + "cluster_desc": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "Description of the cluster.", |
| 64 | + }, |
| 65 | + "vpc_id": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "Vpc id.", |
| 69 | + }, |
| 70 | + "subnet_ids": { |
| 71 | + Type: schema.TypeList, |
| 72 | + Computed: true, |
| 73 | + Description: "Subnet id list.", |
| 74 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 75 | + }, |
| 76 | + "k8s_version": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "EKS cluster kubernetes version.", |
| 80 | + }, |
| 81 | + "status": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "EKS status.", |
| 85 | + }, |
| 86 | + "created_time": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + Description: "Create time of the clusters.", |
| 90 | + }, |
| 91 | + "service_subnet_id": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + Description: "Subnet id of service.", |
| 95 | + }, |
| 96 | + "dns_servers": { |
| 97 | + Type: schema.TypeList, |
| 98 | + Computed: true, |
| 99 | + Description: "List of cluster custom DNS Server info.", |
| 100 | + Elem: &schema.Resource{ |
| 101 | + Schema: map[string]*schema.Schema{ |
| 102 | + "domain": { |
| 103 | + Type: schema.TypeString, |
| 104 | + Computed: true, |
| 105 | + Description: "DNS Server domain. Empty indicates all domain.", |
| 106 | + }, |
| 107 | + "servers": { |
| 108 | + Type: schema.TypeList, |
| 109 | + Computed: true, |
| 110 | + Description: "List of DNS Server IP address.", |
| 111 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + "need_delete_cbs": { |
| 117 | + Type: schema.TypeBool, |
| 118 | + Computed: true, |
| 119 | + Description: "Indicates whether to delete CBS after EKS cluster remove.", |
| 120 | + }, |
| 121 | + "enable_vpc_core_dns": { |
| 122 | + Type: schema.TypeBool, |
| 123 | + Computed: true, |
| 124 | + Description: "Indicates whether to enable dns in user cluster, default value is `true`.", |
| 125 | + }, |
| 126 | + "tags": { |
| 127 | + Type: schema.TypeMap, |
| 128 | + Computed: true, |
| 129 | + Description: "Tags of EKS cluster.", |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func dataSourceTencentCloudEKSClustersRead(d *schema.ResourceData, meta interface{}) error { |
| 139 | + defer logElapsed("data_source.tencentcloud_eks_clusters.read")() |
| 140 | + |
| 141 | + logId := getLogId(contextNil) |
| 142 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 143 | + |
| 144 | + service := EksService{ |
| 145 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 146 | + } |
| 147 | + |
| 148 | + var ( |
| 149 | + id string |
| 150 | + name string |
| 151 | + ) |
| 152 | + |
| 153 | + if v, ok := d.GetOk("cluster_id"); ok { |
| 154 | + id = v.(string) |
| 155 | + } |
| 156 | + |
| 157 | + if v, ok := d.GetOk("cluster_name"); ok { |
| 158 | + name = v.(string) |
| 159 | + } |
| 160 | + |
| 161 | + tags := helper.GetTags(d, "tags") |
| 162 | + |
| 163 | + infos, err := service.DescribeEKSClusters(ctx, id, name) |
| 164 | + if err != nil && id == "" { |
| 165 | + err = resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 166 | + infos, err = service.DescribeEKSClusters(ctx, id, name) |
| 167 | + if err != nil { |
| 168 | + return retryError(err) |
| 169 | + } |
| 170 | + return nil |
| 171 | + }) |
| 172 | + } |
| 173 | + |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + |
| 178 | + list := make([]map[string]interface{}, 0, len(infos)) |
| 179 | + ids := make([]string, 0, len(infos)) |
| 180 | + |
| 181 | +LOOP: |
| 182 | + for _, info := range infos { |
| 183 | + if len(tags) > 0 { |
| 184 | + for k, v := range tags { |
| 185 | + if info.Tags[k] != v { |
| 186 | + continue LOOP |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + var infoMap = map[string]interface{}{ |
| 191 | + "cluster_id": info.ClusterId, |
| 192 | + "cluster_name": info.ClusterName, |
| 193 | + "cluster_desc": info.ClusterDesc, |
| 194 | + "vpc_id": info.VpcId, |
| 195 | + "subnet_ids": info.SubnetIds, |
| 196 | + "dns_servers": info.DnsServers, |
| 197 | + "k8s_version": info.K8SVersion, |
| 198 | + "status": info.Status, |
| 199 | + "created_time": info.CreatedTime, |
| 200 | + "service_subnet_id": info.ServiceSubnetId, |
| 201 | + "need_delete_cbs": info.NeedDeleteCbs, |
| 202 | + "enable_vpc_core_dns": info.EnableVpcCoreDNS, |
| 203 | + } |
| 204 | + |
| 205 | + list = append(list, infoMap) |
| 206 | + ids = append(ids, info.ClusterId) |
| 207 | + } |
| 208 | + |
| 209 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 210 | + err = d.Set("list", list) |
| 211 | + if err != nil { |
| 212 | + log.Printf("[CRITAL]%s provider set tencentcloud_eks_clusters list fail, reason:%s\n ", logId, err.Error()) |
| 213 | + return err |
| 214 | + } |
| 215 | + |
| 216 | + output, ok := d.GetOk("result_output_file") |
| 217 | + if ok && output.(string) != "" { |
| 218 | + if err = writeToFile(output.(string), list); err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + } |
| 222 | + return nil |
| 223 | +} |
0 commit comments