This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
forked from CiscoDevNet/terraform-provider-aci
-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_rest.go
185 lines (155 loc) · 4.48 KB
/
resource_rest.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package aci
import (
"errors"
"fmt"
"github.com/ciscoecosystem/aci-go-client/client"
"github.com/ciscoecosystem/aci-go-client/container"
"github.com/ciscoecosystem/aci-go-client/models"
"github.com/ghodss/yaml"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
const Created = "created"
const Deleted = "deleted"
const ErrDistinguishedNameNotFound = "The Dn is not present in the content"
func resourceAciRest() *schema.Resource {
return &schema.Resource{
Create: resourceAciRestCreate,
Update: resourceAciRestUpdate,
Read: resourceAciRestRead,
Delete: resourceAciRestDelete,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"path": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
// we set it automatically if file config is provided
"class_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"content": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},
"dn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"payload": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAciRestCreate(d *schema.ResourceData, m interface{}) error {
cont, err := PostAndSetStatus(d, m, "created, modified")
if err != nil {
return err
}
classNameIntf := d.Get("class_name")
className := classNameIntf.(string)
dn := cont.Search(className, "attributes", "dn").String()
if dn == "{}" {
d.SetId(GetDN(d, m))
} else {
d.SetId(dn)
}
return resourceAciRestRead(d, m)
}
func resourceAciRestUpdate(d *schema.ResourceData, m interface{}) error {
cont, err := PostAndSetStatus(d, m, "modified")
if err != nil {
return err
}
classNameIntf := d.Get("class_name")
className := classNameIntf.(string)
dn := cont.Search(className, "attributes", "dn").String()
if dn == "{}" {
d.SetId(GetDN(d, m))
} else {
d.SetId(dn)
}
return resourceAciRestRead(d, m)
}
func resourceAciRestRead(d *schema.ResourceData, m interface{}) error {
return nil
}
func resourceAciRestDelete(d *schema.ResourceData, m interface{}) error {
_, err := PostAndSetStatus(d, m, Deleted)
if err != nil {
return err
}
d.SetId("")
return nil
}
func GetDN(d *schema.ResourceData, m interface{}) string {
aciClient := m.(*client.Client)
path := d.Get("path").(string)
className := d.Get("class_name").(string)
cont, _ := aciClient.GetViaURL(path)
dn := models.StripQuotes(models.StripSquareBrackets(cont.Search("imdata", className, "attributes", "dn").String()))
return fmt.Sprintf("%s", dn)
}
// PostAndSetStatus is used to post schema and set the status
func PostAndSetStatus(d *schema.ResourceData, m interface{}, status string) (*container.Container, error) {
aciClient := m.(*client.Client)
path := d.Get("path").(string)
var cont *container.Container
var err error
if content, ok := d.GetOk("content"); ok {
contentStrMap := toStrMap(content.(map[string]interface{}))
if classNameIntf, ok := d.GetOk("class_name"); ok {
className := classNameIntf.(string)
cont, err = preparePayload(className, contentStrMap)
if status == Deleted {
cont.Set(status, className, "attributes", "status")
}
if err != nil {
return nil, err
}
} else {
return nil, errors.New("The className is required when content is provided explicitly")
}
} else if payload, ok := d.GetOk("payload"); ok {
payloadStr := payload.(string)
if len(payloadStr) == 0 {
return nil, fmt.Errorf("Payload cannot be empty string")
}
yamlJsonPayload, err := yaml.YAMLToJSON([]byte(payloadStr))
if err != nil {
// It may be possible that the payload is in JSON
jsonPayload, err := container.ParseJSON([]byte(payloadStr))
if err != nil {
return nil, fmt.Errorf("Invalid format for yaml/JSON payload")
}
cont = jsonPayload
} else {
// we have valid yaml payload and we were able to convert it to json
cont, err = container.ParseJSON(yamlJsonPayload)
if err != nil {
return nil, fmt.Errorf("Failed to convert YAML to JSON.")
}
}
if err != nil {
return nil, fmt.Errorf("Unable to parse the payload to JSON. Please check your payload")
}
} else {
return nil, fmt.Errorf("Either of payload or content is required")
}
req, err := aciClient.MakeRestRequest("POST", path, cont, true)
if err != nil {
return nil, err
}
respCont, _, err := aciClient.Do(req)
if err != nil {
return nil, err
}
err = client.CheckForErrors(respCont, "POST")
if err != nil {
return nil, err
}
return cont, nil
}