Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Fixing issues related to create, update and read (#98)
Browse files Browse the repository at this point in the history
* Fixing issues related to create, update and read
1. When reconfigure machine action is not enabled in the entitlement, do not allow update
2. In read resource, do not populate reconfigure template, just show whatever the provisioned request resource returns
3. Check the validity of config file in both create and update resource
4. Call appropriate vRA APIs to get the allowed actions on a provisioned resource
5. Return the failure message from the API when a request fails.
6. Code clean up: Handle error scenarios, add string constants, separate schema in a different file
Signed-off-by: Prativa Bawri <[email protected]>

Signed-off-by: Prativa Bawri <[email protected]>
  • Loading branch information
Prativa20 authored and Vijay Raghavan committed Oct 26, 2018
1 parent de38ad1 commit 3512b26
Show file tree
Hide file tree
Showing 7 changed files with 662 additions and 477 deletions.
45 changes: 43 additions & 2 deletions utils/contants.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
package utils

const (
REQUEST_STATUS = "request_status"
//utility constants
CATALOG_NAME = "catalog_name"
CATALOG_ID = "catalog_id"
BUSINESS_GROUP_ID = "businessgroup_id"
BUSINESS_GROUP_NAME = "businessgroup_name"
WAIT_TIME_OUT = "wait_timeout"
FAILED_MESSAGE = "failed_message"
DEPLOYMENT_CONFIGURATION = "deployment_configuration"
RESOURCE_CONFIGURATION = "resource_configuration"
CATALOG_CONFIGURATION = "catalog_configuration"
REQUEST_STATUS = "request_status"
INFRASTRUCTURE_VIRTUAL = "Infrastructure.Virtual"
COMPONENT = "Component"
RECONFIGURE = "Reconfigure"

// read resource machine constants
MACHINE_CPU = "cpu"
MACHINE_STORAGE = "storage"
MACHINE_MEMORY = "memory"
IP_ADDRESS = "ip_address"
MACHINE_NAME = "name"
MACHINE_GUEST_OS = "guest_operating_system"
MACHINE_BP_NAME = "blueprint_name"
MACHINE_TYPE = "type"
MACHINE_RESERVATION_NAME = "reservation_name"
MACHINE_INTERFACE_TYPE = "interface_type"
MACHINE_ID = "id"
MACHINE_GROUP_NAME = "group_name"
MACHINE_DESTRUCTION_DATE = "destruction_date"
MACHINE_RECONFIGURE = "reconfigure"
MACHINE_POWER_OFF = "power_off"

// utility constants
LOGGER_ID = "terraform-provider-vra7"
WINDOWS_PATH_SEPARATOR = "\\"
UNIX_PATH_SEPARATOR = "/"
WINDOWS_OS = "windows"

//error constants
CONFIG_INVALID_ERROR = "The resource_configuration in the config file has invalid component name(s): %v "

// api constants
CATALOG_SERVICE = "/catalog-service"
CATALOG_SERVICE_API = CATALOG_SERVICE + "/api"
CONSUMER = CATALOG_SERVICE_API + "/consumer"
CONSUMER_REQUESTS = CONSUMER + "/requests"
CONSUMER_RESOURCES = CONSUMER + "/resources"
GET_RESOURCE_API = CONSUMER_REQUESTS + "/" + "%s" + "/resources"
POST_ACTION_TEMPLATE_API = CONSUMER_RESOURCES + "/" + "%s" + "/actions/" + "%s" + "/requests"
GET_ACTION_TEMPLATE_API = POST_ACTION_TEMPLATE_API + "/template"
GET_REQUEST_RESOURCE_VIEW_API = CONSUMER_REQUESTS + "/" + "%s" + "/resourceViews"
)
3 changes: 2 additions & 1 deletion vrealize/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package vrealize
import (
"errors"
"fmt"
"gopkg.in/jarcoal/httpmock.v1"
"testing"

"gopkg.in/jarcoal/httpmock.v1"
)

//var client APIClient
Expand Down
23 changes: 11 additions & 12 deletions vrealize/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
)


