Skip to content

Commit

Permalink
Merge pull request #66 from HewlettPackard/resourcePoolIDFix
Browse files Browse the repository at this point in the history
US49806: ResourcePoolID datatype changed to interface
  • Loading branch information
manjunath-batakurki authored Jun 12, 2023
2 parents 3d5d1f3 + 1fae7d0 commit 0c28f92
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 53 deletions.
69 changes: 39 additions & 30 deletions pkg/client/instances.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP
// (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP

package client

import (
"context"
"encoding/json"
"fmt"
"reflect"

"github.com/antihax/optional"

Expand Down Expand Up @@ -49,6 +50,18 @@ func (a *InstancesAPIService) CreateAnInstance(ctx context.Context,
localVarOptionals *models.CreateInstanceBody) (models.GetInstanceResponse, error) {
createInstanceResp := models.GetInstanceResponse{}

// Pre-pending 'pool-' to ResourcePoolId in 6.0.3 and above
if v, _ := parseVersion("6.0.3"); v <= a.Client.getVersion() {
// To check if the key exists
localVarOptionalsValue := reflect.ValueOf(localVarOptionals).Elem()
if field := localVarOptionalsValue.FieldByName("Config"); field.IsValid() && !field.IsZero() {
configValue := reflect.ValueOf(localVarOptionals.Config).Elem()
if configField := configValue.FieldByName("ResourcePoolID"); configField.IsValid() && !configField.IsZero() {
localVarOptionals.Config.ResourcePoolID = fmt.Sprintf("pool-%v",
localVarOptionals.Config.ResourcePoolID)
}
}
}
createInstanceAPI := &api{
method: "POST",
path: consts.InstancesPath,
Expand Down Expand Up @@ -102,10 +115,11 @@ func (a *InstancesAPIService) DeleteAnInstance(ctx context.Context,
/*
InstancesAPIService
Get a Specific Instance
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
* @param serviceInstanceID
* @param instanceID
- @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
- @param serviceInstanceID
- @param instanceID
@return models.GetInstanceResponse
*/
func (a *InstancesAPIService) GetASpecificInstance(ctx context.Context,
Expand Down Expand Up @@ -156,11 +170,10 @@ func (a *InstancesAPIService) GetAllInstances(ctx context.Context,
/*
InstancesAPIService
Lists VMware Snapshot of the instance
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
* @param serviceInstanceID
* @param instanceID
- @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
- @param serviceInstanceID
- @param instanceID
*/
func (a *InstancesAPIService) GetListOfSnapshotsForAnInstance(ctx context.Context,
instanceID int) (models.ListSnapshotResponse, error) {
Expand Down Expand Up @@ -216,11 +229,10 @@ func (a *InstancesAPIService) ImportSnapshotOfAnInstance(ctx context.Context, in
/*
InstancesAPIService
Restarts all VM running within an instance
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
* @param serviceInstanceID
* @param instanceID
- @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
- @param serviceInstanceID
- @param instanceID
*/
func (a *InstancesAPIService) RestartAnInstance(ctx context.Context,
instanceID int) (models.InstancePowerResponse, error) {
Expand Down Expand Up @@ -275,11 +287,10 @@ func (a *InstancesAPIService) SnapshotAnInstance(ctx context.Context, instanceID
/*
InstancesAPIService
Starts all VM running within an instance
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
* @param serviceInstanceID
* @param instanceID
- @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
- @param serviceInstanceID
- @param instanceID
*/
func (a *InstancesAPIService) StartAnInstance(ctx context.Context,
instanceID int) (models.InstancePowerResponse, error) {
Expand All @@ -303,11 +314,10 @@ func (a *InstancesAPIService) StartAnInstance(ctx context.Context,
/*
InstancesAPIService
Stops all VM running within an instance
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
* @param serviceInstanceID
* @param instanceID
- @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
- @param serviceInstanceID
- @param instanceID
*/
func (a *InstancesAPIService) StopAnInstance(ctx context.Context,
instanceID int) (models.InstancePowerResponse, error) {
Expand All @@ -331,11 +341,10 @@ func (a *InstancesAPIService) StopAnInstance(ctx context.Context,
/*
InstancesAPIService
Suspends all VM running within an instance
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
* @param serviceInstanceID
* @param instanceID
- @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc.
Passed from http.Request or context.Background().
- @param serviceInstanceID
- @param instanceID
*/
func (a *InstancesAPIService) SuspendAnInstance(ctx context.Context,
instanceID int) (models.InstancePowerResponse, error) {
Expand Down
54 changes: 51 additions & 3 deletions pkg/client/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,55 @@ func TestInstancesAPIService_CreateAnInstance(t *testing.T) {
CloneName: "Instance_Create",
}

m.EXPECT().getVersion().Return(999999)
m.EXPECT().getVersion().Return(999999).MaxTimes(2)
m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{},
url.Values{}, "", nil).Return(req, nil)

m.EXPECT().callAPI(req).Return(&http.Response{
StatusCode: 200,
Body: respBody,
}, nil)
},
want: models.GetInstanceResponse{
Instance: &models.GetInstanceResponseInstance{
ID: 1,
Name: "test_create_an_instance",
},
},
wantErr: false,
},
{
name: "Normal Test case 2: Create an Instance",
param: models.CreateInstanceBody{
ZoneID: "1",
CloneName: "Instance_Create",
},
given: func(m *MockAPIClientHandler) {
m.EXPECT().getHost().Return(mockHost)
path := mockHost + "/" + consts.VmaasCmpAPIBasePath + "/instances"
method := "POST"
headers := getDefaultHeaders()
postBody := ioutil.NopCloser(bytes.NewReader([]byte(`
{
"zoneId": "1",
"name": "Instance_Create"
}
`)))
req, _ := http.NewRequest(method, path, postBody)
respBody := ioutil.NopCloser(bytes.NewReader([]byte(`
{
"instance": {
"id": 1,
"name": "test_create_an_instance"
}
}
`)))
pBody := &models.CreateInstanceBody{
ZoneID: "1",
CloneName: "Instance_Create",
}

m.EXPECT().getVersion().Return(50412).MaxTimes(2)
m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{},
url.Values{}, "", nil).Return(req, nil)

Expand Down Expand Up @@ -255,7 +303,7 @@ func TestInstancesAPIService_CreateAnInstance(t *testing.T) {
ZoneID: "1",
CloneName: "Instance_Create",
}
m.EXPECT().getVersion().Return(999999)
m.EXPECT().getVersion().Return(999999).MaxTimes(2)
m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{},
url.Values{}, "", nil).Return(nil, errors.New("prepare request error"))
},
Expand Down Expand Up @@ -293,7 +341,7 @@ func TestInstancesAPIService_CreateAnInstance(t *testing.T) {
ZoneID: "1",
CloneName: "Instance_Create",
}
m.EXPECT().getVersion().Return(999999)
m.EXPECT().getVersion().Return(999999).MaxTimes(2)
m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{},
url.Values{}, "", nil).Return(req, nil)

Expand Down
30 changes: 15 additions & 15 deletions pkg/models/clouds.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP
// (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP

package models

Expand Down Expand Up @@ -53,20 +53,20 @@ type CloudRespBody struct {
Autorecoverpowerstate bool `json:"autoRecoverPowerState"`
Scalepriority int `json:"scalePriority"`
Config struct {
Apiurl string `json:"apiUrl"`
Username string `json:"username"`
Password string `json:"password"`
Datacenter string `json:"datacenter"`
Cluster string `json:"cluster"`
Resourcepoolid string `json:"resourcePoolId"`
Resourcepool string `json:"resourcePool"`
Rpcmode string `json:"rpcMode"`
Hidehostselection string `json:"hideHostSelection"`
Enablevnc string `json:"enableVnc"`
Diskstoragetype string `json:"diskStorageType"`
Applianceurl string `json:"applianceUrl"`
Datacentername string `json:"datacenterName"`
NetworkserverID string `json:"networkServer.id"`
Apiurl string `json:"apiUrl"`
Username string `json:"username"`
Password string `json:"password"`
Datacenter string `json:"datacenter"`
Cluster string `json:"cluster"`
Resourcepoolid interface{} `json:"resourcePoolId"`
Resourcepool string `json:"resourcePool"`
Rpcmode string `json:"rpcMode"`
Hidehostselection string `json:"hideHostSelection"`
Enablevnc string `json:"enableVnc"`
Diskstoragetype string `json:"diskStorageType"`
Applianceurl string `json:"applianceUrl"`
Datacentername string `json:"datacenterName"`
NetworkserverID string `json:"networkServer.id"`
Networkserver struct {
ID string `json:"id"`
} `json:"networkServer"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/models/instances.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP
// (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP

package models

Expand Down Expand Up @@ -57,7 +57,7 @@ type CreateInstanceBodyConfig struct {
// To specify agent install (on/off)
NoAgent string `json:"noAgent,omitempty"`
Template int `json:"template,omitempty"`
ResourcePoolID json.Number `json:"resourcePoolId"`
ResourcePoolID interface{} `json:"resourcePoolId"`
SmbiosAssetTag string `json:"smbiosAssetTag,omitempty"`
HostID string `json:"hostId,omitempty"`
VmwareDomainName string `json:"vmwareDomainName,omitempty"`
Expand Down Expand Up @@ -186,7 +186,7 @@ type GetInstanceResponseInstanceCloud struct {

// GetInstanceResponseInstanceConfig
type GetInstanceResponseInstanceConfig struct {
ResourcePoolID json.Number `json:"resourcePoolId,omitempty"`
ResourcePoolID interface{} `json:"resourcePoolId,omitempty"`
Template int `json:"template,omitempty"`
Poolprovidertype interface{} `json:"poolProviderType,omitempty"`
Isvpcselectable bool `json:"isVpcSelectable,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/models/servers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP
// (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP

package models

Expand Down Expand Up @@ -33,7 +33,7 @@ type Server struct {
Description interface{} `json:"description"`
ZoneID int `json:"zoneId"`
SiteID int `json:"siteId"`
ResourcePoolID int `json:"resourcePoolId"`
ResourcePoolID interface{} `json:"resourcePoolId"`
FolderID int `json:"folderId"`
SSHHost interface{} `json:"sshHost"`
SSHPort int `json:"sshPort"`
Expand Down

0 comments on commit 0c28f92

Please sign in to comment.