scaleway-sdk-go: github.com/scaleway/scaleway-sdk-go Index | Examples | Files | Directories

package scalewaysdkgo

import "github.com/scaleway/scaleway-sdk-go"

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.

Code:

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

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

// Start using the SDKs
_, _ = instanceAPI, lbAPI

Code:

// 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
instanceAPI := instance.NewAPI(client)
lbAPI := lb.NewAPI(client)

// Start using the SDKs
_, _ = instanceAPI, lbAPI

Code:

// Create a Scaleway client
client, err := scw.NewClient(
    scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/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: scw.StringPtr("000a115d-2852-4b0a-9ce8-47f1134ba95a"),
    Region:         scw.RegionFrPar,
})

if err != nil {
    // handle error
}

// Do something with the newly created LB...
fmt.Println(newLB)

Code:

// Create a Scaleway client
client, err := scw.NewClient(
    scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/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_focal"

// 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.
timeout := 5 * time.Minute
err = instanceAPI.ServerActionAndWait(&instance.ServerActionAndWaitRequest{
    ServerID: createRes.Server.ID,
    Action:   instance.ServerActionPoweron,
    Timeout:  &timeout,
})
if err != nil {
    panic(err)
}

Code:

// Create a Scaleway client
client, err := scw.NewClient(
    scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/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)

Code:

// Create a Scaleway client
client, err := scw.NewClient(
    scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/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
timeout := 5 * time.Minute
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:  &timeout,
        })
        if err != nil {
            panic(err)
        }
    }
}
fmt.Println("All servers were successfully rebooted")

Index

Examples

Package Files

doc.go

Directories

PathSynopsis
api/account/v2alpha1Package account provides methods and message types of the account v2alpha1 API.
api/baremetal/v1Package baremetal provides methods and message types of the baremetal v1 API.
api/baremetal/v1alpha1Package baremetal provides methods and message types of the baremetal v1alpha1 API.
api/domain/v2alpha2Package domain provides methods and message types of the domain v2alpha2 API.
api/instance/v1Package instance provides methods and message types of the instance v1 API.
api/iot/v1beta1Package iot provides methods and message types of the iot v1beta1 API.
api/k8s/v1Package k8s provides methods and message types of the k8s v1 API.
api/k8s/v1beta3Package k8s provides methods and message types of the k8s v1beta3 API.
api/k8s/v1beta4Package k8s provides methods and message types of the k8s v1beta4 API.
api/lb/v1Package lb provides methods and message types of the lb v1 API.
api/marketplace/v1Package marketplace provides methods and message types of the marketplace v1 API.
api/rdb/v1Package rdb provides methods and message types of the rdb v1 API.
api/registry/v1Package registry provides methods and message types of the registry v1 API.
api/test/v1Package test provides methods and message types of the test v1 API.
internal/async
internal/auth
internal/errors
internal/marshaler
internal/parameter
internal/testhelpers
internal/testhelpers/httprecorder
logger
namegenerator
scw
strcase
validationPackage validation provides format validation functions.

Updated 2020-10-16. Refresh now. Tools for package owners.