Help Center > > Developer Guide> Go> Getting Started

Getting Started

Updated at: Oct 09, 2019 GMT+08:00

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.

Prerequisites

  1. You have obtained a cloud platform account and provisioned all required services.
  2. 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.
Table 1 Parameter description

Parameter

Description

Example Value

IdentityEndpoint

Specifies the endpoint of the IAM service.

example in the https://iam.example.com/v3, indicates the Region.Cloud platform domain name. For details about the parameter, see here.

https://iam.cn-north-1.myhuaweicloud.com/v3

ProjectID

Specifies the project ID. For details about how to obtain the project ID, see How Can I Obtain domain_name, project_name, and project_id?.

None

DomainID

Specifies the account ID. For details about how to obtain the account ID, see How Can I Obtain domain_name, project_name, and project_id?.

None

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel