| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package govcd |
| 6 |
|
| 7 |
import ( |
| 8 |
|
| 9 |
|
| 10 |
"fmt" |
| 11 |
|
| 12 |
"net/url" |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
types "github.com/hmrc/vmware-govcd/types/v56" |
| 17 |
) |
| 18 |
|
| 19 |
type VM struct { |
| 20 |
VM *types.VM |
| 21 |
c *Client |
| 22 |
} |
| 23 |
|
| 24 |
func NewVM(c *Client) *VM { |
| 25 |
return &VM{ |
| 26 |
VM: new(types.VM), |
| 27 |
c: c, |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func (c *VCDClient) FindVMByHREF(vmhref string) (VM, error) { |
| 32 |
|
| 33 |
u, err := url.ParseRequestURI(vmhref) |
| 34 |
|
| 35 |
if err != nil { |
| 36 |
return VM{}, fmt.Errorf("error decoding vm HREF: %s", err) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
req := c.Client.NewRequest(map[string]string{}, "GET", *u, nil) |
| 41 |
|
| 42 |
resp, err := checkResp(c.Client.Http.Do(req)) |
| 43 |
if err != nil { |
| 44 |
return VM{}, fmt.Errorf("error retrieving VM: %s", err) |
| 45 |
} |
| 46 |
|
| 47 |
newvm := NewVM(&c.Client) |
| 48 |
|
| 49 |
if err = decodeBody(resp, newvm.VM); err != nil { |
| 50 |
return VM{}, fmt.Errorf("error decoding VM response: %s", err) |
| 51 |
} |
| 52 |
|
| 53 |
return *newvm, nil |
| 54 |
|
| 55 |
} |