dep
parent
e5e91c751d
commit
278bc89da3
@ -0,0 +1,191 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
Copyright 2016 Apcera, Inc.
|
||||
|
||||
Licensed 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.
|
||||
|
@ -0,0 +1,78 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Ip struct {
|
||||
Status string `json:"status"`
|
||||
LocationIata string `json:"location_iata"`
|
||||
Failover bool `json:"failover"`
|
||||
ObjectUUID string `json:"object_uuid" mapstructure:"object_uuid"`
|
||||
ChangeTime time.Time `json:"change_time"`
|
||||
Family int `json:"family"`
|
||||
LocationUUID string `json:"location_uuid"`
|
||||
Prefix string `json:"prefix"`
|
||||
ReverseDNS string `json:"reverse_dns"`
|
||||
DeleteBlock bool `json:"delete_block"`
|
||||
UsageInMinutes float64 `json:"usage_in_minutes"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
Name string `json:"name"`
|
||||
IP string `json:"ip"`
|
||||
Labels []interface{} `json:"labels"`
|
||||
LocationCountry string `json:"location_country"`
|
||||
LocationName string `json:"location_name"`
|
||||
Server []*Server `json:"servers"`
|
||||
LoadBalancer []*LoadBalancer `json:"loadbalancers"`
|
||||
}
|
||||
|
||||
type LoadBalancer struct {
|
||||
}
|
||||
|
||||
func GetIp(userId string, token string, name string) (*Ip, error) {
|
||||
|
||||
ips, err := GetIPs(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, ip := range ips {
|
||||
if ip.Name == name {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetIPs(userId string, token string) (map[string]*Ip, error) {
|
||||
// return map
|
||||
var retIps = make(map[string]*Ip)
|
||||
|
||||
// get our objects
|
||||
objs, err := GetObjects(userId, token, apiIPBase)
|
||||
ips := (*objs["ips"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range ips {
|
||||
var ip1 Ip
|
||||
err := mapstructure.Decode(value.(map[string]interface{}), &ip1)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
retIps[key] = &ip1
|
||||
}
|
||||
|
||||
return retIps, nil
|
||||
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Isoimage struct {
|
||||
LocationCountry string `json:"location_country"`
|
||||
Status string `json:"status"`
|
||||
Labels []interface{} `json:"labels"`
|
||||
SourceURL string `json:"source_url"`
|
||||
Capacity int `json:"capacity"`
|
||||
Description interface{} `json:"description"`
|
||||
ChangeTime time.Time `json:"change_time"`
|
||||
Version interface{} `json:"version"`
|
||||
UsageInMinutes int `json:"usage_in_minutes"`
|
||||
LocationIata string `json:"location_iata"`
|
||||
Name string `json:"name"`
|
||||
ObjectUUID string `json:"object_uuid" mapstructure:"object_uuid"`
|
||||
Private bool `json:"private"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
LocationUUID string `json:"location_uuid"`
|
||||
LocationName string `json:"location_name"`
|
||||
}
|
||||
|
||||
func GetIsoimage(userId string, token string, name string) (*Isoimage, error) {
|
||||
|
||||
ips, err := GetIsoimages(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, ip := range ips {
|
||||
if ip.Name == name {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetIsoimages(userId string, token string) (map[string]*Isoimage, error) {
|
||||
// return map
|
||||
var retIsoimages = make(map[string]*Isoimage)
|
||||
|
||||
// get our objects
|
||||
objs, err := GetObjects(userId, token, apiIsoimageBase)
|
||||
isoimages := (*objs["isoimages"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range isoimages {
|
||||
var isoimage1 Isoimage
|
||||
err := mapstructure.Decode(value.(map[string]interface{}), &isoimage1)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
retIsoimages[key] = &isoimage1
|
||||
}
|
||||
|
||||
return retIsoimages, nil
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
type Location struct {
|
||||
Country string `json:"country"`
|
||||
Name string `json:"name"`
|
||||
ObjectUUID string `json:"object_uuid" mapstructure:"object_uuid"`
|
||||
Labels []interface{} `json:"labels"`
|
||||
Iata string `json:"iata"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func GetLocation(userId string, token string, name string) (*Location, error) {
|
||||
|
||||
locations, err := GetLocations(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, location := range locations {
|
||||
if location.Name == name {
|
||||
return location, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetLocations(userId string, token string) (map[string]*Location, error) {
|
||||
// return map
|
||||
var retLocations = make(map[string]*Location)
|
||||
|
||||
// get our objects
|
||||
objs, err := GetObjects(userId, token, apiLocationBase)
|
||||
locations := (*objs["locations"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range locations {
|
||||
var location1 Location
|
||||
err := mapstructure.Decode(value.(map[string]interface{}), &location1)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
retLocations[key] = &location1
|
||||
}
|
||||
|
||||
return retLocations, nil
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Network struct {
|
||||
LocationUUID interface{} `json:"location_uuid"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
PublicNet bool `json:"public_net"`
|
||||
LocationCountry interface{} `json:"location_country"`
|
||||
NetworkType string `json:"network_type"`
|
||||
L2Security bool `json:"l2security"`
|
||||
// LocationIata interface{} `json:"location_iata"`
|
||||
ObjectUUID string `json:"object_uuid" mapstructure:"object_uuid"`
|
||||
DeleteBlock bool `json:"delete_block"`
|
||||
Vlans []*Vlan `json:"vlans"`
|
||||
Servers []*Server `json:"servers"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
ChangeTime time.Time `json:"change_time"`
|
||||
LocationName interface{} `json:"location_name"`
|
||||
Labels []interface{} `json:"labels"`
|
||||
}
|
||||
|
||||
type Vlan struct {
|
||||
}
|
||||
|
||||
func GetNetwork(userId string, token string, name string) (*Network, error) {
|
||||
|
||||
networks, err := GetNetworks(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, network := range networks {
|
||||
if network.Name == name {
|
||||
return network, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetNetworks(userId string, token string) (map[string]*Network, error) {
|
||||
// return map
|
||||
var retNetworks = make(map[string]*Network)
|
||||
|
||||
// get our objects
|
||||
objs, err := GetObjects(userId, token, apiNetworkBase)
|
||||
netwworks := (*objs["networks"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range netwworks {
|
||||
var network1 Network
|
||||
err := mapstructure.Decode(value.(map[string]interface{}), &network1)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
retNetworks[key] = &network1
|
||||
}
|
||||
|
||||
return retNetworks, nil
|
||||
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Snapshot struct {
|
||||
LocationName string `json:"location_name"`
|
||||
ParentName string `json:"parent_name"`
|
||||
Name string `json:"name"`
|
||||
ChangeTime *time.Time `json:"change_time" mapstructure:"change_time"`
|
||||
LastUsedTemplate interface{} `json:"last_used_template"`
|
||||
ParentUUID string `json:"parent_uuid"`
|
||||
Labels []interface{} `json:"labels"`
|
||||
UsageInMinutes int `json:"usage_in_minutes"`
|
||||
LocationCountry string `json:"location_country"`
|
||||
Status string `json:"status"`
|
||||
LocationIata string `json:"location_iata"`
|
||||
CreateTime *time.Time `json:"create_time" mapstructure:"create_time"`
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
LocationUUID string `json:"location_uuid"`
|
||||
Capacity int `json:"capacity"`
|
||||
}
|
||||
|
||||
func GetSnapshot(userId string, token string, name string) (*Storage, error) {
|
||||
|
||||
storages, err := GetStorages(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, storage := range storages {
|
||||
if storage.Name == name {
|
||||
return storage, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetSnapshotByUUID(userId string, token string, objectUuid string) (*Storage, error) {
|
||||
retStorage, err := GetObjects(userId, token, apiStorageBase+"/"+objectUuid)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
storageMap := (*retStorage["storage"]).(map[string]interface{})
|
||||
|
||||
var storage1 Storage
|
||||
err = mapstructure.Decode(storageMap, &storage1)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &storage1, nil
|
||||
}
|
||||
|
||||
func (storage Storage) GetSnapshots(userId string, token string) (map[string]*Snapshot, error) {
|
||||
// return map
|
||||
var retSnapshots = make(map[string]*Snapshot)
|
||||
|
||||
// get our objects
|
||||
objs, err := GetObjects(userId, token, apiStorageBase +"/"+ storage.ObjectUUID +"/snapshots")
|
||||
snapshots := (*objs["snapshots"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range snapshots {
|
||||
var snapshot1 Snapshot
|
||||
|
||||
config := mapstructure.DecoderConfig{
|
||||
DecodeHook: StringToDateTimeHook,
|
||||
Result: &snapshot1,
|
||||
}
|
||||
|
||||
decoder, err := mapstructure.NewDecoder(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
error := decoder.Decode(value.(map[string]interface{}))
|
||||
if error != nil {
|
||||
return nil, error
|
||||
}
|
||||
retSnapshots[key] = &snapshot1
|
||||
}
|
||||
|
||||
return retSnapshots, nil
|
||||
}
|
||||
|
||||
func (storage Storage) GetYoungestSnapshot(userId string, token string) (*Snapshot, error) {
|
||||
snapshots, err := storage.GetSnapshots(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var retSnapshot *Snapshot
|
||||
|
||||
for _, snapshot := range snapshots {
|
||||
|
||||
if retSnapshot == nil {
|
||||
retSnapshot = snapshot
|
||||
} else {
|
||||
if snapshot.CreateTime.After(*retSnapshot.CreateTime) {
|
||||
retSnapshot = snapshot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retSnapshot, nil
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SSHKey struct {
|
||||
ObjectUUID string `json:"object_uuid" mapstructure:"object_uuid"`
|
||||
Name string `json:"name"`
|
||||
Sshkey string `json:"sshkey"`
|
||||
UserUUID string `json:"user_uuid"`
|
||||
Labels []interface{} `json:"labels"`
|
||||
Status string `json:"status"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
ChangeTime time.Time `json:"change_time"`
|
||||
}
|
||||
|
||||
func GetSSHKey(userId string, token string, name string) (*SSHKey, error) {
|
||||
|
||||
sshKeys, err := GetSSHKeys(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, sshKey := range sshKeys {
|
||||
if sshKey.Name == name {
|
||||
return sshKey, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetSSHKeys(userId string, token string) (map[string]*SSHKey, error) {
|
||||
// return map
|
||||
var retSSHKeys = make(map[string]*SSHKey)
|
||||
|
||||
// get our objects
|
||||
objs, err := GetObjects(userId, token, apiSSHKeyBase)
|
||||
sshKeys := (*objs["sshkeys"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range sshKeys {
|
||||
var sshKey1 SSHKey
|
||||
err := mapstructure.Decode(value.(map[string]interface{}), &sshKey1)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
retSSHKeys[key] = &sshKey1
|
||||
}
|
||||
|
||||
return retSSHKeys, nil
|
||||
|
||||
}
|
@ -0,0 +1,191 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
StorageRegular = "storage"
|
||||
StorageHigh = "storage_high"
|
||||
StorageInsane = "storage_insane"
|
||||
)
|
||||
|
||||
const (
|
||||
StatusActive string = "active"
|
||||
StatusDeleted string = "deleted"
|
||||
StatusToBeDeleted string = "to-be-deleted"
|
||||
StatusProvisioning string = "provisioning"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
ObjectUUID string `json:"object_uuid,omitempty" mapstructure:"object_uuid"`
|
||||
Labels []interface{} `json:"labels,omitempty"`
|
||||
CreateTime time.Time `json:"create_time,omitempty" mapstructure:"create_time"`
|
||||
LocationName string `json:"location_name,omitempty"`
|
||||
LastUsedTemplate string `json:"last_used_template,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
ChangeTime time.Time `json:"change_time,omitempty" mapstructure:"change_time"`
|
||||
StorageType string `json:"storage_type,omitempty"`
|
||||
Snapshots []interface{} `json:"snapshots,omitempty"`
|
||||
Capacity int `json:"capacity,omitempty"`
|
||||
ParentUUID string `json:"parent_uuid,omitempty"`
|
||||
LocationIata string `json:"location_iata,omitempty"`
|
||||
LocationUUID string `json:"location_uuid,omitempty"`
|
||||
LicenseProductNo interface{} `json:"license_product_no,omitempty"`
|
||||
LocationCountry string `json:"location_country,omitempty"`
|
||||
UsageInMinutes int `json:"usage_in_minutes,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
type StorageProvisionRequest struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Labels []interface{} `json:"labels,omitempty"`
|
||||
StorageType string `json:"storage_type,omitempty"`
|
||||
Capacity int `json:"capacity,omitempty"`
|
||||
LocationUUID string `json:"location_uuid,omitempty"`
|
||||
Template *StorageTemplateRequest `json:"template,omitempty"`
|
||||
}
|
||||
|
||||
type StorageTemplateRequest struct {
|
||||
Hostname string `json:"hostname"`
|
||||
Password string `json:"password,omitempty"`
|
||||
PasswordType string `json:"password_type,omitempty"`
|
||||
TemplateUUID string `json:"template_uuid,omitempty"`
|
||||
}
|
||||
|
||||
func (storage *Storage) Provision(config UserConfig, requestTemplate *StorageTemplateRequest) error {
|
||||
|
||||
request := StorageProvisionRequest{
|
||||
Name: storage.Name,
|
||||
Labels: storage.Labels,
|
||||
StorageType: storage.StorageType,
|
||||
Capacity: storage.Capacity,
|
||||
LocationUUID: storage.LocationUUID,
|
||||
Template: requestTemplate}
|
||||
|
||||
b, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := BuildRequest(config.UserId, config.APIToken,
|
||||
"POST", apiBaseURL+apiStorageBase, bytes.NewReader(b))
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err = ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
r := &GenericPostResponse{}
|
||||
err = json.Unmarshal(b, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
storage.ObjectUUID = r.ObjectUUID
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetStorageByUUID(userId string, token string, objectUuid string) (*Storage, error) {
|
||||
objs, err := GetObjects(userId, token, apiStorageBase+"/" + objectUuid)
|
||||
storage := (*objs["storage"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var storage1 Storage
|
||||
config := mapstructure.DecoderConfig{
|
||||
DecodeHook: StringToDateTimeHook,
|
||||
Result: &storage1,
|
||||
}
|
||||
|
||||
decoder, err := mapstructure.NewDecoder(&config)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
error := decoder.Decode(storage)
|
||||
if error != nil {
|
||||
return nil, error
|
||||
}
|
||||
|
||||
return &storage1, nil
|
||||
}
|
||||
|
||||
func GetStorage(userId string, token string, name string) (*Storage, error) {
|
||||
|
||||
storages, err := GetStorages(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, storage := range storages {
|
||||
if storage.Name == name {
|
||||
return storage, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetStorages(userId string, token string) (map[string]*Storage, error) {
|
||||
// return map
|
||||
var retStorages = make(map[string]*Storage)
|
||||
|
||||
// get our objects
|
||||
objs, err := GetObjects(userId, token, apiStorageBase)
|
||||
storages := (*objs["storages"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range storages {
|
||||
var storage1 Storage
|
||||
|
||||
config := mapstructure.DecoderConfig{
|
||||
DecodeHook: StringToDateTimeHook,
|
||||
Result: &storage1,
|
||||
}
|
||||
|
||||
decoder, err := mapstructure.NewDecoder(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
error := decoder.Decode(value.(map[string]interface{}))
|
||||
if error != nil {
|
||||
return nil, error
|
||||
}
|
||||
retStorages[key] = &storage1
|
||||
}
|
||||
|
||||
return retStorages, nil
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Template struct {
|
||||
LicenseProductNo interface{} `json:"license_product_no"`
|
||||
LocationCountry string `json:"location_country"`
|
||||
Labels []interface{} `json:"labels"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Ostype string `json:"ostype"`
|
||||
LocationIata string `json:"location_iata"`
|
||||
Status string `json:"status"`
|
||||
Description string `json:"description"`
|
||||
LocationName string `json:"location_name"`
|
||||
ObjectUUID string `json:"object_uuid" mapstructure:"object_uuid"`
|
||||
Distro string `json:"distro"`
|
||||
Private bool `json:"private"`
|
||||
ChangeTime time.Time `json:"change_time"`
|
||||
LocationUUID string `json:"location_uuid"`
|
||||
Capacity int `json:"capacity"`
|
||||
UsageInMinutes int `json:"usage_in_minutes"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
}
|
||||
|
||||
func GetTemplate(userId string, token string, name string) (*Template, error) {
|
||||
|
||||
templates, err := GetTemplates(userId, token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, template := range templates {
|
||||
if template.Name == name {
|
||||
return template, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetTemplates(userId string, token string) (map[string]*Template, error) {
|
||||
|
||||
// return map
|
||||
var retTemplates = make(map[string]*Template)
|
||||
|
||||
// get our objects
|
||||
objs, err := GetObjects(userId, token, apiTemplatesBase)
|
||||
templates := (*objs["templates"]).(map[string]interface{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range templates {
|
||||
var template1 Template
|
||||
err := mapstructure.Decode(value.(map[string]interface{}), &template1)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
retTemplates[key] = &template1
|
||||
}
|
||||
|
||||
return retTemplates, nil
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"time"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// BuildRequest builds an http request for this provider.
|
||||
// from DigitalOcean package
|
||||
func BuildRequest(userId, token, method, url string, body io.Reader) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Auth-UserId", userId)
|
||||
req.Header.Set("X-Auth-Token", token)
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
func DecodeData(objects map[string]*[]interface{} , dataType reflect.Type ) (map[string]*interface{}, error) {
|
||||
|
||||
var retObjects = make(map[string]*interface{})
|
||||
|
||||
stringToDateTimeHook := func(
|
||||
f reflect.Type,
|
||||
t reflect.Type,
|
||||
data interface{}) (interface{}, error) {
|
||||
if t == reflect.TypeOf(time.Time{}) && f == reflect.TypeOf("") {
|
||||
return time.Parse(time.RFC3339, data.(string))
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
for key, value := range objects {
|
||||
var object1 interface{}
|
||||
|
||||
config := mapstructure.DecoderConfig{
|
||||
DecodeHook: stringToDateTimeHook,
|
||||
Result: &object1,
|
||||
}
|
||||
|
||||
decoder, err := mapstructure.NewDecoder(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
error := decoder.Decode(value.(map[string]interface{}))
|
||||
if error != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
retObjects[key] = &object1
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
var StringToDateTimeHook = func(
|
||||
f reflect.Type,
|
||||
t reflect.Type,
|
||||
data interface{}) (interface{}, error) {
|
||||
if t == reflect.TypeOf(time.Time{}) && f == reflect.TypeOf("") {
|
||||
return time.Parse(time.RFC3339, data.(string))
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
/*
|
||||
func Extractor(objects map[string]interface{}) (map[string]interface{}, error){
|
||||
|
||||
var retObjects = make(map[string]interface{})
|
||||
|
||||
for key, value := range objects {
|
||||
var object1 reflect.Type
|
||||
|
||||
config := mapstructure.DecoderConfig{
|
||||
DecodeHook: StringToDateTimeHook,
|
||||
Result: &object1,
|
||||
}
|
||||
|
||||
decoder, err := mapstructure.NewDecoder(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
error := decoder.Decode(value.(map[string]interface{}))
|
||||
if error != nil {
|
||||
return nil, error
|
||||
}
|
||||
retObjects[key] = &object1
|
||||
}
|
||||
|
||||
return retObjects, nil
|
||||
}
|
||||
*/
|
||||
|
||||
func GetObject(userId string, token string, endpoint string) (interface{}, error) {
|
||||
client := &http.Client{}
|
||||
req, err := BuildRequest(userId, token, "GET", apiBaseURL+endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err := ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return nil, fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
var obj *interface{}
|
||||
err = json.Unmarshal(b, &obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
func GetObjects(userId string, token string, endpoint string) (map[string]*interface{}, error) {
|
||||
client := &http.Client{}
|
||||
req, err := BuildRequest(userId, token, "GET", apiBaseURL+endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err := ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return nil, fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
var objs map[string]*interface{}
|
||||
err = json.Unmarshal(b, &objs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return objs, nil
|
||||
}
|
||||
|
||||
type GenericPostResponse struct {
|
||||
ObjectUUID string `json:"object_uuid" mapstructure:"object_uuid"`
|
||||
}
|
@ -0,0 +1,578 @@
|
||||
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
||||
// Based on the digitial ocean provider
|
||||
|
||||
package gridscale
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"bytes"
|
||||
libssh "github.com/apcera/libretto/ssh"
|
||||
"github.com/apcera/libretto/util"
|
||||
lvm "github.com/apcera/libretto/virtualmachine"
|
||||
"strconv"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
// Base API URL strings
|
||||
const (
|
||||
apiBaseURL = "https://api.gridscale.io/objects"
|
||||
apiIPBase = "/ips"
|
||||
apiIsoimageBase = "/isoimages"
|
||||
apiLocationBase = "/locations"
|
||||
apiNetworkBase = "/networks"
|
||||
apiServerBase = "/servers"
|
||||
apiSSHKeyBase = "/sshkeys"
|
||||
apiStorageBase = "/storages"
|
||||
apiTemplatesBase = "/templates"
|
||||
)
|
||||
|
||||
// Vm struct represents a full gridscale VM in libretto. It contains the server
|
||||
// itself, along with associated storages, networks and such.
|
||||
type VM struct {
|
||||
Name string
|
||||
Hostname string
|
||||
Password string
|
||||
Config UserConfig
|
||||
Server *Server
|
||||
Location *Location
|
||||
Storages []*Storage
|
||||
Networks []*Network
|
||||
IPs []*Ip
|
||||
Credentials libssh.Credentials
|
||||
BootDevice string
|
||||
}
|
||||
|
||||
type UserConfig struct {
|
||||
UserId string
|
||||
APIToken string
|
||||
}
|
||||
|
||||
// ServersResponse is the API response containing multiple servers
|
||||
type ServersResponse struct {
|
||||
Servers map[string]*Server `json:"servers"`
|
||||
}
|
||||
|
||||
// ServerResponse is the API response containing one server
|
||||
type ServerResponse struct {
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
}
|
||||
|
||||
type ServerIPsResponse struct {
|
||||
IPrelations []*IPrelations `json:"ip_relations"`
|
||||
}
|
||||
|
||||
type IPrelations struct {
|
||||
ServerUUID string `json:"server_uuid"`
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
Prefix string `json:"prefix"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
Family int `json:"family"`
|
||||
IP net.IP `json:"ip"`
|
||||
}
|
||||
|
||||
type PowerResponse struct {
|
||||
Power bool `json:"power"`
|
||||
}
|
||||
|
||||
type ServerAttachStorageRequest struct {
|
||||
BootDevice bool `json:"server_bootdevice"`
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
}
|
||||
|
||||
type ServerAttachObjectRequest struct {
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Legacy bool `json:"legacy,omitempty"`
|
||||
LocationName string `json:"location_name,omitempty"`
|
||||
ObjectUUID string `json:"object_uuid,omitempty"`
|
||||
UsageInMinutesMemory int `json:"usage_in_minutes_memory,omitempty"`
|
||||
Memory int `json:"memory,omitempty"`
|
||||
CreateTime time.Time `json:"create_time,omitempty" mapstructure:"create_time"`
|
||||
LocationUUID string `json:"location_uuid,omitempty"`
|
||||
ConsoleToken string `json:"console_token,omitempty"`
|
||||
Cores int `json:"cores,omitempty"`
|
||||
AutoRecovery bool `json:"auto_recovery,omitempty"`
|
||||
LocationIata string `json:"location_iata,omitempty"`
|
||||
AvailabilityZone interface{} `json:"availability_zone,omitempty"`
|
||||
Power bool `json:"power,omitempty"`
|
||||
Labels []interface{} `json:"labels,omitempty"`
|
||||
UsageInMinutesCores int `json:"usage_in_minutes_cores,omitempty"`
|
||||
ChangeTime time.Time `json:"change_time,omitempty" mapstructure:"change_time"`
|
||||
LocationCountry string `json:"location_country,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
type Labels struct {
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
var _ lvm.VirtualMachine = (*VM)(nil)
|
||||
|
||||
// GetName returns the name of the virtual machine
|
||||
func (vm *VM) GetName() string {
|
||||
return vm.Name
|
||||
}
|
||||
|
||||
// Provision creates a new VM
|
||||
func (vm *VM) Provision() error {
|
||||
|
||||
b, err := json.Marshal(vm.Server)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := BuildRequest(vm.Config.UserId, vm.Config.APIToken,
|
||||
"POST", apiBaseURL+apiServerBase, bytes.NewReader(b))
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err = ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
r := &ServerResponse{}
|
||||
err = json.Unmarshal(b, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vm.Server.ObjectUUID = r.ObjectUUID
|
||||
|
||||
if vm.Storages != nil {
|
||||
index := 0
|
||||
for _, storage := range vm.Storages {
|
||||
err := vm.AttachStorage(storage, index)
|
||||
index++
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if vm.IPs != nil {
|
||||
for _, ip := range vm.IPs {
|
||||
err := vm.AttachIP(ip)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if vm.Networks != nil {
|
||||
for _, network := range vm.Networks {
|
||||
err := vm.AttachNetwork(network)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vm *VM) AttachNetwork(network *Network) error {
|
||||
|
||||
attachRequest := ServerAttachObjectRequest{network.ObjectUUID}
|
||||
|
||||
b, err := json.Marshal(attachRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := BuildRequest(vm.Config.UserId, vm.Config.APIToken,
|
||||
"POST", apiBaseURL+apiServerBase+"/"+vm.Server.ObjectUUID+"/networks", bytes.NewReader(b))
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err = ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vm *VM) AttachIP(ip *Ip) error {
|
||||
|
||||
attachRequest := ServerAttachObjectRequest{ip.ObjectUUID}
|
||||
|
||||
b, err := json.Marshal(attachRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := BuildRequest(vm.Config.UserId, vm.Config.APIToken,
|
||||
"POST", apiBaseURL+apiServerBase+"/"+vm.Server.ObjectUUID+"/ips", bytes.NewReader(b))
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err = ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vm *VM) AttachStorage(storage *Storage, index int) error {
|
||||
|
||||
freshProvisioned := false
|
||||
|
||||
if storage.ObjectUUID == "" {
|
||||
tStorage, err := GetStorage(vm.Config.UserId, vm.Config.APIToken, storage.Name)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// there is an existing storage with this name
|
||||
if tStorage != nil {
|
||||
storage = tStorage
|
||||
|
||||
} else {
|
||||
/* apparently there is no existing storage with this name - let's provision it */
|
||||
// let's see if there is a template with that name
|
||||
templateInstance, err := GetTemplate(vm.Config.UserId, vm.Config.APIToken, storage.Name)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var templateRequest *StorageTemplateRequest
|
||||
|
||||
if templateInstance != nil {
|
||||
// there is a template based on that name
|
||||
templateRequest = &StorageTemplateRequest{
|
||||
Hostname: vm.Hostname,
|
||||
Password: vm.Password,
|
||||
PasswordType: "plain",
|
||||
TemplateUUID: templateInstance.ObjectUUID}
|
||||
} else {
|
||||
templateRequest = nil
|
||||
}
|
||||
|
||||
err = storage.Provision(vm.Config, templateRequest)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// we need to wait for the storage to be provisioned
|
||||
for {
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
tStorage2, err := GetStorageByUUID(vm.Config.UserId, vm.Config.APIToken, storage.ObjectUUID)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if tStorage2.Status == StatusActive {
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
freshProvisioned = true
|
||||
}
|
||||
}
|
||||
|
||||
bootDevice := false
|
||||
|
||||
if vm.BootDevice == "" && index == 0 && freshProvisioned {
|
||||
vm.BootDevice = storage.ObjectUUID
|
||||
}
|
||||
|
||||
if vm.BootDevice == storage.ObjectUUID {
|
||||
bootDevice = true
|
||||
}
|
||||
attachRequest := ServerAttachStorageRequest{bootDevice, storage.ObjectUUID}
|
||||
|
||||
b, err := json.Marshal(attachRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := BuildRequest(vm.Config.UserId, vm.Config.APIToken,
|
||||
"POST", apiBaseURL+apiServerBase+"/"+vm.Server.ObjectUUID+"/storages", bytes.NewReader(b))
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err = ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetIPs returns a list of ip addresses associated with the VM
|
||||
func (vm *VM) GetIPs() ([]net.IP, error) {
|
||||
client := &http.Client{}
|
||||
req, err := BuildRequest(vm.Config.UserId, vm.Config.APIToken,
|
||||
"GET", apiBaseURL+apiServerBase+"/"+vm.Server.ObjectUUID+"/ips", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err := ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return nil, fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
r := &ServerIPsResponse{}
|
||||
err = json.Unmarshal(b, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ips := make([]net.IP, len(r.IPrelations))
|
||||
i := 0
|
||||
|
||||
for _, bar := range r.IPrelations {
|
||||
ips[i] = bar.IP
|
||||
}
|
||||
|
||||
return ips, nil
|
||||
}
|
||||
|
||||
// GetSSH returns an ssh client for the the vm.
|
||||
func (vm *VM) GetSSH(options libssh.Options) (libssh.Client, error) {
|
||||
ips, err := util.GetVMIPs(vm, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := libssh.SSHClient{Creds: &vm.Credentials, IP: ips[0], Port: 22, Options: options}
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
// Destroy powers off the VM and deletes its files from disk
|
||||
func (vm *VM) Destroy() error {
|
||||
client := &http.Client{}
|
||||
req, err := BuildRequest(vm.Config.UserId, vm.Config.APIToken, "DELETE",
|
||||
apiBaseURL+apiServerBase+"/"+vm.Server.ObjectUUID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err := ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vm *VM) GetState() (string, error) {
|
||||
client := &http.Client{}
|
||||
req, err := BuildRequest(vm.Config.UserId, vm.Config.APIToken, "GET",
|
||||
apiBaseURL+apiServerBase+"/"+vm.Server.ObjectUUID+"/power", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rsp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err := ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if rsp.Status[0] != StatusOk {
|
||||
return "", fmt.Errorf("error: %s: %s", rsp.Status, string(b))
|
||||
}
|
||||
|
||||
r := &PowerResponse{}
|
||||
err = json.Unmarshal(b, r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strconv.FormatBool(r.Power), nil
|
||||
}
|
||||
|
||||
// Start powers on the VM
|
||||
func (vm *VM) Start() error {
|
||||
return vm.TogglePower(true)
|
||||
}
|
||||
|
||||
// Halt powers off the VM without destroying it
|
||||
func (vm *VM) Halt() error {
|
||||
return vm< |