/
client_options.go
executable file
·101 lines (77 loc) · 2.47 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
/*
* Copyright (C) 2017-2019 Alibaba Group Holding Limited
*/
package registry
import (
"fmt"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
// 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
Host string `json:"host,omitempty"`
// BOSH Registry port
Port int `json:"port,omitempty"`
// BOSH Registry username
Username string `json:"username,omitempty"`
// BOSH Registry password
Password string `json:"password,omitempty"`
// BOSH Registry TLS options (only when using protocol https)
TLS ClientTLSOptions `json:"tls,omitempty"`
}
// ClientTLSOptions are the TLS options used to create a BOSH Registry client.
type ClientTLSOptions struct {
// If the Client must skip the verification of the server certificates
InsecureSkipVerify bool `json:"insecure_skip_verify,omitempty"`
// 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", o.Protocol, o.Host, o.Port)
}
// EndpointWithCredentials returns the BOSH Registry endpoint including credentials.
func (o ClientOptions) EndpointWithCredentials() string {
return fmt.Sprintf("%s://%s:%s@%s:%d", o.Protocol, o.Username, o.Password, o.Host, o.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.Host == "" {
return bosherr.Error("Must provide a non-empty Host")
}
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
}