2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-09-26 18:58:15 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
|
|
|
|
hcpcfg "github.com/hashicorp/hcp-sdk-go/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CloudConfig defines configuration for connecting to HCP services
|
|
|
|
type CloudConfig struct {
|
|
|
|
ResourceID string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
Hostname string
|
|
|
|
AuthURL string
|
2022-10-13 09:31:28 +00:00
|
|
|
ScadaAddress string
|
2023-04-27 20:27:39 +00:00
|
|
|
|
|
|
|
// Management token used by HCP management plane.
|
|
|
|
// Cannot be set via config files.
|
|
|
|
ManagementToken string
|
|
|
|
|
|
|
|
// TlsConfig for testing.
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudConfig) WithTLSConfig(cfg *tls.Config) {
|
|
|
|
c.TLSConfig = cfg
|
2022-09-26 18:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudConfig) HCPConfig(opts ...hcpcfg.HCPConfigOption) (hcpcfg.HCPConfig, error) {
|
2023-04-27 20:27:39 +00:00
|
|
|
if c.TLSConfig == nil {
|
|
|
|
c.TLSConfig = &tls.Config{}
|
|
|
|
}
|
2022-09-26 18:58:15 +00:00
|
|
|
if c.ClientID != "" && c.ClientSecret != "" {
|
|
|
|
opts = append(opts, hcpcfg.WithClientCredentials(c.ClientID, c.ClientSecret))
|
|
|
|
}
|
|
|
|
if c.AuthURL != "" {
|
2023-04-27 20:27:39 +00:00
|
|
|
opts = append(opts, hcpcfg.WithAuth(c.AuthURL, c.TLSConfig))
|
2022-09-26 18:58:15 +00:00
|
|
|
}
|
|
|
|
if c.Hostname != "" {
|
2023-04-27 20:27:39 +00:00
|
|
|
opts = append(opts, hcpcfg.WithAPI(c.Hostname, c.TLSConfig))
|
2022-09-26 18:58:15 +00:00
|
|
|
}
|
2022-10-13 09:31:28 +00:00
|
|
|
if c.ScadaAddress != "" {
|
2023-04-27 20:27:39 +00:00
|
|
|
opts = append(opts, hcpcfg.WithSCADA(c.ScadaAddress, c.TLSConfig))
|
2022-10-13 09:31:28 +00:00
|
|
|
}
|
2022-09-26 18:58:15 +00:00
|
|
|
opts = append(opts, hcpcfg.FromEnv())
|
|
|
|
return hcpcfg.NewHCPConfig(opts...)
|
|
|
|
}
|