diff --git a/pkg/client/instances.go b/pkg/client/instances.go index d78205f..26fc256 100644 --- a/pkg/client/instances.go +++ b/pkg/client/instances.go @@ -125,72 +125,22 @@ Create an Instance func (a *InstancesAPIService) CreateAnInstance(ctx context.Context, localVarOptionals *models.CreateInstanceBody) (models.GetInstanceResponse, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Post") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - createInstanceResponse models.GetInstanceResponse - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{"application/json"} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - // body params - if localVarOptionals != nil { - var err error - localVarPostBody, err = json.Marshal(localVarOptionals) - if err != nil { - return createInstanceResponse, err - } - } + createInstanceResp := models.GetInstanceResponse{} - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return createInstanceResponse, err - } - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return createInstanceResponse, err - } - if localVarHTTPResponse.StatusCode >= 300 { - return createInstanceResponse, ParseError(localVarHTTPResponse) - } + createInstanceAPI := &api{ + method: "POST", + path: fmt.Sprintf("%s/%s/%s", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath), + client: a.Client, - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() - if err != nil { - return createInstanceResponse, err + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &createInstanceResp) + }, } - if err = json.Unmarshal(localVarBody, &createInstanceResponse); err != nil { - return createInstanceResponse, err - } + err := createInstanceAPI.do(ctx, localVarOptionals, nil) - return createInstanceResponse, nil + return createInstanceResp, err } /* @@ -211,67 +161,21 @@ type InstancesAPIDeleteAnIstanceOpts struct { func (a *InstancesAPIService) DeleteAnInstance(ctx context.Context, instanceID int) (models.SuccessOrErrorMessage, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Delete") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return models.SuccessOrErrorMessage{}, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return models.SuccessOrErrorMessage{}, err - } - - if localVarHTTPResponse.StatusCode >= 300 { - return models.SuccessOrErrorMessage{}, ParseError(localVarHTTPResponse) - } + delInstanceResp := models.SuccessOrErrorMessage{} - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() - if err != nil { - return models.SuccessOrErrorMessage{}, err - } + delInstanceAPI := &api{ + method: "DELETE", + path: fmt.Sprintf("%s/%s/%s/%d", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, - // fmt.Println(string(localVarBody)) - var instancesResponse models.SuccessOrErrorMessage - if err = json.Unmarshal(localVarBody, &instancesResponse); err != nil { - return models.SuccessOrErrorMessage{}, err + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &delInstanceResp) + }, } + err := delInstanceAPI.do(ctx, nil, nil) - return instancesResponse, nil + return delInstanceResp, err } /* @@ -285,66 +189,21 @@ Get a Specific Instance */ func (a *InstancesAPIService) GetASpecificInstance(ctx context.Context, instanceID int) (models.GetInstanceResponse, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Get") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - getInstanceResponse models.GetInstanceResponse - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return getInstanceResponse, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return getInstanceResponse, err - } - - if localVarHTTPResponse.StatusCode >= 300 { - return getInstanceResponse, ParseError(localVarHTTPResponse) - } + specificInstResp := models.GetInstanceResponse{} - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() - if err != nil { - return getInstanceResponse, err - } + specificInstanceAPI := &api{ + method: "GET", + path: fmt.Sprintf("%s/%s/%s/%d", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, - if err = json.Unmarshal(localVarBody, &getInstanceResponse); err != nil { - return getInstanceResponse, err + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &specificInstResp) + }, } + err := specificInstanceAPI.do(ctx, nil, nil) - return getInstanceResponse, nil + return specificInstResp, err } /* @@ -355,7 +214,8 @@ Fetch the list of available instance types * @param serviceInstanceID */ -func (a *InstancesAPIService) GetAllInstanceTypesForProvisioning(ctx context.Context) (*http.Response, error) { +func (a *InstancesAPIService) GetAllInstanceTypesForProvisioning(ctx context.Context) (*http.Response, + error) { var ( localVarHTTPMethod = strings.ToUpper("Get") localVarPostBody interface{} @@ -442,66 +302,21 @@ type InstancesAPIGetAllInstancesOpts struct { func (a *InstancesAPIService) GetAllInstances(ctx context.Context, queryParams map[string]string) (models.Instances, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Get") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - instancesResponse models.Instances - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := getURLValues(queryParams) - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return instancesResponse, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return instancesResponse, err - } - - if localVarHTTPResponse.StatusCode >= 300 { - return instancesResponse, ParseError(localVarHTTPResponse) - } + getAllInstance := models.Instances{} - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() - if err != nil { - return instancesResponse, err - } + instanceAPI := &api{ + method: "GET", + path: fmt.Sprintf("%s/%s/%s", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath), + client: a.Client, - if err = json.Unmarshal(localVarBody, &instancesResponse); err != nil { - return instancesResponse, err + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &getAllInstance) + }, } + err := instanceAPI.do(ctx, nil, queryParams) - return instancesResponse, nil + return getAllInstance, err } /* @@ -521,457 +336,32 @@ the list of available plans when creating a new instance or resizing an existing type InstancesAPIGetAvailableServicePlansForAnInstanceOpts struct { SiteID optional.Int32 -} - -func (a *InstancesAPIService) GetAvailableServicePlansForAnInstance(ctx context.Context, zoneID int, layoutID int, - localVarOptionals *InstancesAPIGetAvailableServicePlansForAnInstanceOpts) (models.GetServicePlanResponse, - *http.Response, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Get") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - localVarReturnValue models.GetServicePlanResponse - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/service-plans", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - localVarQueryParams.Add("zoneId", parameterToString(zoneID, "")) - localVarQueryParams.Add("layoutId", parameterToString(layoutID, "")) - if localVarOptionals != nil && localVarOptionals.SiteID.IsSet() { - localVarQueryParams.Add("siteId", parameterToString(localVarOptionals.SiteID.Value(), "")) - } - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return localVarReturnValue, nil, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return localVarReturnValue, localVarHTTPResponse, err - } - - if localVarHTTPResponse.StatusCode >= 300 { - return localVarReturnValue, localVarHTTPResponse, ParseError(localVarHTTPResponse) - } - - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - localVarHTTPResponse.Body.Close() - if err != nil { - return localVarReturnValue, localVarHTTPResponse, err - } - - if localVarHTTPResponse.StatusCode < 300 { - // If we succeed, return the data, otherwise pass on to decode error. - err = a.Client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) - if err == nil { - return localVarReturnValue, localVarHTTPResponse, err - } - } - - return localVarReturnValue, localVarHTTPResponse, nil -} - -/* -InstancesAPIService -List all environment variables associated with the instance - * @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) GetEnvVariables(ctx context.Context, instanceID int) (*http.Response, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Get") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/envs", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return nil, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return localVarHTTPResponse, err - } - - if localVarHTTPResponse.StatusCode >= 300 { - return localVarHTTPResponse, ParseError(localVarHTTPResponse) - } - - return localVarHTTPResponse, nil -} - -/* -InstancesAPIService -Retrieves the process history for 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 - -*/ - -/* -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 - -*/ -func (a *InstancesAPIService) GetListOfSnapshotsForAnInstance(ctx context.Context, - instanceID int) (models.ListSnapshotResponse, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Get") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - snapshotResponse models.ListSnapshotResponse - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/snapshots", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return snapshotResponse, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return snapshotResponse, err - } - - if localVarHTTPResponse.StatusCode >= 300 { - return snapshotResponse, ParseError(localVarHTTPResponse) - } - - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() - if err != nil { - return snapshotResponse, err - } - if err = json.Unmarshal(localVarBody, &snapshotResponse); err != nil { - return snapshotResponse, err - } - - return snapshotResponse, nil -} - -/* -InstancesAPIService -Fetch an instance type by ID - * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. - Passed from http.Request or context.Background(). - * @param serviceInstanceID - * @param instanceTypeID - -*/ -func (a *InstancesAPIService) GetSpecificInstanceTypeForProvisioning(ctx context.Context, - instanceTypeID int) (*http.Response, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Get") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstanceTypesPath, instanceTypeID) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return nil, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return localVarHTTPResponse, err - } - - if localVarHTTPResponse.StatusCode >= 300 { - return localVarHTTPResponse, ParseError(localVarHTTPResponse) - } - - return localVarHTTPResponse, nil -} - -/* -InstancesAPIService -It is possible to import a snapshot of an instance. This creates a Virtual Image of the instance as -it currently exists. - * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. - Passed from http.Request or context.Background(). - * @param serviceInstanceID - * @param instanceID - * @param optional nil or *InstancesAPIImportSnapshotOfAnInstanceOpts - Optional Parameters: - * @param "Body" (optional.Interface of ImportSnapshotBody) - - -*/ - -func (a *InstancesAPIService) ImportSnapshotOfAnInstance(ctx context.Context, instanceID int, - localVarOptionals *models.ImportSnapshotBody) (models.SuccessOrErrorMessage, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Put") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/import-snapshot", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{"application/json"} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - // body params - if localVarOptionals != nil { - var err error - localVarPostBody, err = json.Marshal(localVarOptionals) - if err != nil { - return models.SuccessOrErrorMessage{}, err - } - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return models.SuccessOrErrorMessage{}, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return models.SuccessOrErrorMessage{}, err - } - if localVarHTTPResponse.StatusCode >= 300 { - return models.SuccessOrErrorMessage{}, ParseError(localVarHTTPResponse) - } - - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - localVarHTTPResponse.Body.Close() - if err != nil { - return models.SuccessOrErrorMessage{}, err - } - - var instanceResponse models.SuccessOrErrorMessage - if err = json.Unmarshal(localVarBody, &instanceResponse); err != nil { - return models.SuccessOrErrorMessage{}, err - } - - return instanceResponse, nil -} - -/* -InstancesAPIService -'This will lock the instance. While locked, instances may not be removed.' - * @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) LockAnInstance(ctx context.Context, instanceID int) (*http.Response, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Put") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/lock", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) - - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} - - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} - - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType - } - - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} - - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } - - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return nil, err - } - - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return localVarHTTPResponse, err - } - - if localVarHTTPResponse.StatusCode >= 300 { - return localVarHTTPResponse, ParseError(localVarHTTPResponse) - } - - return localVarHTTPResponse, nil -} - -/* -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 +} -*/ -func (a *InstancesAPIService) RestartAnInstance(ctx context.Context, instanceID int) (models.InstancePowerResponse, error) { +func (a *InstancesAPIService) GetAvailableServicePlansForAnInstance(ctx context.Context, zoneID int, layoutID int, + localVarOptionals *InstancesAPIGetAvailableServicePlansForAnInstanceOpts) (models.GetServicePlanResponse, + *http.Response, error) { var ( - localVarHTTPMethod = strings.ToUpper("Put") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - instanceStateResponse models.InstancePowerResponse + localVarHTTPMethod = strings.ToUpper("Get") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte + localVarReturnValue models.GetServicePlanResponse ) // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/restart", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) + localVarPath := fmt.Sprintf("%s/%s/%s/service-plans", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath) localVarHeaderParams := make(map[string]string) localVarQueryParams := url.Values{} localVarFormParams := url.Values{} + localVarQueryParams.Add("zoneId", parameterToString(zoneID, "")) + localVarQueryParams.Add("layoutId", parameterToString(layoutID, "")) + if localVarOptionals != nil && localVarOptionals.SiteID.IsSet() { + localVarQueryParams.Add("siteId", parameterToString(localVarOptionals.SiteID.Value(), "")) + } // to determine the Content-Type header localVarHTTPContentTypes := []string{} @@ -993,63 +383,62 @@ func (a *InstancesAPIService) RestartAnInstance(ctx context.Context, instanceID r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { - return instanceStateResponse, err + return localVarReturnValue, nil, err } localVarHTTPResponse, err := a.Client.callAPI(r) if err != nil || localVarHTTPResponse == nil { - return instanceStateResponse, err + return localVarReturnValue, localVarHTTPResponse, err } if localVarHTTPResponse.StatusCode >= 300 { - return instanceStateResponse, ParseError(localVarHTTPResponse) + return localVarReturnValue, localVarHTTPResponse, ParseError(localVarHTTPResponse) } localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() + localVarHTTPResponse.Body.Close() if err != nil { - return instanceStateResponse, err + return localVarReturnValue, localVarHTTPResponse, err } - if err = json.Unmarshal(localVarBody, &instanceStateResponse); err != nil { - return instanceStateResponse, err + if localVarHTTPResponse.StatusCode < 300 { + // If we succeed, return the data, otherwise pass on to decode error. + err = a.Client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) + if err == nil { + return localVarReturnValue, localVarHTTPResponse, err + } } - return instanceStateResponse, nil + return localVarReturnValue, localVarHTTPResponse, nil } /* InstancesAPIService -Creates VMware Snapshot of the instance +List all environment variables associated with 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 optional nil or *InstancesAPISnapshotAnInstanceOpts - Optional Parameters: - * @param "Body" (optional.Interface of SnapshotBody) - */ - -func (a *InstancesAPIService) SnapshotAnInstance(ctx context.Context, instanceID int, - localVarOptionals *models.SnapshotBody) (models.Instances, error) { +func (a *InstancesAPIService) GetEnvVariables(ctx context.Context, + instanceID int) (*http.Response, error) { var ( - localVarHTTPMethod = strings.ToUpper("Put") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - snapshotInstanceresp models.Instances + localVarHTTPMethod = strings.ToUpper("Get") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte ) // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/snapshot", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + localVarPath := fmt.Sprintf("%s/%s/%s/%d/envs", a.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.InstancesPath, instanceID) - localVarHeaderParams := make(map[string]string) localVarQueryParams := url.Values{} localVarFormParams := url.Values{} // to determine the Content-Type header - localVarHTTPContentTypes := []string{"application/json"} + localVarHTTPContentTypes := []string{} // set Content-Type header localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) @@ -1065,64 +454,84 @@ func (a *InstancesAPIService) SnapshotAnInstance(ctx context.Context, instanceID if localVarHTTPHeaderAccept != "" { localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept } - // body params - if localVarOptionals != nil { - var err error - localVarPostBody, err = json.Marshal(localVarOptionals) - if err != nil { - return snapshotInstanceresp, err - } - } r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { - return snapshotInstanceresp, err + return nil, err } localVarHTTPResponse, err := a.Client.callAPI(r) if err != nil || localVarHTTPResponse == nil { - return snapshotInstanceresp, err + return localVarHTTPResponse, err } if localVarHTTPResponse.StatusCode >= 300 { - return snapshotInstanceresp, ParseError(localVarHTTPResponse) + return localVarHTTPResponse, ParseError(localVarHTTPResponse) } - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - localVarHTTPResponse.Body.Close() - if err != nil { - return snapshotInstanceresp, err - } + return localVarHTTPResponse, nil +} + +/* +InstancesAPIService +Retrieves the process history for 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 + +*/ + +/* +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 + +*/ +func (a *InstancesAPIService) GetListOfSnapshotsForAnInstance(ctx context.Context, + instanceID int) (models.ListSnapshotResponse, error) { + listSnapshotResp := models.ListSnapshotResponse{} + + listSnapshotAPI := &api{ + method: "GET", + path: fmt.Sprintf("%s/%s/%s/%d/snapshots", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, - if err := json.Unmarshal(localVarBody, &snapshotInstanceresp); err != nil { - return snapshotInstanceresp, err + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &listSnapshotResp) + }, } + err := listSnapshotAPI.do(ctx, nil, nil) - return snapshotInstanceresp, nil + return listSnapshotResp, err } /* InstancesAPIService -Starts all VM running within an instance +Fetch an instance type by ID * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). * @param serviceInstanceID - * @param instanceID + * @param instanceTypeID */ -func (a *InstancesAPIService) StartAnInstance(ctx context.Context, instanceID int) (models.InstancePowerResponse, error) { +func (a *InstancesAPIService) GetSpecificInstanceTypeForProvisioning(ctx context.Context, + instanceTypeID int) (*http.Response, error) { var ( - localVarHTTPMethod = strings.ToUpper("Put") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - instanceStateResponse models.InstancePowerResponse + localVarHTTPMethod = strings.ToUpper("Get") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte ) // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/start", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) + localVarPath := fmt.Sprintf("%s/%s/%s/%d", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstanceTypesPath, instanceTypeID) localVarHeaderParams := make(map[string]string) localVarQueryParams := url.Values{} @@ -1149,50 +558,73 @@ func (a *InstancesAPIService) StartAnInstance(ctx context.Context, instanceID in r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { - return instanceStateResponse, err + return nil, err } localVarHTTPResponse, err := a.Client.callAPI(r) if err != nil || localVarHTTPResponse == nil { - return instanceStateResponse, err + return localVarHTTPResponse, err } + if localVarHTTPResponse.StatusCode >= 300 { - return instanceStateResponse, ParseError(localVarHTTPResponse) + return localVarHTTPResponse, ParseError(localVarHTTPResponse) } - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() - if err != nil { - return instanceStateResponse, err - } + return localVarHTTPResponse, nil +} + +/* +InstancesAPIService +It is possible to import a snapshot of an instance. This creates a Virtual Image of the instance as +it currently exists. + * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. + Passed from http.Request or context.Background(). + * @param serviceInstanceID + * @param instanceID + * @param optional nil or *InstancesAPIImportSnapshotOfAnInstanceOpts - Optional Parameters: + * @param "Body" (optional.Interface of ImportSnapshotBody) - + +*/ - if err = json.Unmarshal(localVarBody, &instanceStateResponse); err != nil { - return instanceStateResponse, err +func (a *InstancesAPIService) ImportSnapshotOfAnInstance(ctx context.Context, instanceID int, + localVarOptionals *models.ImportSnapshotBody) (models.SuccessOrErrorMessage, error) { + importSnapshotResp := models.SuccessOrErrorMessage{} + + importSnapshotAPI := &api{ + method: "PUT", + path: fmt.Sprintf("%s/%s/%s/%d/import-snapshot", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, + + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &importSnapshotResp) + }, } + err := importSnapshotAPI.do(ctx, localVarOptionals, nil) - return instanceStateResponse, nil + return importSnapshotResp, err } /* InstancesAPIService -Stops all VM running within an instance +'This will lock the instance. While locked, instances may not be removed.' * @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) { +func (a *InstancesAPIService) LockAnInstance(ctx context.Context, + instanceID int) (*http.Response, error) { var ( - localVarHTTPMethod = strings.ToUpper("Put") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - instanceStateResponse models.InstancePowerResponse + localVarHTTPMethod = strings.ToUpper("Put") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte ) // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/stop", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + localVarPath := fmt.Sprintf("%s/%s/%s/%d/lock", a.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.InstancesPath, instanceID) localVarHeaderParams := make(map[string]string) @@ -1220,99 +652,161 @@ func (a *InstancesAPIService) StopAnInstance(ctx context.Context, instanceID int r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { - return instanceStateResponse, err + return nil, err } localVarHTTPResponse, err := a.Client.callAPI(r) if err != nil || localVarHTTPResponse == nil { - return instanceStateResponse, err + return localVarHTTPResponse, err } + if localVarHTTPResponse.StatusCode >= 300 { - return instanceStateResponse, ParseError(localVarHTTPResponse) + return localVarHTTPResponse, ParseError(localVarHTTPResponse) } - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() - if err != nil { - return instanceStateResponse, err - } + return localVarHTTPResponse, nil +} + +/* +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 + +*/ +func (a *InstancesAPIService) RestartAnInstance(ctx context.Context, + instanceID int) (models.InstancePowerResponse, error) { + restartInstResp := models.InstancePowerResponse{} + + restartInstAPI := &api{ + method: "PUT", + path: fmt.Sprintf("%s/%s/%s/%d/restart", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, - if err = json.Unmarshal(localVarBody, &instanceStateResponse); err != nil { - return instanceStateResponse, err + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &restartInstResp) + }, } + err := restartInstAPI.do(ctx, nil, nil) - return instanceStateResponse, nil + return restartInstResp, err } /* InstancesAPIService -Suspends all VM running within an instance +Creates 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 optional nil or *InstancesAPISnapshotAnInstanceOpts - Optional Parameters: + * @param "Body" (optional.Interface of SnapshotBody) - */ -func (a *InstancesAPIService) SuspendAnInstance(ctx context.Context, instanceID int) (models.InstancePowerResponse, error) { - var ( - localVarHTTPMethod = strings.ToUpper("Put") - localVarPostBody interface{} - localVarFileName string - localVarFileBytes []byte - instanceStateResponse models.InstancePowerResponse - ) - - // create path and map variables - localVarPath := fmt.Sprintf("%s/%s/%s/%d/suspend", a.Cfg.Host, consts.VmaasCmpAPIBasePath, - consts.InstancesPath, instanceID) - localVarHeaderParams := make(map[string]string) - localVarQueryParams := url.Values{} - localVarFormParams := url.Values{} +func (a *InstancesAPIService) SnapshotAnInstance(ctx context.Context, instanceID int, + localVarOptionals *models.SnapshotBody) (models.Instances, error) { + snapshotInstResp := models.Instances{} - // to determine the Content-Type header - localVarHTTPContentTypes := []string{} + instanceAPI := &api{ + method: "PUT", + path: fmt.Sprintf("%s/%s/%s/%d/snapshot", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, - // set Content-Type header - localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) - if localVarHTTPContentType != "" { - localVarHeaderParams["Content-Type"] = localVarHTTPContentType + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &snapshotInstResp) + }, } + err := instanceAPI.do(ctx, localVarOptionals, nil) - // to determine the Accept header - localVarHTTPHeaderAccepts := []string{"application/json"} + return snapshotInstResp, err +} - // set Accept header - localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) - if localVarHTTPHeaderAccept != "" { - localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept - } +/* +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 - r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, - localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) - if err != nil { - return instanceStateResponse, err - } +*/ +func (a *InstancesAPIService) StartAnInstance(ctx context.Context, + instanceID int) (models.InstancePowerResponse, error) { + startInstanceResp := models.InstancePowerResponse{} - localVarHTTPResponse, err := a.Client.callAPI(r) - if err != nil || localVarHTTPResponse == nil { - return instanceStateResponse, err - } - if localVarHTTPResponse.StatusCode >= 300 { - return instanceStateResponse, ParseError(localVarHTTPResponse) + startInstanceAPI := &api{ + method: "PUT", + path: fmt.Sprintf("%s/%s/%s/%d/start", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, + + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &startInstanceResp) + }, } + err := startInstanceAPI.do(ctx, nil, nil) - localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body) - defer localVarHTTPResponse.Body.Close() - if err != nil { - return instanceStateResponse, err + return startInstanceResp, err +} + +/* +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 + +*/ +func (a *InstancesAPIService) StopAnInstance(ctx context.Context, + instanceID int) (models.InstancePowerResponse, error) { + stopInstanceResp := models.InstancePowerResponse{} + + stopInstanceAPI := &api{ + method: "PUT", + path: fmt.Sprintf("%s/%s/%s/%d/stop", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, + + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &stopInstanceResp) + }, } + err := stopInstanceAPI.do(ctx, nil, nil) + + return stopInstanceResp, err +} + +/* +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 + +*/ +func (a *InstancesAPIService) SuspendAnInstance(ctx context.Context, + instanceID int) (models.InstancePowerResponse, error) { + suspendResp := models.InstancePowerResponse{} + suspendInstanceAPI := &api{ + method: "PUT", + path: fmt.Sprintf("%s/%s/%s/%d/suspend", a.Cfg.Host, consts.VmaasCmpAPIBasePath, + consts.InstancesPath, instanceID), + client: a.Client, - if err = json.Unmarshal(localVarBody, &instanceStateResponse); err != nil { - return instanceStateResponse, err + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &suspendResp) + }, } + err := suspendInstanceAPI.do(ctx, nil, nil) - return instanceStateResponse, nil + return suspendResp, err } /* @@ -1401,7 +895,8 @@ Unlocks the instance * @param instanceID */ -func (a *InstancesAPIService) UnlockAnInstance(ctx context.Context, instanceID int) (*http.Response, error) { +func (a *InstancesAPIService) UnlockAnInstance(ctx context.Context, + instanceID int) (*http.Response, error) { var ( localVarHTTPMethod = strings.ToUpper("Put") localVarPostBody interface{} @@ -1507,9 +1002,7 @@ func (a *InstancesAPIService) UpdatingAnInstance( } func (a *InstancesAPIService) GetInstanceHistory( - ctx context.Context, - instanceID int, -) (models.GetInstanceHistory, error) { + ctx context.Context, instanceID int) (models.GetInstanceHistory, error) { history := models.GetInstanceHistory{} historyAPI := &api{ diff --git a/pkg/client/instances_test.go b/pkg/client/instances_test.go index c028fe6..d222fef 100644 --- a/pkg/client/instances_test.go +++ b/pkg/client/instances_test.go @@ -5,7 +5,6 @@ package client import ( "bytes" "context" - "encoding/json" "errors" "io/ioutil" "net/http" @@ -194,11 +193,7 @@ func TestInstancesAPIService_CreateAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances" method := "POST" - // headers := getDefaultHeaders() - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } + headers := getDefaultHeaders() postBody := ioutil.NopCloser(bytes.NewReader([]byte(` { "zoneId": "1", @@ -214,10 +209,10 @@ func TestInstancesAPIService_CreateAnInstance(t *testing.T) { } } `))) - pBody, _ := json.Marshal(models.CreateInstanceBody{ + pBody := &models.CreateInstanceBody{ ZoneID: "1", CloneName: "Instance_Create", - }) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(req, nil) @@ -244,17 +239,12 @@ func TestInstancesAPIService_CreateAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances" method := "POST" - // headers := getDefaultHeaders() - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } + headers := getDefaultHeaders() // mock the context only since it is not validated in this function - pBody, _ := json.Marshal(models.CreateInstanceBody{ + pBody := &models.CreateInstanceBody{ ZoneID: "1", CloneName: "Instance_Create", - }) - // pBody := []byte(`{"zoneId":"1","CloneName":"Instance_Clone"}`) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(nil, errors.New("prepare request error")) }, @@ -270,11 +260,7 @@ func TestInstancesAPIService_CreateAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances" method := "POST" - // headers := getDefaultHeaders() - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } + headers := getDefaultHeaders() postBody := ioutil.NopCloser(bytes.NewReader([]byte(` { "zoneId": "1", @@ -291,10 +277,10 @@ func TestInstancesAPIService_CreateAnInstance(t *testing.T) { } `))) // mock the context only since it is not validated in this function - pBody, _ := json.Marshal(models.CreateInstanceBody{ + pBody := &models.CreateInstanceBody{ ZoneID: "1", CloneName: "Instance_Create", - }) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(req, nil) @@ -452,10 +438,7 @@ func TestInstancesAPIService_ImportSnapshotOfAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances/1/import-snapshot" method := "PUT" - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } + headers := getDefaultHeaders() postBody := ioutil.NopCloser(bytes.NewReader([]byte(`{ "storageProviderId" : 1 }`))) @@ -466,9 +449,9 @@ func TestInstancesAPIService_ImportSnapshotOfAnInstance(t *testing.T) { "message": "Successfully imported a snapshot of an instance" } `))) - pBody, _ := json.Marshal(models.ImportSnapshotBody{ + pBody := &models.ImportSnapshotBody{ StorageProviderID: 1, - }) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(req, nil) m.EXPECT().callAPI(req).Return(&http.Response{ @@ -491,13 +474,10 @@ func TestInstancesAPIService_ImportSnapshotOfAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances/1/import-snapshot" method := "PUT" - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } - pBody, _ := json.Marshal(models.ImportSnapshotBody{ + headers := getDefaultHeaders() + pBody := &models.ImportSnapshotBody{ StorageProviderID: 1, - }) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(nil, errors.New("prepare request error")) }, @@ -513,10 +493,7 @@ func TestInstancesAPIService_ImportSnapshotOfAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances/1/import-snapshot" method := "PUT" - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } + headers := getDefaultHeaders() postBody := ioutil.NopCloser(bytes.NewReader([]byte(`{ "storageProviderId" : 1 }`))) @@ -529,9 +506,9 @@ func TestInstancesAPIService_ImportSnapshotOfAnInstance(t *testing.T) { ] } `))) - pBody, _ := json.Marshal(models.ImportSnapshotBody{ + pBody := &models.ImportSnapshotBody{ StorageProviderID: 1, - }) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(req, nil) m.EXPECT().callAPI(req).Return(&http.Response{ @@ -763,10 +740,7 @@ func TestInstancesAPIService_SnapshotAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances/1/snapshot" method := "PUT" - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } + headers := getDefaultHeaders() postBody := ioutil.NopCloser(bytes.NewReader([]byte(` { "snapshot": { @@ -782,12 +756,12 @@ func TestInstancesAPIService_SnapshotAnInstance(t *testing.T) { }], "success" : true }`))) - pBody, _ := json.Marshal(models.SnapshotBody{ + pBody := &models.SnapshotBody{ Snapshot: &models.SnapshotBodySnapshot{ Name: "test_snapshot_name", Description: "test_snapshot_description", }, - }) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(req, nil) m.EXPECT().callAPI(req).Return(&http.Response{ @@ -816,16 +790,13 @@ func TestInstancesAPIService_SnapshotAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances/1/snapshot" method := "PUT" - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } - pBody, _ := json.Marshal(models.SnapshotBody{ + headers := getDefaultHeaders() + pBody := &models.SnapshotBody{ Snapshot: &models.SnapshotBodySnapshot{ Name: "test_snapshot_name", Description: "test_snapshot_description", }, - }) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(nil, errors.New("prepare request error")) }, @@ -844,10 +815,7 @@ func TestInstancesAPIService_SnapshotAnInstance(t *testing.T) { given: func(m *MockAPIClientHandler) { path := mockHost + "/v1/instances/1/snapshot" method := "PUT" - headers := map[string]string{ - "Accept": "application/json", - "Content-Type": "application/json", - } + headers := getDefaultHeaders() postBody := ioutil.NopCloser(bytes.NewReader([]byte(` { "snapshot": { @@ -864,12 +832,12 @@ func TestInstancesAPIService_SnapshotAnInstance(t *testing.T) { ] } `))) - pBody, _ := json.Marshal(models.SnapshotBody{ + pBody := &models.SnapshotBody{ Snapshot: &models.SnapshotBodySnapshot{ Name: "test_snapshot_name", Description: "test_snapshot_description", }, - }) + } m.EXPECT().prepareRequest(gomock.Any(), path, method, pBody, headers, url.Values{}, url.Values{}, "", nil).Return(req, nil) m.EXPECT().callAPI(req).Return(&http.Response{ @@ -1526,3 +1494,404 @@ func TestInstancesAPIService_GetInstanceHistory(t *testing.T) { }) } } + +func TestInstancesAPIService_RestartAnInstance(t *testing.T) { + ctx := context.Background() + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + tests := []struct { + name string + instanceID int + given func(m *MockAPIClientHandler) + want models.InstancePowerResponse + wantErr bool + }{ + // TODO: Add test cases. + { + name: "Normal Test case 1: Restart an Instance", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/restart" + method := "PUT" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "success": true + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(req, nil) + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 200, + Body: respBody, + }, nil) + }, + want: models.InstancePowerResponse{ + Success: true, + }, + wantErr: false, + }, + { + name: "Failed test case 2: Error in call prepare request", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/restart" + method := "PUT" + headers := getDefaultHeaders() + + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(nil, errors.New("prepare request error")) + }, + want: models.InstancePowerResponse{}, + wantErr: true, + }, + { + name: "Failed test case 3: error in callAPI", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/restart" + method := "PUT" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "message": "Internal Server Error", + "recommendedActions": [ + "Unknown error occurred. Please contact the administrator" + ] + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(req, nil) + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 500, + Body: respBody, + }, nil) + }, + want: models.InstancePowerResponse{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAPIClient := NewMockAPIClientHandler(ctrl) + a := InstancesAPIService{ + Client: mockAPIClient, + Cfg: Configuration{ + Host: mockHost, + }, + } + tt.given(mockAPIClient) + got, err := a.RestartAnInstance(ctx, tt.instanceID) + if (err != nil) != tt.wantErr { + t.Errorf("InstancesAPIService.RestartAnInstance() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("InstancesAPIService.RestartAnInstance() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestInstancesAPIService_StartAnInstance(t *testing.T) { + ctx := context.Background() + ctrl := gomock.NewController(t) + defer ctrl.Finish() + tests := []struct { + name string + instanceID int + given func(m *MockAPIClientHandler) + want models.InstancePowerResponse + wantErr bool + }{ + // TODO: Add test cases. + { + name: "Normal Test case 1: Start an Instance", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/start" + method := "PUT" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "success": true + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(req, nil) + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 200, + Body: respBody, + }, nil) + }, + want: models.InstancePowerResponse{ + Success: true, + }, + wantErr: false, + }, + { + name: "Failed test case 2: Error in call prepare request", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/start" + method := "PUT" + headers := getDefaultHeaders() + + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(nil, errors.New("prepare request error")) + }, + want: models.InstancePowerResponse{}, + wantErr: true, + }, + { + name: "Failed test case 3: error in callAPI", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/start" + method := "PUT" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "message": "Internal Server Error", + "recommendedActions": [ + "Unknown error occurred. Please contact the administrator" + ] + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(req, nil) + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 500, + Body: respBody, + }, nil) + }, + want: models.InstancePowerResponse{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAPIClient := NewMockAPIClientHandler(ctrl) + a := InstancesAPIService{ + Client: mockAPIClient, + Cfg: Configuration{ + Host: mockHost, + }, + } + tt.given(mockAPIClient) + got, err := a.StartAnInstance(ctx, tt.instanceID) + if (err != nil) != tt.wantErr { + t.Errorf("InstancesAPIService.StartAnInstance() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("InstancesAPIService.StartAnInstance() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestInstancesAPIService_StopAnInstance(t *testing.T) { + ctx := context.Background() + ctrl := gomock.NewController(t) + defer ctrl.Finish() + tests := []struct { + name string + instanceID int + given func(m *MockAPIClientHandler) + want models.InstancePowerResponse + wantErr bool + }{ + // TODO: Add test cases. + { + name: "Normal Test case 1: Stop an Instance", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/stop" + method := "PUT" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "success": true + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(req, nil) + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 200, + Body: respBody, + }, nil) + }, + want: models.InstancePowerResponse{ + Success: true, + }, + wantErr: false, + }, + { + name: "Failed test case 2: Error in call prepare request", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/stop" + method := "PUT" + headers := getDefaultHeaders() + + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(nil, errors.New("prepare request error")) + }, + want: models.InstancePowerResponse{}, + wantErr: true, + }, + { + name: "Failed test case 3: error in callAPI", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/stop" + method := "PUT" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "message": "Internal Server Error", + "recommendedActions": [ + "Unknown error occurred. Please contact the administrator" + ] + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(req, nil) + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 500, + Body: respBody, + }, nil) + }, + want: models.InstancePowerResponse{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAPIClient := NewMockAPIClientHandler(ctrl) + a := InstancesAPIService{ + Client: mockAPIClient, + Cfg: Configuration{ + Host: mockHost, + }, + } + tt.given(mockAPIClient) + got, err := a.StopAnInstance(ctx, tt.instanceID) + if (err != nil) != tt.wantErr { + t.Errorf("InstancesAPIService.StopAnInstance() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("InstancesAPIService.StopAnInstance() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestInstancesAPIService_SuspendAnInstance(t *testing.T) { + ctx := context.Background() + ctrl := gomock.NewController(t) + defer ctrl.Finish() + tests := []struct { + name string + instanceID int + given func(m *MockAPIClientHandler) + want models.InstancePowerResponse + wantErr bool + }{ + // TODO: Add test cases. + { + name: "Normal Test case 1: Suspend an Instance", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/suspend" + method := "PUT" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "success": true + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(req, nil) + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 200, + Body: respBody, + }, nil) + }, + want: models.InstancePowerResponse{ + Success: true, + }, + wantErr: false, + }, + { + name: "Failed test case 2: Error in call prepare request", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/suspend" + method := "PUT" + headers := getDefaultHeaders() + + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(nil, errors.New("prepare request error")) + }, + want: models.InstancePowerResponse{}, + wantErr: true, + }, + { + name: "Failed test case 3: error in callAPI", + instanceID: 1, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1/instances/1/suspend" + method := "PUT" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "message": "Internal Server Error", + "recommendedActions": [ + "Unknown error occurred. Please contact the administrator" + ] + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{}, + url.Values{}, "", nil).Return(req, nil) + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 500, + Body: respBody, + }, nil) + }, + want: models.InstancePowerResponse{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAPIClient := NewMockAPIClientHandler(ctrl) + a := InstancesAPIService{ + Client: mockAPIClient, + Cfg: Configuration{ + Host: mockHost, + }, + } + tt.given(mockAPIClient) + got, err := a.SuspendAnInstance(ctx, tt.instanceID) + if (err != nil) != tt.wantErr { + t.Errorf("InstancesAPIService.SuspendAnInstance() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("InstancesAPIService.SuspendAnInstance() = %v, want %v", got, tt.want) + } + }) + } +}