/
resource_citrixadc_policymap.go
121 lines (107 loc) · 3.08 KB
/
resource_citrixadc_policymap.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
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/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
func resourceCitrixAdcPolicymap() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createPolicymapFunc,
Read: readPolicymapFunc,
Delete: deletePolicymapFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"mappolicyname": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sd": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"su": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"td": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"tu": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func createPolicymapFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createPolicymapFunc")
client := meta.(*NetScalerNitroClient).client
var policymapName string
if v, ok := d.GetOk("mappolicyname"); ok {
policymapName = v.(string)
} else {
policymapName = resource.PrefixedUniqueId("tf-policymap-")
d.Set("mappolicyname", policymapName)
}
policymap := policy.Policymap{
Mappolicyname: d.Get("mappolicyname").(string),
Sd: d.Get("sd").(string),
Su: d.Get("su").(string),
Td: d.Get("td").(string),
Tu: d.Get("tu").(string),
}
_, err := client.AddResource(service.Policymap.Type(), policymapName, &policymap)
if err != nil {
return err
}
d.SetId(policymapName)
err = readPolicymapFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this policymap but we can't read it ?? %s", policymapName)
return nil
}
return nil
}
func readPolicymapFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readPolicymapFunc")
client := meta.(*NetScalerNitroClient).client
policymapName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading policymap state %s", policymapName)
data, err := client.FindResource(service.Policymap.Type(), policymapName)
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing policymap state %s", policymapName)
d.SetId("")
return nil
}
d.Set("mappolicyname", data["mappolicyname"])
d.Set("mappolicyname", data["mappolicyname"])
d.Set("sd", data["sd"])
d.Set("su", data["su"])
d.Set("td", data["td"])
d.Set("tu", data["tu"])
return nil
}
func deletePolicymapFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deletePolicymapFunc")
client := meta.(*NetScalerNitroClient).client
policymapName := d.Id()
err := client.DeleteResource(service.Policymap.Type(), policymapName)
if err != nil {
return err
}
d.SetId("")
return nil
}