Cherry Servers golang API client library for Cherry Servers RESTful API.
You can view the client API docs here: https://pkg.go.dev/github.com/cherryservers/cherrygo
You can view Cherry Servers API docs here: https://api.cherryservers.com/doc
Download the library to you GOPATH:
go get github.com/cherryservers/cherrygo
Then import the library in your Go code:
import "github.com/cherryservers/cherrygo"
To authenticate to the Cherry Servers API, you must have API token, you can create authentication tokens in the Cherry Server client portal. Token must be exported in env var CHERRY_AUTH_TOKEN
or passed to client directly.
export CHERRY_AUTH_TOKEN="4bdc0acb8f7af4bdc0acb8f7afe78522e6dae9b7e03b0e78522e6dae9b7e03b0"
Use exported CHERRY_AUTH_TOKEN env variable:
func main() {
c, err := cherrygo.NewClient()
}
Pass token directly to client:
func main() {
c, err := cherrygo.NewClient(cherrygo.WithAuthToken("your-api-token"))
}
You will need team ID for later calls, for example to get projects for specified team, you will need to provide team ID.
teams, _, err := c.Teams.List(nil)
if err != nil {
log.Fatal("Error", err)
}
for _, t := range teams {
log.Println(t.ID, t.Name, t.Credit.Promo.Remaining, t.Credit.Promo.Usage, t.Credit.Resources.Pricing.Price)
}
After you have your team ID, you can list your projects. You will need your project ID to list your servers or order new ones.
projects, _, err := c.Projects.List(teamID, nil)
if err != nil {
log.Fatal("Error", err)
}
for _, p := range projects {
log.Println(p.ID, p.Name, p.Href)
}
Next thing in order to get new server is to choose one, we call it plans
plans, _, err := c.Plans.List(teamID, nil)
if err != nil {
log.Fatalf("Plans error: %v", err)
}
for _, p := range plans {
log.Println(p.Name, p.Slug)
}
After you manage to know desired plan, you need to get available images for that plan
images, _, err := c.Images.List(planSlug, nil)
if err != nil {
log.Fatal("Error", err)
}
for _, i := range images {
log.Println(i.Name, i.Slug)
}
Now you are ready to order new server
addServerRequest := cherrygo.CreateServer{
ProjectID: projectID,
Hostname: hostname,
Image: imageSlug,
Region: regionSlug,
Plan: planSlug,
}
server, _, err := c.Server.Create(&addServerRequest)
if err != nil {
log.Fatal("Error while creating new server: ", err)
}
log.Println(server.ID, server.Name, server.Hostname)
In case you want to debug this library and get requests and responses from API you need to export CHERRY_DEBUG variable
export CHERRY_DEBUG="true"
When you done, just unset that variable:
unset CHERRY_DEBUG
See the LICENSE file for license rights and limitations.