/
resource_citrixadc_netprofile.go
226 lines (209 loc) · 7.52 KB
/
resource_citrixadc_netprofile.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
package citrixadc
import (
"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"fmt"
"log"
)
// netprofile struct is defined here to add proxyProtocolAfterTLSHandshake support.
// Once this attribute available in the main builds, respective go-notro file will be taken care.
type netprofile struct {
Mbf string `json:"mbf,omitempty"`
Name string `json:"name,omitempty"`
Overridelsn string `json:"overridelsn,omitempty"`
Proxyprotocol string `json:"proxyprotocol,omitempty"`
Proxyprotocoltxversion string `json:"proxyprotocoltxversion,omitempty"`
Srcip string `json:"srcip,omitempty"`
Srcippersistency string `json:"srcippersistency,omitempty"`
Td int `json:"td,omitempty"`
Proxyprotocolaftertlshandshake string `json:"proxyprotocolaftertlshandshake,omitempty"`
}
func resourceCitrixAdcNetprofile() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createNetprofileFunc,
Read: readNetprofileFunc,
Update: updateNetprofileFunc,
Delete: deleteNetprofileFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"mbf": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"overridelsn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"proxyprotocol": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"proxyprotocoltxversion": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"srcip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"srcippersistency": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"td": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"proxyprotocolaftertlshandshake": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func createNetprofileFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createNetprofileFunc")
client := meta.(*NetScalerNitroClient).client
var netprofileName string
if v, ok := d.GetOk("name"); ok {
netprofileName = v.(string)
} else {
netprofileName = resource.PrefixedUniqueId("tf-netprofile-")
d.Set("name", netprofileName)
}
netprofile := netprofile{
Mbf: d.Get("mbf").(string),
Name: d.Get("name").(string),
Overridelsn: d.Get("overridelsn").(string),
Proxyprotocol: d.Get("proxyprotocol").(string),
Proxyprotocoltxversion: d.Get("proxyprotocoltxversion").(string),
Srcip: d.Get("srcip").(string),
Srcippersistency: d.Get("srcippersistency").(string),
Td: d.Get("td").(int),
Proxyprotocolaftertlshandshake: d.Get("proxyprotocolaftertlshandshake").(string),
}
_, err := client.AddResource(service.Netprofile.Type(), netprofileName, &netprofile)
if err != nil {
return err
}
d.SetId(netprofileName)
err = readNetprofileFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this netprofile but we can't read it ?? %s", netprofileName)
return nil
}
return nil
}
func readNetprofileFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readNetprofileFunc")
client := meta.(*NetScalerNitroClient).client
netprofileName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading netprofile state %s", netprofileName)
data, err := client.FindResource(service.Netprofile.Type(), netprofileName)
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing netprofile state %s", netprofileName)
d.SetId("")
return nil
}
d.Set("name", data["name"])
d.Set("mbf", data["mbf"])
d.Set("name", data["name"])
d.Set("overridelsn", data["overridelsn"])
d.Set("proxyprotocol", data["proxyprotocol"])
d.Set("proxyprotocoltxversion", data["proxyprotocoltxversion"])
d.Set("srcip", data["srcip"])
d.Set("srcippersistency", data["srcippersistency"])
d.Set("td", data["td"])
d.Set("proxyprotocolaftertlshandshake", data["proxyprotocolaftertlshandshake"])
return nil
}
func updateNetprofileFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In updateNetprofileFunc")
client := meta.(*NetScalerNitroClient).client
netprofileName := d.Get("name").(string)
netprofile := netprofile{
Name: d.Get("name").(string),
}
hasChange := false
if d.HasChange("mbf") {
log.Printf("[DEBUG] citrixadc-provider: Mbf has changed for netprofile %s, starting update", netprofileName)
netprofile.Mbf = d.Get("mbf").(string)
hasChange = true
}
if d.HasChange("name") {
log.Printf("[DEBUG] citrixadc-provider: Name has changed for netprofile %s, starting update", netprofileName)
netprofile.Name = d.Get("name").(string)
hasChange = true
}
if d.HasChange("overridelsn") {
log.Printf("[DEBUG] citrixadc-provider: Overridelsn has changed for netprofile %s, starting update", netprofileName)
netprofile.Overridelsn = d.Get("overridelsn").(string)
hasChange = true
}
if d.HasChange("proxyprotocol") {
log.Printf("[DEBUG] citrixadc-provider: Proxyprotocol has changed for netprofile %s, starting update", netprofileName)
netprofile.Proxyprotocol = d.Get("proxyprotocol").(string)
hasChange = true
}
if d.HasChange("proxyprotocoltxversion") {
log.Printf("[DEBUG] citrixadc-provider: Proxyprotocoltxversion has changed for netprofile %s, starting update", netprofileName)
netprofile.Proxyprotocoltxversion = d.Get("proxyprotocoltxversion").(string)
hasChange = true
}
if d.HasChange("srcip") {
log.Printf("[DEBUG] citrixadc-provider: Srcip has changed for netprofile %s, starting update", netprofileName)
netprofile.Srcip = d.Get("srcip").(string)
hasChange = true
}
if d.HasChange("srcippersistency") {
log.Printf("[DEBUG] citrixadc-provider: Srcippersistency has changed for netprofile %s, starting update", netprofileName)
netprofile.Srcippersistency = d.Get("srcippersistency").(string)
hasChange = true
}
if d.HasChange("td") {
log.Printf("[DEBUG] citrixadc-provider: Td has changed for netprofile %s, starting update", netprofileName)
netprofile.Td = d.Get("td").(int)
hasChange = true
}
if d.HasChange("proxyprotocolaftertlshandshake") {
log.Printf("[DEBUG] citrixadc-provider: Proxyprotocolaftertlshandshake has changed for netprofile %s, starting update", netprofileName)
netprofile.Proxyprotocolaftertlshandshake = d.Get("proxyprotocolaftertlshandshake").(string)
hasChange = true
}
if hasChange {
_, err := client.UpdateResource(service.Netprofile.Type(), netprofileName, &netprofile)
if err != nil {
return fmt.Errorf("Error updating netprofile %s", netprofileName)
}
}
return readNetprofileFunc(d, meta)
}
func deleteNetprofileFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteNetprofileFunc")
client := meta.(*NetScalerNitroClient).client
netprofileName := d.Id()
err := client.DeleteResource(service.Netprofile.Type(), netprofileName)
if err != nil {
return err
}
d.SetId("")
return nil
}