Skip to content

Commit 26f7939

Browse files
authored
add api call createConsoleEndpoint (#78)
2 parents dd1ad53 + 66a2862 commit 26f7939

File tree

6 files changed

+291
-0
lines changed

6 files changed

+291
-0
lines changed

cloudstack/ConsoleEndpointService.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"encoding/json"
24+
"net/url"
25+
"strconv"
26+
)
27+
28+
type ConsoleEndpointServiceIface interface {
29+
CreateConsoleEndpoint(p *CreateConsoleEndpointParams) (*CreateConsoleEndpointResponse, error)
30+
NewCreateConsoleEndpointParams(virtualmachineid string) *CreateConsoleEndpointParams
31+
}
32+
33+
type CreateConsoleEndpointParams struct {
34+
p map[string]interface{}
35+
}
36+
37+
func (p *CreateConsoleEndpointParams) toURLValues() url.Values {
38+
u := url.Values{}
39+
if p.p == nil {
40+
return u
41+
}
42+
if v, found := p.p["token"]; found {
43+
u.Set("token", v.(string))
44+
}
45+
if v, found := p.p["virtualmachineid"]; found {
46+
u.Set("virtualmachineid", v.(string))
47+
}
48+
return u
49+
}
50+
51+
func (p *CreateConsoleEndpointParams) SetToken(v string) {
52+
if p.p == nil {
53+
p.p = make(map[string]interface{})
54+
}
55+
p.p["token"] = v
56+
}
57+
58+
func (p *CreateConsoleEndpointParams) GetToken() (string, bool) {
59+
if p.p == nil {
60+
p.p = make(map[string]interface{})
61+
}
62+
value, ok := p.p["token"].(string)
63+
return value, ok
64+
}
65+
66+
func (p *CreateConsoleEndpointParams) SetVirtualmachineid(v string) {
67+
if p.p == nil {
68+
p.p = make(map[string]interface{})
69+
}
70+
p.p["virtualmachineid"] = v
71+
}
72+
73+
func (p *CreateConsoleEndpointParams) GetVirtualmachineid() (string, bool) {
74+
if p.p == nil {
75+
p.p = make(map[string]interface{})
76+
}
77+
value, ok := p.p["virtualmachineid"].(string)
78+
return value, ok
79+
}
80+
81+
// You should always use this function to get a new CreateConsoleEndpointParams instance,
82+
// as then you are sure you have configured all required params
83+
func (s *ConsoleEndpointService) NewCreateConsoleEndpointParams(virtualmachineid string) *CreateConsoleEndpointParams {
84+
p := &CreateConsoleEndpointParams{}
85+
p.p = make(map[string]interface{})
86+
p.p["virtualmachineid"] = virtualmachineid
87+
return p
88+
}
89+
90+
// Create a console endpoint to connect to a VM console
91+
func (s *ConsoleEndpointService) CreateConsoleEndpoint(p *CreateConsoleEndpointParams) (*CreateConsoleEndpointResponse, error) {
92+
resp, err := s.cs.newRequest("createConsoleEndpoint", p.toURLValues())
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
var nested struct {
98+
Response CreateConsoleEndpointResponse `json:"consoleendpoint"`
99+
}
100+
if err := json.Unmarshal(resp, &nested); err != nil {
101+
return nil, err
102+
}
103+
r := nested.Response
104+
105+
return &r, nil
106+
}
107+
108+
type CreateConsoleEndpointResponse struct {
109+
Details string `json:"details"`
110+
JobID string `json:"jobid"`
111+
Jobstatus int `json:"jobstatus"`
112+
Success bool `json:"success"`
113+
Url string `json:"url"`
114+
Websocket map[string]interface{} `json:"websocket"`
115+
}
116+
117+
func (r *CreateConsoleEndpointResponse) UnmarshalJSON(b []byte) error {
118+
var m map[string]interface{}
119+
err := json.Unmarshal(b, &m)
120+
if err != nil {
121+
return err
122+
}
123+
124+
if success, ok := m["success"].(string); ok {
125+
m["success"] = success == "true"
126+
b, err = json.Marshal(m)
127+
if err != nil {
128+
return err
129+
}
130+
}
131+
132+
if ostypeid, ok := m["ostypeid"].(float64); ok {
133+
m["ostypeid"] = strconv.Itoa(int(ostypeid))
134+
b, err = json.Marshal(m)
135+
if err != nil {
136+
return err
137+
}
138+
}
139+
140+
type alias CreateConsoleEndpointResponse
141+
return json.Unmarshal(b, (*alias)(r))
142+
}

