/ucloud-sdk-go

golang sdk for ucloud api

Primary LanguageGoApache License 2.0Apache-2.0

UCloud Go SDK

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

UCloud Logo

UCloud SDK is a Go client library for accessing the UCloud API.

This client can run on Linux, macOS and Windows. It requires Golang 1.10.x and above.

Install

via source

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

via dep

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

Usage

import (
   "github.com/ucloud/ucloud-sdk-go"
)

Create a new UCloud client of each service to access the different parts of the UCloud API.

Authentication

Currently, user public key & private key is the only method of authenticating with the API. You could get your keys here: Key Generation

You can then use your keys to create a new client of uhost service:

package main

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

func loadConfig() (*ucloud.Config, *auth.Credential) {
    cfg := ucloud.NewConfig()

    credential := auth.NewCredential()
    credential.PrivateKey ="my_privatekey"
    credential.PublicKey = "my_publickey"

    return &cfg, &credential
}

func main() {
    cfg, credential := loadConfig()
    uhostClient := uhost.NewClient(cfg, credential)
}

Quick Start

To create a new uhost:

// build Request
req := uhostClient.NewCreateUHostInstanceRequest()
req.Name       = ucloud.String("sdk-example-uhost")
req.Zone       = ucloud.String("cn-bj2-05")
req.ImageId    = ucloud.String("uimage-ixczxu")
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)
}

fmt.Printf("resource id of the uhost: %s\n", newUHost.UHostIds[0])

Note

UHost created above cannot be accessed via Internet unless an EIP is created and bind to the UHost.

Complex structure in query

You can also set array as query, such as:

req.UHostIds = []string{"uhost-xxx", "uhost-yyy"}

will encoded as UHostIds.0=uhost-xxx&UHostIds.1=uhost-yyy

dataDisk := uhost.UHostDisk{
    Size: ucloud.Int(20),
    Type: ucloud.String("CLOUD_NORMAL"),
    IsBoot: ucloud.Bool(false),
}
req.Disks = []uhost.UHostDisk{dataDisk}

will encoded as Disks.0.Size=20&Disks.0.Type=CLOUD_NORMAL&Disks.0.IsBoot=false

Docs

Feedback & Contribution