-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_config.go
More file actions
127 lines (105 loc) · 3.7 KB
/
Copy pathcloud_config.go
File metadata and controls
127 lines (105 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright 2025 Vates
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package xok8scommon provides shared Kubernetes integration helpers for Xen Orchestra
// components (CSI driver, Cloud Controller Manager, etc.).
package xok8scommon
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
yaml "gopkg.in/yaml.v3"
)
// XoConfig holds the Xen Orchestra connection configuration.
type XoConfig struct {
URL string `yaml:"url"`
Insecure bool `yaml:"insecure,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
Token string `yaml:"token,omitempty"`
ClientTimeout time.Duration `yaml:"clientTimeout,omitempty"`
}
func validateXOConfig(cfg XoConfig) (XoConfig, error) {
if cfg.Username != "" && cfg.Password != "" {
if cfg.Token != "" {
return XoConfig{}, fmt.Errorf("token is not allowed when username and password are set")
}
} else if cfg.Token == "" {
return XoConfig{}, fmt.Errorf("either token or username/password are required for authentication")
}
if cfg.URL == "" || !strings.HasPrefix(cfg.URL, "http") {
return XoConfig{}, fmt.Errorf("url is required")
}
return cfg, nil
}
// ReadCloudConfig reads and validates the XO cloud config from a reader.
func ReadCloudConfig(config io.Reader) (XoConfig, error) {
cfg := XoConfig{}
if config != nil {
if err := yaml.NewDecoder(config).Decode(&cfg); err != nil {
return XoConfig{}, err
}
}
return validateXOConfig(cfg)
}
// ReadCloudConfigFromFile reads and validates the XO cloud config from a file path.
func ReadCloudConfigFromFile(file string) (XoConfig, error) {
f, err := os.Open(filepath.Clean(file))
if err != nil {
return XoConfig{}, fmt.Errorf("error reading %s: %v", file, err)
}
defer f.Close() // nolint: errcheck
return ReadCloudConfig(f)
}
// LoadXOConfigFromEnv loads Xen Orchestra configuration from environment variables.
// It uses the same environment variables as the Xen Orchestra Go SDK:
// - XOA_URL: the base URL of the Xen Orchestra API (required)
// - XOA_TOKEN: the authentication token (optional if username/password provided)
// - XOA_USER: the username (optional if token provided)
// - XOA_PASSWORD: the password (optional if token provided)
// - XOA_INSECURE: whether to skip TLS verification (optional, defaults to false)
func LoadXOConfigFromEnv() (XoConfig, error) {
url := os.Getenv("XOA_URL")
token := os.Getenv("XOA_TOKEN")
username := os.Getenv("XOA_USER")
password := os.Getenv("XOA_PASSWORD")
insecureStr := os.Getenv("XOA_INSECURE")
clientTimeoutStr := os.Getenv("XOA_CLIENT_TIMEOUT")
cfg := XoConfig{
URL: url,
Token: token,
Username: username,
Password: password,
ClientTimeout: 0,
}
// Parse client timeout
if clientTimeoutStr != "" {
var err error
cfg.ClientTimeout, err = time.ParseDuration(clientTimeoutStr)
if err != nil {
return XoConfig{}, fmt.Errorf("invalid XOA_CLIENT_TIMEOUT value: %v", err)
}
}
// Parse insecure flag
if insecureStr != "" {
var err error
cfg.Insecure, err = strconv.ParseBool(insecureStr)
if err != nil {
return XoConfig{}, fmt.Errorf("invalid XOA_INSECURE value: %v", err)
}
}
return validateXOConfig(cfg)
}