|
| 1 | +/* |
| 2 | +Provide a datasource to query EKS cluster credential info. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_eks_cluster_credential" "foo" { |
| 8 | + cluster_id = "cls-xxxxxxxx" |
| 9 | +} |
| 10 | +
|
| 11 | +# example outputs |
| 12 | +output "addresses" { |
| 13 | + value = data.tencentcloud_eks_cluster_credential.cred.addresses |
| 14 | +} |
| 15 | +
|
| 16 | +output "ca_cert" { |
| 17 | + value = data.tencentcloud_eks_cluster_credential.cred.credential.ca_cert |
| 18 | +} |
| 19 | +
|
| 20 | +output "token" { |
| 21 | + value = data.tencentcloud_eks_cluster_credential.cred.credential.token |
| 22 | +} |
| 23 | +
|
| 24 | +output "public_lb_param" { |
| 25 | + value = data.tencentcloud_eks_cluster_credential.cred.public_lb.0.extra_param |
| 26 | +} |
| 27 | +
|
| 28 | +output "internal_lb_subnet" { |
| 29 | + value = data.tencentcloud_eks_cluster_credential.cred.internal_lb.0.subnet_id |
| 30 | +} |
| 31 | +
|
| 32 | +``` |
| 33 | +*/ |
| 34 | +package tencentcloud |
| 35 | + |
| 36 | +import ( |
| 37 | + "context" |
| 38 | + |
| 39 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 40 | + tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" |
| 41 | +) |
| 42 | + |
| 43 | +func datasourceTencentCloudEksClusterCredential() *schema.Resource { |
| 44 | + return &schema.Resource{ |
| 45 | + Read: datasourceTencentCloudEksClusterCredentialRead, |
| 46 | + |
| 47 | + Importer: &schema.ResourceImporter{ |
| 48 | + State: schema.ImportStatePassthrough, |
| 49 | + }, |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "cluster_id": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + Description: "EKS Cluster ID.", |
| 55 | + }, |
| 56 | + "result_output_file": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + Description: "Used for save result.", |
| 60 | + }, |
| 61 | + "addresses": { |
| 62 | + Type: schema.TypeList, |
| 63 | + Computed: true, |
| 64 | + Description: "List of IP Address information.", |
| 65 | + Elem: &schema.Resource{ |
| 66 | + Schema: map[string]*schema.Schema{ |
| 67 | + "type": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "Type of IP, can be `advertise`, `public`, etc.", |
| 71 | + }, |
| 72 | + "ip": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "IP Address.", |
| 76 | + }, |
| 77 | + "port": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Port.", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + "credential": { |
| 86 | + Type: schema.TypeMap, |
| 87 | + Computed: true, |
| 88 | + Description: "Credential info.", |
| 89 | + Elem: &schema.Resource{ |
| 90 | + Schema: map[string]*schema.Schema{ |
| 91 | + "ca_cert": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + Description: "CA root certification.", |
| 95 | + }, |
| 96 | + "token": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Computed: true, |
| 99 | + Description: "Certification token.", |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + "public_lb": { |
| 105 | + Type: schema.TypeList, |
| 106 | + Computed: true, |
| 107 | + Description: "Cluster public access LoadBalancer info.", |
| 108 | + Elem: &schema.Resource{ |
| 109 | + Schema: map[string]*schema.Schema{ |
| 110 | + "enabled": { |
| 111 | + Type: schema.TypeBool, |
| 112 | + Computed: true, |
| 113 | + |
| 114 | + Description: "Indicates weather the public access LB enabled.", |
| 115 | + }, |
| 116 | + "allow_from_cidrs": { |
| 117 | + Type: schema.TypeList, |
| 118 | + Computed: true, |
| 119 | + Description: "List of CIDRs which allowed to access.", |
| 120 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 121 | + }, |
| 122 | + "security_policies": { |
| 123 | + Type: schema.TypeList, |
| 124 | + Computed: true, |
| 125 | + Description: "List of security allow IP or CIDRs, default deny all.", |
| 126 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 127 | + }, |
| 128 | + "extra_param": { |
| 129 | + Type: schema.TypeString, |
| 130 | + Computed: true, |
| 131 | + Description: "Extra param text json.", |
| 132 | + }, |
| 133 | + "security_group": { |
| 134 | + Type: schema.TypeString, |
| 135 | + Computed: true, |
| 136 | + Description: "Security group.", |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + "internal_lb": { |
| 142 | + Type: schema.TypeList, |
| 143 | + Computed: true, |
| 144 | + Description: "Cluster internal access LoadBalancer info.", |
| 145 | + Elem: &schema.Resource{ |
| 146 | + Schema: map[string]*schema.Schema{ |
| 147 | + "enabled": { |
| 148 | + Type: schema.TypeBool, |
| 149 | + Computed: true, |
| 150 | + Description: "Indicates weather the internal access LB enabled.", |
| 151 | + }, |
| 152 | + "subnet_id": { |
| 153 | + Type: schema.TypeString, |
| 154 | + Computed: true, |
| 155 | + Description: "ID of subnet which related to Internal LB.", |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + "proxy_lb": { |
| 161 | + Type: schema.TypeBool, |
| 162 | + Computed: true, |
| 163 | + Description: "Indicates whether the new internal/public network function.", |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func datasourceTencentCloudEksClusterCredentialRead(d *schema.ResourceData, meta interface{}) error { |
| 170 | + defer logElapsed("datasource.tencentcloud_eks_cluster_credential.read")() |
| 171 | + defer inconsistentCheck(d, meta)() |
| 172 | + |
| 173 | + logId := getLogId(contextNil) |
| 174 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 175 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 176 | + service := EksService{client: client} |
| 177 | + |
| 178 | + clusterId := d.Get("cluster_id").(string) |
| 179 | + |
| 180 | + request := tke.NewDescribeEKSClusterCredentialRequest() |
| 181 | + request.ClusterId = &clusterId |
| 182 | + |
| 183 | + info, err := service.DescribeEKSClusterCredential(ctx, request) |
| 184 | + |
| 185 | + if err != nil { |
| 186 | + d.SetId("") |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + d.SetId("eks-cluster-credential-" + clusterId) |
| 191 | + |
| 192 | + _ = d.Set("proxy_lb", info.ProxyLB) |
| 193 | + |
| 194 | + addresses := make([]map[string]interface{}, 0) |
| 195 | + |
| 196 | + for i := range info.Addresses { |
| 197 | + item := info.Addresses[i] |
| 198 | + addr := map[string]interface{}{ |
| 199 | + "type": item.Type, |
| 200 | + "ip": item.Ip, |
| 201 | + "port": item.Port, |
| 202 | + } |
| 203 | + addresses = append(addresses, addr) |
| 204 | + } |
| 205 | + _ = d.Set("addresses", addresses) |
| 206 | + |
| 207 | + credential := make(map[string]interface{}) |
| 208 | + if info.Credential != nil { |
| 209 | + credential = map[string]interface{}{ |
| 210 | + "token": info.Credential.Token, |
| 211 | + "ca_cert": info.Credential.CACert, |
| 212 | + } |
| 213 | + _ = d.Set("credential", credential) |
| 214 | + } |
| 215 | + |
| 216 | + internalLB := make([]map[string]interface{}, 0) |
| 217 | + if info.InternalLB != nil { |
| 218 | + lb := map[string]interface{}{ |
| 219 | + "enabled": info.InternalLB.Enabled, |
| 220 | + "subnet_id": info.InternalLB.SubnetId, |
| 221 | + } |
| 222 | + internalLB = append(internalLB, lb) |
| 223 | + _ = d.Set("internal_lb", internalLB) |
| 224 | + } |
| 225 | + |
| 226 | + publicLB := make([]map[string]interface{}, 0) |
| 227 | + if info.PublicLB != nil { |
| 228 | + lb := map[string]interface{}{ |
| 229 | + "enabled": info.PublicLB.Enabled, |
| 230 | + "extra_param": info.PublicLB.ExtraParam, |
| 231 | + "allow_from_cidrs": info.PublicLB.AllowFromCidrs, |
| 232 | + "security_group": info.PublicLB.SecurityGroup, |
| 233 | + "security_policies": info.PublicLB.SecurityPolicies, |
| 234 | + } |
| 235 | + publicLB = append(publicLB, lb) |
| 236 | + _ = d.Set("public_lb", publicLB) |
| 237 | + } |
| 238 | + |
| 239 | + result := map[string]interface{}{ |
| 240 | + "credential": credential, |
| 241 | + "addresses": addresses, |
| 242 | + "public_lb": publicLB, |
| 243 | + "internal_lb": internalLB, |
| 244 | + "proxy_lb": info.ProxyLB, |
| 245 | + } |
| 246 | + |
| 247 | + if output, ok := d.GetOk("result_output_file"); ok { |
| 248 | + return writeToFile(output.(string), result) |
| 249 | + } |
| 250 | + |
| 251 | + return nil |
| 252 | +} |
0 commit comments