2018-05-30 22:28:41 +00:00
|
|
|
/*
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
Package egoscale is a mapping for the Exoscale API (https://community.exoscale.com/api/compute/).
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
Requests and Responses
|
|
|
|
|
2018-09-08 10:09:59 +00:00
|
|
|
To build a request, construct the adequate struct. This library expects a pointer for efficiency reasons only. The response is a struct corresponding to the data at stake. E.g. DeployVirtualMachine gives a VirtualMachine, as a pointer as well to avoid big copies.
|
2018-05-30 22:28:41 +00:00
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
Then everything within the struct is not a pointer. Find below some examples of how egoscale may be used. If anything feels odd or unclear, please let us know: https://github.com/exoscale/egoscale/issues
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
req := &egoscale.DeployVirtualMachine{
|
|
|
|
Size: 10,
|
2019-02-18 09:49:41 +00:00
|
|
|
ServiceOfferingID: egoscale.MustParseUUID("..."),
|
|
|
|
TemplateID: egoscale.MustParseUUID("..."),
|
|
|
|
ZoneID: egoscale.MastParseUUID("..."),
|
2018-05-30 22:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Deployment started")
|
|
|
|
resp, err := cs.Request(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-09-08 10:09:59 +00:00
|
|
|
vm := resp.(*egoscale.VirtualMachine)
|
2018-05-30 22:28:41 +00:00
|
|
|
fmt.Printf("Virtual Machine ID: %s\n", vm.ID)
|
|
|
|
|
2018-09-08 10:09:59 +00:00
|
|
|
This example deploys a virtual machine while controlling the job status as it goes. It enables a finer control over errors, e.g. HTTP timeout, and eventually a way to kill it of (from the client side).
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
req := &egoscale.DeployVirtualMachine{
|
|
|
|
Size: 10,
|
2019-02-18 09:49:41 +00:00
|
|
|
ServiceOfferingID: egoscale.MustParseUUID("..."),
|
|
|
|
TemplateID: egoscale.MustParseUUID("..."),
|
|
|
|
ZoneID: egoscale.MustParseUUID("..."),
|
2018-05-30 22:28:41 +00:00
|
|
|
}
|
2018-09-08 10:09:59 +00:00
|
|
|
vm := &egoscale.VirtualMachine{}
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
fmt.Println("Deployment started")
|
|
|
|
cs.AsyncRequest(req, func(jobResult *egoscale.AsyncJobResult, err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
// any kind of error
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep waiting
|
|
|
|
if jobResult.JobStatus == egoscale.Pending {
|
|
|
|
fmt.Println("wait...")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal the response into the response struct
|
2018-09-08 10:09:59 +00:00
|
|
|
if err := jobResult.Response(vm); err != nil {
|
2018-05-30 22:28:41 +00:00
|
|
|
// JSON unmarshaling error
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop waiting
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2018-09-08 10:09:59 +00:00
|
|
|
fmt.Printf("Virtual Machine ID: %s\n", vm.ID)
|
|
|
|
|
|
|
|
Debugging and traces
|
|
|
|
|
|
|
|
As this library is mostly an HTTP client, you can reuse all the existing tools around it.
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
cs := egoscale.NewClient("https://api.exoscale.com/compute", "EXO...", "...")
|
2018-09-08 10:09:59 +00:00
|
|
|
// sets a logger on stderr
|
2019-02-18 09:49:41 +00:00
|
|
|
cs.Logger = log.New(os.Stderr, "prefix", log.LstdFlags)
|
2018-09-08 10:09:59 +00:00
|
|
|
// activates the HTTP traces
|
|
|
|
cs.TraceOn()
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
Nota bene: when running the tests or the egoscale library via another tool, e.g. the exo cli, the environment variable EXOSCALE_TRACE=prefix does the above configuration for you. As a developer using egoscale as a library, you'll find it more convenient to plug your favorite io.Writer as it's a Logger.
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
APIs
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
All the available APIs on the server and provided by the API Discovery plugin.
|
2018-05-30 22:28:41 +00:00
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
cs := egoscale.NewClient("https://api.exoscale.com/compute", "EXO...", "...")
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
resp, err := cs.Request(&egoscale.ListAPIs{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, api := range resp.(*egoscale.ListAPIsResponse).API {
|
|
|
|
fmt.Printf("%s %s\n", api.Name, api.Description)
|
|
|
|
}
|
|
|
|
// Output:
|
|
|
|
// listNetworks Lists all available networks
|
|
|
|
// ...
|
|
|
|
|
|
|
|
Security Groups
|
|
|
|
|
|
|
|
Security Groups provide a way to isolate traffic to VMs. Rules are added via the two Authorization commands.
|
|
|
|
|
|
|
|
resp, err := cs.Request(&egoscale.CreateSecurityGroup{
|
|
|
|
Name: "Load balancer",
|
2019-02-18 09:49:41 +00:00
|
|
|
Description: "Open HTTP/HTTPS ports from the outside world",
|
2018-05-30 22:28:41 +00:00
|
|
|
})
|
2018-09-08 10:09:59 +00:00
|
|
|
securityGroup := resp.(*egoscale.SecurityGroup)
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
resp, err = cs.Request(&egoscale.AuthorizeSecurityGroupIngress{
|
|
|
|
Description: "SSH traffic",
|
|
|
|
SecurityGroupID: securityGroup.ID,
|
2019-02-18 09:49:41 +00:00
|
|
|
CidrList: []CIDR{
|
|
|
|
*egoscale.MustParseCIDR("0.0.0.0/0"),
|
|
|
|
*egoscale.MustParseCIDR("::/0"),
|
|
|
|
},
|
2018-05-30 22:28:41 +00:00
|
|
|
Protocol: "tcp",
|
|
|
|
StartPort: 22,
|
|
|
|
EndPort: 22,
|
|
|
|
})
|
|
|
|
// The modified SecurityGroup is returned
|
2018-09-08 10:09:59 +00:00
|
|
|
securityGroup := resp.(*egoscale.SecurityGroup)
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
// ...
|
|
|
|
err = client.BooleanRequest(&egoscale.DeleteSecurityGroup{
|
|
|
|
ID: securityGroup.ID,
|
|
|
|
})
|
|
|
|
// ...
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
Security Group also implement the generic List, Get and Delete interfaces (Listable and Deletable).
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
// List all Security Groups
|
2019-02-18 09:49:41 +00:00
|
|
|
sgs, _ := cs.List(&egoscale.SecurityGroup{})
|
2018-05-30 22:28:41 +00:00
|
|
|
for _, s := range sgs {
|
|
|
|
sg := s.(egoscale.SecurityGroup)
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a Security Group
|
2019-02-18 09:49:41 +00:00
|
|
|
sgQuery := &egoscale.SecurityGroup{Name: "Load balancer"}
|
|
|
|
resp, err := cs.Get(sgQuery); err != nil {
|
2018-05-30 22:28:41 +00:00
|
|
|
...
|
|
|
|
}
|
2019-02-18 09:49:41 +00:00
|
|
|
sg := resp.(*egoscale.SecurityGroup)
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
if err := cs.Delete(sg); err != nil {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
// The SecurityGroup has been deleted
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
See: https://community.exoscale.com/documentation/compute/security-groups/
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
Zones
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
A Zone corresponds to a Data Center. You may list them. Zone implements the Listable interface, which let you perform a list in two different ways. The first exposes the underlying request while the second one hide them and you only manipulate the structs of your interest.
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
// Using ListZones request
|
|
|
|
req := &egoscale.ListZones{}
|
|
|
|
resp, err := client.Request(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, zone := range resp.(*egoscale.ListZonesResponse) {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using client.List
|
|
|
|
zone := &egoscale.Zone{}
|
|
|
|
zones, err := client.List(zone)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, z := range zones {
|
|
|
|
zone := z.(egoscale.Zone)
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
Elastic IPs
|
|
|
|
|
|
|
|
An Elastic IP is a way to attach an IP address to many Virtual Machines. The API side of the story configures the external environment, like the routing. Some work is required within the machine to properly configure the interfaces.
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
See: https://community.exoscale.com/documentation/compute/eip/
|
2018-05-30 22:28:41 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
package egoscale
|