/
data_source_ibm_container_cluster_config.go
180 lines (168 loc) · 5.42 KB
/
data_source_ibm_container_cluster_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package ibm
import (
"fmt"
"log"
"path/filepath"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
homedir "github.com/mitchellh/go-homedir"
v1 "github.com/IBM-Cloud/bluemix-go/api/container/containerv1"
"github.com/IBM-Cloud/bluemix-go/helpers"
)
func dataSourceIBMContainerClusterConfig() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMContainerClusterConfigRead,
Schema: map[string]*schema.Schema{
"org_guid": {
Description: "The bluemix organization guid this cluster belongs to",
Type: schema.TypeString,
Optional: true,
Deprecated: "This field is deprecated",
},
"space_guid": {
Description: "The bluemix space guid this cluster belongs to",
Type: schema.TypeString,
Optional: true,
Deprecated: "This field is deprecated",
},
"account_guid": {
Description: "The bluemix account guid this cluster belongs to",
Type: schema.TypeString,
Optional: true,
Deprecated: "This field is deprecated",
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: "The cluster region",
Deprecated: "This field is deprecated",
},
"resource_group_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the resource group.",
},
"cluster_name_id": {
Description: "The name/id of the cluster",
Type: schema.TypeString,
Required: true,
},
"config_dir": {
Description: "The directory where the cluster config to be downloaded. Default is home directory ",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"download": {
Description: "If set to false will not download the config, otherwise they are downloaded each time but onto the same path for a given cluster name/id",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"admin": {
Description: "If set to true will download the config for admin",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"network": {
Description: "If set to true will download the Calico network config with the Admin config",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"config_file_path": {
Description: "The absolute path to the kubernetes config yml file ",
Type: schema.TypeString,
Computed: true,
},
"calico_config_file_path": {
Description: "The absolute path to the calico network config file ",
Type: schema.TypeString,
Computed: true,
},
"admin_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"admin_certificate": {
Type: schema.TypeString,
Computed: true,
},
"ca_certificate": {
Type: schema.TypeString,
Computed: true,
},
"host": {
Type: schema.TypeString,
Computed: true,
},
"token": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceIBMContainerClusterConfigRead(d *schema.ResourceData, meta interface{}) error {
csClient, err := meta.(ClientSession).ContainerAPI()
if err != nil {
return err
}
csAPI := csClient.Clusters()
name := d.Get("cluster_name_id").(string)
download := d.Get("download").(bool)
admin := d.Get("admin").(bool)
configDir := d.Get("config_dir").(string)
network := d.Get("network").(bool)
if len(configDir) == 0 {
configDir, err = homedir.Dir()
if err != nil {
return fmt.Errorf("Error fetching homedir: %s", err)
}
}
configDir, _ = filepath.Abs(configDir)
var configPath string
if !download {
log.Println("Skipping download of the cluster config", "Going to check if it already exists")
expectedDir := v1.ComputeClusterConfigDir(configDir, name, admin)
configPath = filepath.Join(expectedDir, "config.yml")
if !helpers.FileExists(configPath) {
return fmt.Errorf(`Couldn't find the cluster config at expected path %s. Please set "download" to true to download the new config`, configPath)
}
d.Set("config_file_path", configPath)
} else {
targetEnv, err := getClusterTargetHeader(d, meta)
if err != nil {
return err
}
if network {
// For the Network config we need to gather the certs so we must override the admin value
calicoConfigFilePath, clusterKeyDetails, err := csAPI.StoreConfigDetail(name, configDir, admin || true, network, targetEnv)
if err != nil {
return fmt.Errorf("Error downloading the cluster config [%s]: %s", name, err)
}
d.Set("calico_config_file_path", calicoConfigFilePath)
d.Set("admin_key", clusterKeyDetails.AdminKey)
d.Set("admin_certificate", clusterKeyDetails.Admin)
d.Set("ca_certificate", clusterKeyDetails.ClusterCACertificate)
d.Set("host", clusterKeyDetails.Host)
d.Set("token", clusterKeyDetails.Token)
d.Set("config_file_path", clusterKeyDetails.FilePath)
} else {
clusterKeyDetails, err := csAPI.GetClusterConfigDetail(name, configDir, admin, targetEnv)
if err != nil {
return fmt.Errorf("Error downloading the cluster config [%s]: %s", name, err)
}
d.Set("admin_key", clusterKeyDetails.AdminKey)
d.Set("admin_certificate", clusterKeyDetails.Admin)
d.Set("ca_certificate", clusterKeyDetails.ClusterCACertificate)
d.Set("host", clusterKeyDetails.Host)
d.Set("token", clusterKeyDetails.Token)
d.Set("config_file_path", clusterKeyDetails.FilePath)
}
}
d.SetId(name)
d.Set("config_dir", configDir)
return nil
}