Skip to content

ucloud/ucloud-sdk-go

Repository files navigation

English | 简体中文

UCloud Go SDK

GitHub (pre-)release Go Report Card Codecov Build Status SonarQube GoDoc GitHub

Installation

Requirements

  • Go 1.10+

Use go get

go get github.com/ucloud/ucloud-sdk-go

Note if meet network problem, you can use go proxy to speed up the downloaded, eg: use GOPROXY environment variable

export GOPROXY=https://goproxy.io

Replay the command to retry installation.

Use go mod

Add the following snippet to any code.

import _ "github.com/ucloud/ucloud-sdk-go"

And execute this commands:

go mod init
go mod tidy

Note:If using go mod and Goland IDE together, please search vgo on Settings, and enable vgo support.

Note:If using go modGOPATH, notice the go mod init/tidy can not run with GOPATH, please move out current project from GOPATH.

Use dep

dep ensure -add github.com/ucloud/ucloud-sdk-go

First Using

Currently, Go SDK use PublicKey/PrivateKey as authentication method, the key can be found from:

Here is a simple example:

package main

import (
    "fmt"

    "github.com/ucloud/ucloud-sdk-go/ucloud"
    "github.com/ucloud/ucloud-sdk-go/ucloud/auth"
    "github.com/ucloud/ucloud-sdk-go/services/uhost"
)

func main() {
    cfg := ucloud.NewConfig()
    cfg.Region = "cn-bj2"

    // replace the public/private key by your own
    credential := auth.NewCredential()
    credential.PublicKey = "my_public_key"
    credential.PrivateKey = "my_private_key"

    uhostClient := uhost.NewClient(&cfg, &credential)

    req := uhostClient.NewCreateUHostInstanceRequest()
    req.Name       = ucloud.String("sdk-example-uhost")
    req.Zone       = ucloud.String("cn-bj2-05")
    req.ImageId    = ucloud.String("uimage-xxx") // you can replace the image with an available id
    req.LoginMode  = ucloud.String("Password")
    req.Password   = ucloud.String("my_uhost_password")
    req.ChargeType = ucloud.String("Dynamic")
    req.CPU        = ucloud.Int(1)
    req.Memory     = ucloud.Int(1024)
    req.Tag        = ucloud.String("sdk-example")

    // send request
    newUHost,err := uhostClient.CreateUHostInstance(req)
    if err != nil {
        fmt.Printf("something bad happened: %s\n", err)
    } else {
        fmt.Printf("resource id of the uhost: %s\n", newUHost.UHostIds[0])
    }
}

Replace the client configuration and host image id by your custom value, then you can create a cloud host。

At this example, you had already completed a CreateUHostInstance request by UCloud Go SDK, it is covered most of feature of sdk, you can write your own script for free!

Each API call in the SDK has a detailed comment and document, You can use Editor/IDE to jump to the specific API method code to view (can also to see Go Doc), and use completion of IDE and error logging to inspect usage of SDK。

If you are interested the advanced usage about above code example, please see the documentation:

  • Configuration, Learn how to configure SDK, such as Logging, Retrying, Service endpoint(Public Cloud & Private Cloud), and etc.
  • Error Handling, Learn how to resolve the several exception types, include parameters error, business error for RetCode is not 0.
  • Type System, Learn how to validate parameters, and normalize the response of API.
  • Request Middleware, Learn how to intercept request that applied by SDK, and add common logic to the lifecycle.
  • Toolbox & Helpers, The additional helpers, such as the poll function for waiting resource state.

More Examples

Examples based on Environment

SDK provided some example based on environment, and provide resource provisioning logic . You can click the following link to view details:

Examples based on Request

UCloud UAPI product provided examples based on request, you can fill request parameters and generate example of SDK, it can be copied immediately:

  • Open UAPI
  • Select your interested API, such as CreateUHostInstance
  • Fill parameters, copy the sdk example code at right panel
  • Save code into main.go
  • Executing go mod init main
  • Executing go mod tidy
  • go run ./main.go

Note if meet network problem, you can use go proxy to speed up the downloaded, eg: use GOPROXY environment variable

export GOPROXY=https://goproxy.io

Note:If using go mod and Goland IDE together, please search vgo on Settings, and enable vgo support.

Note:If using go modGOPATH, notice the go mod init/tidy can not run with GOPATH, please move out current project from GOPATH.

Feedback & Contribution