psu/common/client.go

52 lines
1.2 KiB
Go
Raw Normal View History

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