Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit ff1922c

Browse files
committed
Adds support for generic responses
Uses Go `map[string]string` instead of `DeploymentResourceData` to abstract generic properties from the responses of vRA deployment resources. This solves issues where we might want to handle custom properties on the blueprints, as well as XaaS blueprints that contain entirely custom properties. Signed-off-by: skylerto <skylerclayne@gmail.com>
1 parent cdcc923 commit ff1922c

File tree

4 files changed

+110
-50
lines changed

4 files changed

+110
-50
lines changed

sdk/schema.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,7 @@ type DeploymentResource struct {
5555
RequestID string `json:"requestId,omitempty"`
5656
ResourceID string `json:"resourceId,omitempty"`
5757
ResourceType string `json:"resourceType,omitempty"`
58-
ResourcesData DeploymentResourceData `json:"data,omitempty"`
59-
}
60-
61-
// DeploymentResourceData - view of the resources/machines in a deployment
62-
type DeploymentResourceData struct {
63-
Memory int `json:"MachineMemory,omitempty"`
64-
CPU int `json:"MachineCPU,omitempty"`
65-
IPAddress string `json:"ip_address,omitempty"`
66-
Storage int `json:"MachineStorage,omitempty"`
67-
MachineInterfaceType string `json:"MachineInterfaceType,omitempty"`
68-
MachineName string `json:"MachineName,omitempty"`
69-
MachineGuestOperatingSystem string `json:"MachineGuestOperatingSystem,omitempty"`
70-
MachineDestructionDate string `json:"MachineDestructionDate,omitempty"`
71-
MachineGroupName string `json:"MachineGroupName,omitempty"`
72-
MachineBlueprintName string `json:"MachineBlueprintName,omitempty"`
73-
MachineReservationName string `json:"MachineReservationName,omitempty"`
74-
MachineType string `json:"MachineType,omitempty"`
75-
MachineID string `json:"machineId,omitempty"`
76-
MachineExpirationDate string `json:"MachineExpirationDate,omitempty"`
77-
Component string `json:"Component,omitempty"`
78-
Expire bool `json:"Expire,omitempty"`
79-
Reconfigure bool `json:"Reconfigure,omitempty"`
80-
Reset bool `json:"Reset,omitempty"`
81-
Reboot bool `json:"Reboot,omitempty"`
82-
PowerOff bool `json:"PowerOff,omitempty"`
83-
Destroy bool `json:"Destroy,omitempty"`
84-
Shutdown bool `json:"Shutdown,omitempty"`
85-
Suspend bool `json:"Suspend,omitempty"`
86-
Reprovision bool `json:"Reprovision,omitempty"`
87-
ChangeLease bool `json:"ChangeLease,omitempty"`
88-
ChangeOwner bool `json:"ChangeOwner,omitempty"`
89-
CreateSnapshot bool `json:"CreateSnapshot,omitempty"`
58+
ResourcesData map[string]interface{} `json:"data,omitempty"`
9059
}
9160

9261
// ResourceActions - Retrieves the resources that were provisioned as a result of a given request.

utils/utilities.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"reflect"
78
"strconv"
89
"strings"
@@ -116,3 +117,46 @@ func AddValueToRequestTemplate(templateInterface map[string]interface{}, field s
116117
//Return updated map interface type
117118
return templateInterface
118119
}
120+
121+
// Flatten takes a generic map of maps and flattens the properties via a dot seperator.
122+
func Flatten(m map[string]interface{}) map[string]interface{} {
123+
o := make(map[string]interface{})
124+
for k, v := range m {
125+
switch child := v.(type) {
126+
case map[string]interface{}:
127+
nm := Flatten(child)
128+
for nk, nv := range nm {
129+
o[k+"."+nk] = nv
130+
}
131+
default:
132+
o[k] = v
133+
}
134+
}
135+
return o
136+
}
137+
138+
// FlattenJSON takes a generic map of maps (representing nested json data) and flattens the properties via a do seperator.
139+
func FlattenJSON(jsonMap map[string]interface{}, key string) map[string]interface{} {
140+
viewData := make(map[string]interface{})
141+
142+
value := jsonMap[key]
143+
if value == nil {
144+
value = ""
145+
}
146+
147+
if reflect.TypeOf(value).String() == "[]interface {}" {
148+
for _, data := range value.([]interface{}) {
149+
if reflect.TypeOf(data).String() == "map[string]interface {}" {
150+
res := Flatten(data.(map[string]interface{}))
151+
for nestedKey := range res {
152+
viewData[fmt.Sprintf("%s.%s", key, nestedKey)] = fmt.Sprintf("%v", res[nestedKey])
153+
}
154+
} else {
155+
viewData[key] = fmt.Sprintf("%v", data)
156+
}
157+
}
158+
} else {
159+
viewData[key] = fmt.Sprintf("%v", value)
160+
}
161+
return viewData
162+
}

utils/utilities_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFlatten(t *testing.T) {
8+
inside := make(map[string]interface{})
9+
inside["outside"] = "valid"
10+
outside := make(map[string]interface{})
11+
outside["test"] = inside
12+
actual := Flatten(outside)
13+
14+
expected := make(map[string]interface{})
15+
expected["test.outside"] = "valid"
16+
17+
for k := range actual {
18+
if actual[k] != expected[k] {
19+
t.Fatalf("Expected %s, got %s at key %s", expected, actual, k)
20+
}
21+
}
22+
}
23+
24+
func TestFlattenComplex(t *testing.T) {
25+
deep := make(map[string]interface{})
26+
deep["outside"] = "valid"
27+
28+
inside := make(map[string]interface{})
29+
inside["outside"] = "valid"
30+
inside["deep"] = deep
31+
32+
outside := make(map[string]interface{})
33+
outside["test"] = inside
34+
actual := Flatten(outside)
35+
36+
expected := make(map[string]interface{})
37+
expected["test.outside"] = "valid"
38+
expected["test.deep.outside"] = "valid"
39+
40+
for k := range actual {
41+
if actual[k] != expected[k] {
42+
t.Fatalf("Expected %s, got %s at key %s", expected, actual, k)
43+
}
44+
}
45+
}

vra7/resource_vra7_deployment.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ func resourceVra7Deployment() *schema.Resource {
105105
Computed: true,
106106
Elem: schema.TypeString,
107107
},
108+
"resource_views_data": {
109+
Type: schema.TypeList,
110+
Elem: &schema.Schema{
111+
Type: schema.TypeMap,
112+
Elem: schema.TypeString,
113+
},
114+
Description: "The response data located along side the resource view API calls",
115+
Computed: true,
116+
},
108117
},
109118
}
110119
}
@@ -347,27 +356,19 @@ func resourceVra7DeploymentRead(d *schema.ResourceData, meta interface{}) error
347356
}
348357

349358
resourceDataMap := make(map[string]map[string]interface{})
359+
var resourceViewsData []map[string]interface{}
350360
for _, resource := range requestResourceView.Content {
351-
if resource.ResourceType == sdk.InfrastructureVirtual {
352-
resourceData := resource.ResourcesData
353-
log.Info("The resource data map of the resource %v is: \n%v", resourceData.Component, resource.ResourcesData)
354-
dataVals := make(map[string]interface{})
355-
resourceDataMap[resourceData.Component] = dataVals
356-
dataVals[sdk.MachineCPU] = resourceData.CPU
357-
dataVals[sdk.MachineStorage] = resourceData.Storage
358-
dataVals[sdk.IPAddress] = resourceData.IPAddress
359-
dataVals[sdk.MachineMemory] = resourceData.Memory
360-
dataVals[sdk.MachineName] = resourceData.MachineName
361-
dataVals[sdk.MachineGuestOs] = resourceData.MachineGuestOperatingSystem
362-
dataVals[sdk.MachineBpName] = resourceData.MachineBlueprintName
363-
dataVals[sdk.MachineType] = resourceData.MachineType
364-
dataVals[sdk.MachineReservationName] = resourceData.MachineReservationName
365-
dataVals[sdk.MachineInterfaceType] = resourceData.MachineInterfaceType
366-
dataVals[sdk.MachineID] = resourceData.MachineID
367-
dataVals[sdk.MachineGroupName] = resourceData.MachineGroupName
368-
dataVals[sdk.MachineDestructionDate] = resourceData.MachineDestructionDate
361+
viewData := make(map[string]interface{})
362+
for key := range resource.ResourcesData {
363+
viewData[key] = utils.FlattenJSON(resource.ResourcesData, key)
369364
}
365+
resourceViewsData = append(resourceViewsData, utils.Flatten(viewData))
366+
}
367+
setDataError := d.Set("resource_views_data", resourceViewsData)
368+
if setDataError != nil {
369+
return fmt.Errorf(setDataError.Error())
370370
}
371+
371372
resourceConfiguration, _ := d.Get("resource_configuration").(map[string]interface{})
372373

373374
resourceConfiguration, changed := utils.UpdateResourceConfigurationMap(resourceConfiguration, resourceDataMap)
@@ -378,6 +379,7 @@ func resourceVra7DeploymentRead(d *schema.ResourceData, meta interface{}) error
378379
return fmt.Errorf(setError.Error())
379380
}
380381
}
382+
381383
return nil
382384
}
383385

0 commit comments

Comments
 (0)