Skip to content

Commit

Permalink
Merge pull request #46 from qiniu/develop
Browse files Browse the repository at this point in the history
QC0S-0 merge develop into master
  • Loading branch information
nowenL authored Dec 16, 2016
2 parents 53cc577 + 7f36f17 commit 63799b0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# vNext
- Service SCALING 状态拆分为 SCALING-UP SCALING-DOWN
- 日志搜索结果添加CollectedAtNano字段

# Release 1.2.0
- 添加禁用/启用AP端口的API,并在查看/搜索AP的API返回的端口信息中返回端口的启用状态(启用/禁用)。
Expand Down
27 changes: 27 additions & 0 deletions kirksdk/account_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ type AccountClient interface {

// GetQcosClient 用于得到与某个 App 交互的 QcosClient
GetQcosClient(ctx context.Context, appURI string) (client QcosClient, err error)

// CreateAppGrant 将应用授权给用户
CreateAppGrant(ctx context.Context, appURI, username string) (err error)

// DeleteAppGrant 删除应用授权
DeleteAppGrant(ctx context.Context, appURI, username string) (err error)

// ListAppGrantedUsers 列出应用已授权的用户列表
ListAppGrantedUsers(ctx context.Context, appURI string) (ret []AppGrantedUser, err error)

// ListGrantedApps 列出已被授权的应用
ListGrantedApps(ctx context.Context) (ret []AppInfo, err error)

// GetGrantedAppKey 获取被授权应用的key
GetGrantedAppKey(ctx context.Context, appURI string) (ret GrantedAppKey, err error)
}

// AccountConfig 包含创建 AccountClient 所需的信息
Expand Down Expand Up @@ -178,3 +193,15 @@ type UpdateAlertMethodArgs struct {
Nationality string `json:"nationality"`
Code string `json:"code"`
}

// AppGrantedUser 包含列出应用被授权的用户信息
type AppGrantedUser struct {
ID uint32 `json:"id"`
Name string `json:"name"`
}

// GrantedAppKey 包含被授权应用的key信息
type GrantedAppKey struct {
Ak string `json:"ak"`
Sk string `json:"sk"`
}
82 changes: 76 additions & 6 deletions kirksdk/account_client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package kirksdk

import (
"errors"
"fmt"
"net/http"
"strings"

"golang.org/x/net/context"
"qiniupkg.com/kirk/kirksdk/mac"
Expand All @@ -11,6 +13,8 @@ import (

const appVersionPrefix = "/v3"

var ErrInvalidAppURI = errors.New("app uri is invalid")

type accountClientImp struct {
accessKey string
secretKey string
Expand Down Expand Up @@ -133,6 +137,36 @@ func (p *accountClientImp) UpdateAlertMethod(ctx context.Context, appURI string,
return
}

func (p *accountClientImp) CreateAppGrant(ctx context.Context, appURI, username string) (err error) {
url := fmt.Sprintf("%s%s/apps/%s/grant/%s", p.host, appVersionPrefix, appURI, username)
err = p.client.Call(ctx, nil, "PUT", url)
return
}

func (p *accountClientImp) DeleteAppGrant(ctx context.Context, appURI, username string) (err error) {
url := fmt.Sprintf("%s%s/apps/%s/grant/%s", p.host, appVersionPrefix, appURI, username)
err = p.client.Call(ctx, nil, "DELETE", url)
return
}

func (p *accountClientImp) ListAppGrantedUsers(ctx context.Context, appURI string) (ret []AppGrantedUser, err error) {
url := fmt.Sprintf("%s%s/apps/%s/grants", p.host, appVersionPrefix, appURI)
err = p.client.Call(ctx, &ret, "GET", url)
return
}

func (p *accountClientImp) ListGrantedApps(ctx context.Context) (ret []AppInfo, err error) {
url := fmt.Sprintf("%s%s/granted", p.host, appVersionPrefix)
err = p.client.Call(ctx, &ret, "GET", url)
return
}

func (p *accountClientImp) GetGrantedAppKey(ctx context.Context, appURI string) (ret GrantedAppKey, err error) {
url := fmt.Sprintf("%s%s/granted/%s/key", p.host, appVersionPrefix, appURI)
err = p.client.Call(ctx, &ret, "GET", url)
return
}

func (p *accountClientImp) GetIndexClient(ctx context.Context) (client IndexClient, err error) {
accountInfo, err := p.GetAccountInfo(ctx)
if err != nil {
Expand Down Expand Up @@ -163,12 +197,21 @@ func (p *accountClientImp) GetQcosClient(ctx context.Context, appURI string) (cl
err error
}

keyChan := make(chan keyResult)
endpointChan := make(chan endpointResult)
// app uri should follow the format: "username.appname"
// or it will return an invalid app uri
appURIParts := strings.Split(appURI, ".")
if len(appURIParts) < 2 {
return nil, ErrInvalidAppURI
}

// Get app access key & secret key
go func() {
var result keyResult
// check if app is granted
accountInfo, err := p.GetAccountInfo(ctx)
if err != nil {
return
}
isGranted := (accountInfo.Name != appURIParts[0])

getAppKeyFunc := func() (result keyResult) {
keyPairs, err := p.GetAppKeys(ctx, appURI)
if err != nil {
result.err = err
Expand All @@ -182,6 +225,33 @@ func (p *accountClientImp) GetQcosClient(ctx context.Context, appURI string) (cl
}
}
}
return
}

getGrantedAppKeyFunc := func() (result keyResult) {
keyPair, err := p.GetGrantedAppKey(ctx, appURI)
result.err = err
if err == nil {
result.ak = keyPair.Ak
result.sk = keyPair.Sk
}
return
}

// set up list apps and get key func
listAppsFunc := p.ListApps
getKeyFunc := getAppKeyFunc
if isGranted {
listAppsFunc = p.ListGrantedApps
getKeyFunc = getGrantedAppKeyFunc
}

keyChan := make(chan keyResult)
endpointChan := make(chan endpointResult)

// Get app access key & secret key
go func() {
result := getKeyFunc()

if result.ak == "" {
result.err = fmt.Errorf("Fail to find keys for app \"%s\"", appURI)
Expand All @@ -193,7 +263,7 @@ func (p *accountClientImp) GetQcosClient(ctx context.Context, appURI string) (cl
// Get qcos end point
go func() {
var result endpointResult
appInfos, err := p.ListApps(ctx)
appInfos, err := listAppsFunc(ctx)
if err != nil {
result.err = err
endpointChan <- result
Expand Down
16 changes: 9 additions & 7 deletions kirksdk/qcos_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ const (

const (
StateCreate = State("CREATING")
StateScaling = State("SCALING")
StateScalingUp = State("SCALING-UP")
StateScalingDown = State("SCALING-DOWN")
StateAutoUpdating = State("AUTO-UPDATING")
StateManualUpdating = State("MANUAL-UPDATING")
StateStarting = State("STARTING")
Expand Down Expand Up @@ -748,12 +749,13 @@ type LogsSearchResult struct {
}

type Hit struct {
Log string `json:"log"`
CollectedAt time.Time `json:"collectedAt"`
PodIP string `json:"podIp"`
ProcessName string `json:"processName"`
GateID string `json:"gateId"`
Domain string `json:"domain"`
Log string `json:"log"`
CollectedAt time.Time `json:"collectedAt"`
CollectedAtNano int64 `json:"collectedAtNano"`
PodIP string `json:"podIp"`
ProcessName string `json:"processName"`
GateID string `json:"gateId"`
Domain string `json:"domain"`
}

var (
Expand Down

0 comments on commit 63799b0

Please sign in to comment.