Skip to content

Commit

Permalink
initial commit (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeiminds authored Oct 10, 2018
1 parent f3b1df5 commit 00b7ab8
Show file tree
Hide file tree
Showing 365 changed files with 32,550 additions and 2,297 deletions.
23 changes: 19 additions & 4 deletions .gitignore
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 added CHANGELOG.md
Empty file.
8 changes: 0 additions & 8 deletions Dockerfile

This file was deleted.

39 changes: 39 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Gopkg.toml
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
42 changes: 35 additions & 7 deletions Makefile
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/
30 changes: 28 additions & 2 deletions README.md
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)
68 changes: 68 additions & 0 deletions docs/Configure.md
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")
```
106 changes: 106 additions & 0 deletions docs/Usage.md
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)
}
}
```
Loading

0 comments on commit 00b7ab8

Please sign in to comment.