Skip to content

Commit

Permalink
Merge pull request #10 from StackPointCloud/master
Browse files Browse the repository at this point in the history
Updates for 1&1 Cloud Server API release 1.5
  • Loading branch information
Matt Baldwin committed Apr 25, 2016
2 parents dc91e53 + 268c396 commit a0fe6f0
Show file tree
Hide file tree
Showing 30 changed files with 1,883 additions and 392 deletions.
622 changes: 322 additions & 300 deletions README.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions datacenters.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ func (api *API) ListDatacenters(args ...interface{}) ([]Datacenter, error) {

return result, nil
}

// GET /datacenters/{datacenter_id}
func (api *API) GetDatacenter(dc_id string) (*Datacenter, error) {
result := new(Datacenter)
url := createUrl(api, datacenterPathSegment, dc_id)
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}

return result, nil
}
30 changes: 30 additions & 0 deletions datacenters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"testing"
"time"
)

// /datacenters tests
Expand Down Expand Up @@ -61,3 +62,32 @@ func TestListDatacenters(t *testing.T) {
}
}
}

func TestGetDatacenter(t *testing.T) {
dcs, err := api.ListDatacenters()

if len(dcs) == 0 {
t.Errorf("No datacenter found. " + err.Error())
return
}

for i, _ := range dcs {
time.Sleep(time.Second)
fmt.Printf("Getting datacenter '%s'...\n", dcs[i].CountryCode)
dc, err := api.GetDatacenter(dcs[i].Id)

if err != nil {
t.Errorf("GetDatacenter failed. Error: " + err.Error())
return
}
if dc.Id != dcs[i].Id {
t.Errorf("Wrong datacenter ID.")
}
if dc.CountryCode != dcs[i].CountryCode {
t.Errorf("Wrong country code of the datacenter.")
}
if dc.Location != dcs[i].Location {
t.Errorf("Wrong datacenter location.")
}
}
}
12 changes: 6 additions & 6 deletions dvdisos.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import "net/http"
//
type DvdIso struct {
Identity
OsFamily string `json:"os_family,omitempty"`
Os string `json:"os,omitempty"`
OsVersion string `json:"os_version,omitempty"`
Type string `json:"type,omitempty"`
AvailableSites []string `json:"available_sites,omitempty"`
Architecture interface{} `json:"architecture"`
OsFamily string `json:"os_family,omitempty"`
Os string `json:"os,omitempty"`
OsVersion string `json:"os_version,omitempty"`
Type string `json:"type,omitempty"`
AvailableDatacenters []string `json:"available_datacenters,omitempty"`
Architecture interface{} `json:"os_architecture,omitempty"`
ApiPtr
}

Expand Down
5 changes: 1 addition & 4 deletions dvdisos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestListDvdIsos(t *testing.T) {
}

