/
data_source_ipblock.go
192 lines (172 loc) · 4.84 KB
/
data_source_ipblock.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
package ionoscloud
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
ionoscloud "github.com/ionos-cloud/sdk-go/v5"
"log"
)
func dataSourceIpBlock() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceIpBlockRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"location": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.All(validation.StringIsNotWhiteSpace),
},
"size": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"ips": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"ip_consumers": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Computed: true,
},
"mac": {
Type: schema.TypeString,
Computed: true,
},
"nic_id": {
Type: schema.TypeString,
Computed: true,
},
"server_id": {
Type: schema.TypeString,
Computed: true,
},
"server_name": {
Type: schema.TypeString,
Computed: true,
},
"datacenter_id": {
Type: schema.TypeString,
Computed: true,
},
"datacenter_name": {
Type: schema.TypeString,
Computed: true,
},
"k8s_nodepool_uuid": {
Type: schema.TypeString,
Computed: true,
},
"k8s_cluster_uuid": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func datasourceIpBlockRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
id, idOk := data.GetOk("id")
var name, location string
t, nameOk := data.GetOk("name")
if nameOk {
name = t.(string)
}
t, locationOk := data.GetOk("location")
if locationOk {
location = t.(string)
}
var ipBlock ionoscloud.IpBlock
var apiResponse *ionoscloud.APIResponse
var err error
client := meta.(*ionoscloud.APIClient)
if !idOk && !nameOk && !locationOk {
return diag.FromErr(fmt.Errorf("either id, location or name must be set"))
}
if idOk {
ipBlock, apiResponse, err = client.IPBlocksApi.IpblocksFindById(ctx, id.(string)).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("error getting ip block with id %s %s", id.(string), err))
}
if nameOk {
if *ipBlock.Properties.Name != name {
return diag.FromErr(fmt.Errorf("name of ip block (UUID=%s, name=%s) does not match expected name: %s",
*ipBlock.Id, *ipBlock.Properties.Name, name))
}
}
if locationOk {
if *ipBlock.Properties.Location != location {
return diag.FromErr(fmt.Errorf("location of ip block (UUID=%s, location=%s) does not match expected location: %s",
*ipBlock.Id, *ipBlock.Properties.Location, location))
}
}
log.Printf("[INFO] Got ip block [Name=%s, Location=%s]", *ipBlock.Properties.Name, *ipBlock.Properties.Location)
} else {
ipBlocks, apiResponse, err := client.IPBlocksApi.IpblocksGet(ctx).Depth(1).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occured while fetching ipBlocks: %s ", err))
}
var results []ionoscloud.IpBlock
if nameOk && ipBlocks.Items != nil {
for _, block := range *ipBlocks.Items {
if block.Properties != nil && block.Properties.Name != nil && *block.Properties.Name == name {
results = append(results, block)
//found based on name only, save this in case we don't find based on location
if !locationOk {
ipBlock = results[0]
}
}
}
if results == nil {
return diag.FromErr(fmt.Errorf("could not find an ip block with name %s", name))
}
}
if locationOk {
if results != nil {
for _, block := range results {
if block.Properties.Location != nil && *block.Properties.Location == location {
ipBlock = block
break
}
}
} else if ipBlocks.Items != nil {
/* find the first ipblock matching the location */
for _, block := range *ipBlocks.Items {
if block.Properties.Location != nil && *block.Properties.Location == location {
ipBlock = block
break
}
}
}
}
}
if ipBlock.Id == nil {
return diag.FromErr(fmt.Errorf("there are no ip blocks that match the search criteria id = %s, name = %s, location = %s", id, name, location))
}
if err := IpBlockSetData(data, &ipBlock); err != nil {
return diag.FromErr(err)
}
return nil
}