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

Fix for issues # 59 and 60, reading cluster value in the state file #62

Merged
merged 1 commit into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/vra7_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (c *APIClient) GetResourceActions(resourceID string) ([]Operation, error) {
// GetResourceActionTemplate get the action template corresponding to the action id
func (c *APIClient) GetResourceActionTemplate(resourceID, actionID string) (*ResourceActionTemplate, error) {
getActionTemplatePath := fmt.Sprintf(GetActionTemplateAPI, resourceID, actionID)
log.Info("Call GET to fetch the reconfigure action template %v ", getActionTemplatePath)
log.Info("Call GET to fetch the action template %v ", getActionTemplatePath)
url := c.BuildEncodedURL(getActionTemplatePath, nil)
resp, respErr := c.Get(url, nil)
if respErr != nil {
Expand Down
21 changes: 5 additions & 16 deletions vra7/data_source_vra7_deployment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vra7

import (
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -116,9 +115,7 @@ func dataSourceVra7DeploymentRead(d *schema.ResourceData, meta interface{}) erro
return errTemplate
}

j, _ := json.Marshal(requestResourceView)
log.Critical("the view is %v ", string(j))

clusterCountMap := make(map[string]int)
var resourceConfigList []sdk.ResourceConfigurationStruct
for _, resource := range requestResourceView.Content {
rMap := resource.(map[string]interface{})
Expand Down Expand Up @@ -154,17 +151,9 @@ func dataSourceVra7DeploymentRead(d *schema.ResourceData, meta interface{}) erro
if rMap["status"] != nil {
resourceConfigStruct.Status = rMap["status"].(string)
}

// the cluster value is fetched from scale out action template as the resourceViews API does not return that information
deploymentResourceActions, _ := vraClient.GetResourceActions(parentResourceID)
deploymentActionsMap := GetActionNameIDMap(deploymentResourceActions)
scaleOutActionID := deploymentActionsMap["Scale Out"]
resourceActionTemplate, _ := vraClient.GetResourceActionTemplate(parentResourceID, scaleOutActionID)
actionTemplateResourceDataMap := GetActionTemplateDataByComponent(resourceActionTemplate.Data, componentName)
rDataMap := actionTemplateResourceDataMap["data"].(map[string]interface{})
var cluster int = int(rDataMap["_cluster"].(float64))
resourceConfigStruct.Cluster = cluster
// end
// the cluster value is calculated from the map based on the component name as the
// resourceViews API does not return that information
clusterCountMap[componentName] = clusterCountMap[componentName] + 1

resourceConfigList = append(resourceConfigList, resourceConfigStruct)

Expand Down Expand Up @@ -204,7 +193,7 @@ func dataSourceVra7DeploymentRead(d *schema.ResourceData, meta interface{}) erro
}
}
}
if err := d.Set("resource_configuration", flattenResourceConfigurations(resourceConfigList)); err != nil {
if err := d.Set("resource_configuration", flattenResourceConfigurations(resourceConfigList, clusterCountMap)); err != nil {
return fmt.Errorf("error setting resource configuration - error: %v", err)
}
d.SetId(id.(string))
Expand Down
4 changes: 2 additions & 2 deletions vra7/resource_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func expandResourceConfiguration(rConfigurations []interface{}) []sdk.ResourceCo
return configs
}

func flattenResourceConfigurations(configs []sdk.ResourceConfigurationStruct) []map[string]interface{} {
func flattenResourceConfigurations(configs []sdk.ResourceConfigurationStruct, clusterCountMap map[string]int) []map[string]interface{} {
if len(configs) == 0 {
return make([]map[string]interface{}, 0)
}
Expand All @@ -128,7 +128,7 @@ func flattenResourceConfigurations(configs []sdk.ResourceConfigurationStruct) []
helper["status"] = config.Status
helper["request_state"] = config.RequestState
helper["resource_type"] = config.ResourceType
helper["cluster"] = config.Cluster
helper["cluster"] = clusterCountMap[config.ComponentName]
helper["ip_address"] = config.IPAddress
rConfigs = append(rConfigs, helper)
}
Expand Down
40 changes: 16 additions & 24 deletions vra7/resource_vra7_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,19 @@ func resourceVra7DeploymentCreate(d *schema.ResourceData, meta interface{}) erro

requestTemplate.Description = p.Description
requestTemplate.Reasons = p.Reasons
requestTemplate.BusinessGroupID = p.BusinessGroupID
// if business group is not provided, the default business group in the request template is used
if p.BusinessGroupID != "" {
requestTemplate.BusinessGroupID = p.BusinessGroupID
}
requestTemplate.Data["_leaseDays"] = p.Lease
for field, value := range p.DeploymentConfiguration {
requestTemplate.Data[field] = utils.UnmarshalJSONStringIfNecessary(field, value)
}

for _, rConfig := range p.ResourceConfiguration {
rConfig.Configuration["_cluster"] = rConfig.Cluster
if rConfig.Cluster != 0 {
rConfig.Configuration["_cluster"] = rConfig.Cluster
}
for propertyName, propertyValue := range rConfig.Configuration {
requestTemplate.Data[rConfig.ComponentName] = updateRequestTemplate(
requestTemplate.Data[rConfig.ComponentName].(map[string]interface{}),
Expand All @@ -193,7 +198,7 @@ func resourceVra7DeploymentCreate(d *schema.ResourceData, meta interface{}) erro
catalogRequest, err := vraClient.RequestCatalogItem(requestTemplate)

if err != nil {
return fmt.Errorf("The catalog item qequest failed with error %v", err)
return fmt.Errorf("The catalog item request failed with error %v", err)
}
_, err = waitForRequestCompletion(d, meta, catalogRequest.ID)
if err != nil {
Expand Down Expand Up @@ -270,8 +275,8 @@ func resourceVra7DeploymentUpdate(d *schema.ResourceData, meta interface{}) erro
if oldResourceConfig.ComponentName != "" {
deploymentResourceActions, _ := vraClient.GetResourceActions(p.DeploymentID)
deploymentActionsMap := GetActionNameIDMap(deploymentResourceActions)
if oldResourceConfig.Cluster != newResourceConfig.Cluster {
if oldResourceConfig.Cluster < newResourceConfig.Cluster {
if newResourceConfig.Cluster != 0 && oldResourceConfig.Cluster != newResourceConfig.Cluster {
if oldResourceConfig.Cluster < newResourceConfig.Cluster && deploymentActionsMap[sdk.ScaleOut] != "" {
// Scale Out Day 2 operation
scaleOutActionID := deploymentActionsMap[sdk.ScaleOut]
// get the action template for scale out
Expand All @@ -298,7 +303,7 @@ func resourceVra7DeploymentUpdate(d *schema.ResourceData, meta interface{}) erro
return err
}
log.Info("Successfully completed the Scale In action for the deployment with id %v.", p.DeploymentID)
} else if oldResourceConfig.Cluster > newResourceConfig.Cluster {
} else if oldResourceConfig.Cluster > newResourceConfig.Cluster && deploymentActionsMap[sdk.ScaleIn] != "" {
// Scale In Day 2 operation
scaleInActionID := deploymentActionsMap[sdk.ScaleIn]
// get the action template for scale in
Expand Down Expand Up @@ -398,6 +403,7 @@ func resourceVra7DeploymentRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Resource view failed to load with the error %v", errTemplate)
}

clusterCountMap := make(map[string]int)
var resourceConfigList []sdk.ResourceConfigurationStruct
for _, resource := range requestResourceView.Content {
rMap := resource.(map[string]interface{})
Expand Down Expand Up @@ -433,18 +439,9 @@ func resourceVra7DeploymentRead(d *schema.ResourceData, meta interface{}) error
if rMap["status"] != nil {
resourceConfigStruct.Status = rMap["status"].(string)
}

// the cluster value is fetched from scale out action template as the resourceViews API does not return that information
deploymentResourceActions, _ := vraClient.GetResourceActions(parentResourceID)
deploymentActionsMap := GetActionNameIDMap(deploymentResourceActions)
scaleOutActionID := deploymentActionsMap["Scale Out"]
resourceActionTemplate, _ := vraClient.GetResourceActionTemplate(parentResourceID, scaleOutActionID)
actionTemplateResourceDataMap := GetActionTemplateDataByComponent(resourceActionTemplate.Data, componentName)
rDataMap := actionTemplateResourceDataMap["data"].(map[string]interface{})
var cluster int = int(rDataMap["_cluster"].(float64))
resourceConfigStruct.Cluster = cluster
// end

// the cluster value is calculated from the map based on the component name as the
// resourceViews API does not return that information
clusterCountMap[componentName] = clusterCountMap[componentName] + 1
resourceConfigList = append(resourceConfigList, resourceConfigStruct)

} else if resourceType == sdk.DeploymentResourceType {
Expand Down Expand Up @@ -483,7 +480,7 @@ func resourceVra7DeploymentRead(d *schema.ResourceData, meta interface{}) error
}
}
}
if err := d.Set("resource_configuration", flattenResourceConfigurations(resourceConfigList)); err != nil {
if err := d.Set("resource_configuration", flattenResourceConfigurations(resourceConfigList, clusterCountMap)); err != nil {
return fmt.Errorf("error setting resource configuration - error: %v", err)
}

Expand Down Expand Up @@ -585,11 +582,6 @@ func checkConfigValuesValidity(d *schema.ResourceData) error {
return fmt.Errorf("Provide either a catalog_item_name or a catalog_item_id in the configuration")
}

// If businessgroupID and businessgroupName both not provided then return an error
if businessgroupID == "" && businessgroupName == "" {
return fmt.Errorf("Provide either a businessgroup_id or a businessgroup_name in the configuration")
}

// If both businessgroupID and businessgroupName return an error
if businessgroupID != "" && businessgroupName != "" {
return fmt.Errorf("Provide either a businessgroup_id or a businessgroup_name in the configuration")
Expand Down