-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_satellite_location.go
236 lines (219 loc) · 6.72 KB
/
data_source_ibm_satellite_location.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package satellite
import (
"fmt"
"log"
"time"
"github.com/IBM-Cloud/container-services-go-sdk/kubernetesserviceapiv1"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMSatelliteLocation() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMSatelliteLocationRead,
Schema: map[string]*schema.Schema{
"location": {
Type: schema.TypeString,
Required: true,
Description: "A unique name for the new Satellite location",
},
"managed_from": {
Type: schema.TypeString,
Computed: true,
Description: "The IBM Cloud metro from which the Satellite location is managed",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "A description of the new Satellite location",
},
"coreos_enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "If Red Hat CoreOS features are enabled within the Satellite location",
},
"logging_account_id": {
Type: schema.TypeString,
Computed: true,
Description: "The account ID for IBM Log Analysis with LogDNA log forwarding",
},
"zones": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "The names of at least three high availability zones to use for the location",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "Location CRN",
},
"resource_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the resource group",
},
flex.ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "Name of the resource group",
},
"tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
},
"host_attached_count": {
Type: schema.TypeInt,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The total number of hosts that are attached to the Satellite location.",
},
"host_available_count": {
Type: schema.TypeInt,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The available number of hosts that can be assigned to a cluster resource in the Satellite location.",
},
"created_on": {
Type: schema.TypeString,
Computed: true,
Description: "Created Date",
},
"ingress_hostname": {
Type: schema.TypeString,
Computed: true,
},
"ingress_secret": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"hosts": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host_id": {
Type: schema.TypeString,
Computed: true,
},
"host_name": {
Type: schema.TypeString,
Computed: true,
},
"cluster_name": {
Type: schema.TypeString,
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"zone": {
Type: schema.TypeString,
Computed: true,
},
"host_labels": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
},
},
"service_subnet": {
Type: schema.TypeString,
Computed: true,
Description: "Custom subnet CIDR to provide private IP addresses for services",
},
"pod_subnet": {
Type: schema.TypeString,
Computed: true,
Description: "Custom subnet CIDR to provide private IP addresses for pods",
},
},
}
}
func dataSourceIBMSatelliteLocationRead(d *schema.ResourceData, meta interface{}) error {
location := d.Get("location").(string)
satClient, err := meta.(conns.ClientSession).SatelliteClientSession()
if err != nil {
return err
}
getSatLocOptions := &kubernetesserviceapiv1.GetSatelliteLocationOptions{
Controller: &location,
}
var instance *kubernetesserviceapiv1.MultishiftGetController
var response *core.DetailedResponse
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
instance, response, err = satClient.GetSatelliteLocation(getSatLocOptions)
if err != nil || instance == nil {
if response != nil && response.StatusCode == 404 {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if conns.IsResourceTimeoutError(err) {
instance, response, err = satClient.GetSatelliteLocation(getSatLocOptions)
}
if err != nil || instance == nil {
return fmt.Errorf("[ERROR] Error retrieving IBM cloud satellite location %s : %s\n%s", location, err, response)
}
d.SetId(*instance.ID)
d.Set("location", location)
d.Set("description", *instance.Description)
if instance.CoreosEnabled != nil {
d.Set("coreos_enabled", *instance.CoreosEnabled)
}
d.Set("zones", instance.WorkerZones)
d.Set("managed_from", *instance.Datacenter)
d.Set("crn", *instance.Crn)
d.Set("resource_group_id", *instance.ResourceGroup)
d.Set(flex.ResourceGroupName, *instance.ResourceGroupName)
d.Set("created_on", *instance.CreatedDate)
if instance.Hosts != nil {
d.Set("host_attached_count", *instance.Hosts.Total)
d.Set("host_available_count", *instance.Hosts.Available)
}
if instance.Ingress != nil {
d.Set("ingress_hostname", *instance.Ingress.Hostname)
d.Set("ingress_secret", *instance.Ingress.SecretName)
}
getSatHostOptions := &kubernetesserviceapiv1.GetSatelliteHostsOptions{
Controller: &location,
}
hostList, response, err := satClient.GetSatelliteHosts(getSatHostOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving location hosts %s : %s\n%s", location, err, response)
}
if hostList != nil {
d.Set("hosts", flex.FlattenSatelliteHosts(hostList))
}
tags, err := flex.GetTagsUsingCRN(meta, *instance.Crn)
if err != nil {
log.Printf(
"An error occured during reading of instance (%s) tags : %s", d.Id(), err)
}
d.Set("tags", tags)
if instance.PodSubnet != nil {
d.Set("pod_subnet", *instance.PodSubnet)
}
if instance.ServiceSubnet != nil {
d.Set("service_subnet", *instance.ServiceSubnet)
}
return nil
}