-
Notifications
You must be signed in to change notification settings - Fork 117
/
datasource_bigip_ltm_node.go
176 lines (170 loc) · 5.1 KB
/
datasource_bigip_ltm_node.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
/*
Copyright 2019 F5 Networks Inc.
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package bigip
import (
"context"
"fmt"
"log"
"regexp"
bigip "github.com/f5devcentral/go-bigip"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceBigipLtmNode() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBigipLtmNodeRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the node.",
},
"partition": {
Type: schema.TypeString,
Required: true,
Description: "Partition of resource group",
},
"full_path": {
Type: schema.TypeString,
Optional: true,
Description: "Full path of the node (partition and name)",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "User defined description of the node.",
},
"address": {
Type: schema.TypeString,
Optional: true,
Description: "IP address of the node of the node.",
},
"connection_limit": {
Type: schema.TypeInt,
Computed: true,
Description: "Node connection limit.",
},
"dynamic_ratio": {
Type: schema.TypeInt,
Computed: true,
Description: "The dynamic ratio number for the node.",
},
"monitor": {
Type: schema.TypeString,
Computed: true,
Description: "Specifies the health monitors the system currently uses to monitor this node.",
},
"rate_limit": {
Type: schema.TypeString,
Computed: true,
Description: "Node rate limit.",
},
"ratio": {
Type: schema.TypeInt,
Computed: true,
Description: "Node ratio weight.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "The current state of the node.",
},
"session": {
Type: schema.TypeString,
Description: "Enables or disables the node for new sessions.",
Computed: true,
},
"fqdn": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_family": {
Type: schema.TypeString,
Optional: true,
Description: "The FQDN node's address family.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The fully qualified domain name of the node.",
},
"interval": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The amount of time before sending the next DNS query.",
},
"downinterval": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The number of attempts to resolve a domain name.",
},
"autopopulate": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies if the node should scale to the IP address set returned by DNS.",
},
},
},
},
},
}
}
func dataSourceBigipLtmNodeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
d.SetId("")
name := fmt.Sprintf("/%s/%s", d.Get("partition").(string), d.Get("name").(string))
log.Println("[DEBUG] Reading Node : " + name)
node, err := client.GetNode(name)
if err != nil {
return diag.FromErr(fmt.Errorf("error retrieving node %s: %v", name, err))
}
if node == nil {
log.Printf("[DEBUG] Node %s not found, removing from state", name)
d.SetId("")
return nil
}
if node.FQDN.Name != "" {
_ = d.Set("address", node.FQDN.Name)
} else {
// xxx.xxx.xxx.xxx(%x)
// x:x(%x)
regex := regexp.MustCompile(`((?:(?:[0-9]{1,3}\.){3}[0-9]{1,3})|(?:.*:[^%]*))(?:\%\d+)?`)
address := regex.FindStringSubmatch(node.Address)
log.Println("[INFO] Address: " + address[1])
_ = d.Set("address", node.Address)
}
_ = d.Set("name", node.Name)
_ = d.Set("partition", node.Partition)
_ = d.Set("full_path", node.FullPath)
_ = d.Set("connection_limit", node.ConnectionLimit)
_ = d.Set("dynamic_ratio", node.DynamicRatio)
_ = d.Set("monitor", node.Monitor)
_ = d.Set("rate_limit", node.RateLimit)
_ = d.Set("ratio", node.Ratio)
_ = d.Set("state", node.State)
_ = d.Set("session", node.Session)
var fqdn []map[string]interface{}
fqdnelements := map[string]interface{}{
"interval": node.FQDN.Interval,
"downinterval": node.FQDN.DownInterval,
"autopopulate": node.FQDN.AutoPopulate,
"address_family": node.FQDN.AddressFamily,
}
fqdn = append(fqdn, fqdnelements)
_ = d.Set("fqdn", fqdn)
// _ = d.Set("fqdn.0.interval", node.FQDN.Interval)
// _ = d.Set("fqdn.0.downinterval", node.FQDN.DownInterval)
// _ = d.Set("fqdn.0.autopopulate", node.FQDN.AutoPopulate)
// _ = d.Set("fqdn.0.address_family", node.FQDN.AddressFamily)
d.SetId(node.Name)
return nil
}