forked from leg100/otf
-
Notifications
You must be signed in to change notification settings - Fork 5
/
hostname.go
71 lines (62 loc) · 1.89 KB
/
hostname.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
package internal
import (
"fmt"
"net"
"net/url"
"strings"
)
type HostnameService struct {
hostname string
webhookHostname string
}
func NewHostnameService(hostname string) *HostnameService {
return &HostnameService{hostname, hostname}
}
func (s *HostnameService) Hostname() string { return s.hostname }
func (s *HostnameService) WebhookHostname() string {
if s.webhookHostname == "" {
return s.hostname
}
return s.webhookHostname
}
func (s *HostnameService) SetHostname(hostname string) { s.hostname = hostname }
func (s *HostnameService) SetWebhookHostname(webhookHostname string) {
s.webhookHostname = webhookHostname
}
func (s *HostnameService) URL(path string) string {
u := url.URL{
Scheme: "https",
Host: s.Hostname(),
Path: path,
}
return u.String()
}
func (s *HostnameService) WebhookURL(path string) string {
u := url.URL{
Scheme: "https",
Host: s.WebhookHostname(),
Path: path,
}
return u.String()
}
// NormalizeAddress takes a host:port and converts it into a host:port
// appropriate for setting as the addressable hostname of otfd, e.g. converting
// 0.0.0.0 to 127.0.0.1.
func NormalizeAddress(addr *net.TCPAddr) string {
// If ip is unspecified assume 127.0.0.1 - an IP is used instead of
// 'localhost' because terraform insists on a dot in the registry hostname.
if addr.IP.IsUnspecified() {
return fmt.Sprintf("127.0.0.1:%d", addr.Port)
}
return fmt.Sprintf("%s:%d", addr.IP.String(), addr.Port)
}
// CredentialEnvKey returns the environment variable key for an API
// token specific to the given hostname.
func CredentialEnvKey(hostname string) string {
return fmt.Sprintf("TF_TOKEN_%s", strings.ReplaceAll(hostname, ".", "_"))
}
// CredentialEnv returns a host-specific environment variable credential for
// terraform.
func CredentialEnv(hostname string, token []byte) string {
return fmt.Sprintf("%s=%s", CredentialEnvKey(hostname), string(token))
}