/
resource_citrixadc_policystringmap.go
119 lines (100 loc) · 3.46 KB
/
resource_citrixadc_policystringmap.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
package citrixadc
import (
"github.com/citrix/adc-nitro-go/resource/config/policy"
"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"fmt"
"log"
)
func resourceCitrixAdcPolicystringmap() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createPolicystringmapFunc,
Read: readPolicystringmapFunc,
Update: updatePolicystringmapFunc,
Delete: deletePolicystringmapFunc,
Schema: map[string]*schema.Schema{
"comment": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func createPolicystringmapFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createPolicystringmapFunc")
client := meta.(*NetScalerNitroClient).client
policystringmapName := d.Get("name").(string)
policystringmap := policy.Policystringmap{
Comment: d.Get("comment").(string),
Name: d.Get("name").(string),
}
_, err := client.AddResource(service.Policystringmap.Type(), policystringmapName, &policystringmap)
if err != nil {
return err
}
d.SetId(policystringmapName)
err = readPolicystringmapFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this policystringmap but we can't read it ?? %s", policystringmapName)
return nil
}
return nil
}
func readPolicystringmapFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readPolicystringmapFunc")
client := meta.(*NetScalerNitroClient).client
policystringmapName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading policystringmap state %s", policystringmapName)
data, err := client.FindResource(service.Policystringmap.Type(), policystringmapName)
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing policystringmap state %s", policystringmapName)
d.SetId("")
return nil
}
d.Set("comment", data["comment"])
d.Set("name", data["name"])
return nil
}
func updatePolicystringmapFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In updatePolicystringmapFunc")
client := meta.(*NetScalerNitroClient).client
policystringmapName := d.Get("name").(string)
policystringmap := policy.Policystringmap{
Name: d.Get("name").(string),
}
hasChange := false
if d.HasChange("comment") {
log.Printf("[DEBUG] citrixadc-provider: Comment has changed for policystringmap %s, starting update", policystringmapName)
policystringmap.Comment = d.Get("comment").(string)
hasChange = true
}
if d.HasChange("name") {
log.Printf("[DEBUG] citrixadc-provider: Name has changed for policystringmap %s, starting update", policystringmapName)
policystringmap.Name = d.Get("name").(string)
hasChange = true
}
if hasChange {
_, err := client.UpdateResource(service.Policystringmap.Type(), policystringmapName, &policystringmap)
if err != nil {
return fmt.Errorf("Error updating policystringmap %s", policystringmapName)
}
}
return readPolicystringmapFunc(d, meta)
}
func deletePolicystringmapFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deletePolicystringmapFunc")
client := meta.(*NetScalerNitroClient).client
policystringmapName := d.Id()
err := client.DeleteResource(service.Policystringmap.Type(), policystringmapName)
if err != nil {
return err
}
d.SetId("")
return nil
}