cloudstack/ConsoleEndpointService_mock.go

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloudstack/cloudstack.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type CloudStackClient struct {
115115
CloudIdentifier CloudIdentifierServiceIface
116116
Cluster ClusterServiceIface
117117
Configuration ConfigurationServiceIface
118+
ConsoleEndpoint ConsoleEndpointServiceIface
118119
Custom CustomServiceIface
119120
DiskOffering DiskOfferingServiceIface
120121
Domain DomainServiceIface
@@ -221,6 +222,7 @@ func newClient(apiurl string, apikey string, secret string, async bool, verifyss
221222
cs.CloudIdentifier = NewCloudIdentifierService(cs)
222223
cs.Cluster = NewClusterService(cs)
223224
cs.Configuration = NewConfigurationService(cs)
225+
cs.ConsoleEndpoint = NewConsoleEndpointService(cs)
224226
cs.Custom = NewCustomService(cs)
225227
cs.DiskOffering = NewDiskOfferingService(cs)
226228
cs.Domain = NewDomainService(cs)
@@ -300,6 +302,7 @@ func newMockClient(ctrl *gomock.Controller) *CloudStackClient {
300302
cs.CloudIdentifier = NewMockCloudIdentifierServiceIface(ctrl)
301303
cs.Cluster = NewMockClusterServiceIface(ctrl)
302304
cs.Configuration = NewMockConfigurationServiceIface(ctrl)
305+
cs.ConsoleEndpoint = NewMockConsoleEndpointServiceIface(ctrl)
303306
cs.Custom = NewMockCustomServiceIface(ctrl)
304307
cs.DiskOffering = NewMockDiskOfferingServiceIface(ctrl)
305308
cs.Domain = NewMockDomainServiceIface(ctrl)
@@ -867,6 +870,14 @@ func NewConfigurationService(cs *CloudStackClient) ConfigurationServiceIface {
867870
return &ConfigurationService{cs: cs}
868871
}
869872

873+
type ConsoleEndpointService struct {
874+
cs *CloudStackClient
875+
}
876+
877+
func NewConsoleEndpointService(cs *CloudStackClient) ConsoleEndpointServiceIface {
878+
return &ConsoleEndpointService{cs: cs}
879+
}
880+
870881
type CustomService struct {
871882
cs *CloudStackClient
872883
}

generate/generate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ var nestedResponse = map[string]string{
8383
"getCloudIdentifier": "cloudidentifier",
8484
"getKubernetesClusterConfig": "clusterconfig",
8585
"getPathForVolume": "apipathforvolume",
86+
"createConsoleEndpoint": "consoleendpoint",
8687
}
8788

8889
// longToStringConvertedParams is a prefilled map with the list of
@@ -2176,6 +2177,8 @@ func mapType(aName string, pName string, pType string) string {
21762177
return "OutOfBandManagementResponse"
21772178
case "hostharesponse":
21782179
return "HAForHostResponse"
2180+
case "consoleendpointwebsocketresponse":
2181+
return "map[string]interface{}"
21792182
default:
21802183
return "string"
21812184
}

generate/layout.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,4 +725,7 @@ var layout = apiInfo{
725725
"listManagementServersMetrics",
726726
"listDbMetrics",
727727
},
728+
"ConsoleEndpointService": {
729+
"createConsoleEndpoint",
730+
},
728731
}

test/ConsoleEndpointService_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package test
21+
22+
import (
23+
"testing"
24+
25+
"github.com/apache/cloudstack-go/v2/cloudstack"
26+
)
27+
28+
func TestConsoleEndpointService(t *testing.T) {
29+
service := "ConsoleEndpointService"
30+
response, err := readData(service)
31+
if err != nil {
32+
t.Skipf("Skipping test as %v", err)
33+
}
34+
server := CreateTestServer(t, response)
35+
client := cloudstack.NewClient(server.URL, "APIKEY", "SECRETKEY", true)
36+
defer server.Close()
37+
38+
testcreateConsoleEndpoint := func(t *testing.T) {
39+
if _, ok := response["createConsoleEndpoint"]; !ok {
40+
t.Skipf("Skipping as no json response is provided in testdata")
41+
}
42+
p := client.ConsoleEndpoint.NewCreateConsoleEndpointParams("virtualmachineid")
43+
_, err := client.ConsoleEndpoint.CreateConsoleEndpoint(p)
44+
if err != nil {
45+
t.Errorf(err.Error())
46+
}
47+
}
48+
t.Run("CreateConsoleEndpoint", testcreateConsoleEndpoint)
49+
50+
}

0 commit comments

Comments
 (0)