An introduction to gsclient-go

Datum: 30.10.2019

Golang ClientWith Golang gaining so much traction, we started to implement support for the language in November ‘18. Since then, we’ve implemented a lot of functionality – adding support for endpoints, implementing testing and adding high-end documentation – Along with this (and much more) we’re proud to release v2.0.0 of our gsclient-go.

This post is going to take you briefly into our Golang library, explain some concepts and help you get started.

The latest release brings some important updates:

  • Enabling sync mode
  • Default resource configurations
  • Auto retry upon receiving 5xx and 424 error codes
  • Fully controlled library through context

Getting started

First of all, a gridscale account will be required, which can be created here. Then an API-token should be created. You’ll need to install go.

Then install our gsclient-go with `go get “github.com/gridscale/gsclient-go”`

@nvthongswansea has done a huge amount of work recently, making the v2.0.0 release possible. He’s also gone ahead and made it even easier to get started, and has given us some examples to get started. We will take a look at the server.go examples.

Authentication

We’ll set our credentials as environment variables

`export GRIDSCALE_TOKEN=”API_TOKEN” GRIDSCALE_UUID=”USER_UUID”`

Replace API_TOKEN & USER_UUID with your API credentials

Spinning up a simple setup

Navigate to the gsclient-go directory

`cd $GOPATH/src/github.com/gridscale/gsclient-go/examples/`

Running `go run server.go` will now go through these steps, prompting you to press enter before executing each step:

  1. Create a new server with `CreateServer()`.
  2. Get the created server with `GetServer()`.
  3. Start the created server with `StartServer()` as a test.
  4. Switch it off again with `ShutdownServer()` – this is an ACPI shutdown. The alternative, but less safe is `StopServer()`.
  5. Update the details and resources of the server with `UpdateServer()`.
  6. Print the event log of the created server with `GetServerEventList()`.
  7. Create a storage with `CreateStorage()`.
  8. Create a network with `CreateNetwork()`.
  9. Create an IP address with `CreateIP()`.
  10. Create an ISO Image with `CreateISOImage()` – This downloads an image from tinycorelinux.net
  11. We then link all the created resources with `LinkStorage()`, `LinkNetwork()`, `LinkIP()` and `LinkIsoImage()` respectively.

You’ll then be prompted to unlink and delete all of the created resources. Either confirm with `enter`, or exit with `ctrl+c` to keep the resources and take some further steps yourself.

You can check out the other files in the example directory, they’re pretty verbose so customizing them to your needs should be a walk in the park.

Have fun managing infrastructure with golang, this library is also the basis for our Terraform integration.


Marc Harriss

Marc Harriss | Product Owner
As gridscales Product Owner, Marc works closely on product processes and maximising the value of the product from working with the Development teams. He works with the philosophy of "develop products as a customer" in mind, has a passion for software and how things work. With a professional background in I.T. and Logistics management in the startup scene, Marc is used to working with fast paced, interdisciplinary teams, to move things forward.

Back to overview