|
| 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 r CreateConsoleEndpointResponse |
| 98 | + if err := json.Unmarshal(resp, &r); err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + return &r, nil |
| 103 | +} |
| 104 | + |
| 105 | +type CreateConsoleEndpointResponse struct { |
| 106 | + Details string `json:"details"` |
| 107 | + JobID string `json:"jobid"` |
| 108 | + Jobstatus int `json:"jobstatus"` |
| 109 | + Success bool `json:"success"` |
| 110 | + Url string `json:"url"` |
| 111 | + Websocket string `json:"websocket"` |
| 112 | +} |
| 113 | + |
| 114 | +func (r *CreateConsoleEndpointResponse) UnmarshalJSON(b []byte) error { |
| 115 | + var m map[string]interface{} |
| 116 | + err := json.Unmarshal(b, &m) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + if success, ok := m["success"].(string); ok { |
| 122 | + m["success"] = success == "true" |
| 123 | + b, err = json.Marshal(m) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if ostypeid, ok := m["ostypeid"].(float64); ok { |
| 130 | + m["ostypeid"] = strconv.Itoa(int(ostypeid)) |
| 131 | + b, err = json.Marshal(m) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + type alias CreateConsoleEndpointResponse |
| 138 | + return json.Unmarshal(b, (*alias)(r)) |
| 139 | +} |
0 commit comments