1

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 !

Mayank Patel
  • 8,088
  • 5
  • 55
  • 75
Sanket
  • 149
  • 1
  • 8

2 Answers2

3

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.

Mayank Patel
  • 8,088
  • 5
  • 55
  • 75
  • yes, your understanding is correct, I used Python SL package for my earlier development. And about using the official go package, yes I did read about it but I am having hard time to identify the service/method name for create snapshot and also for datatypes which needs to be provided as input to create snapshot method/service btw, thank you Mayank for your inputs so far! – Sanket Mar 14 '17 at 18:55
1

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(&notes)
    if err != nil {
        fmt.Printf("%s\n", 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: