mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Add flexibility to Portainer client creation
This commit is contained in:
parent
a0d75c8df9
commit
cdc54111a1
10
cmd/login.go
10
cmd/login.go
@ -14,12 +14,10 @@ var loginCmd = &cobra.Command{
|
||||
Short: "Log in to a Portainer instance",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Get auth token
|
||||
client, err := common.NewClient(common.ClientConfig{
|
||||
Url: viper.GetString("url"),
|
||||
User: viper.GetString("user"),
|
||||
Password: viper.GetString("password"),
|
||||
DoNotUseToken: true,
|
||||
})
|
||||
config := common.GetDefaultClientConfig()
|
||||
config.DoNotUseToken = true
|
||||
|
||||
client, err := common.NewClient(common.GetDefaultHttpClient(), config)
|
||||
common.CheckError(err)
|
||||
|
||||
authToken, err := client.Authenticate()
|
||||
|
@ -11,7 +11,6 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@ -24,12 +23,10 @@ type ClientConfig struct {
|
||||
Password string
|
||||
Token string
|
||||
DoNotUseToken bool
|
||||
Insecure bool
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type PortainerClient struct {
|
||||
http.Client
|
||||
httpClient *http.Client
|
||||
url *url.URL
|
||||
user string
|
||||
password string
|
||||
@ -93,7 +90,7 @@ func (n *PortainerClient) do(uri, method string, request io.Reader, requestType
|
||||
|
||||
PrintDebugRequest("Request", req)
|
||||
|
||||
resp, err = n.Do(req)
|
||||
resp, err = n.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -264,6 +261,7 @@ func (n *PortainerClient) GetStatus() (status Status, err error) {
|
||||
// Get a clone of the client
|
||||
func (n *PortainerClient) Clone() (c *PortainerClient) {
|
||||
c = &PortainerClient{
|
||||
httpClient: n.httpClient,
|
||||
url: n.url,
|
||||
user: n.user,
|
||||
password: n.password,
|
||||
@ -271,48 +269,62 @@ func (n *PortainerClient) Clone() (c *PortainerClient) {
|
||||
doNotUseToken: n.doNotUseToken,
|
||||
}
|
||||
|
||||
c.Timeout = n.Timeout
|
||||
|
||||
c.Transport = n.Transport
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 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/")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
c = &PortainerClient{
|
||||
httpClient: httpClient,
|
||||
url: apiUrl,
|
||||
user: config.User,
|
||||
password: config.Password,
|
||||
token: config.Token,
|
||||
}
|
||||
|
||||
c.Timeout = config.Timeout
|
||||
|
||||
c.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: config.Insecure,
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Get the cached client or a new one
|
||||
func GetClient() (c *PortainerClient, err error) {
|
||||
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"),
|
||||
User: viper.GetString("user"),
|
||||
Password: viper.GetString("password"),
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user