Add flexibility to Portainer client creation

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-02 18:20:18 -04:00
parent a0d75c8df9
commit cdc54111a1
2 changed files with 46 additions and 36 deletions

View File

@ -14,12 +14,10 @@ var loginCmd = &cobra.Command{
Short: "Log in to a Portainer instance", Short: "Log in to a Portainer instance",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Get auth token // Get auth token
client, err := common.NewClient(common.ClientConfig{ config := common.GetDefaultClientConfig()
Url: viper.GetString("url"), config.DoNotUseToken = true
User: viper.GetString("user"),
Password: viper.GetString("password"), client, err := common.NewClient(common.GetDefaultHttpClient(), config)
DoNotUseToken: true,
})
common.CheckError(err) common.CheckError(err)
authToken, err := client.Authenticate() authToken, err := client.Authenticate()

View File

@ -11,7 +11,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -24,12 +23,10 @@ type ClientConfig struct {
Password string Password string
Token string Token string
DoNotUseToken bool DoNotUseToken bool
Insecure bool
Timeout time.Duration
} }
type PortainerClient struct { type PortainerClient struct {
http.Client httpClient *http.Client
url *url.URL url *url.URL
user string user string
password string password string
@ -93,7 +90,7 @@ func (n *PortainerClient) do(uri, method string, request io.Reader, requestType
PrintDebugRequest("Request", req) PrintDebugRequest("Request", req)
resp, err = n.Do(req) resp, err = n.httpClient.Do(req)
if err != nil { if err != nil {
return return
} }
@ -264,6 +261,7 @@ func (n *PortainerClient) GetStatus() (status Status, err error) {
// Get a clone of the client // Get a clone of the client
func (n *PortainerClient) Clone() (c *PortainerClient) { func (n *PortainerClient) Clone() (c *PortainerClient) {
c = &PortainerClient{ c = &PortainerClient{
httpClient: n.httpClient,
url: n.url, url: n.url,
user: n.user, user: n.user,
password: n.password, password: n.password,
@ -271,48 +269,62 @@ func (n *PortainerClient) Clone() (c *PortainerClient) {
doNotUseToken: n.doNotUseToken, doNotUseToken: n.doNotUseToken,
} }
c.Timeout = n.Timeout
c.Transport = n.Transport
return return
} }
// Create a new client // Create a new client
func NewClient(config ClientConfig) (c *PortainerClient, err error) { func NewClient(httpClient *http.Client, config ClientConfig) (c *PortainerClient, err error) {
apiUrl, err := url.Parse(strings.TrimRight(config.Url, "/") + "/api/") apiUrl, err := url.Parse(strings.TrimRight(config.Url, "/") + "/api/")
if err != nil { if err != nil {
return return
} }
c = &PortainerClient{ c = &PortainerClient{
httpClient: httpClient,
url: apiUrl, url: apiUrl,
user: config.User, user: config.User,
password: config.Password, password: config.Password,
token: config.Token, token: config.Token,
} }
c.Timeout = config.Timeout
c.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: config.Insecure,
},
}
return return
} }
// Get the cached client or a new one // Get the cached client or a new one
func GetClient() (c *PortainerClient, err error) { func GetClient() (c *PortainerClient, err error) {
if cachedClient == nil { if cachedClient == nil {
cachedClient, err = NewClient(ClientConfig{ cachedClient, err = GetDefaultClient()
if err != nil {
return
}
}
return cachedClient, nil
}
// Get the default client
func GetDefaultClient() (c *PortainerClient, err error) {
return NewClient(GetDefaultHttpClient(), GetDefaultClientConfig())
}
// Get the default config for a client
func GetDefaultClientConfig() ClientConfig {
return ClientConfig{
Url: viper.GetString("url"), Url: viper.GetString("url"),
User: viper.GetString("user"), User: viper.GetString("user"),
Password: viper.GetString("password"), Password: viper.GetString("password"),
Token: viper.GetString("auth-token"), Token: viper.GetString("auth-token"),
}) DoNotUseToken: false,
}
}
// Get the default http client for a Portainer client
func GetDefaultHttpClient() *http.Client {
return &http.Client{
Timeout: viper.GetDuration("timeout"),
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: viper.GetBool("insecure"),
},
},
} }
c = cachedClient
return
} }