Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fake mode enabled when non of AK,SK,HOST,REGION specified #3

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions hack/gen_client/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,23 @@ import (

// {{.Service}} is a base client
type {{.Service}} struct {
Client *client.Client
Client client.Client
}

// NewAPIClient returns an api client object
func NewAPIClient(ak, sk, host, service, region string) *{{.Service}} {
c := client.NewBaseClient()
c.ServiceInfo = client.NewServiceInfo()
c.ServiceInfo.Host = host
c.ServiceInfo.Credentials.AccessKeyID = ak
c.ServiceInfo.Credentials.SecretAccessKey = sk
c.ServiceInfo.Credentials.Service = service
c.ServiceInfo.Credentials.Region = region
c.SdkVersion = client.DefaultSdkVersion
func NewAPIClient(ak, sk, host, service, region string, fake bool) *{{.Service}} {
if fake {
return &{{.Service}}{Client: client.NewFakeClient()}
}

serviceInfo := client.NewServiceInfo()
serviceInfo.Host = host
serviceInfo.Credentials.AccessKeyID = ak
serviceInfo.Credentials.SecretAccessKey = sk
serviceInfo.Credentials.Service = service
serviceInfo.Credentials.Region = region

c := client.NewBaseClient(client.DefaultSdkVersion, serviceInfo)

return &{{.Service}}{Client: c}
}
Expand Down
27 changes: 20 additions & 7 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ vkectl resource GetCluster`,

data := rootCmd.PersistentFlags().StringP("data", "d", "", "json data of action")
verbose := rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
indent := rootCmd.PersistentFlags().BoolP("indent", "i", false, "indent output")

ak := os.Getenv("AK")
sk := os.Getenv("SK")
host := os.Getenv("HOST")
region := os.Getenv("REGION")
var fake bool

if ak == "" && sk == "" && host == "" && region == "" {
fake = true
fmt.Println("non of AK, SK, HOST, REGION specified, fake mode enabled")
}

addModuleCmd := func(module string, client interface{}) {
cmd := &cobra.Command{
Expand All @@ -49,16 +56,16 @@ vkectl resource GetCluster`,
actionCmd := &cobra.Command{
Use: mtpe.Name,
PreRunE: func(cmd *cobra.Command, args []string) error {
if ak == "" {
if !fake && ak == "" {
return errors.New("AK is not set, can set by \"export AK=YOUR AK\"")
}
if sk == "" {
if !fake && sk == "" {
return errors.New("SK is not set, can set by \"export SK=YOUR SK\"")
}
if host == "" {
if !fake && host == "" {
return errors.New("HOST is not set, can set by \"export AK=YOUR HOST\"")
}
if region == "" {
if !fake && region == "" {
return errors.New("REGION is not set, can set by \"export REGION=YOUR REGION\"")
}
return nil
Expand All @@ -73,7 +80,13 @@ vkectl resource GetCluster`,
if err, ok := ret[2].Interface().(error); ok && err != nil {
return errors.WithMessage(err, "call api")
} else {
output, err := json.MarshalIndent(ret[0].Interface(), "", "\t")
var output []byte
var err error
if *indent {
output, err = json.MarshalIndent(ret[0].Interface(), "", "\t")
} else {
output, err = json.Marshal(ret[0].Interface())
}
if err != nil {
return errors.WithMessage(err, "marshal output")
}
Expand All @@ -87,8 +100,8 @@ vkectl resource GetCluster`,
rootCmd.AddCommand(cmd)
}

addModuleCmd("resource", resource.NewAPIClient(ak, sk, host, "vke", region))
addModuleCmd("security", security.NewAPIClient(ak, sk, host, "vke", region))
addModuleCmd("resource", resource.NewAPIClient(ak, sk, host, "vke", region, fake))
addModuleCmd("security", security.NewAPIClient(ak, sk, host, "vke", region, fake))

rootCmd.AddCommand(
&cobra.Command{
Expand Down
39 changes: 20 additions & 19 deletions pkg/client/baseclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ const (
DefaultSdkVersion = "2021-03-03"
)

// Client defines base client struct
type Client struct {
type baseClient struct {
Client http.Client
SdkVersion string
ServiceInfo *base.ServiceInfo
sdkVersion string
serviceInfo *base.ServiceInfo
headers http.Header
}

// NewServiceInfo return base ServiceInfo.
// NewServiceInfo return base serviceInfo.
func NewServiceInfo() *base.ServiceInfo {
return &base.ServiceInfo{
Timeout: 120 * time.Second, // TOP gateway timeout
Expand All @@ -41,17 +40,19 @@ func NewServiceInfo() *base.ServiceInfo {
}
}

// NewBaseClient return a base Client
func NewBaseClient() *Client {
// NewBaseClient return a base baseClient
func NewBaseClient(sdkVersion string, serviceInfo *base.ServiceInfo) Client {
transport := &http.Transport{
MaxIdleConns: 1000,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 10 * time.Second,
}

c := &Client{
Client: http.Client{Transport: transport},
headers: http.Header{},
c := &baseClient{
Client: http.Client{Transport: transport},
sdkVersion: sdkVersion,
serviceInfo: serviceInfo,
headers: http.Header{},
}

header := http.Header{
Expand All @@ -64,7 +65,7 @@ func NewBaseClient() *Client {
}

// AddExtraHeaders add custom headers into request
func (client *Client) AddExtraHeaders(h map[string]string) {
func (client *baseClient) AddExtraHeaders(h map[string]string) {
if client.headers == nil {
client.headers = http.Header{}
}
Expand All @@ -76,7 +77,7 @@ func (client *Client) AddExtraHeaders(h map[string]string) {
}

// CommonHandler handle http request and response.
func (client *Client) CommonHandler(action string, query url.Values, body string, resp interface{}) (int, error) {
func (client *baseClient) CommonHandler(action string, query url.Values, body string, resp interface{}) (int, error) {
respBody, statusCode, err := client.JSON(action, query, body)
if err != nil {
return statusCode, errors.WithMessage(err, "client send json")
Expand All @@ -98,18 +99,18 @@ func (client *Client) CommonHandler(action string, query url.Values, body string
}

// JSON send a post json request
func (client *Client) JSON(action string, query url.Values, body string) ([]byte, int, error) {
timeout := client.ServiceInfo.Timeout
func (client *baseClient) JSON(action string, query url.Values, body string) ([]byte, int, error) {
timeout := client.serviceInfo.Timeout

if query == nil {
query = url.Values{}
}
query.Add("Action", action)
query.Add("Version", client.SdkVersion)
query.Add("Version", client.sdkVersion)

u := url.URL{
Scheme: client.ServiceInfo.Scheme,
Host: client.ServiceInfo.Host,
Scheme: client.serviceInfo.Scheme,
Host: client.serviceInfo.Host,
Path: "/",
RawQuery: query.Encode(),
}
Expand All @@ -122,8 +123,8 @@ func (client *Client) JSON(action string, query url.Values, body string) ([]byte
return client.makeRequest(action, req, body, timeout)
}

func (client *Client) makeRequest(action string, req *http.Request, reqBody string, timeout time.Duration) ([]byte, int, error) {
req = client.ServiceInfo.Credentials.Sign(req)
func (client *baseClient) makeRequest(action string, req *http.Request, reqBody string, timeout time.Duration) ([]byte, int, error) {
req = client.serviceInfo.Credentials.Sign(req)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req = req.WithContext(ctx)
Expand Down
7 changes: 7 additions & 0 deletions pkg/client/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package client

import "net/url"

type Client interface {
CommonHandler(action string, query url.Values, body string, resp interface{}) (int, error)
}
18 changes: 18 additions & 0 deletions pkg/client/fakeclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package client

import (
"net/http"
"net/url"
)

// NewFakeClient return a fake client
func NewFakeClient() Client {
return &fakeClient{}
}

type fakeClient struct{}

// CommonHandler handle fake request and response.
func (client *fakeClient) CommonHandler(action string, query url.Values, body string, resp interface{}) (int, error) {
return int(http.StatusOK), nil
}
24 changes: 14 additions & 10 deletions pkg/client/resource/generated.resourceservice.go

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

24 changes: 14 additions & 10 deletions pkg/client/security/generated.securityservice.go

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