/
resource_citrixadc_lsngroup_lsnappsprofile_binding.go
143 lines (117 loc) · 4.07 KB
/
resource_citrixadc_lsngroup_lsnappsprofile_binding.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
package citrixadc
import (
"github.com/citrix/adc-nitro-go/resource/config/lsn"
"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"fmt"
"log"
"strings"
)
func resourceCitrixAdcLsngroup_lsnappsprofile_binding() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createLsngroup_lsnappsprofile_bindingFunc,
Read: readLsngroup_lsnappsprofile_bindingFunc,
Delete: deleteLsngroup_lsnappsprofile_bindingFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"appsprofilename": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"groupname": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func createLsngroup_lsnappsprofile_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createLsngroup_lsnappsprofile_bindingFunc")
client := meta.(*NetScalerNitroClient).client
groupname := d.Get("groupname")
appsprofilename := d.Get("appsprofilename")
bindingId := fmt.Sprintf("%s,%s", groupname, appsprofilename)
lsngroup_lsnappsprofile_binding := lsn.Lsngrouplsnappsprofilebinding{
Appsprofilename: d.Get("appsprofilename").(string),
Groupname: d.Get("groupname").(string),
}
err := client.UpdateUnnamedResource("lsngroup_lsnappsprofile_binding", &lsngroup_lsnappsprofile_binding)
if err != nil {
return err
}
d.SetId(bindingId)
err = readLsngroup_lsnappsprofile_bindingFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this lsngroup_lsnappsprofile_binding but we can't read it ?? %s", bindingId)
return nil
}
return nil
}
func readLsngroup_lsnappsprofile_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readLsngroup_lsnappsprofile_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 2)
groupname := idSlice[0]
appsprofilename := idSlice[1]
log.Printf("[DEBUG] citrixadc-provider: Reading lsngroup_lsnappsprofile_binding state %s", bindingId)
findParams := service.FindParams{
ResourceType: "lsngroup_lsnappsprofile_binding",
ResourceName: groupname,
ResourceMissingErrorCode: 258,
}
dataArr, err := client.FindResourceArrayWithParams(findParams)
// Unexpected error
if err != nil {
log.Printf("[DEBUG] citrixadc-provider: Error during FindResourceArrayWithParams %s", err.Error())
return err
}
// Resource is missing
if len(dataArr) == 0 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams returned empty array")
log.Printf("[WARN] citrixadc-provider: Clearing lsngroup_lsnappsprofile_binding state %s", bindingId)
d.SetId("")
return nil
}
// Iterate through results to find the one with the right id
foundIndex := -1
for i, v := range dataArr {
if v["appsprofilename"].(string) == appsprofilename {
foundIndex = i
break
}
}
// Resource is missing
if foundIndex == -1 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams appsprofilename not found in array")
log.Printf("[WARN] citrixadc-provider: Clearing lsngroup_lsnappsprofile_binding state %s", bindingId)
d.SetId("")
return nil
}
// Fallthrough
data := dataArr[foundIndex]
d.Set("appsprofilename", data["appsprofilename"])
d.Set("groupname", data["groupname"])
return nil
}
func deleteLsngroup_lsnappsprofile_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteLsngroup_lsnappsprofile_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 2)
name := idSlice[0]
appsprofilename := idSlice[1]
args := make([]string, 0)
args = append(args, fmt.Sprintf("appsprofilename:%s", appsprofilename))
err := client.DeleteResourceWithArgs("lsngroup_lsnappsprofile_binding", name, args)
if err != nil {
return err
}
d.SetId("")
return nil
}