- Website: https://www.ucloud.cn/
- Mailing list: Google Group
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.
go get github.com/ucloud/ucloud-sdk-go
dep ensure -add github.com/ucloud/ucloud-sdk-go
import {
"github.com/ucloud/ucloud-sdk-go"
}
Create a new UCloud client of each service to access the different parts of the UCloud API.
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/sdk"
"github.com/ucloud/ucloud-sdk-go/sdk/auth"
"github.com/ucloud/ucloud-sdk-go/services/uhost"
)
func loadConfig()(*sdk.Config, *auth.Credential){
cfg := sdk.NewConfig()
credential := auth.NewCredential()
credential.PrivateKey ="my_privatekey"
credential.PublicKey = "my_publickey"
return &cfg, &credential
}
func main(){
cfg, credentail := loadConfig()
uhostClient := uhost.Newclient(cfg, credential)
}
To create a new uhost:
# build Request
req := uhostClient.NewCreateUHostInstanceRequest()
req.Name = sdk.String("sdk-example-uhost")
req.Zone = sdk.String("cn-bj2-02")
req.ImageId = sdk.String("uimage-ixczxu")
req.LoginMode = sdk.String("Password")
req.Password = sdk.String("my_uhost_password")
req.ChargeType = sdk.String("Dynamic")
req.CPU = sdk.Int(1)
req.Memory = sdk.Int(1024)
req.Tag = sdk.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.