-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_container_worker_pool.go
171 lines (150 loc) · 4.48 KB
/
data_source_ibm_container_worker_pool.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kubernetes
import (
"strings"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMContainerWorkerPool() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMContainerWorkerPoolRead,
Schema: map[string]*schema.Schema{
"cluster": {
Type: schema.TypeString,
Required: true,
Description: "Name or ID of the cluster",
ValidateFunc: validate.InvokeDataSourceValidator(
"ibm_container_worker_pool",
"cluster"),
},
"worker_pool_name": {
Type: schema.TypeString,
Required: true,
Description: "worker pool name",
},
"machine_type": {
Type: schema.TypeString,
Computed: true,
Description: "worker nodes machine type",
},
"size_per_zone": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of nodes per zone",
},
"hardware": {
Type: schema.TypeString,
Computed: true,
Description: "Hardware type",
},
"disk_encryption": {
Type: schema.TypeBool,
Computed: true,
Description: "worker node disk encrypted if set to true",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "worker pool state",
},
"zones": {
Type: schema.TypeList,
Computed: true,
Description: "worker pool zones",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Computed: true,
Description: "worker pool zone name",
},
"private_vlan": {
Type: schema.TypeString,
Computed: true,
Description: "worker pool zone private vlan",
},
"public_vlan": {
Type: schema.TypeString,
Computed: true,
Description: "worker pool zone public vlan",
},
"worker_count": {
Type: schema.TypeInt,
Computed: true,
Description: "worker pool zone worker count",
},
},
},
},
"labels": {
Type: schema.TypeMap,
Computed: true,
Description: "list of labels to worker pool",
},
"resource_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the resource group.",
},
},
}
}
func DataSourceIBMContainerWorkerPoolValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "cluster",
ValidateFunctionIdentifier: validate.ValidateCloudData,
Type: validate.TypeString,
Required: true,
CloudDataType: "cluster",
CloudDataRange: []string{"resolved_to:name"}})
iBMContainerWorkerPoolValidator := validate.ResourceValidator{ResourceName: "ibm_container_worker_pool", Schema: validateSchema}
return &iBMContainerWorkerPoolValidator
}
func dataSourceIBMContainerWorkerPoolRead(d *schema.ResourceData, meta interface{}) error {
csClient, err := meta.(conns.ClientSession).ContainerAPI()
if err != nil {
return err
}
workerPoolName := d.Get("worker_pool_name").(string)
cluster := d.Get("cluster").(string)
workerPoolsAPI := csClient.WorkerPools()
targetEnv, err := getWorkerPoolTargetHeader(d, meta)
if err != nil {
return err
}
workerPool, err := workerPoolsAPI.GetWorkerPool(cluster, workerPoolName, targetEnv)
if err != nil {
return err
}
machineType := workerPool.MachineType
d.SetId(workerPool.ID)
d.Set("machine_type", strings.Split(machineType, ".encrypted")[0])
d.Set("size_per_zone", workerPool.Size)
hardware := workerPool.Isolation
switch strings.ToLower(hardware) {
case "":
hardware = hardwareShared
case isolationPrivate:
hardware = hardwareDedicated
case isolationPublic:
hardware = hardwareShared
}
d.Set("hardware", hardware)
d.Set("state", workerPool.State)
if workerPool.Labels != nil {
d.Set("labels", workerPool.Labels)
}
d.Set("zones", flex.FlattenZones(workerPool.Zones))
if strings.Contains(machineType, "encrypted") {
d.Set("disk_encryption", true)
} else {
d.Set("disk_encryption", false)
}
d.Set("resource_group_id", targetEnv.ResourceGroup)
return nil
}