[REV]Update cloud auth show table format

This commit is contained in:
崔斌 2017-12-08 18:47:55 +08:00
parent c68f30daad
commit f0a43de3b9
3 changed files with 51 additions and 26 deletions

View File

@ -42,7 +42,7 @@ type DefineCloudAuth struct {
//DefineCloudAuthInterface DefineCloudAuthInterface
type DefineCloudAuthInterface interface {
GetToken() ([]byte, error)
PostToken() error
PostToken() ([]byte, error)
PutToken() error
}
@ -68,25 +68,25 @@ func (d *DefineCloudAuth) GetToken() ([]byte, error) {
}
//PostToken PostToken
func (d *DefineCloudAuth) PostToken() error {
func (d *DefineCloudAuth) PostToken() ([]byte, error) {
data, err := ffjson.Marshal(d.GT.Body)
if err != nil {
return err
return nil, err
}
_, status, err := DoRequest(
resp, status, err := DoRequest(
"/cloud/auth",
"POST",
data,
)
if err != nil {
logrus.Errorf("create auth token error, %v", err)
return err
return nil, err
}
if status > 400 {
logrus.Errorf("create auth token error")
return fmt.Errorf("cretae auth token failed")
return nil, fmt.Errorf("cretae auth token failed")
}
return nil
return resp, nil
}
//PutToken PutToken

View File

@ -26,11 +26,11 @@ func (t *RegionUserInfo) TableName() string {
//RegionUserInfo RegionUserInfo
type RegionUserInfo struct {
Model
EID string `gorm:"column:eid;size:34"`
APIRange string `gorm:"column:api_range;size:24"`
RegionTag string `gorm:"column:region_tag;size:24"`
ValidityPeriod int `gorm:"column:validity_period;size:10"`
Token string `gorm:"column:token;size:32"`
CA string `gorm:"column:ca;size:4096"`
Key string `gorm:"column:key;size:4096"`
EID string `gorm:"column:eid;size:34" json:"eid"`
APIRange string `gorm:"column:api_range;size:24" json:"api_range"`
RegionTag string `gorm:"column:region_tag;size:24" json:"region_tag"`
ValidityPeriod int `gorm:"column:validity_period;size:10" json:"validity_period"`
Token string `gorm:"column:token;size:32" json:"token"`
CA string `gorm:"column:ca;size:4096" json:"ca"`
Key string `gorm:"column:key;size:4096" json:"key"`
}

View File

@ -22,7 +22,11 @@ import (
"fmt"
"strconv"
"github.com/gosuri/uitable"
"github.com/pquerna/ffjson/ffjson"
api_model "github.com/goodrain/rainbond/pkg/api/model"
dbmodel "github.com/goodrain/rainbond/pkg/db/model"
"github.com/goodrain/rainbond/pkg/grctl/clients"
"github.com/urfave/cli"
)
@ -32,6 +36,10 @@ type CloudAuth struct {
c *cli.Context
}
type respBean struct {
Bean *dbmodel.RegionUserInfo `json:"bean"`
}
//NewCmdCloudAuth 云市授权相关操作
func NewCmdCloudAuth() cli.Command {
c := cli.Command{
@ -59,7 +67,7 @@ func NewCmdCloudAuth() cli.Command {
Name: "update",
Usage: "更新授权有效期。 grctl auth update -e EID -t VALIDITY_DATE",
Action: func(c *cli.Context) error {
return sourcesAction(c, "update")
return authAction(c, "update")
},
Flags: []cli.Flag{
cli.StringFlag{
@ -74,9 +82,9 @@ func NewCmdCloudAuth() cli.Command {
},
{
Name: "get",
Usage: "获取授权信息。 grctl sources get -e EID",
Usage: "获取授权信息。 grctl auth get -e EID",
Action: func(c *cli.Context) error {
return sourcesAction(c, "delete")
return authAction(c, "get")
},
Flags: []cli.Flag{
cli.StringFlag{
@ -119,11 +127,23 @@ func (ca *CloudAuth) createToken() error {
tt, _ := strconv.Atoi(ttl)
gt.Body.EID = eid
gt.Body.ValidityPeriod = tt
if err := clients.RegionClient.Tenants().DefineCloudAuth(&gt).PostToken(); err != nil {
resp, err := clients.RegionClient.Tenants().DefineCloudAuth(&gt).PostToken()
if err != nil {
fmt.Printf("create auth %s failure\n", gt.Body.EID)
return err
}
fmt.Printf("create auth %s success\n", gt.Body.EID)
var rb respBean
if err := ffjson.Unmarshal(resp, &rb); err != nil {
return err
}
table := uitable.New()
table.Wrap = true // wrap columns
fmt.Printf("-------------------------------------------------\n")
table.AddRow("EID:", rb.Bean.EID)
table.AddRow("TOKEN:", rb.Bean.Token)
table.AddRow("VALIDITY_DATE:", rb.Bean.ValidityPeriod)
table.AddRow("CA:", rb.Bean.CA)
fmt.Println(table)
return nil
}
@ -153,19 +173,24 @@ func (ca *CloudAuth) getToken() error {
if err != nil {
return err
}
ttl, err := checkoutKV(ca.c, "ttl")
if err != nil {
return err
}
var gt api_model.GetUserToken
tt, _ := strconv.Atoi(ttl)
gt.Body.EID = eid
gt.Body.ValidityPeriod = tt
resp, err := clients.RegionClient.Tenants().DefineCloudAuth(&gt).GetToken()
if err != nil {
fmt.Printf("get auth %s failure\n", gt.Body.EID)
return err
}
fmt.Printf("%v", resp)
var rb respBean
if err := ffjson.Unmarshal(resp, &rb); err != nil {
return err
}
table := uitable.New()
table.Wrap = true // wrap columns
fmt.Printf("-------------------------------------------------\n")
table.AddRow("EID:", rb.Bean.EID)
table.AddRow("TOKEN:", rb.Bean.Token)
table.AddRow("VALIDITY_DATE:", rb.Bean.ValidityPeriod)
table.AddRow("CA:", rb.Bean.CA)
fmt.Println(table)
return nil
}