-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathssh_endpoint.go
More file actions
161 lines (137 loc) · 4.28 KB
/
Copy pathssh_endpoint.go
File metadata and controls
161 lines (137 loc) · 4.28 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package machines
import (
"encoding/json"
"net/url"
"strconv"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/go-playground/validator/v10"
)
type IEndpointWithFingerprint interface {
GetFingerprint() string
}
type IEndpointWithHostname interface {
GetHost() string
}
// SSHEndpoint contains the information necessary to communicate with an SSH
// endpoint. If a private key file is provided it will be used; otherwise we
// fall back to username/password.
type SSHEndpoint struct {
AccountID string `json:"AccountId,omitempty"`
DotNetCorePlatform string
Fingerprint string
Host string
ProxyID string `json:"ProxyId,omitempty"`
Port int
URI *url.URL `json:"Uri"`
endpoint
}
// NewSSHEndpoint creates and initializes a new SSH endpoint.
func NewSSHEndpoint(host string, port int, fingerprint string) *SSHEndpoint {
sshEndpoint := &SSHEndpoint{
Fingerprint: fingerprint,
Host: host,
Port: port,
endpoint: *newEndpoint("Ssh"),
}
url, _ := url.Parse("ssh://" + host + ":" + strconv.Itoa(port) + "/")
sshEndpoint.URI = url
return sshEndpoint
}
// GetAccountID returns the account ID associated with this SSH endpoint.
func (s *SSHEndpoint) GetAccountID() string {
return s.AccountID
}
// GetCommunicationStyle returns the communication style of this endpoint.
func (s *SSHEndpoint) GetCommunicationStyle() string {
return s.CommunicationStyle
}
// GetFingerprint returns the fingerprint associated with this SSH endpoint.
func (s *SSHEndpoint) GetFingerprint() string {
return s.Fingerprint
}
// GetHost returns the host associated with this SSH endpoint.
func (s *SSHEndpoint) GetHost() string {
return s.Host
}
// GetProxyID returns the proxy ID associated with this SSH endpoint.
func (s *SSHEndpoint) GetProxyID() string {
return s.ProxyID
}
func (s *SSHEndpoint) MarshalJSON() ([]byte, error) {
sshEndpoint := struct {
AccountID string `json:"AccountId,omitempty"`
CommunicationStyle string `json:"CommunicationStyle" validate:"required,eq=Ssh"`
DotNetCorePlatform string
Fingerprint string
Host string
Port int
ProxyID string `json:"ProxyId,omitempty"`
URI string `json:"Uri"`
resources.Resource
}{
AccountID: s.AccountID,
CommunicationStyle: s.CommunicationStyle,
DotNetCorePlatform: s.DotNetCorePlatform,
Fingerprint: s.Fingerprint,
Host: s.Host,
Port: s.Port,
ProxyID: s.ProxyID,
URI: s.URI.String(),
Resource: s.Resource,
}
return json.Marshal(sshEndpoint)
}
// SetProxyID sets the proxy ID associated with this SSH endpoint.
func (s *SSHEndpoint) SetProxyID(proxyID string) {
s.ProxyID = proxyID
}
// UnmarshalJSON sets this SSH endpoint to its representation in JSON.
func (s *SSHEndpoint) UnmarshalJSON(b []byte) error {
var fields struct {
AccountID string `json:"AccountId,omitempty"`
CommunicationStyle string `json:"CommunicationStyle" validate:"required,eq=Ssh"`
DotNetCorePlatform string
Fingerprint string
Host string
Port int
ProxyID string `json:"ProxyId,omitempty"`
URI string `json:"Uri"`
resources.Resource
}
err := json.Unmarshal(b, &fields)
if err != nil {
return err
}
// validate JSON representation
validate := validator.New()
err = validate.Struct(fields)
if err != nil {
return err
}
// return error if unable to parse URI string
u, err := url.Parse(fields.URI)
if err != nil {
return err
}
// set endpoint fields
s.AccountID = fields.AccountID
s.CommunicationStyle = fields.CommunicationStyle
s.DotNetCorePlatform = fields.DotNetCorePlatform
s.Fingerprint = fields.Fingerprint
s.Host = fields.Host
s.Port = fields.Port
s.ProxyID = fields.ProxyID
s.Resource = fields.Resource
s.URI = u
return nil
}
// Validate checks the state of the SSH endpoint and returns an error if
// invalid.
func (s *SSHEndpoint) Validate() error {
return validator.New().Struct(s)
}
var _ IEndpoint = &SSHEndpoint{}
var _ IEndpointWithAccount = &SSHEndpoint{}
var _ IEndpointWithFingerprint = &SSHEndpoint{}
var _ IEndpointWithHostname = &SSHEndpoint{}
var _ IEndpointWithProxy = &SSHEndpoint{}