github.com/openshift/installer@v1.4.17/pkg/asset/installconfig/ovirt/config.go (about)

     1  package ovirt
     2  
     3  import (
     4  	"crypto/x509"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	ovirtsdk "github.com/ovirt/go-ovirt"
     9  	"github.com/pkg/errors"
    10  	"gopkg.in/yaml.v2"
    11  )
    12  
    13  var defaultOvirtConfigEnvVar = "OVIRT_CONFIG"
    14  var defaultOvirtConfigPath = filepath.Join(os.Getenv("HOME"), ".ovirt", "ovirt-config.yaml")
    15  
    16  // Config holds oVirt api access details
    17  type Config struct {
    18  	URL      string `yaml:"ovirt_url"`
    19  	FQDN     string `yaml:"ovirt_fqdn"`
    20  	PemURL   string `yaml:"ovirt_pem_url"`
    21  	Username string `yaml:"ovirt_username"`
    22  	Password string `yaml:"ovirt_password"`
    23  	CAFile   string `yaml:"ovirt_cafile,omitempty"`
    24  	Insecure bool   `yaml:"ovirt_insecure,omitempty"`
    25  	CABundle string `yaml:"ovirt_ca_bundle,omitempty"`
    26  }
    27  
    28  // clientHTTP struct - Hold info about http calls
    29  type clientHTTP struct {
    30  	saveFilePath string // Path for saving file (GET method)
    31  	urlAddr      string // URL or Address
    32  	skipVerify   bool   // skipt cert validatin in the http call
    33  	certPool     *x509.CertPool
    34  }
    35  
    36  // LoadOvirtConfig from the following location (first wins):
    37  // 1. OVIRT_CONFIG env variable
    38  // 2  $defaultOvirtConfigPath
    39  // See #@Config for the expected format
    40  func LoadOvirtConfig() ([]byte, error) {
    41  	data, err := os.ReadFile(discoverPath())
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return data, nil
    46  }
    47  
    48  // NewConfig will return an Config by loading
    49  // the configuration from locations specified in @LoadOvirtConfig
    50  func NewConfig() (Config, error) {
    51  	c := Config{}
    52  	in, err := LoadOvirtConfig()
    53  	if err != nil {
    54  		return c, err
    55  	}
    56  
    57  	err = yaml.Unmarshal(in, &c)
    58  	if err != nil {
    59  		return c, err
    60  	}
    61  
    62  	return c, nil
    63  }
    64  
    65  func discoverPath() string {
    66  	path, _ := os.LookupEnv(defaultOvirtConfigEnvVar)
    67  	if path != "" {
    68  		return path
    69  	}
    70  
    71  	return defaultOvirtConfigPath
    72  }
    73  
    74  // Save will serialize the config back into the locations
    75  // specified in @LoadOvirtConfig, first location with a file, wins.
    76  func (c *Config) Save() error {
    77  	out, err := yaml.Marshal(c)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	path := discoverPath()
    83  	err = os.MkdirAll(filepath.Dir(path), 0700)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	return os.WriteFile(path, out, 0o600)
    88  }
    89  
    90  // getValidatedConnection will create a connection and validate it before returning.
    91  func (c *Config) getValidatedConnection() (*ovirtsdk.Connection, error) {
    92  	connection, err := getConnection(*c)
    93  	if err != nil {
    94  		return nil, errors.Wrap(err, "failed to build configuration for ovirt connection validation")
    95  	}
    96  
    97  	if err := connection.Test(); err != nil {
    98  		_ = connection.Close()
    99  		return nil, err
   100  	}
   101  	return connection, nil
   102  }