简体   繁体   English

如何在Golang中编写客户端代码以调用createSnapshot软层API

[英]how to write a client code in Golang to call createSnapshot softlayer API

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. 作为Golang的新手(实际上是几天前就开始学习它),对于创建用于使用SL API的客户端代码,我有一个非常基本的问题。

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. 因此,我的要求是使用Golang调用createnapshot SL API,该API将为我的耐力卷拍摄快照,条件是卷ID是其输入参数。 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中执行此操作,这是我在python中执行的操作,但现在我想要在golang中(更改请求。您知道;))

python code snippet: python代码段:

    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. 如果我没有误会,您正在使用适用于python的Softlayer软件包来执行给定代码中的操作。

Softlayer has official go package as well here Softlayer 在这里也有官方的go包

Download the package in your go environment by 通过以下方式在您的go环境中下载软件包

go get github.com/softlayer/softlayer-go/... 去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(&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. 用您自己的信息替换::用户名,apikey,storageId和注释。

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM