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

Commit

Permalink
Re-designed the resource_configuration schema
Browse files Browse the repository at this point in the history
1. Re-designed the resource_configuration schema
2. Updated the create and update logic as per the new schema
3. Modify read deployment to read all the resource properties retuned from the API

Signed-off-by: Prativa Bawri <[email protected]>
  • Loading branch information
Prativa20 committed Mar 24, 2020
1 parent 9c401d0 commit d973230
Show file tree
Hide file tree
Showing 6 changed files with 486 additions and 265 deletions.
69 changes: 23 additions & 46 deletions sdk/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,33 @@ type BusinessGroup struct {

// RequestResourceView - resource view of a provisioned request
type RequestResourceView struct {
Content []DeploymentResource `json:"content,omitempty"`
Links []interface{} `json:"links,omitempty"`
Content []interface{} `json:"content,omitempty"`
Links []interface{} `json:"links,omitempty"`
}

// DeploymentResource - deployment level view of the provisionined request
type DeploymentResource struct {
RequestState string `json:"requestState,omitempty"`
Description string `json:"description,omitempty"`
LastUpdated string `json:"lastUpdated,omitempty"`
TenantID string `json:"tenantId,omitempty"`
Name string `json:"name,omitempty"`
BusinessGroupID string `json:"businessGroupId,omitempty"`
DateCreated string `json:"dateCreated,omitempty"`
Status string `json:"status,omitempty"`
RequestID string `json:"requestId,omitempty"`
ResourceID string `json:"resourceId,omitempty"`
ResourceType string `json:"resourceType,omitempty"`
ResourcesData DeploymentResourceData `json:"data,omitempty"`
}

// DeploymentResourceData - view of the resources/machines in a deployment
type DeploymentResourceData struct {
Memory int `json:"MachineMemory,omitempty"`
CPU int `json:"MachineCPU,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
Storage int `json:"MachineStorage,omitempty"`
MachineInterfaceType string `json:"MachineInterfaceType,omitempty"`
MachineName string `json:"MachineName,omitempty"`
MachineGuestOperatingSystem string `json:"MachineGuestOperatingSystem,omitempty"`
MachineDestructionDate string `json:"MachineDestructionDate,omitempty"`
MachineGroupName string `json:"MachineGroupName,omitempty"`
MachineBlueprintName string `json:"MachineBlueprintName,omitempty"`
MachineReservationName string `json:"MachineReservationName,omitempty"`
MachineType string `json:"MachineType,omitempty"`
MachineID string `json:"machineId,omitempty"`
MachineExpirationDate string `json:"MachineExpirationDate,omitempty"`
Component string `json:"Component,omitempty"`
Expire bool `json:"Expire,omitempty"`
Reconfigure bool `json:"Reconfigure,omitempty"`
Reset bool `json:"Reset,omitempty"`
Reboot bool `json:"Reboot,omitempty"`
PowerOff bool `json:"PowerOff,omitempty"`
Destroy bool `json:"Destroy,omitempty"`
Shutdown bool `json:"Shutdown,omitempty"`
Suspend bool `json:"Suspend,omitempty"`
Reprovision bool `json:"Reprovision,omitempty"`
ChangeLease bool `json:"ChangeLease,omitempty"`
ChangeOwner bool `json:"ChangeOwner,omitempty"`
CreateSnapshot bool `json:"CreateSnapshot,omitempty"`
Networks []NWDetails `json:"NETWORK_LIST,omitempty"`
ResourceID string `json:"resourceId,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Status string `json:"status,omitempty"`
CatalogItemID string `json:"catalogItemId,omitempty"`
CatalogItemLabel string `json:"catalogItemLabel,omitempty"`
RequestID string `json:"requestId,omitempty"`
RequestState string `json:"requestState,omitempty"`
ResourceType string `json:"resourceType,omitempty"`
Owners []string `json:"owners,omitempty"`
BusinessGroupID string `json:"businessGroupId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
DateCreated string `json:"dateCreated,omitempty"`
LastUpdated string `json:"lastUpdated,omitempty"`
Lease struct {
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
} `json:"lease,omitempty"`

ParentResourceID string `json:"parentResourceId,omitempty"`
ResourcesData map[string]interface{} `json:"data,omitempty"`
}

//Networks Related structs
Expand Down
26 changes: 20 additions & 6 deletions utils/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package utils
import (
"bytes"
"encoding/json"
"github.com/op/go-logging"
"reflect"
"strconv"
"strings"

"github.com/op/go-logging"
)

// terraform provider constants
const (
// utility constants

LoggerID = "terraform-provider-vra7"
)

Expand Down Expand Up @@ -58,8 +58,8 @@ func ConvertInterfaceToString(interfaceData interface{}) string {
return stringData
}

// Parse value and if it's JSON string, unmarshal it
func UnmarshalJsonStringIfNecessary(field string, value interface{}) interface{} {
// UnmarshalJSONStringIfNecessary parses value and if it's JSON string, unmarshal it
func UnmarshalJSONStringIfNecessary(field string, value interface{}) interface{} {
// Cast value to string. Provider schema requires DeploymentConfiguration to be map[string]string
stringValue, ok := value.(string)

Expand Down Expand Up @@ -117,7 +117,7 @@ func ReplaceValueInRequestTemplate(templateInterface map[string]interface{}, fie
} else if key == field {
//If value type is not map then compare field name with provided field name
//If both matches then update field value with provided value
templateInterface[key] = value
templateInterface[key] = UnmarshalJSONStringIfNecessary(field, value)
return templateInterface, true
}
}
Expand All @@ -135,9 +135,23 @@ func AddValueToRequestTemplate(templateInterface map[string]interface{}, field s
template, _ := v.(map[string]interface{})
v = AddValueToRequestTemplate(template, field, value)
} else { //if i == "data" {
templateInterface[field] = value
templateInterface[field] = UnmarshalJSONStringIfNecessary(field, value)
}
}
//Return updated map interface type
return templateInterface
}

// ResourceMapper returns the mapping of resource attributes from ResourceView APIs
// to Catalog Item Request Template APIs
func ResourceMapper() map[string]string {
m := make(map[string]string)
m["MachineName"] = "name"
m["MachineDescription"] = "description"
m["MachineMemory"] = "memory"
m["MachineStorage"] = "storage"
m["MachineCPU"] = "cpu"
m["MachineStatus"] = "status"
m["MachineType"] = "type"
return m
}
19 changes: 10 additions & 9 deletions utils/utilities_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package utils

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUnmarshalJsonStringIfNecessaryFunction(t *testing.T) {
Expand All @@ -13,15 +14,15 @@ func TestUnmarshalJsonStringIfNecessaryFunction(t *testing.T) {
"key1": "string",
"key2": 1,
}}
computedValue1 := UnmarshalJsonStringIfNecessary(fieldName, notStringValue)
computedValue1 := UnmarshalJSONStringIfNecessary(fieldName, notStringValue)
assertLocal.Equal(notStringValue, computedValue1)

var notJsonStringValue = "some custom value"
computedValue2 := UnmarshalJsonStringIfNecessary(fieldName, notJsonStringValue)
assertLocal.Equal(notJsonStringValue, computedValue2)
var notJSONStringValue = "some custom value"
computedValue2 := UnmarshalJSONStringIfNecessary(fieldName, notJSONStringValue)
assertLocal.Equal(notJSONStringValue, computedValue2)

var jsonStringValue = "[\"bg1\", \"bg2\"]"
var expectedJsonValue = []interface{}{"bg1", "bg2"}
computedValue3 := UnmarshalJsonStringIfNecessary(fieldName, jsonStringValue)
assertLocal.Equal(expectedJsonValue, computedValue3)
var jSONStringValue = "[\"bg1\", \"bg2\"]"
var expectedJSONValue = []interface{}{"bg1", "bg2"}
computedValue3 := UnmarshalJSONStringIfNecessary(fieldName, jSONStringValue)
assertLocal.Equal(expectedJSONValue, computedValue3)
}
Loading

0 comments on commit d973230

Please sign in to comment.