-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_networkloadbalancer.go
144 lines (131 loc) · 4.97 KB
/
data_source_networkloadbalancer.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
package ionoscloud
import (
"context"
"errors"
"fmt"
"strings"
"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"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cloudapi/flowlog"
)
func dataSourceNetworkLoadBalancer() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNetworkLoadBalancerRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"listener_lan": {
Type: schema.TypeInt,
Description: "Id of the listening LAN. (inbound)",
Computed: true,
},
"ips": {
Type: schema.TypeList,
Description: "Collection of IP addresses of the Network Load Balancer. (inbound and outbound) IP of the " +
"listenerLan must be a customer reserved IP for the public load balancer and private IP " +
"for the private load balancer.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"target_lan": {
Type: schema.TypeInt,
Description: "Id of the balanced private target LAN. (outbound)",
Computed: true,
},
"lb_private_ips": {
Type: schema.TypeList,
Description: "Collection of private IP addresses with subnet mask of the Network Load Balancer. IPs " +
"must contain valid subnet mask. If user will not provide any IP then the system will " +
"generate one IP with /24 subnet.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"datacenter_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"flowlog": {
Type: schema.TypeSet,
Computed: true,
Elem: cloudapiflowlog.FlowlogSchemaDatasource,
Description: `Flow logs holistically capture network information such as source and destination
IP addresses, source and destination ports, number of packets, amount of bytes,
the start and end time of the recording, and the type of protocol –
and log the extent to which your instances are being accessed.`,
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func dataSourceNetworkLoadBalancerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).CloudApiClient
datacenterId, dcIdOk := d.GetOk("datacenter_id")
if !dcIdOk {
return diag.FromErr(errors.New("no datacenter_id was specified"))
}
dcID := datacenterId.(string)
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 lan id or name"))
}
var networkLoadBalancer ionoscloud.NetworkLoadBalancer
var err error
var apiResponse *ionoscloud.APIResponse
if idOk {
/* search by ID */
networkLoadBalancer, apiResponse, err = client.NetworkLoadBalancersApi.DatacentersNetworkloadbalancersFindByNetworkLoadBalancerId(ctx, dcID, id.(string)).Depth(4).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching the network loadbalancer %s, dcID %s : %w", id.(string), dcID, err))
}
} else {
/* search by name */
var networkLoadBalancers ionoscloud.NetworkLoadBalancers
networkLoadBalancers, apiResponse, err := client.NetworkLoadBalancersApi.DatacentersNetworkloadbalancersGet(ctx, dcID).Depth(4).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching network loadbalancers, dcID %s : %w", dcID, err))
}
var results []ionoscloud.NetworkLoadBalancer
if networkLoadBalancers.Items != nil {
for _, nlb := range *networkLoadBalancers.Items {
if nlb.Properties != nil && nlb.Properties.Name != nil && strings.EqualFold(*nlb.Properties.Name, name.(string)) {
tmpNetworkLoadBalancer, apiResponse, err := client.NetworkLoadBalancersApi.DatacentersNetworkloadbalancersFindByNetworkLoadBalancerId(ctx, dcID, *nlb.Id).Depth(4).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching network loadbalancer with ID %s: %w", *nlb.Id, err))
}
results = append(results, tmpNetworkLoadBalancer)
}
}
}
if results == nil || len(results) == 0 {
return diag.FromErr(fmt.Errorf("no network load balancer found with the specified criteria: name = %s", name.(string)))
} else if len(results) > 1 {
return diag.FromErr(fmt.Errorf("more than one network load balancer found with the specified criteria: name = %s", name.(string)))
} else {
networkLoadBalancer = results[0]
}
}
if err = setNetworkLoadBalancerData(d, &networkLoadBalancer); err != nil {
return diag.FromErr(err)
}
return nil
}