-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add state waiter and fix all acceptance tests (#5)
* initial commit * set default log level to warn level * update version * fix test filename * fix test set 687 * fix test set 302 * fix test set 687 * fix backend type error and support array of request * fix test set 1839 * fix test set 1840 * fix test set 1839 * fix test set 1839 * fix test set 1839 * add StateWaiter
- Loading branch information
1 parent
6385fcd
commit 175ad0c
Showing
25 changed files
with
477 additions
and
25 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
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
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
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,19 @@ | ||
# UCloud SDK Wait Example | ||
|
||
## What is the goal | ||
|
||
To enable state waiter to wait remote resource is completed. | ||
|
||
## Setup Environment | ||
|
||
```go | ||
export UCLOUD_PUBLIC_KEY="your public key" | ||
export UCLOUD_PRIVATE_KEY="your private key" | ||
export UCLOUD_PROJECT_ID="your project id" | ||
``` | ||
|
||
## How to run | ||
|
||
```sh | ||
go run main.go | ||
``` |
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,101 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/ucloud/ucloud-sdk-go/ucloud" | ||
"github.com/ucloud/ucloud-sdk-go/ucloud/auth" | ||
"github.com/ucloud/ucloud-sdk-go/ucloud/log" | ||
|
||
"github.com/ucloud/ucloud-sdk-go/ucloud/helpers/waiter" | ||
|
||
"github.com/ucloud/ucloud-sdk-go/services/uhost" | ||
) | ||
|
||
const region = "cn-bj2" | ||
const zone = "cn-bj2-05" | ||
const imageID = "uimage-kg0w4u" | ||
|
||
var uhostClient *uhost.UHostClient | ||
|
||
func init() { | ||
cfg := ucloud.NewConfig() | ||
cfg.LogLevel = log.DebugLevel | ||
cfg.Region = region | ||
cfg.ProjectId = os.Getenv("UCLOUD_PROJECT_ID") | ||
|
||
credential := auth.NewCredential() | ||
credential.PrivateKey = os.Getenv("UCLOUD_PRIVATE_KEY") | ||
credential.PublicKey = os.Getenv("UCLOUD_PUBLIC_KEY") | ||
|
||
uhostClient = uhost.NewClient(&cfg, &credential) | ||
|
||
log.Info("setup clients ...") | ||
} | ||
|
||
func main() { | ||
uhostID, err := createUHost("sdk-example-wait") | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
|
||
w := waiter.StateWaiter{ | ||
Pending: []string{"pending"}, | ||
Target: []string{"avaliable"}, | ||
Refresh: func() (interface{}, string, error) { | ||
inst, err := describeUHostByID(uhostID) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
if inst == nil || inst.State != "Running" { | ||
return nil, "pending", nil | ||
} | ||
|
||
return inst, "avaliable", nil | ||
}, | ||
Timeout: 5 * time.Minute, | ||
} | ||
|
||
if resp, err := w.Wait(); err != nil { | ||
log.Error(err) | ||
} else { | ||
log.Infof("%#v", resp) | ||
} | ||
} | ||
|
||
func describeUHostByID(uhostID string) (*uhost.UHostInstanceSet, error) { | ||
req := uhostClient.NewDescribeUHostInstanceRequest() | ||
req.UHostIds = []string{uhostID} | ||
|
||
resp, err := uhostClient.DescribeUHostInstance(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(resp.UHostSet) < 1 { | ||
return nil, nil | ||
} | ||
|
||
return &resp.UHostSet[0], nil | ||
} | ||
|
||
func createUHost(name string) (string, error) { | ||
req := uhostClient.NewCreateUHostInstanceRequest() | ||
req.Name = ucloud.String(name) | ||
req.Zone = ucloud.String(zone) // TODO: use random zone | ||
req.ImageId = ucloud.String(imageID) // TODO: use random image | ||
req.LoginMode = ucloud.String("Password") | ||
req.Password = ucloud.String("somePassword_") | ||
req.ChargeType = ucloud.String("Dynamic") | ||
req.CPU = ucloud.Int(1) | ||
req.Memory = ucloud.Int(1024) | ||
req.Tag = ucloud.String("sdk-example") | ||
|
||
resp, err := uhostClient.CreateUHostInstance(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return resp.UHostIds[0], nil | ||
} |
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
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
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
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
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
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
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
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
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 |
---|---|---|
|
@@ -14,7 +14,7 @@ type ResourceInfo struct { | |
// 资源id | ||
ResourceId string | ||
|
||
// 创建时间 | ||
// 资源类型 | ||
ResourceType string | ||
|
||
// ip地址 | ||
|
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
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
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
File renamed without changes.
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
Oops, something went wrong.