sdk

package module
v0.22.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

README

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

Documentation

Overview

Package sdk is a toolkit for developer to create custom infrastructure via ucloud open api.

Directories

Path Synopsis
examples
internal
reverse
this helper function to reverse the sdk services to json
this helper function to reverse the sdk services to json
services/ipsecvpn
Package ipsecvpn include resources of ucloud ipsecvpn product
Package ipsecvpn include resources of ucloud ipsecvpn product
services/ufile
Package ufile include resources of ucloud ufile product
Package ufile include resources of ucloud ufile product
services/vpc
Package vpc include resources of ucloud vpc product
Package vpc include resources of ucloud vpc product
private
protocol/http
Package http is an implementation of http protocol
Package http is an implementation of http protocol
services/ulb
Package ulb include resources of ucloud ulb product
Package ulb include resources of ucloud ulb product
services/vpc
Package vpc include resources of ucloud vpc 2.0 product
Package vpc include resources of ucloud vpc 2.0 product
utils
Package utils is the utilities to process internal data of sdk
Package utils is the utilities to process internal data of sdk
services
cube
Package cube include resources of ucloud cube product
Package cube include resources of ucloud cube product
iam
Package iam include resources of ucloud iam product
Package iam include resources of ucloud iam product
ipsecvpn
Package ipsecvpn include resources of ucloud ipsecvpn product
Package ipsecvpn include resources of ucloud ipsecvpn product
isms
Package isms include resources of ucloud isms product
Package isms include resources of ucloud isms product
label
Package label include resources of ucloud label product
Package label include resources of ucloud label product
nlb
Package nlb include resources of ucloud nlb product
Package nlb include resources of ucloud nlb product
pathx
Package pathx include resources of ucloud pathx product
Package pathx include resources of ucloud pathx product
stepflow
Package stepflow include resources of ucloud stepflow product
Package stepflow include resources of ucloud stepflow product
sts
Package sts include resources of ucloud sts product
Package sts include resources of ucloud sts product
tidb
Package tidb include resources of ucloud tidb product
Package tidb include resources of ucloud tidb product
uaccount
Package uaccount include resources of ucloud uaccount product
Package uaccount include resources of ucloud uaccount product
uads
Package uads include resources of ucloud uads product
Package uads include resources of ucloud uads product
uai_modelverse
Package uai_modelverse include resources of ucloud uai_modelverse product
Package uai_modelverse include resources of ucloud uai_modelverse product
ubill
Package ubill include resources of ucloud ubill product
Package ubill include resources of ucloud ubill product
ubox
Package ubox include resources of ucloud ubox product
Package ubox include resources of ucloud ubox product
ucdn
Package ucdn include resources of ucloud ucdn product
Package ucdn include resources of ucloud ucdn product
ucloudstack
Package ucloudstack include resources of ucloud ucloudstack product
Package ucloudstack include resources of ucloud ucloudstack product
ucompshare
Package ucompshare include resources of ucloud ucompshare product
Package ucompshare include resources of ucloud ucompshare product
udb
Package udb include resources of ucloud udb product
Package udb include resources of ucloud udb product
udbproxy
Package udbproxy include resources of ucloud udbproxy product
Package udbproxy include resources of ucloud udbproxy product
uddb
Package uddb include resources of ucloud uddb product
Package uddb include resources of ucloud uddb product
udi
Package udi include resources of ucloud udi product
Package udi include resources of ucloud udi product
udisk
Package udisk include resources of ucloud udisk product
Package udisk include resources of ucloud udisk product
udns
Package udns include resources of ucloud udns product
Package udns include resources of ucloud udns product
udpn
Package udpn include resources of ucloud udpn product
Package udpn include resources of ucloud udpn product
udts
Package udts include resources of ucloud udts product
Package udts include resources of ucloud udts product
uec
Package uec include resources of ucloud uec product
Package uec include resources of ucloud uec product
ufile
Package ufile include resources of ucloud ufile product
Package ufile include resources of ucloud ufile product
ufs
Package ufs include resources of ucloud ufs product
Package ufs include resources of ucloud ufs product
ugn
Package ugn include resources of ucloud ugn product
Package ugn include resources of ucloud ugn product
uhost
Package uhost include resources of ucloud uhost product
Package uhost include resources of ucloud uhost product
uhub
Package uhub include resources of ucloud uhub product
Package uhub include resources of ucloud uhub product
uk8s
Package uk8s include resources of ucloud uk8s product
Package uk8s include resources of ucloud uk8s product
ulb
Package ulb include resources of ucloud ulb product
Package ulb include resources of ucloud ulb product
umedia
Package umedia include resources of ucloud umedia product
Package umedia include resources of ucloud umedia product
umem
Package umem include resources of ucloud umem product
Package umem include resources of ucloud umem product
umongodb
Package umongodb include resources of ucloud umongodb product
Package umongodb include resources of ucloud umongodb product
unet
Package unet include resources of ucloud unet product
Package unet include resources of ucloud unet product
unvs
Package unvs include resources of ucloud unvs product
Package unvs include resources of ucloud unvs product
upgsql
Package upgsql include resources of ucloud upgsql product
Package upgsql include resources of ucloud upgsql product
uphone
Package uphone include resources of ucloud uphone product
Package uphone include resources of ucloud uphone product
uphost
Package uphost include resources of ucloud uphost product
Package uphost include resources of ucloud uphost product
urocketmq
Package urocketmq include resources of ucloud urocketmq product
Package urocketmq include resources of ucloud urocketmq product
uslk
Package uslk include resources of ucloud uslk product
Package uslk include resources of ucloud uslk product
usms
Package usms include resources of ucloud usms product
Package usms include resources of ucloud usms product
utsdb
Package utsdb include resources of ucloud utsdb product
Package utsdb include resources of ucloud utsdb product
uvms
Package uvms include resources of ucloud uvms product
Package uvms include resources of ucloud uvms product
vpc
Package vpc include resources of ucloud vpc product
Package vpc include resources of ucloud vpc product
Package ucloud is a package of utilities to setup ucloud sdk and improve using experience
Package ucloud is a package of utilities to setup ucloud sdk and improve using experience
auth
Package auth is the credential utilities of sdk
Package auth is the credential utilities of sdk
error
Package uerr is the error definition of service and sdk
Package uerr is the error definition of service and sdk
helpers/waiter
Package waiter is a helper package use for waiting remote state is transformed into target state.
Package waiter is a helper package use for waiting remote state is transformed into target state.
log
Package log is the log utilities of sdk
Package log is the log utilities of sdk
request
Package request is the request of service
Package request is the request of service
response
Package response is the response of service
Package response is the response of service

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL