-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_private_crossconnect.go
223 lines (195 loc) · 5.63 KB
/
data_source_private_crossconnect.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
package ionoscloud
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
)
func dataSourcePcc() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePccRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"peers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lan_id": {
Type: schema.TypeString,
Computed: true,
Description: "The id of the cross-connected LAN",
},
"lan_name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the cross-connected LAN",
},
"datacenter_id": {
Type: schema.TypeString,
Computed: true,
Description: "The id of the cross-connected VDC",
},
"datacenter_name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the cross-connected VDC",
},
"location": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"connectable_datacenters": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func convertPccPeers(peers *[]ionoscloud.Peer) []interface{} {
if peers == nil {
return make([]interface{}, 0)
}
ret := make([]interface{}, len(*peers), len(*peers))
for i, peer := range *peers {
entry := make(map[string]interface{})
entry["lan_id"] = peer.Id
entry["lan_name"] = peer.Name
entry["datacenter_id"] = peer.DatacenterId
entry["datacenter_name"] = peer.DatacenterName
entry["location"] = peer.Location
ret[i] = entry
}
return ret
}
func convertConnectableDatacenters(dcs *[]ionoscloud.ConnectableDatacenter) []interface{} {
if dcs == nil {
return make([]interface{}, 0)
}
ret := make([]interface{}, len(*dcs), len(*dcs))
for i, dc := range *dcs {
entry := make(map[string]interface{})
entry["id"] = dc.Id
entry["name"] = dc.Name
entry["location"] = dc.Location
ret[i] = entry
}
return ret
}
func setPccDataSource(d *schema.ResourceData, pcc *ionoscloud.PrivateCrossConnect) error {
if pcc.Id != nil {
d.SetId(*pcc.Id)
}
if pcc.Properties != nil {
if pcc.Properties.Name != nil {
if err := d.Set("name", *pcc.Properties.Name); err != nil {
return err
}
}
if pcc.Properties.Description != nil {
if err := d.Set("description", *pcc.Properties.Description); err != nil {
return err
}
}
if pcc.Properties.Peers != nil {
if err := d.Set("peers", convertPccPeers(pcc.Properties.Peers)); err != nil {
return err
}
}
if pcc.Properties.ConnectableDatacenters != nil && len(*pcc.Properties.ConnectableDatacenters) > 0 {
if err := d.Set("connectable_datacenters", convertConnectableDatacenters(pcc.Properties.ConnectableDatacenters)); err != nil {
return err
}
}
}
return nil
}
func dataSourcePccRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).CloudApiClient
id, idOk := d.GetOk("id")
name, nameOk := d.GetOk("name")
if idOk && nameOk {
return diag.FromErr(errors.New("id and name cannot be both specified in the same time"))
}
if !idOk && !nameOk {
return diag.FromErr(errors.New("please provide either the pcc id or name"))
}
var pcc ionoscloud.PrivateCrossConnect
var err error
var apiResponse *ionoscloud.APIResponse
if idOk {
/* search by ID */
pcc, apiResponse, err = client.PrivateCrossConnectsApi.PccsFindById(ctx, id.(string)).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching the pcc with ID %s: %w", id.(string), err))
}
}
if nameOk {
/* search by name */
var pccs ionoscloud.PrivateCrossConnects
pccs, apiResponse, err := client.PrivateCrossConnectsApi.PccsGet(ctx).Depth(1).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching pccs: %w", err))
}
var results []ionoscloud.PrivateCrossConnect
if pccs.Items != nil {
for _, p := range *pccs.Items {
if p.Properties != nil && p.Properties.Name != nil && *p.Properties.Name == name.(string) {
pcc, apiResponse, err = client.PrivateCrossConnectsApi.PccsFindById(ctx, *p.Id).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching the pcc with ID %s: %w", *p.Id, err))
}
results = append(results, pcc)
}
}
}
if results == nil || len(results) == 0 {
return diag.FromErr(fmt.Errorf("no pcc found with the specified criteria: name = %s", name))
} else if len(results) > 1 {
return diag.FromErr(fmt.Errorf("more than one pcc found with the specified criteria: name = %s", name))
} else {
pcc = results[0]
}
}
if err = setPccDataSource(d, &pcc); err != nil {
return diag.FromErr(err)
}
return nil
}