-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_target_group.go
234 lines (211 loc) · 7.76 KB
/
data_source_target_group.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
package ionoscloud
import (
"context"
"errors"
"fmt"
"log"
"strings"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
"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"
)
func dataSourceTargetGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceTargetGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the target group.",
},
"partial_match": {
Type: schema.TypeBool,
Description: "Whether partial matching is allowed or not when using name argument.",
Default: false,
Optional: true,
},
"algorithm": {
Type: schema.TypeString,
Computed: true,
Description: "Balancing algorithm.",
},
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: "Balancing protocol.",
},
"targets": {
Type: schema.TypeList,
Description: "Array of items in the collection",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Description: "The IP of the balanced target VM.",
Computed: true,
},
"port": {
Type: schema.TypeInt,
Description: "The port of the balanced target service; valid range is 1 to 65535.",
Computed: true,
},
"weight": {
Type: schema.TypeInt,
Description: "Traffic is distributed in proportion to target weight, relative to the combined weight of all targets. A target with higher weight receives a greater share of traffic. Valid range is 0 to 256 and default is 1; targets with weight of 0 do not participate in load balancing but still accept persistent connections. It is best use values in the middle of the range to leave room for later adjustments.",
Computed: true,
},
"proxy_protocol": {
Type: schema.TypeString,
Description: "Proxy protocol version",
Computed: true,
},
"health_check_enabled": {
Type: schema.TypeBool,
Description: "Makes the target available only if it accepts periodic health check TCP connection attempts; when turned off, the target is considered always available. The health check only consists of a connection attempt to the address and port of the target. Default is True.",
Computed: true,
},
"maintenance_enabled": {
Type: schema.TypeBool,
Description: "Maintenance mode prevents the target from receiving balanced traffic.",
Computed: true,
},
},
},
},
"health_check": {
Type: schema.TypeList,
Description: "Health check attributes for Application Load Balancer forwarding rule",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"check_timeout": {
Type: schema.TypeInt,
Description: "The maximum time in milliseconds to wait for a target to respond to a check. For target VMs with 'Check Interval' set, the lesser of the two values is used once the TCP connection is established.",
Computed: true,
},
"check_interval": {
Type: schema.TypeInt,
Description: "The interval in milliseconds between consecutive health checks; default is 2000.",
Computed: true,
},
"retries": {
Type: schema.TypeInt,
Description: "The maximum number of attempts to reconnect to a target after a connection failure. Valid range is 0 to 65535, and default is three reconnection.",
Computed: true,
},
},
},
},
"http_health_check": {
Type: schema.TypeList,
Description: "Http health check attributes for Target Group",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"path": {
Type: schema.TypeString,
Description: "The path (destination URL) for the HTTP health check request; the default is /.",
Computed: true,
},
"method": {
Type: schema.TypeString,
Description: "The method for the HTTP health check.",
Computed: true,
},
"match_type": {
Type: schema.TypeString,
Computed: true,
},
"response": {
Type: schema.TypeString,
Description: "The response returned by the request, depending on the match type.",
Computed: true,
},
"regex": {
Type: schema.TypeBool,
Computed: true,
},
"negate": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func dataSourceTargetGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).CloudApiClient
idValue, idOk := d.GetOk("id")
nameValue, nameOk := d.GetOk("name")
id := idValue.(string)
name := nameValue.(string)
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 target group id or name"))
}
var targetGroup ionoscloud.TargetGroup
var err error
var apiResponse *ionoscloud.APIResponse
if idOk {
/* search by ID */
log.Printf("[INFO] Using data source for target group by id %s", id)
targetGroup, apiResponse, err = client.TargetGroupsApi.TargetgroupsFindByTargetGroupId(ctx, id).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching the target group %s: %w", id, err))
}
} else {
/* search by name */
var results []ionoscloud.TargetGroup
partialMatch := d.Get("partial_match").(bool)
log.Printf("[INFO] Using data source for target group by name with partial_match %t and name: %s", partialMatch, name)
if partialMatch {
targetGroups, apiResponse, err := client.TargetGroupsApi.TargetgroupsGet(ctx).Depth(1).Filter("name", name).Limit(constant.TargetGroupLimit).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching target groups: %w", err))
}
results = *targetGroups.Items
} else {
targetGroups, apiResponse, err := client.TargetGroupsApi.TargetgroupsGet(ctx).Depth(1).Limit(constant.TargetGroupLimit).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching target groups: %w", err))
}
if targetGroups.Items != nil {
for _, t := range *targetGroups.Items {
if t.Properties.Name != nil && strings.ToLower(*t.Properties.Name) == strings.ToLower(name) {
tmpTargetGroup, apiResponse, err := client.TargetGroupsApi.TargetgroupsFindByTargetGroupId(ctx, *t.Id).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching target group with ID %s: %w", *t.Id, err))
}
results = append(results, tmpTargetGroup)
}
}
}
}
if results == nil || len(results) == 0 {
return diag.FromErr(fmt.Errorf("no target group found with the specified criteria: name = %s", name))
} else if len(results) > 1 {
return diag.FromErr(fmt.Errorf("more than one target group found with the specified criteria: name = %s", name))
}
targetGroup = results[0]
}
if err = setTargetGroupData(d, &targetGroup); err != nil {
return diag.FromErr(err)
}
return nil
}