/
provider.go
87 lines (78 loc) · 2.5 KB
/
provider.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
package activedirectory
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
log "github.com/sirupsen/logrus"
)
// Provider for terraform ad provider
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("AD_HOST", nil),
Description: "The AD server to connect to.",
},
"port": {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("AD_PORT", 389),
Description: "The AD protocol port (default: 389).",
},
"domain": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("AD_DOMAIN", nil),
Description: "The AD base domain.",
},
"use_tls": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("AD_USE_TLS", true),
Description: "Use TLS to secure the connection (default: true).",
},
"no_cert_verify": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("AD_NO_CERT_VERIFY", false),
Description: "Do not verify TLS certificate (default: false).",
},
"user": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("AD_USER", nil),
Description: "User to be used for authenticating on the AD server.",
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("AD_PASSWORD", nil),
Description: "Password to authenticate the user.",
},
},
ResourcesMap: map[string]*schema.Resource{
"activedirectory_computer": resourceADComputerObject(),
"activedirectory_ou": resourceADOUObject(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
api := &API{
host: d.Get("host").(string),
port: d.Get("port").(int),
domain: d.Get("domain").(string),
useTLS: d.Get("use_tls").(bool),
insecure: d.Get("no_cert_verify").(bool),
user: d.Get("user").(string),
password: d.Get("password").(string),
}
log.Infof("Connecting to %s:%d as user %s@%s.", api.host, api.port, api.user, api.domain)
if err := api.connect(); err != nil {
return nil, fmt.Errorf("providerConfigure - connection to active directory failed: %s", err)
}
return api, nil
}