Skip to content

Commit 51c236b

Browse files
shrsrlhercot
authored andcommitted
Implemented delete for aci_rest payload
1 parent 2105ca2 commit 51c236b

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

aci/resource_rest.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aci
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67

@@ -14,6 +15,8 @@ import (
1415
const Created = "created"
1516
const Deleted = "deleted"
1617

18+
var class, name string
19+
1720
const ErrDistinguishedNameNotFound = "The Dn is not present in the content"
1821

1922
func resourceAciRest() *schema.Resource {
@@ -60,12 +63,9 @@ func resourceAciRestCreate(d *schema.ResourceData, m interface{}) error {
6063
classNameIntf := d.Get("class_name")
6164
className := classNameIntf.(string)
6265
dn := models.StripQuotes(models.StripSquareBrackets(cont.Search(className, "attributes", "dn").String()))
63-
6466
if dn == "{}" {
6567
d.SetId(GetDN(d, m))
66-
6768
} else {
68-
6969
d.SetId(dn)
7070
}
7171
return resourceAciRestRead(d, m)
@@ -82,12 +82,9 @@ func resourceAciRestUpdate(d *schema.ResourceData, m interface{}) error {
8282
dn := models.StripQuotes(models.StripSquareBrackets(cont.Search(className, "attributes", "dn").String()))
8383
if dn == "{}" {
8484
d.SetId(GetDN(d, m))
85-
8685
} else {
87-
8886
d.SetId(dn)
8987
}
90-
9188
return resourceAciRestRead(d, m)
9289
}
9390

@@ -112,9 +109,8 @@ func resourceAciRestDelete(d *schema.ResourceData, m interface{}) error {
112109
func GetDN(d *schema.ResourceData, m interface{}) string {
113110
aciClient := m.(*client.Client)
114111
path := d.Get("path").(string)
115-
className := d.Get("class_name").(string)
116112
cont, _ := aciClient.GetViaURL(path)
117-
dn := models.StripQuotes(models.StripSquareBrackets(cont.Search("imdata", className, "attributes", "dn").String()))
113+
dn := models.StripQuotes(models.StripSquareBrackets(cont.Search("imdata", class, "attributes", "dn").String()))
118114
return fmt.Sprintf("%s", dn)
119115
}
120116

@@ -132,9 +128,6 @@ func PostAndSetStatus(d *schema.ResourceData, m interface{}, status string) (*co
132128
if classNameIntf, ok := d.GetOk("class_name"); ok {
133129
className := classNameIntf.(string)
134130
cont, err = preparePayload(className, contentStrMap)
135-
if status == Deleted {
136-
cont.Set(status, className, "attributes", "status")
137-
}
138131
if err != nil {
139132
return nil, err
140133
}
@@ -148,9 +141,7 @@ func PostAndSetStatus(d *schema.ResourceData, m interface{}, status string) (*co
148141
if len(payloadStr) == 0 {
149142
return nil, fmt.Errorf("Payload cannot be empty string")
150143
}
151-
152144
yamlJsonPayload, err := yaml.YAMLToJSON([]byte(payloadStr))
153-
154145
if err != nil {
155146
// It may be possible that the payload is in JSON
156147
jsonPayload, err := container.ParseJSON([]byte(payloadStr))
@@ -161,28 +152,34 @@ func PostAndSetStatus(d *schema.ResourceData, m interface{}, status string) (*co
161152
} else {
162153
// we have valid yaml payload and we were able to convert it to json
163154
cont, err = container.ParseJSON(yamlJsonPayload)
155+
164156
if err != nil {
165157
return nil, fmt.Errorf("Failed to convert YAML to JSON.")
166158
}
167159
}
168-
169160
if err != nil {
170-
171161
return nil, fmt.Errorf("Unable to parse the payload to JSON. Please check your payload")
172162
}
173163

174-
if status == "deleted" {
175-
method = "DELETE"
176-
}
177-
178164
} else {
179165
return nil, fmt.Errorf("Either of payload or content is required")
180166
}
167+
var output map[string]interface{}
168+
err_output := json.Unmarshal([]byte(cont.String()), &output)
169+
if err_output != nil {
170+
return nil, err_output
171+
}
172+
for key, _ := range output {
173+
class = key
174+
}
175+
176+
if status == Deleted {
177+
cont.Set(status, class, "attributes", "status")
178+
}
181179
req, err := aciClient.MakeRestRequest(method, path, cont, true)
182180
if err != nil {
183181
return nil, err
184182
}
185-
186183
respCont, _, err := aciClient.Do(req)
187184
if err != nil {
188185
return respCont, err

0 commit comments

Comments
 (0)