/
client_options.go
123 lines (93 loc) · 3.05 KB
/
client_options.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
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
package registry
import (
"fmt"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type SoftlayerUserData struct {
Server SoftlayerUserDataServerName `json:"server"`
Registry SoftlayerUserDataRegistryEndpoint `json:"registry"`
DNS SoftlayerUserDataDNSItems `json:"dns,omitempty"`
}
type SoftlayerUserDataServerName struct {
Name string `json:"name"`
}
type SoftlayerUserDataRegistryEndpoint struct {
Endpoint string `json:"endpoint"`
}
type SoftlayerUserDataDNSItems struct {
NameServer []string `json:"nameserver,omitempty"`
}
// ClientOptions are the options used to create a BOSH Registry client.
type ClientOptions struct {
// BOSH Registry protocol
Protocol string `json:"protocol,omitempty"`
// BOSH Registry hostname
Address string `json:"address,omitempty"`
// BOSH Registry port
Port int `json:"port,omitempty"`
// BOSH Registry username
Username string `json:"user,omitempty"`
// BOSH Registry password
Password string `json:"password,omitempty"`
// BOSH Registry TLS options (only when using protocol https)
TLS ClientTLSOptions `json:"tls,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
HTTPOptions HttpRegistryOptions `json:"http,omitempty"`
}
type HttpRegistryOptions struct {
Port int `json:"port,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
}
// ClientTLSOptions are the TLS options used to create a BOSH Registry client.
type ClientTLSOptions struct {
// Certificate file (PEM format)
CertFile string `json:"certfile,omitempty"`
// Private key file (PEM format)
KeyFile string `json:"keyfile,omitempty"`
// Roor CA certificate file (PEM format)
CACertFile string `json:"cacertfile,omitempty"`
}
//// Endpoint returns the BOSH Registry endpoint.
//func (o ClientOptions) Endpoint() string {
// return fmt.Sprintf("%s://%s:%d", "http", o.Address, o.Port)
//}
// EndpointWithCredentials returns the BOSH Registry endpoint including credentials.
func (o ClientOptions) EndpointWithCredentials() string {
return fmt.Sprintf("%s://%s:%s@%s:%d", "http", o.HTTPOptions.User, o.HTTPOptions.Password, o.Address, o.HTTPOptions.Port)
}
// Validate validates the Client options.
func (o ClientOptions) Validate() error {
if o.Protocol == "" {
return bosherr.Error("Must provide a non-empty Protocol")
}
if o.Address == "" {
return bosherr.Error("Must provide a non-empty Address")
}
if o.Port == 0 {
return bosherr.Error("Must provide a non-empty Port")
}
if o.Username == "" {
return bosherr.Error("Must provide a non-empty Username")
}
if o.Password == "" {
return bosherr.Error("Must provide a non-empty Password")
}
if o.Protocol == "https" {
err := o.TLS.Validate()
if err != nil {
return bosherr.WrapError(err, "Validating TLS configuration")
}
}
return nil
}
// Validate validates the TLS options.
func (o ClientTLSOptions) Validate() error {
if o.CertFile == "" {
return bosherr.Error("Must provide a non-empty CertFile")
}
if o.KeyFile == "" {
return bosherr.Error("Must provide a non-empty KeyFile")
}
return nil
}