//CatalogItemRequestTemplate - A structure that captures a catalog request template, to be filled in and POSTED.
type CatalogItemRequestTemplate struct {
Type string `json:"type"`
Expand Down Expand Up @@ -66,7 +65,7 @@ type Metadata struct {
}

// readCatalogItemNameByID - This function returns the catalog item name using catalog item ID
func (c *APIClient) readCatalogItemNameByID(catalogItemID string) (interface{}, error) {
func (c *APIClient) readCatalogItemNameByID(catalogItemID string) (string, error) {
//Form a path to read catalog template via REST call
path := fmt.Sprintf("/catalog-service/api/consumer/entitledCatalogItems/"+
"%s", catalogItemID)
Expand All @@ -77,18 +76,18 @@ func (c *APIClient) readCatalogItemNameByID(catalogItemID string) (interface{},
_, err := c.HTTPClient.New().Get(path).Receive(template, apiError)

if err != nil {
return nil, err
return "", err
}

if !apiError.isEmpty() {
return nil, apiError
return "", apiError
}
//Return catalog Name
return template.CatalogItem.Name, nil
}

//readCatalogItemIdByName - To read id of catalog from vRA using catalog_name
func (c *APIClient) readCatalogItemIDByName(catalogName string) (interface{}, error) {
func (c *APIClient) readCatalogItemIDByName(catalogName string) (string, error) {
var catalogItemID string

log.Info("readCatalogItemIdByName->catalog_name %v\n", catalogName)
Expand All @@ -102,11 +101,11 @@ func (c *APIClient) readCatalogItemIDByName(catalogName string) (interface{}, er
_, preErr := c.HTTPClient.New().Get(path).Receive(template, apiError)

if preErr != nil {
return nil, preErr
return "", preErr
}

if !apiError.isEmpty() {
return nil, apiError
return "", apiError
}

//Fetch all catalogs from vRA
Expand All @@ -115,11 +114,11 @@ func (c *APIClient) readCatalogItemIDByName(catalogName string) (interface{}, er
resp, errResp := c.HTTPClient.New().Get(path).Receive(template, apiError)

if !apiError.isEmpty() {
return nil, apiError
return "", apiError
}

if resp.StatusCode != 200 {
return nil, errResp
return "", errResp
}

var catalogItemNameArray []string
Expand All @@ -134,7 +133,7 @@ func (c *APIClient) readCatalogItemIDByName(catalogName string) (interface{}, er
//If exact name matches then return respective catalog_id
//else if provided catalog matches as a substring in name then store it in array
if catalogName == catalogItem["name"].(string) {
return catalogItem["catalogItemId"].(interface{}), nil
return catalogItem["catalogItemId"].(string), nil
} else if catalogName == catalogItem["name"].(string)[0:catalogItemNameLen] {
catalogItemNameArray = append(catalogItemNameArray, catalogItem["name"].(string))
}
Expand All @@ -153,12 +152,12 @@ func (c *APIClient) readCatalogItemIDByName(catalogName string) (interface{}, er
if len(catalogItemNameArray) > 1 {
punctuation = "are"
}
return nil, fmt.Errorf("There %s total %d catalog(s) present with same name.\n%s\n"+
return "", fmt.Errorf("There %s total %d catalog(s) present with same name.\n%s\n"+
"Please select from above.", punctuation, len(catalogItemNameArray), errorMessage)
}

if !apiError.isEmpty() {
return nil, apiError
return "", apiError
}
return catalogItemID, nil
}
22 changes: 11 additions & 11 deletions vrealize/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package vrealize
import (
"errors"
"fmt"
"gopkg.in/jarcoal/httpmock.v1"
"testing"

"gopkg.in/jarcoal/httpmock.v1"
)

func init() {
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestFetchCatalogItemByName(t *testing.T) {
t.Errorf("Error while fetching catalog Item %v", err)
}

if catalogItemID == nil || catalogItemID == "" {
if catalogItemID == "" {
t.Errorf("Catalog ID is nil")
}

Expand All @@ -85,7 +86,7 @@ func TestFetchCatalogItemByName(t *testing.T) {
// This should return an error
catalogItemID, err = client.readCatalogItemIDByName("Cent OS 6.3")

if catalogItemID != nil && catalogItemID != "" {
if catalogItemID != "" {
t.Errorf("Catalog Item ID is not nil")
}

Expand All @@ -98,7 +99,7 @@ func TestFetchCatalogItemByName(t *testing.T) {
// This should return empty catalogItemID with suggestions with full name
catalogItemID, err = client.readCatalogItemIDByName("CentOS")

if catalogItemID != nil && catalogItemID != "" {
if catalogItemID != "" {
t.Errorf("Catalog Item ID is not nil")
}

Expand All @@ -115,7 +116,7 @@ func TestFetchCatalogItemByName(t *testing.T) {
// This should return empty catalogItemID with suggestions with full name
catalogItemID, err = client.readCatalogItemIDByName("CentOS")

if catalogItemID != nil {
if catalogItemID != "" {
t.Errorf("Catalog Item ID is not nil")
}

Expand All @@ -130,7 +131,7 @@ func TestFetchCatalogItemByName(t *testing.T) {
t.Errorf("Data fetched with wrong catalog ID")
}

if catalogItemID != nil {
if catalogItemID != "" {
t.Errorf("Wrong catalog data got fetched")
}
httpmock.Reset()
Expand All @@ -147,7 +148,7 @@ func TestFetchCatalogItemByName(t *testing.T) {
t.Errorf("Data fetched with wrong catalog item ID")
}

if catalogItemID != nil {
if catalogItemID != "" {
t.Errorf("Wrong catalog item data got fetched")
}

Expand All @@ -158,7 +159,6 @@ func TestFetchCatalogItemByName(t *testing.T) {
catalogItemID, err = client.readCatalogItemIDByName("CentOS")
}


// This unit test function contains test cases related to catalog ID in tf config files
// Scenarios :
// 1) Fetch catalog item details using correct catalog item ID
Expand All @@ -175,7 +175,7 @@ func TestFetchCatalogItemByID(t *testing.T) {
t.Errorf("Error while fetching catalog item %v", err)
}

if catalogItemName == nil {
if catalogItemName == "" {
t.Errorf("Catalog Item Name is is nil")
}

Expand All @@ -188,7 +188,7 @@ func TestFetchCatalogItemByID(t *testing.T) {
t.Errorf("Data fetched with wrong catalog ID")
}

if catalogItemName != nil {
if catalogItemName != "" {
t.Errorf("Wrong catalog item data got fetched")
}

Expand All @@ -200,7 +200,7 @@ func TestFetchCatalogItemByID(t *testing.T) {
t.Errorf("Data fetched with wrong catalog ID")
}

if catalogItemName != nil {
if catalogItemName != "" {
t.Errorf("Wrong catalog data got fetched")
}
}
Loading

0 comments on commit 3512b26

Please sign in to comment.