scalewaysdkgo

package module
v1.0.0-beta.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2019 License: Apache-2.0 Imports: 0 Imported by: 0

README

GoDoc CircleCI GoReportCard

Scaleway GO SDK

⚠ This is an early release, keep in mind that the API can break

Scaleway is a simple way to build, deploy and scale your infrastructure in the cloud. We help thousands of developers and businesses to run their infrastructures without any issue.

Documentation

Installation

go get github.com/scaleway/scaleway-sdk-go

Getting Started

package main

import (
	"fmt"

	"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
	"github.com/scaleway/scaleway-sdk-go/scw"
	"github.com/scaleway/scaleway-sdk-go/utils"
)

func main() {

	// Create a Scaleway client
	client, err := scw.NewClient(
		// Get your credentials at https://console.scaleway.com/account/credentials
		scw.WithDefaultOrganizationID("ORGANISATION_ID"),
		scw.WithAuth("ACCESS_KEY", "SECRET_KEY"),
	)
	if err != nil {
		panic(err)
	}

	// Create SDK objects for Scaleway Instance product
	instanceApi := instance.NewAPI(client)

	// Call the ListServers method on the Instance SDK
	response, err := instanceApi.ListServers(&instance.ListServersRequest{
		Zone: scw.ZoneFrPar1,
	})
	if err != nil {
		panic(err)
	}

	// Do something with the response...
	for _, server := range response.Servers {
		fmt.Println("Server", server.ID, server.Name)
	}

}

Examples

You can find additional examples in the GoDoc.

Development

This repository is at its early stage and is still in active development. If you are looking for a way to contribute please read CONTRIBUTING.md.

Reach us

We love feedback. Feel free to reach us on Scaleway Slack community, we are waiting for you on #opensource.

Documentation

Overview

Package scalewaysdkgo is the Scaleway API SDK for Go.

In order to use the available APIs, create a `Client`. Once created, it can be used to instantiate an API. To use the `instance` API, for example, instantiate it (with the client object) `instance.NewApi(client)`. On this instance API, all the available API functions can be called.

Example (ApiClient)
// Create a Scaleway client
client, err := scw.NewClient(
	scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/account/credentials
)
if err != nil {
	// handle error
}

// Create SDK objects for specific Scaleway Products
instance := instance.NewAPI(client)
lb := lb.NewAPI(client)

// Start using the SDKs
_, _ = instance, lb
Output:

Example (ApiClientWithConfig)
// Get Scaleway Config
config, err := scw.LoadConfig()
if err != nil {
	// handle error
}

// Use active profile
profile, err := config.GetActiveProfile()
if err != nil {
	// handle error
}

// Create a Scaleway client
client, err := scw.NewClient(
	scw.WithProfile(profile),
	scw.WithEnv(), // env variable may overwrite profile values
)
if err != nil {
	// handle error
}

// Create SDK objects for specific Scaleway Products
instance := instance.NewAPI(client)
lb := lb.NewAPI(client)

// Start using the SDKs
_, _ = instance, lb
Output:

Example (CreateLoadBalancer)
// Create a Scaleway client
client, err := scw.NewClient(
	scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/account/credentials
)
if err != nil {
	// handle error
}

// Create SDK objects for Scaleway LoadConfig Balancer product
lbAPI := lb.NewAPI(client)

// Call the CreateLb method on the LB SDK to create a new load balancer.
newLb, err := lbAPI.CreateLb(&lb.CreateLbRequest{
	Name:           "My new load balancer",
	Description:    "This is a example of a load balancer",
	OrganizationID: "000a115d-2852-4b0a-9ce8-47f1134ba95a",
	Region:         scw.RegionFrPar,
})

if err != nil {
	// handle error
}

// Do something with the newly created LB...
fmt.Println(newLb)
Output:

Example (CreateServer)
// Create a Scaleway client
client, err := scw.NewClient(
	scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/account/credentials
	scw.WithDefaultOrganizationID("ORGANIZATION_ID"),
	scw.WithDefaultZone(scw.ZoneFrPar1),
)
if err != nil {
	panic(err)
}

// Create SDK objects for Scaleway Instance and marketplace
instanceAPI := instance.NewAPI(client)

serverType := "DEV1-S"
image := "ubuntu-bionic"

// Create a new DEV1-S server
createRes, err := instanceAPI.CreateServer(&instance.CreateServerRequest{
	Name:              "my-server-01",
	CommercialType:    serverType,
	Image:             image,
	DynamicIPRequired: scw.BoolPtr(true),
})
if err != nil {
	panic(err)
}

// Start the server and wait until it's ready.
err = instanceAPI.ServerActionAndWait(&instance.ServerActionAndWaitRequest{
	ServerID: createRes.Server.ID,
	Action:   instance.ServerActionPoweron,
	Timeout:  5 * time.Minute,
})
if err != nil {
	panic(err)
}
Output:

Example (ListServers)
// Create a Scaleway client
client, err := scw.NewClient(
	scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/account/credentials
)
if err != nil {
	// handle error
}

// Create SDK objects for Scaleway Instance product
instanceAPI := instance.NewAPI(client)

// Call the ListServers method on the Instance SDK
response, err := instanceAPI.ListServers(&instance.ListServersRequest{
	Zone: scw.ZoneFrPar1,
})
if err != nil {
	// handle error
}

// Do something with the response...
fmt.Println(response)
Output:

Example (RebootAllServers)
// Create a Scaleway client
client, err := scw.NewClient(
	scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/account/credentials
	scw.WithDefaultZone(scw.ZoneFrPar1),
)
if err != nil {
	panic(err)
}

// Create SDK objects for Scaleway Instance product
instanceAPI := instance.NewAPI(client)

// Call the ListServers method of the Instance SDK
response, err := instanceAPI.ListServers(&instance.ListServersRequest{})
if err != nil {
	panic(err)
}

// For each server if they are running we reboot them using ServerActionAndWait
for _, server := range response.Servers {
	if server.State == instance.ServerStateRunning {
		fmt.Println("Rebooting server with ID", server.ID)
		err = instanceAPI.ServerActionAndWait(&instance.ServerActionAndWaitRequest{
			ServerID: server.ID,
			Action:   instance.ServerActionReboot,
			Timeout:  5 * time.Minute,
		})
		if err != nil {
			panic(err)
		}
	}
}
fmt.Println("All servers were successfully rebooted")
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL