mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-20 11:48:24 -06:00
rancher: parse Rancher client cli.json config file (#11658)
* rancher: parse Rancher client cli.json config file * Make it work
This commit is contained in:
parent
47bf2eff09
commit
c3042765a0
@ -1,18 +1,31 @@
|
||||
package rancher
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
type CLIConfig struct {
|
||||
AccessKey string `json:"accessKey"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
URL string `json:"url"`
|
||||
Environment string `json:"environment"`
|
||||
Path string `json:"path,omitempty"`
|
||||
}
|
||||
|
||||
// Provider returns a terraform.ResourceProvider.
|
||||
func Provider() terraform.ResourceProvider {
|
||||
return &schema.Provider{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"api_url": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("RANCHER_URL", nil),
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("RANCHER_URL", ""),
|
||||
Description: descriptions["api_url"],
|
||||
},
|
||||
"access_key": &schema.Schema{
|
||||
@ -27,6 +40,12 @@ func Provider() terraform.ResourceProvider {
|
||||
DefaultFunc: schema.EnvDefaultFunc("RANCHER_SECRET_KEY", ""),
|
||||
Description: descriptions["secret_key"],
|
||||
},
|
||||
"config": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("RANCHER_CLIENT_CONFIG", ""),
|
||||
Description: descriptions["config"],
|
||||
},
|
||||
},
|
||||
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
@ -50,17 +69,64 @@ func init() {
|
||||
"secret_key": "API secret used to authenticate with the rancher server",
|
||||
|
||||
"api_url": "The URL to the rancher API",
|
||||
|
||||
"config": "Path to the Rancher client cli.json config file",
|
||||
}
|
||||
}
|
||||
|
||||
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
||||
apiURL := d.Get("api_url").(string)
|
||||
accessKey := d.Get("access_key").(string)
|
||||
secretKey := d.Get("secret_key").(string)
|
||||
|
||||
if configFile := d.Get("config").(string); configFile != "" {
|
||||
config, err := loadConfig(configFile)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
|
||||
if apiURL == "" {
|
||||
u, err := url.Parse(config.URL)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
apiURL = u.Scheme + "://" + u.Host
|
||||
}
|
||||
|
||||
if accessKey == "" {
|
||||
accessKey = config.AccessKey
|
||||
}
|
||||
|
||||
if secretKey == "" {
|
||||
secretKey = config.SecretKey
|
||||
}
|
||||
}
|
||||
|
||||
config := &Config{
|
||||
APIURL: d.Get("api_url").(string) + "/v1",
|
||||
AccessKey: d.Get("access_key").(string),
|
||||
SecretKey: d.Get("secret_key").(string),
|
||||
APIURL: apiURL + "/v1",
|
||||
AccessKey: accessKey,
|
||||
SecretKey: secretKey,
|
||||
}
|
||||
|
||||
_, err := config.GlobalClient()
|
||||
|
||||
return config, err
|
||||
}
|
||||
|
||||
func loadConfig(path string) (CLIConfig, error) {
|
||||
config := CLIConfig{
|
||||
Path: path,
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return config, nil
|
||||
} else if err != nil {
|
||||
return config, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &config)
|
||||
config.Path = path
|
||||
|
||||
return config, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user