Getting Started with Go SDK
Welcome to use HUAWEI CLOUD developer tools (Go SDK). Go SDK allows you to easily access cloud services using codes.
This tutorial describes how to install and use Go SDK and provides examples to help you quickly get started.
The Go SDK is developed based on Gophercloud.
Supported Cloud Services
The Go SDK supports the following cloud services:
Prerequisites
- You have obtained a cloud platform account and provisioned all required services.
- If Go SDK is used, Go 1.9.1 is recommended.
SDK Acquisition and Installation
You can download the source code of Go SDK at the following Github websites:
https://github.com/huaweicloud/huaweicloud-sdk-release/tree/master/go-sdk
Perform the installation on the Linux OS:
Before the installation, ensure that the GOPATH environment variable points to the target directory of Gophercloud to be installed.
mkdir $HOME/go mkdir -p $HOME/go/src export GOPATH=$HOME/go
Download the source code package. Decompress it and install it in the src directory of the go. Run the go build command.
# unzip source code unzip -d $GOPATH/src xxxxxx.zip #run go build cd $GOPATH/src/github.com/gophercloud/gophercloud go build
How to Use
Before making a call to the SDK to access service APIs, you need to configure parameters and complete authentication. For details about the parameters, see Table 1.
package main
import (
"github.com/gophercloud/gophercloud/auth/token"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack"
"fmt"
)
func main() {
// Set the authentication parameters.
tokenOpts := token.TokenOptions{
IdentityEndpoint: "https://iam.example.com/v3",
Username: "{username}",
Password: "{password}",
DomainID: "{domainid}",
ProjectID: "{projectid}",
}
// Initialize the provider client.
provider, providerErr := openstack.AuthenticatedClient(tokenOpts)
if providerErr != nil {
fmt.Println("init provider client error:", providerErr)
panic(providerErr)
}
// Initialize the service client.
sc, serviceErr := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{})
if serviceErr != nil {
fmt.Println("init compute service client error:", serviceErr)
panic(serviceErr)
}
// List all servers.
allPages, err := servers.List(sc, servers.ListOpts{}).AllPages()
if err != nil {
fmt.Println("request server list error:", err)
panic(err)
}
// Parse the return values.
allServers, err := servers.ExtractServers(allPages)
if err != nil {
fmt.Println("extract response data error:", err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
// Print the information.
fmt.Println("List Servers:")
for _, s := range allServers {
fmt.Println("server ID is :", s.ID)
fmt.Println("server name is :", s.Name)
fmt.Println("server Status is :", s.Status)
fmt.Println("server AvailbiltyZone is :", s.AvailbiltyZone)
}
}
- ProviderClient is the top-level client required by all OpenStack services. The client contains all authentication details, such as the URL and token ID. After the authentication, the compiled Go code can access the APIs.
- The Service Client of a service is required if you need to access this service. For details, see the related chapter about this service in this document.
|
Parameter |
Description |
Example Value |
|---|---|---|
|
IdentityEndpoint |
Specifies the endpoint of the IAM service. example in https://iam.example.com/v3 indicates Region.Cloud platform domain name. For details about the parameter, see here. |
https://iam.cn-north-1.myhuaweicloud.com/v3 |
|
Username |
Specifies the IAM username. For details about how to obtain the username, see How Do I Obtain the IAM Username, Account ID, and Project ID?. |
N/A |
|
Password |
Specifies the IAM user password. |
N/A |
|
ProjectID |
Specifies the project ID. For details about how to obtain the project ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?. |
N/A |
|
DomainID |
Specifies the account ID. For details about how to obtain the account ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?. |
N/A |
Last Article: Go
Next Article: Using the Go SDK
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.