for _, dvd := range res {
if !strings.Contains(strings.ToLower(dvd.OsVersion), "freebsd") {
if !strings.Contains(strings.ToLower(dvd.Name), "freebsd") {
t.Errorf("Search parameter failed.")
}
}
Expand All @@ -76,7 +76,4 @@ func TestGetDvdIso(t *testing.T) {
if dvd.Id != dvds[0].Id {
t.Errorf("Wrong ID of the dvd iso.")
}
if dvd.Name != dvds[0].Name {
t.Errorf("Wrong name of the dvd iso.")
}
}
14 changes: 7 additions & 7 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {

var sa oneandone.ServerAppliance
for _, a := range saps {
if a.IsAutomaticInstall && a.Type == "INTERNAL" {
if a.Type == "INTERNAL" {
sa = a
}
}
Expand Down Expand Up @@ -58,14 +58,14 @@ func main() {
// time.Sleep(time.Second * 10)

// Create a load balancer
lbr := oneandone.LoadBalancer{
lbr := oneandone.LoadBalancerRequest{
Name: "Load Balancer Example",
Description: "API created load balancer.",
Method: "ROUND_ROBIN",
Persistence: true,
PersistenceTime: 1200,
Persistence: oneandone.Bool2Pointer(true),
PersistenceTime: oneandone.Int2Pointer(1200),
HealthCheckTest: "TCP",
HealthCheckInterval: 40,
HealthCheckInterval: oneandone.Int2Pointer(40),
Rules: []oneandone.LoadBalancerRule{
{
Protocol: "TCP",
Expand Down Expand Up @@ -240,7 +240,7 @@ func main() {

var sa oneandone.ServerAppliance
for _, a := range saps {
if a.IsAutomaticInstall && a.Type == "APPLICATION" {
if a.Type == "APPLICATION" {
sa = a
break
}
Expand All @@ -252,7 +252,7 @@ func main() {
fixed_flavours, err = api.ListFixedInstanceSizes()
for _, fl := range fixed_flavours {
//look for 'M' size
if fl.Name == "VPS_M" {
if fl.Name == "M" {
fixed_size_id = fl.Id
break
}
Expand Down
26 changes: 13 additions & 13 deletions images.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
type Image struct {
idField
ImageConfig
MinHddSize int `json:"min_hdd_size"`
Architecture int `json:"architecture"`
CloudPanelId string `json:"cloudpanel_id,omitempty"`
CreationDate string `json:"creation_date,omitempty"`
State string `json:"state,omitempty"`
OsImageType string `json:"os_image_type,omitempty"`
OsFamily string `json:"os_family,omitempty"`
Os string `json:"os,omitempty"`
OsVersion string `json:"os_version,omitempty"`
Type string `json:"type,omitempty"`
AvailableSites []string `json:"available_sites,omitempty"`
Licenses []License `json:"licenses,omitempty"`
Hdds []Hdd `json:"hdds,omitempty"`
MinHddSize int `json:"min_hdd_size"`
Architecture *int `json:"os_architecture"`
CloudPanelId string `json:"cloudpanel_id,omitempty"`
CreationDate string `json:"creation_date,omitempty"`
State string `json:"state,omitempty"`
OsImageType string `json:"os_image_type,omitempty"`
OsFamily string `json:"os_family,omitempty"`
Os string `json:"os,omitempty"`
OsVersion string `json:"os_version,omitempty"`
Type string `json:"type,omitempty"`
Licenses []License `json:"licenses,omitempty"`
Hdds []Hdd `json:"hdds,omitempty"`
Datacenter *Datacenter `json:"datacenter,omitempty"`
ApiPtr
}

Expand Down
12 changes: 9 additions & 3 deletions images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func create_image(ser_id string) *Image {
fmt.Printf("Unable to create image '%s'.", image_name)
return nil
}
api.WaitForState(img, "ACTIVE", 10, 60)
api.WaitForState(img, "ENABLED", 10, 90)
return img
}

func setup_image() {
_, image_serv, _ = create_test_server(true)
api.WaitForState(image_serv, "POWERED_ON", 10, 70)
api.WaitForState(image_serv, "POWERED_ON", 10, 180)
test_image = create_image(image_serv.Id)
}

Expand All @@ -67,21 +67,27 @@ func TestCreateImage(t *testing.T) {
}
if test_image.Id == "" {
t.Errorf("Missing image ID.")
time.Sleep(60 * time.Second)
}
if !strings.Contains(test_image.Name, image_name) {
t.Errorf("Wrong image name.")
time.Sleep(60 * time.Second)
}
if test_image.Description != image_desc {
t.Errorf("Wrong image description.")
time.Sleep(60 * time.Second)
}
if test_image.ServerId != image_serv.Id {
t.Errorf("Wrong server ID in image '%s'.", test_image.Name)
time.Sleep(60 * time.Second)
}
if test_image.Frequency != img_freq {
t.Errorf("Wrong image frequency.")
time.Sleep(60 * time.Second)
}
if test_image.NumImages != img_numb {
t.Errorf("Wrong number of images in image '%s'.", test_image.Name)
time.Sleep(60 * time.Second)
}
}

Expand All @@ -101,7 +107,7 @@ func TestGetImage(t *testing.T) {
if img.Type != test_image.Type {
t.Errorf("Wrong image type.")
}
if img.Architecture != test_image.Architecture {
if *img.Architecture != *test_image.Architecture {
t.Errorf("Wrong image architecture.")
}
if img.Description != test_image.Description {
Expand Down
31 changes: 17 additions & 14 deletions loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type LoadBalancer struct {
Method string `json:"method,omitempty"`
Rules []LoadBalancerRule `json:"rules,omitempty"`
ServerIps []ServerIpInfo `json:"server_ips,omitempty"`
Datacenter *Datacenter `json:"datacenter,omitempty"`
CloudPanelId string `json:"cloudpanel_id,omitempty"`
}

Expand All @@ -32,16 +33,18 @@ type LoadBalancerRule struct {
Source string `json:"source,omitempty"`
}

type LoadBalancerUpdate struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
HealthCheckTest string `json:"health_check_test,omitempty"`
HealthCheckInterval *int `json:"health_check_interval"`
HealthCheckPath string `json:"health_check_path,omitempty"`
HealthCheckPathParser string `json:"health_check_path_parser,omitempty"`
Persistence *bool `json:"persistence"`
PersistenceTime *int `json:"persistence_time"`
Method string `json:"method,omitempty"`
type LoadBalancerRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
DatacenterId string `json:"datacenter_id,omitempty"`
HealthCheckTest string `json:"health_check_test,omitempty"`
HealthCheckInterval *int `json:"health_check_interval"`
HealthCheckPath string `json:"health_check_path,omitempty"`
HealthCheckPathParser string `json:"health_check_path_parser,omitempty"`
Persistence *bool `json:"persistence"`
PersistenceTime *int `json:"persistence_time"`
Method string `json:"method,omitempty"`
Rules []LoadBalancerRule `json:"rules,omitempty"`
}

// GET /load_balancers
Expand All @@ -62,10 +65,10 @@ func (api *API) ListLoadBalancers(args ...interface{}) ([]LoadBalancer, error) {
}

// POST /load_balancers
func (api *API) CreateLoadBalancer(lb *LoadBalancer) (string, *LoadBalancer, error) {
func (api *API) CreateLoadBalancer(request *LoadBalancerRequest) (string, *LoadBalancer, error) {
url := createUrl(api, loadBalancerPathSegment)
result := new(LoadBalancer)
err := api.Client.Post(url, &lb, &result, http.StatusAccepted)
err := api.Client.Post(url, &request, &result, http.StatusAccepted)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -98,10 +101,10 @@ func (api *API) DeleteLoadBalancer(lb_id string) (*LoadBalancer, error) {
}

// PUT /load_balancers/{id}
func (api *API) UpdateLoadBalancer(lb_id string, lb_update *LoadBalancerUpdate) (*LoadBalancer, error) {
func (api *API) UpdateLoadBalancer(lb_id string, request *LoadBalancerRequest) (*LoadBalancer, error) {
url := createUrl(api, loadBalancerPathSegment, lb_id)
result := new(LoadBalancer)
err := api.Client.Put(url, &lb_update, &result, http.StatusAccepted)
err := api.Client.Put(url, &request, &result, http.StatusAccepted)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func create_load_balancer() *LoadBalancer {
rint := rand.Intn(999)
test_lb_name = fmt.Sprintf("LoadBalancer_%d", rint)
test_lb_desc = fmt.Sprintf("LoadBalancer_%d description", rint)
req := LoadBalancer{
req := LoadBalancerRequest{
Name: test_lb_name,
Description: test_lb_desc,
Method: lb_round_robin,
Persistence: true,
PersistenceTime: 60,
Persistence: Bool2Pointer(true),
PersistenceTime: Int2Pointer(60),
HealthCheckTest: "TCP",
HealthCheckInterval: 300,
HealthCheckInterval: Int2Pointer(300),
Rules: []LoadBalancerRule{
{
Protocol: "TCP",
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestUpdateLoadBalancer(t *testing.T) {
fmt.Printf("Updating load balancer '%s'...\n", test_lb.Name)
new_name := test_lb.Name + "_updated"
new_desc := test_lb.Description + "_updated"
lbu := LoadBalancerUpdate{
lbu := LoadBalancerRequest{
Name: new_name,
Description: new_desc,
Method: lb_least_con,
Expand Down
5 changes: 5 additions & 0 deletions oneandone.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ const (
logPathSegment = "logs"
monitorCenterPathSegment = "monitoring_center"
monitorPolicyPathSegment = "monitoring_policies"
pingPathSegment = "ping"
pingAuthPathSegment = "ping_auth"
pricingPathSegment = "pricing"
privateNetworkPathSegment = "private_networks"
publicIpPathSegment = "public_ips"
rolePathSegment = "roles"
serverPathSegment = "servers"
serverAppliancePathSegment = "server_appliances"
sharedStoragePathSegment = "shared_storages"
usagePathSegment = "usages"
userPathSegment = "users"
vpnPathSegment = "vpns"
)

// Struct to hold the status of an API object.
Expand Down
29 changes: 29 additions & 0 deletions ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package oneandone

import "net/http"

// GET /ping
// Returns "PONG" if API is running
func (api *API) Ping() ([]string, error) {
url := createUrl(api, pingPathSegment)
result := []string{}
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}

return result, nil
}

// GET /ping_auth
// Returns "PONG" if the API is running and the authentication token is valid
func (api *API) PingAuth() ([]string, error) {
url := createUrl(api, pingAuthPathSegment)
result := []string{}
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}

return result, nil
}
Loading

0 comments on commit a0fe6f0

Please sign in to comment.