/
resource_citrixadc_icapolicy.go
148 lines (132 loc) · 4.04 KB
/
resource_citrixadc_icapolicy.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
package citrixadc
import (
"github.com/citrix/adc-nitro-go/resource/config/ica"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"fmt"
"log"
)
func resourceCitrixAdcIcapolicy() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createIcapolicyFunc,
Read: readIcapolicyFunc,
Update: updateIcapolicyFunc,
Delete: deleteIcapolicyFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"action": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"rule": {
Type: schema.TypeString,
Required: true,
},
"comment": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"logaction": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func createIcapolicyFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createIcapolicyFunc")
client := meta.(*NetScalerNitroClient).client
icapolicyName := d.Get("name").(string)
icapolicy := ica.Icapolicy{
Action: d.Get("action").(string),
Comment: d.Get("comment").(string),
Logaction: d.Get("logaction").(string),
Name: d.Get("name").(string),
Rule: d.Get("rule").(string),
}
_, err := client.AddResource("icapolicy", icapolicyName, &icapolicy)
if err != nil {
return err
}
d.SetId(icapolicyName)
err = readIcapolicyFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this icapolicy but we can't read it ?? %s", icapolicyName)
return nil
}
return nil
}
func readIcapolicyFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readIcapolicyFunc")
client := meta.(*NetScalerNitroClient).client
icapolicyName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading icapolicy state %s", icapolicyName)
data, err := client.FindResource("icapolicy", icapolicyName)
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing icapolicy state %s", icapolicyName)
d.SetId("")
return nil
}
d.Set("action", data["action"])
d.Set("comment", data["comment"])
d.Set("logaction", data["logaction"])
d.Set("name", data["name"])
d.Set("rule", data["rule"])
return nil
}
func updateIcapolicyFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In updateIcapolicyFunc")
client := meta.(*NetScalerNitroClient).client
icapolicyName := d.Get("name").(string)
icapolicy := ica.Icapolicy{
Name: d.Get("name").(string),
}
hasChange := false
if d.HasChange("action") {
log.Printf("[DEBUG] citrixadc-provider: Action has changed for icapolicy %s, starting update", icapolicyName)
icapolicy.Action = d.Get("action").(string)
hasChange = true
}
if d.HasChange("comment") {
log.Printf("[DEBUG] citrixadc-provider: Comment has changed for icapolicy %s, starting update", icapolicyName)
icapolicy.Comment = d.Get("comment").(string)
hasChange = true
}
if d.HasChange("logaction") {
log.Printf("[DEBUG] citrixadc-provider: Logaction has changed for icapolicy %s, starting update", icapolicyName)
icapolicy.Logaction = d.Get("logaction").(string)
hasChange = true
}
if d.HasChange("rule") {
log.Printf("[DEBUG] citrixadc-provider: Rule has changed for icapolicy %s, starting update", icapolicyName)
icapolicy.Rule = d.Get("rule").(string)
hasChange = true
}
if hasChange {
err := client.UpdateUnnamedResource("icapolicy", &icapolicy)
if err != nil {
return fmt.Errorf("Error updating icapolicy %s", icapolicyName)
}
}
return readIcapolicyFunc(d, meta)
}
func deleteIcapolicyFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteIcapolicyFunc")
client := meta.(*NetScalerNitroClient).client
icapolicyName := d.Id()
err := client.DeleteResource("icapolicy", icapolicyName)
if err != nil {
return err
}
d.SetId("")
return nil
}