英文:
how to write a client code in Golang to call createSnapshot softlayer API
问题
作为你的中文翻译,我将为你翻译以下内容:
对于刚开始学习Golang(实际上只是几天前开始学习)的我来说,我有一个非常基本的问题,关于如何创建一个用于调用SL API的客户端代码。
我的需求是使用Golang调用createsnapshot 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")
谢谢!
英文:
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 !
答案1
得分: 3
如果我没有误解,你正在使用Python的Softlayer包来执行你给出的代码。
Softlayer也有官方的Go包,可以在这里下载1。
在你的Go环境中下载该包:
go get github.com/softlayer/softlayer-go/...
然后在你的应用程序中导入该包并使用它。
基本示例:
// 1. 创建一个会话
sess := session.New(username, apikey)
// 2. 获取一个服务
accountService := services.GetAccountService(sess)
// 3. 调用一个方法:
account, err := accountService.GetObject()
你需要找到适合你的工作的方法。
英文:
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.
答案2
得分: 1
请尝试以下脚本:
// 创建快照
//
// 此脚本用于创建存储的快照
//
// 更多详细信息请参考以下参考资料。
// 重要的手册页面:
// 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. 创建会话
sess := session.New(username, apikey)
// 2. 获取服务
service := services.GetNetworkStorageService(sess)
result, err := service.Id(storageId).CreateSnapshot(¬es)
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))
}
请将 username
、apikey
、storageId
和 notes
替换为您自己的信息。
参考资料:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论