This repository was archived by the owner on Jan 18, 2023. It is now read-only.
forked from tenstad/terraform-provider-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
151 lines (129 loc) · 4.23 KB
/
provider.go
File metadata and controls
151 lines (129 loc) · 4.23 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
package provider
import (
"context"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/crypto/ssh"
)
func init() {
// Set descriptions to support markdown syntax, this will be used in document generation
// and the language server.
schema.DescriptionKind = schema.StringMarkdown
// Customize the content of descriptions when output. For example you can add defaults on
// to the exported descriptions if present.
// schema.SchemaDescriptionBuilder = func(s *schema.Schema) string {
// desc := s.Description
// if s.Default != nil {
// desc += fmt.Sprintf(" Defaults to `%v`.", s.Default)
// }
// return strings.TrimSpace(desc)
// }
}
func New(version string) func() *schema.Provider {
return func() *schema.Provider {
p := &schema.Provider{
DataSourcesMap: map[string]*schema.Resource{
"remotefile": dataSourceRemotefile(),
},
ResourcesMap: map[string]*schema.Resource{
"remotefile": resourceRemotefile(),
},
Schema: map[string]*schema.Schema{},
}
p.ConfigureContextFunc = configure(version, p)
return p
}
}
type apiClient struct {
mux *sync.Mutex
remoteClients map[string]*RemoteClient
}
func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
return func(c context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
client := apiClient{
mux: &sync.Mutex{},
remoteClients: map[string]*RemoteClient{},
}
return &client, diag.Diagnostics{}
}
}
func (c *apiClient) getRemoteClient(d *schema.ResourceData) (*RemoteClient, error) {
connectionID := resourceConnectionHash(d)
c.mux.Lock()
defer c.mux.Unlock()
client, ok := c.remoteClients[connectionID]
if ok {
return client, nil
}
client, err := RemoteClientFromResource(d)
if err != nil {
return nil, err
}
c.remoteClients[connectionID] = client
return client, nil
}
func RemoteClientFromResource(d *schema.ResourceData) (*RemoteClient, error) {
clientConfig := ssh.ClientConfig{
User: d.Get("conn.0.username").(string),
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
password, ok := d.GetOk("conn.0.password")
if ok {
clientConfig.Auth = append(clientConfig.Auth, ssh.Password(password.(string)))
}
private_key, ok := d.GetOk("conn.0.private_key")
if ok {
signer, err := ssh.ParsePrivateKey([]byte(private_key.(string)))
if err != nil {
return nil, fmt.Errorf("couldn't create a ssh client config from private key: %s", err.Error())
}
clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeys(signer))
}
private_key_path, ok := d.GetOk("conn.0.private_key_path")
if ok {
content, err := ioutil.ReadFile(private_key_path.(string))
if err != nil {
return nil, fmt.Errorf("couldn't read private key: %s", err.Error())
}
signer, err := ssh.ParsePrivateKey(content)
if err != nil {
return nil, fmt.Errorf("couldn't create a ssh client config from private key file: %s", err.Error())
}
clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeys(signer))
}
private_key_env_var, ok := d.GetOk("conn.0.private_key_env_var")
if ok {
private_key := os.Getenv(private_key_env_var.(string))
signer, err := ssh.ParsePrivateKey([]byte(private_key))
if err != nil {
return nil, fmt.Errorf("couldn't create a ssh client config from private key env var: %s", err.Error())
}
clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeys(signer))
}
host := fmt.Sprintf("%s:%d", d.Get("conn.0.host").(string), d.Get("conn.0.port").(int))
return NewRemoteClient(host, clientConfig)
}
func resourceConnectionHash(d *schema.ResourceData) string {
elements := []string{
d.Get("conn.0.host").(string),
d.Get("conn.0.username").(string),
strconv.Itoa(d.Get("conn.0.port").(int)),
resourceStringWithDefault(d, "conn.0.password", ""),
resourceStringWithDefault(d, "conn.0.private_key", ""),
resourceStringWithDefault(d, "conn.0.private_key_path", ""),
}
return strings.Join(elements, "::")
}
func resourceStringWithDefault(d *schema.ResourceData, key string, defaultValue string) string {
str, ok := d.GetOk(key)
if ok {
return str.(string)
}
return defaultValue
}