/
data_source_edgecenter_k8s_client_config.go
99 lines (85 loc) · 3.03 KB
/
data_source_edgecenter_k8s_client_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package edgecenter
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/k8s/v1/clusters"
)
func dataSourceK8sClientConfig() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceK8sReadClientConfig,
Description: "Represent k8s cluster with one default pool.",
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"project_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"region_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"region_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: "The uuid of the Kubernetes cluster.",
},
"client_certificate_data": {
Type: schema.TypeString,
Computed: true,
Description: "The client_certificate_data field from k8s config.",
},
"client_key_data": {
Type: schema.TypeString,
Computed: true,
Description: "The client_key_data field from k8s config.",
},
},
}
}
func dataSourceK8sReadClientConfig(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start K8s client config reading")
var diags diag.Diagnostics
config := m.(*Config)
provider := config.Provider
client, err := CreateClient(provider, d, K8sPoint, VersionPointV1)
if err != nil {
return diag.FromErr(err)
}
clusterID := d.Get("cluster_id").(string)
d.SetId(clusterID)
getConfigResult, err := clusters.GetConfig(client, clusterID).Extract()
if err != nil {
return diag.FromErr(err)
}
clusterConfig, err := parseK8sConfig(getConfigResult.Config)
if err != nil {
return diag.Errorf("failed to parse k8s config: %s", err)
}
clientCertificateData := clusterConfig.Users[0].User.ClientCertificateData
if err := d.Set("client_certificate_data", clientCertificateData); err != nil {
return diag.Errorf("couldn't get client_certificate_data: %s", err)
}
clientKeyData := clusterConfig.Users[0].User.ClientKeyData
if err := d.Set("client_key_data", clientKeyData); err != nil {
return diag.Errorf("couldn't get client_key_data: %s", err)
}
log.Println("[DEBUG] Finish K8s client config reading")
return diags
}