Skip to content

Commit

Permalink
add api call createConsoleEndpoint (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh92 authored Mar 13, 2024
2 parents dd1ad53 + 66a2862 commit 26f7939
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 0 deletions.
142 changes: 142 additions & 0 deletions cloudstack/ConsoleEndpointService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

package cloudstack

import (
"encoding/json"
"net/url"
"strconv"
)

type ConsoleEndpointServiceIface interface {
CreateConsoleEndpoint(p *CreateConsoleEndpointParams) (*CreateConsoleEndpointResponse, error)
NewCreateConsoleEndpointParams(virtualmachineid string) *CreateConsoleEndpointParams
}

type CreateConsoleEndpointParams struct {
p map[string]interface{}
}

func (p *CreateConsoleEndpointParams) toURLValues() url.Values {
u := url.Values{}
if p.p == nil {
return u
}
if v, found := p.p["token"]; found {
u.Set("token", v.(string))
}
if v, found := p.p["virtualmachineid"]; found {
u.Set("virtualmachineid", v.(string))
}
return u
}

func (p *CreateConsoleEndpointParams) SetToken(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["token"] = v
}

func (p *CreateConsoleEndpointParams) GetToken() (string, bool) {
if p.p == nil {
p.p = make(map[string]interface{})
}
value, ok := p.p["token"].(string)
return value, ok
}

func (p *CreateConsoleEndpointParams) SetVirtualmachineid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["virtualmachineid"] = v
}

func (p *CreateConsoleEndpointParams) GetVirtualmachineid() (string, bool) {
if p.p == nil {
p.p = make(map[string]interface{})
}
value, ok := p.p["virtualmachineid"].(string)
return value, ok
}

// You should always use this function to get a new CreateConsoleEndpointParams instance,
// as then you are sure you have configured all required params
func (s *ConsoleEndpointService) NewCreateConsoleEndpointParams(virtualmachineid string) *CreateConsoleEndpointParams {
p := &CreateConsoleEndpointParams{}
p.p = make(map[string]interface{})
p.p["virtualmachineid"] = virtualmachineid
return p
}

// Create a console endpoint to connect to a VM console
func (s *ConsoleEndpointService) CreateConsoleEndpoint(p *CreateConsoleEndpointParams) (*CreateConsoleEndpointResponse, error) {
resp, err := s.cs.newRequest("createConsoleEndpoint", p.toURLValues())
if err != nil {
return nil, err
}

var nested struct {
Response CreateConsoleEndpointResponse `json:"consoleendpoint"`
}
if err := json.Unmarshal(resp, &nested); err != nil {
return nil, err
}
r := nested.Response

return &r, nil
}

type CreateConsoleEndpointResponse struct {
Details string `json:"details"`
JobID string `json:"jobid"`
Jobstatus int `json:"jobstatus"`
Success bool `json:"success"`
Url string `json:"url"`
Websocket map[string]interface{} `json:"websocket"`
}

func (r *CreateConsoleEndpointResponse) UnmarshalJSON(b []byte) error {
var m map[string]interface{}
err := json.Unmarshal(b, &m)
if err != nil {
return err
}

if success, ok := m["success"].(string); ok {
m["success"] = success == "true"
b, err = json.Marshal(m)
if err != nil {
return err
}
}

if ostypeid, ok := m["ostypeid"].(float64); ok {
m["ostypeid"] = strconv.Itoa(int(ostypeid))
b, err = json.Marshal(m)
if err != nil {
return err
}
}

type alias CreateConsoleEndpointResponse
return json.Unmarshal(b, (*alias)(r))
}
82 changes: 82 additions & 0 deletions cloudstack/ConsoleEndpointService_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions cloudstack/cloudstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type CloudStackClient struct {
CloudIdentifier CloudIdentifierServiceIface
Cluster ClusterServiceIface
Configuration ConfigurationServiceIface
ConsoleEndpoint ConsoleEndpointServiceIface
Custom CustomServiceIface
DiskOffering DiskOfferingServiceIface
Domain DomainServiceIface
Expand Down Expand Up @@ -221,6 +222,7 @@ func newClient(apiurl string, apikey string, secret string, async bool, verifyss
cs.CloudIdentifier = NewCloudIdentifierService(cs)
cs.Cluster = NewClusterService(cs)
cs.Configuration = NewConfigurationService(cs)
cs.ConsoleEndpoint = NewConsoleEndpointService(cs)
cs.Custom = NewCustomService(cs)
cs.DiskOffering = NewDiskOfferingService(cs)
cs.Domain = NewDomainService(cs)
Expand Down Expand Up @@ -300,6 +302,7 @@ func newMockClient(ctrl *gomock.Controller) *CloudStackClient {
cs.CloudIdentifier = NewMockCloudIdentifierServiceIface(ctrl)
cs.Cluster = NewMockClusterServiceIface(ctrl)
cs.Configuration = NewMockConfigurationServiceIface(ctrl)
cs.ConsoleEndpoint = NewMockConsoleEndpointServiceIface(ctrl)
cs.Custom = NewMockCustomServiceIface(ctrl)
cs.DiskOffering = NewMockDiskOfferingServiceIface(ctrl)
cs.Domain = NewMockDomainServiceIface(ctrl)
Expand Down Expand Up @@ -867,6 +870,14 @@ func NewConfigurationService(cs *CloudStackClient) ConfigurationServiceIface {
return &ConfigurationService{cs: cs}
}

type ConsoleEndpointService struct {
cs *CloudStackClient
}

func NewConsoleEndpointService(cs *CloudStackClient) ConsoleEndpointServiceIface {
return &ConsoleEndpointService{cs: cs}
}

type CustomService struct {
cs *CloudStackClient
}
Expand Down
3 changes: 3 additions & 0 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var nestedResponse = map[string]string{
"getCloudIdentifier": "cloudidentifier",
"getKubernetesClusterConfig": "clusterconfig",
"getPathForVolume": "apipathforvolume",
"createConsoleEndpoint": "consoleendpoint",
}

// longToStringConvertedParams is a prefilled map with the list of
Expand Down Expand Up @@ -2176,6 +2177,8 @@ func mapType(aName string, pName string, pType string) string {
return "OutOfBandManagementResponse"
case "hostharesponse":
return "HAForHostResponse"
case "consoleendpointwebsocketresponse":
return "map[string]interface{}"
default:
return "string"
}
Expand Down
3 changes: 3 additions & 0 deletions generate/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,7 @@ var layout = apiInfo{
"listManagementServersMetrics",
"listDbMetrics",
},
"ConsoleEndpointService": {
"createConsoleEndpoint",
},
}
50 changes: 50 additions & 0 deletions test/ConsoleEndpointService_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

package test

import (
"testing"

"github.com/apache/cloudstack-go/v2/cloudstack"
)

func TestConsoleEndpointService(t *testing.T) {
service := "ConsoleEndpointService"
response, err := readData(service)
if err != nil {
t.Skipf("Skipping test as %v", err)
}
server := CreateTestServer(t, response)
client := cloudstack.NewClient(server.URL, "APIKEY", "SECRETKEY", true)
defer server.Close()

testcreateConsoleEndpoint := func(t *testing.T) {
if _, ok := response["createConsoleEndpoint"]; !ok {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.ConsoleEndpoint.NewCreateConsoleEndpointParams("virtualmachineid")
_, err := client.ConsoleEndpoint.CreateConsoleEndpoint(p)
if err != nil {
t.Errorf(err.Error())
}
}
t.Run("CreateConsoleEndpoint", testcreateConsoleEndpoint)

}

0 comments on commit 26f7939

Please sign in to comment.