/
resource_citrixadc_lsnappsprofile_lsnappsattributes_binding.go
143 lines (117 loc) · 4.36 KB
/
resource_citrixadc_lsnappsprofile_lsnappsattributes_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 resourceCitrixAdcLsnappsprofile_lsnappsattributes_binding() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createLsnappsprofile_lsnappsattributes_bindingFunc,
Read: readLsnappsprofile_lsnappsattributes_bindingFunc,
Delete: deleteLsnappsprofile_lsnappsattributes_bindingFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"appsattributesname": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"appsprofilename": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func createLsnappsprofile_lsnappsattributes_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createLsnappsprofile_lsnappsattributes_bindingFunc")
client := meta.(*NetScalerNitroClient).client
appsprofilename := d.Get("appsprofilename")
appsattributesname := d.Get("appsattributesname")
bindingId := fmt.Sprintf("%s,%s", appsprofilename, appsattributesname)
lsnappsprofile_lsnappsattributes_binding := lsn.Lsnappsprofilelsnappsattributesbinding{
Appsattributesname: d.Get("appsattributesname").(string),
Appsprofilename: d.Get("appsprofilename").(string),
}
err := client.UpdateUnnamedResource("lsnappsprofile_lsnappsattributes_binding", &lsnappsprofile_lsnappsattributes_binding)
if err != nil {
return err
}
d.SetId(bindingId)
err = readLsnappsprofile_lsnappsattributes_bindingFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this lsnappsprofile_lsnappsattributes_binding but we can't read it ?? %s", bindingId)
return nil
}
return nil
}
func readLsnappsprofile_lsnappsattributes_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readLsnappsprofile_lsnappsattributes_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 2)
appsprofilename := idSlice[0]
appsattributesname := idSlice[1]
log.Printf("[DEBUG] citrixadc-provider: Reading lsnappsprofile_lsnappsattributes_binding state %s", bindingId)
findParams := service.FindParams{
ResourceType: "lsnappsprofile_lsnappsattributes_binding",
ResourceName: appsprofilename,
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 lsnappsprofile_lsnappsattributes_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["appsattributesname"].(string) == appsattributesname {
foundIndex = i
break
}
}
// Resource is missing
if foundIndex == -1 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams appsattributesname not found in array")
log.Printf("[WARN] citrixadc-provider: Clearing lsnappsprofile_lsnappsattributes_binding state %s", bindingId)
d.SetId("")
return nil
}
// Fallthrough
data := dataArr[foundIndex]
d.Set("appsattributesname", data["appsattributesname"])
d.Set("appsprofilename", data["appsprofilename"])
return nil
}
func deleteLsnappsprofile_lsnappsattributes_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteLsnappsprofile_lsnappsattributes_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 2)
name := idSlice[0]
appsattributesname := idSlice[1]
args := make([]string, 0)
args = append(args, fmt.Sprintf("appsattributesname:%s", appsattributesname))
err := client.DeleteResourceWithArgs("lsnappsprofile_lsnappsattributes_binding", name, args)
if err != nil {
return err
}
d.SetId("")
return nil
}