/
resource_citrixadc_ip6tunnel.go
134 lines (120 loc) · 3.38 KB
/
resource_citrixadc_ip6tunnel.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
package citrixadc
import (
"github.com/citrix/adc-nitro-go/resource/config/network"
"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
func resourceCitrixAdcIp6tunnel() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createIp6tunnelFunc,
Read: readIp6tunnelFunc,
Delete: deleteIp6tunnelFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"local": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"remote": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"ownergroup": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func createIp6tunnelFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createIp6tunnelFunc")
client := meta.(*NetScalerNitroClient).client
ip6tunnelName := d.Get("name").(string)
ip6tunnel := network.Ip6tunnel{
Local: d.Get("local").(string),
Name: d.Get("name").(string),
Ownergroup: d.Get("ownergroup").(string),
Remote: d.Get("remote").(string),
}
_, err := client.AddResource(service.Ip6tunnel.Type(), ip6tunnelName, &ip6tunnel)
if err != nil {
return err
}
d.SetId(ip6tunnelName)
err = readIp6tunnelFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this ip6tunnel but we can't read it ?? %s", ip6tunnelName)
return nil
}
return nil
}
func readIp6tunnelFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readIp6tunnelFunc")
client := meta.(*NetScalerNitroClient).client
ip6tunnelName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading ip6tunnel state %s", ip6tunnelName)
findParams := service.FindParams{
ResourceType: service.Ip6tunnel.Type(),
}
dataArray, err := client.FindResourceArrayWithParams(findParams)
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing ip6tunnel state %s", ip6tunnelName)
d.SetId("")
return nil
}
if len(dataArray) == 0 {
log.Printf("[WARN] citrixadc-provider: ip6tunnel does not exist. Clearing state.")
d.SetId("")
return nil
}
foundIndex := -1
for i, ip6tunnel := range dataArray {
match := true
if ip6tunnel["remoteip"] != d.Get("remote").(string) {
match = false
}
if match {
foundIndex = i
break
}
}
if foundIndex == -1 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams ip6tunnel not found in array")
log.Printf("[WARN] citrixadc-provider: Clearing ip6tunnel state %s", ip6tunnelName)
d.SetId("")
return nil
}
data := dataArray[foundIndex]
d.Set("local", data["local"])
d.Set("name", data["name"])
d.Set("ownergroup", data["ownergroup"])
d.Set("remote", data["remoteip"])
return nil
}
func deleteIp6tunnelFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteIp6tunnelFunc")
client := meta.(*NetScalerNitroClient).client
ip6tunnelName := d.Id()
err := client.DeleteResource(service.Ip6tunnel.Type(), ip6tunnelName)
if err != nil {
return err
}
d.SetId("")
return nil
}