Being new to Golang (in fact started learning it just a couple of days back) I have a very basic question on creating a client code for consuming the SL APIs.
So my requirement is to call a createsnapshot SL API using Golang which will take snapshot of my endurance volume, provided that volume id is a input parameter to it. Can you please help me with a sample code for writing this client ?
I know how to do it in python, here is how I did it python, but now I want it in golang (change in req. you know ;) )
python code snippet:
client = SoftLayer.create_client_from_env("softlayer username", "softlayer apikey")
result = client['SoftLayer_Network_Storage'].createSnapshot("snapshot_name", "volume id")
thank you !
If i am not misunderstanding, you are using Softlayer package for python to do what you are doing in your given code.
Softlayer has official go package as well here
Download the package in your go environment by
go get github.com/softlayer/softlayer-go/...
Then import he package in your application and use it.
basic Example:
// 1. Create a session
sess := session.New(username, apikey)
// 2. Get a service
accountService := services.GetAccountService(sess)
// 3. Invoke a method:
account, err := accountService.GetObject()
You need to find methods that does the job for you.
Try the following script please:
// Create Snapshot
//
// This script creates a snapshot for storage
//
// See below references for more details.
// important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Network_Storage/createSnapshot
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
package main
import (
"fmt"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/session"
"encoding/json"
)
func main() {
username := "set me"
apikey := "set me"
storageId := 21015123
notes := "test"
// 1. Create a session
sess := session.New(username, apikey)
// 2. Get a service
service := services.GetNetworkStorageService(sess)
result, err := service.Id(storageId).CreateSnapshot(¬es)
if err != nil {
fmt.Printf("%s
", err)
return
}
res, errMarsh := json.Marshal(result)
if errMarsh != nil {
fmt.Println(errMarsh)
return
}
fmt.Println(string(res))
}
Replace:: username, apikey, storageId and notes with your own information.
References: