2019-08-02 02:48:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
2019-08-03 00:25:22 +00:00
|
|
|
|
|
|
|
"github.com/greenled/portainer-stack-utils/client"
|
2019-08-02 02:48:38 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2019-08-03 00:25:22 +00:00
|
|
|
var cachedClient client.PortainerClient
|
2019-08-02 02:48:38 +00:00
|
|
|
|
2019-08-02 17:21:29 +00:00
|
|
|
// Get the cached client or a new one
|
2019-08-03 00:25:22 +00:00
|
|
|
func GetClient() (c client.PortainerClient, err error) {
|
2019-08-02 20:32:26 +00:00
|
|
|
if cachedClient == nil {
|
2019-08-02 22:20:18 +00:00
|
|
|
cachedClient, err = GetDefaultClient()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cachedClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default client
|
2019-08-03 00:25:22 +00:00
|
|
|
func GetDefaultClient() (c client.PortainerClient, err error) {
|
|
|
|
return client.NewClient(GetDefaultHttpClient(), GetDefaultClientConfig())
|
2019-08-02 22:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default config for a client
|
2019-08-03 00:28:48 +00:00
|
|
|
func GetDefaultClientConfig() client.Config {
|
|
|
|
return client.Config{
|
2019-08-02 22:20:18 +00:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2019-08-02 02:48:38 +00:00
|
|
|
}
|
|
|
|
}
|