-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add api call createConsoleEndpoint (#78)
- Loading branch information
Showing
6 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |