簡體   English   中英

如何在Golang中編寫客戶端代碼以調用createSnapshot軟層API

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

作為Golang的新手(實際上是幾天前就開始學習它),對於創建用於使用SL API的客戶端代碼,我有一個非常基本的問題。

因此,我的要求是使用Golang調用createnapshot SL API,該API將為我的耐力卷拍攝快照,條件是卷ID是其輸入參數。 您能幫我提供編寫此客戶端的示例代碼嗎?

我知道如何在python中執行此操作,這是我在python中執行的操作,但現在我想要在golang中(更改請求。您知道;))

python代碼段:

    client = SoftLayer.create_client_from_env("softlayer username", "softlayer apikey")
    result = client['SoftLayer_Network_Storage'].createSnapshot("snapshot_name", "volume id")

謝謝 !

如果我沒有誤會,您正在使用適用於python的Softlayer軟件包來執行給定代碼中的操作。

Softlayer 在這里也有官方的go包

通過以下方式在您的go環境中下載軟件包

去github.com/softlayer/softlayer-go / ...

然后將他的軟件包導入您的應用程序並使用。

基本示例:

// 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()

您需要找到適合您的方法。

請嘗試以下腳本:

// 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))

}

用您自己的信息替換::用戶名,apikey,storageId和注釋。

參考文獻:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM