2017-02-07 21:33:23 +00:00
|
|
|
package egoscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetSecurityGroups returns all security groups
|
|
|
|
//
|
|
|
|
// Deprecated: do it yourself
|
2017-02-07 21:33:23 +00:00
|
|
|
func (exo *Client) GetSecurityGroups() (map[string]SecurityGroup, error) {
|
|
|
|
var sgs map[string]SecurityGroup
|
2018-02-12 17:10:05 +00:00
|
|
|
resp, err := exo.Request(&ListSecurityGroups{})
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sgs = make(map[string]SecurityGroup)
|
2018-02-12 17:10:05 +00:00
|
|
|
for _, sg := range resp.(*ListSecurityGroupsResponse).SecurityGroup {
|
|
|
|
sgs[sg.Name] = sg
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
return sgs, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetSecurityGroupID returns security group by name
|
|
|
|
//
|
|
|
|
// Deprecated: do it yourself
|
|
|
|
func (exo *Client) GetSecurityGroupID(name string) (string, error) {
|
|
|
|
resp, err := exo.Request(&ListSecurityGroups{SecurityGroupName: name})
|
2017-10-31 09:42:03 +00:00
|
|
|
if err != nil {
|
2017-02-07 21:33:23 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
for _, sg := range resp.(*ListSecurityGroupsResponse).SecurityGroup {
|
2017-02-07 21:33:23 +00:00
|
|
|
if sg.Name == name {
|
2018-02-12 17:10:05 +00:00
|
|
|
return sg.ID, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetAllZones returns all the zone id by name
|
|
|
|
//
|
|
|
|
// Deprecated: do it yourself
|
|
|
|
func (exo *Client) GetAllZones() (map[string]string, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
var zones map[string]string
|
2018-02-12 17:10:05 +00:00
|
|
|
resp, err := exo.Request(&ListZones{})
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2018-02-12 17:10:05 +00:00
|
|
|
return zones, err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zones = make(map[string]string)
|
2018-02-12 17:10:05 +00:00
|
|
|
for _, zone := range resp.(*ListZonesResponse).Zone {
|
|
|
|
zones[strings.ToLower(zone.Name)] = zone.ID
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
return zones, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetProfiles returns a mapping of the service offerings by name
|
|
|
|
//
|
|
|
|
// Deprecated: do it yourself
|
2017-02-07 21:33:23 +00:00
|
|
|
func (exo *Client) GetProfiles() (map[string]string, error) {
|
2018-02-12 17:10:05 +00:00
|
|
|
profiles := make(map[string]string)
|
|
|
|
resp, err := exo.Request(&ListServiceOfferings{})
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2018-02-12 17:10:05 +00:00
|
|
|
return profiles, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
for _, offering := range resp.(*ListServiceOfferingsResponse).ServiceOffering {
|
|
|
|
profiles[strings.ToLower(offering.Name)] = offering.ID
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return profiles, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetKeypairs returns the list of SSH keyPairs
|
|
|
|
//
|
|
|
|
// Deprecated: do it yourself
|
2017-02-07 21:33:23 +00:00
|
|
|
func (exo *Client) GetKeypairs() ([]SSHKeyPair, error) {
|
2018-02-12 17:10:05 +00:00
|
|
|
var keypairs []SSHKeyPair
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
resp, err := exo.Request(&ListSSHKeyPairs{})
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2018-02-12 17:10:05 +00:00
|
|
|
return keypairs, err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
r := resp.(*ListSSHKeyPairsResponse)
|
|
|
|
keypairs = make([]SSHKeyPair, r.Count)
|
|
|
|
for i, keypair := range r.SSHKeyPair {
|
|
|
|
keypairs[i] = keypair
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
return keypairs, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetAffinityGroups returns a mapping of the (anti-)affinity groups
|
|
|
|
//
|
|
|
|
// Deprecated: do it yourself
|
2017-02-07 21:33:23 +00:00
|
|
|
func (exo *Client) GetAffinityGroups() (map[string]string, error) {
|
|
|
|
var affinitygroups map[string]string
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
resp, err := exo.Request(&ListAffinityGroups{})
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2018-02-12 17:10:05 +00:00
|
|
|
return affinitygroups, err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
affinitygroups = make(map[string]string)
|
2018-02-12 17:10:05 +00:00
|
|
|
for _, affinitygroup := range resp.(*ListAffinityGroupsResponse).AffinityGroup {
|
|
|
|
affinitygroups[affinitygroup.Name] = affinitygroup.ID
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
return affinitygroups, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetImages list the available featured images and group them by name, then size.
|
|
|
|
//
|
|
|
|
// Deprecated: do it yourself
|
|
|
|
func (exo *Client) GetImages() (map[string]map[int64]string, error) {
|
|
|
|
var images map[string]map[int64]string
|
|
|
|
images = make(map[string]map[int64]string)
|
|
|
|
re := regexp.MustCompile(`^Linux (?P<name>.+?) (?P<version>[0-9.]+)\b`)
|
|
|
|
|
|
|
|
resp, err := exo.Request(&ListTemplates{
|
|
|
|
TemplateFilter: "featured",
|
|
|
|
ZoneID: "1", // XXX: Hack to list only CH-GVA
|
|
|
|
})
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2018-02-12 17:10:05 +00:00
|
|
|
return images, err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
for _, template := range resp.(*ListTemplatesResponse).Template {
|
|
|
|
size := int64(template.Size >> 30) // B to GiB
|
|
|
|
|
|
|
|
fullname := strings.ToLower(template.Name)
|
|
|
|
|
|
|
|
if _, present := images[fullname]; !present {
|
|
|
|
images[fullname] = make(map[int64]string)
|
|
|
|
}
|
|
|
|
images[fullname][size] = template.ID
|
2017-02-07 21:33:23 +00:00
|
|
|
|
|
|
|
submatch := re.FindStringSubmatch(template.Name)
|
|
|
|
if len(submatch) > 0 {
|
|
|
|
name := strings.Replace(strings.ToLower(submatch[1]), " ", "-", -1)
|
|
|
|
version := submatch[2]
|
|
|
|
image := fmt.Sprintf("%s-%s", name, version)
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
if _, present := images[image]; !present {
|
|
|
|
images[image] = make(map[int64]string)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
2018-02-12 17:10:05 +00:00
|
|
|
images[image][size] = template.ID
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return images, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// GetTopology returns an big, yet incomplete view of the world
|
|
|
|
//
|
|
|
|
// Deprecated: will go away in the future
|
2017-02-07 21:33:23 +00:00
|
|
|
func (exo *Client) GetTopology() (*Topology, error) {
|
2018-02-12 17:10:05 +00:00
|
|
|
zones, err := exo.GetAllZones()
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
images, err := exo.GetImages()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
securityGroups, err := exo.GetSecurityGroups()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
groups := make(map[string]string)
|
2017-10-31 09:42:03 +00:00
|
|
|
for k, v := range securityGroups {
|
2018-02-12 17:10:05 +00:00
|
|
|
groups[k] = v.ID
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keypairs, err := exo.GetKeypairs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the ssh keypair to contain just the name */
|
|
|
|
keynames := make([]string, len(keypairs))
|
|
|
|
for i, k := range keypairs {
|
|
|
|
keynames[i] = k.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
affinitygroups, err := exo.GetAffinityGroups()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-12 17:10:05 +00:00
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
profiles, err := exo.GetProfiles()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
topo := &Topology{
|
|
|
|
Zones: zones,
|
|
|
|
Images: images,
|
|
|
|
Keypairs: keynames,
|
2018-02-12 17:10:05 +00:00
|
|
|
Profiles: profiles,
|
2017-02-07 21:33:23 +00:00
|
|
|
AffinityGroups: affinitygroups,
|
|
|
|
SecurityGroups: groups,
|
|
|
|
}
|
|
|
|
|
|
|
|
return topo, nil
|
|
|
|
}
|