-
Notifications
You must be signed in to change notification settings - Fork 15
/
provider.go
119 lines (98 loc) · 2.9 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
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
package dcnm
import (
"fmt"
"github.com/ciscoecosystem/dcnm-go-client/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("DCNM_USERNAME", nil),
Description: "Username for the DCNM account",
},
"password": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("DCNM_PASSWORD", nil),
Description: "Password for the DCNM account",
},
"url": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("DCNM_URL", nil),
Description: "URL for the DCNM server",
},
"insecure": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Allow insecure HTTPS client",
},
"proxy_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Proxy server URL for DCNM",
},
"expiry": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 900000,
Description: "Expiration time in miliseconds for DCNM server",
},
},
ResourcesMap: map[string]*schema.Resource{
"dcnm_vrf": resourceDCNMVRF(),
"dcnm_inventory": resourceDCNMInventroy(),
"dcnm_network": resourceDCNMNetwork(),
"dcnm_interface": resourceDCNMInterface(),
"dcnm_rest": resourceDCNMRest(),
},
DataSourcesMap: map[string]*schema.Resource{
"dcnm_vrf": datasourceDCNMVRF(),
"dcnm_inventory": datasourceDCNMInventory(),
"dcnm_network": datasourceDCNMNetwork(),
"dcnm_interface": datasourceDCNMInterface(),
},
ConfigureFunc: configClient,
}
}
func configClient(d *schema.ResourceData) (interface{}, error) {
config := Config{
Username: d.Get("username").(string),
Password: d.Get("password").(string),
URL: d.Get("url").(string),
IsInsecure: d.Get("insecure").(bool),
ProxyURL: d.Get("proxy_url").(string),
Expiry: d.Get("expiry").(int),
}
if err := config.Valid(); err != nil {
return nil, err
}
return config.getClient(), nil
}
func (c Config) Valid() error {
if c.Username == "" {
return fmt.Errorf("Username must be provided for the DCNM provider")
}
if c.Password == "" {
return fmt.Errorf("Password must be provided for the DCNM provider")
}
if c.URL == "" {
return fmt.Errorf("The URL must be provided for the DCNM provider")
}
return nil
}
func (c Config) getClient() interface{} {
return client.GetClient(c.URL, c.Username, c.Password, int64(c.Expiry), client.Insecure(c.IsInsecure), client.ProxyUrl(c.ProxyURL))
}
type Config struct {
Username string
Password string
URL string
IsInsecure bool
ProxyURL string
Expiry int
}