-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3b1df5
commit 00b7ab8
Showing
365 changed files
with
32,550 additions
and
2,297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
.idea/ | ||
*.iml | ||
*.example | ||
# Temporary | ||
.*.swp | ||
.idea | ||
/.vs | ||
.DS_Store | ||
core.* | ||
debug | ||
debug.test | ||
.vscode | ||
|
||
uhost_example | ||
# Executable | ||
bin/ | ||
*.dll | ||
*.exe | ||
|
||
# Build | ||
coverage.out | ||
|
||
# Test Data | ||
# autotest/set_* |
Empty file.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Gopkg.toml example | ||
# | ||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
# | ||
# [prune] | ||
# non-go = false | ||
# go-tests = true | ||
# unused-packages = true | ||
|
||
|
||
[prune] | ||
go-tests = true | ||
unused-packages = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,37 @@ | ||
.PHONY: test example | ||
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) | ||
|
||
default: test | ||
|
||
test: | ||
script/test | ||
.PHONY: help | ||
help: | ||
@echo "fmt re-format source codes." | ||
@echo "build build binary from source code as './bin/ucloud-cli'." | ||
@echo "test run unit test cases." | ||
@echo "test-acc run acc test cases." | ||
@echo "test-cov run unit test cases with coverage reporting." | ||
|
||
example: | ||
script/example | ||
.PHONY: fmt | ||
fmt: | ||
gofmt -w $(GOFMT_FILES) | ||
|
||
.PHONY: fmtcheck | ||
fmtcheck: | ||
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'" | ||
|
||
.PHONY: vet | ||
vet: | ||
go vet ./... | ||
|
||
.PHONY: test | ||
test: fmtcheck vet | ||
go test -v ./ucloud/... --parallel=4 | ||
|
||
.PHONY: test-acc | ||
test-acc: fmtcheck vet | ||
go test -v ./tests/... --parallel=32 | ||
|
||
.PHONY: test-cov | ||
test-cov: fmtcheck vet | ||
go test -cover -coverprofile=coverage.out ./... --parallel=32 | ||
|
||
.PHONY: cyclo | ||
cyclo: | ||
gocyclo -over 10 sdk/ service/ private/cli/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,28 @@ | ||
# goucloud | ||
golang sdk for ucloud api | ||
# UCloud Golang SDK | ||
|
||
UCloud Golang SDK is a toolkit for developer to create custom infrastructure via ucloud open api. | ||
|
||
## Install | ||
|
||
### via source | ||
|
||
```go | ||
go get github.com/ucloud/ucloud-sdk-go | ||
``` | ||
|
||
### via dep | ||
|
||
``` | ||
dep add github.com/ucloud/ucloud-sdk-go | ||
``` | ||
|
||
## Useful Docs | ||
|
||
- [Configure Document](./docs/Configure.md) | ||
- [Usage Document](./docs/Usage.md) | ||
- *Retry Policy (wait for writing)* | ||
|
||
## Feedback && Contributing | ||
|
||
- [Issue](https://github.com/ucloud/ucloud-sdk-go/issues) | ||
- [Pull Request](https://github.com/ucloud/ucloud-sdk-go/pulls) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
## Configure SDK | ||
|
||
### Client Config | ||
|
||
client config can control many common behaviors of sdk, it is required for any client. | ||
|
||
#### Common Request Fields | ||
|
||
To set common region and project id for any request, you can also overwrite it by request. | ||
|
||
```go | ||
cfg := ucloud.NewConfig() | ||
|
||
cfg.Region = "cn-bj2" | ||
cfg.ProjectId = "xxx" | ||
``` | ||
|
||
#### Auto Retry | ||
|
||
To enable auto-retry for any request, default is disabled(max retries is 0), it will auto retry for | ||
|
||
* network error | ||
* server error with http status 429, 502, 503 and 504 | ||
|
||
```go | ||
cfg.MaxRetries = 3 | ||
``` | ||
|
||
#### Logging | ||
|
||
To set log level, you can see log with different level on **stdout**, you can also use it to enable or disable log. | ||
|
||
```go | ||
// disable sdk log, default is log.InfoLevel | ||
cfg.LogLevel = log.PanicLevel | ||
|
||
// enable sdk debug log level | ||
// if debug log is enable, it will print all of request params and response body | ||
cfg.LogLevel = log.DebugLevel | ||
``` | ||
|
||
#### Timeout | ||
|
||
To set timeout for any network request, default is 30s. | ||
|
||
```go | ||
cfg.Timeout = 30 * time.Second | ||
``` | ||
|
||
#### Internal Usage | ||
|
||
the followed configuration should not be set in general usage. | ||
|
||
To set User-Agent, you can append custom UserAgent for any request, it also used for setting up special channel for customer. | ||
|
||
```go | ||
cfg.UserAgent = "UCloud-CLI/0.1.0" | ||
``` | ||
|
||
### Credential Config | ||
|
||
To set credential info for any request, credential is required for any client | ||
```go | ||
credential := auth.NewCredential() | ||
|
||
credential.PublicKey = os.Getenv("UCLOUD_PUBLIC_KEY") | ||
credential.PrivateKey = os.Getenv("UCLOUD_PRIVATE_KEY") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
## Usage | ||
|
||
### New Client | ||
|
||
sdk client for any ucloud product openapi service should constructed by client config and credential config, | ||
|
||
```go | ||
import ( | ||
"github.com/ucloud/ucloud-sdk-go/services/uhost" | ||
) | ||
|
||
client := uhost.NewClient(&cfg, &credential) | ||
``` | ||
|
||
### Build Request | ||
|
||
```go | ||
req := client.NewCreateUHostInstanceRequest() | ||
|
||
req.Password = ucloud.String("xxx") | ||
req.LoginMode = ucloud.String("Password") | ||
req.Region = ucloud.String("cn-bj2") | ||
req.Zone = ucloud.String("cn-bj2-04") | ||
req.ImageId = ucloud.String("uimage-xxxx") | ||
req.CPU = ucloud.Int(1) | ||
req.Memory = ucloud.Int(1024) | ||
``` | ||
|
||
#### Request Timeout | ||
|
||
To set request timeout for current request, it will overwrite the client Timeout at current request only. | ||
|
||
```go | ||
req.WithTimeout(10 * time.Second) | ||
``` | ||
|
||
#### Request Retring | ||
|
||
To set request max retries > 0 to enable auto retry, it will overwrite the client MaxRetries at current request only. | ||
|
||
```go | ||
req.WithRetry(3) | ||
``` | ||
|
||
### Send Request | ||
|
||
```go | ||
resp, err := client.CreateUHostInstance(req) | ||
log.Printf("%#v", resp.UHostIds) | ||
``` | ||
|
||
### Error Handling | ||
|
||
#### General Usage | ||
|
||
In general usage, you can serialize error to string for free, use ``err.Error()`` or | ||
|
||
```go | ||
fmt.Printf("got error: %s", err) | ||
``` | ||
|
||
For bussiness error with RetCode > 0 at the body of server response, you can check it easily, such as | ||
|
||
```go | ||
if uerr.IsCodeError(err) { | ||
fmt.Printf("%v %s", e.Code(), e.Message()) | ||
} else { | ||
fmt.Printf("%s", e) | ||
} | ||
``` | ||
|
||
#### Advanced Usage | ||
|
||
In advanced usage, error could be infered ucloud sdk client error / server error, such as | ||
|
||
```go | ||
import ( | ||
uerr "github.com/ucloud/ucloud-sdk-go/ucloud/error" | ||
) | ||
|
||
if err != nil { | ||
switch err.(type) { | ||
case uerr.(ClientError): | ||
fmt.Printf("client error: %s", err) | ||
case uerr.(ServerError): | ||
fmt.Printf("server error: %s", err) | ||
} | ||
} | ||
``` | ||
|
||
or enum by error name | ||
|
||
```go | ||
import ( | ||
uerr "github.com/ucloud/ucloud-sdk-go/ucloud/error" | ||
) | ||
|
||
if e, ok := err.(uerr.Error); err != nil && ok { | ||
switch e.Name() { | ||
case uerr.ErrRetCode: | ||
fmt.Printf("%v %s", e.Code(), e.Message()) | ||
default: | ||
fmt.Printf("%s", e) | ||
} | ||
} | ||
``` |
Oops, something went wrong.