-
Notifications
You must be signed in to change notification settings - Fork 2
/
environments.go
84 lines (64 loc) · 2.01 KB
/
environments.go
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
package config
import (
"crypto/x509"
"encoding/pem"
"errors"
yaml "gopkg.in/yaml.v2"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
const EnvironmentOptionAuthBind string = "auth.bind"
const EnvironmentOptionAuthOpenCommand string = "auth.open_command"
type State struct {
Environments EnvironmentsState `yaml:"environments,omitempty"`
}
type EnvironmentsState []EnvironmentState
type EnvironmentState struct {
URL string `yaml:"url"`
CACertificate string `yaml:"ca_certificate,omitempty"`
Alias string `yaml:"alias,omitempty"`
Auth *EnvironmentAuthState `yaml:"auth,omitempty"`
Options map[string]interface{} `yaml:"options,omitempty"`
}
func (e EnvironmentState) GetCACertificate() (*x509.Certificate, error) {
var cert *x509.Certificate
block, _ := pem.Decode([]byte(e.CACertificate))
if block == nil {
return cert, errors.New("Parsing CA certificate: Missing PEM block")
}
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
return cert, errors.New("Parsing CA certificate: Not a certificate")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return cert, bosherr.WrapError(err, "Parsing CA certificate")
}
return cert, nil
}
func (e EnvironmentState) GetOption(option EnvironmentOption, def interface{}) error {
val, found := e.Options[option.Key()]
if !found {
val = def
}
return option.SetValue(val)
}
func (e *EnvironmentState) SetOption(key string, val interface{}) {
if e.Options == nil {
e.Options = map[string]interface{}{}
}
e.Options[key] = val
}
type EnvironmentAuthState struct {
Type string `yaml:"type"`
Options interface{} `yaml:"options"`
}
func (ea EnvironmentAuthState) UnmarshalOptions(typed interface{}) error {
bytes, err := yaml.Marshal(ea.Options)
if err != nil {
return bosherr.WrapError(err, "Marshalling")
}
err = yaml.Unmarshal(bytes, typed)
if err != nil {
return bosherr.WrapError(err, "Unmarshalling to typed options")
}
return nil
}