/
provider.go
141 lines (124 loc) · 4.7 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package vcd
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/go-vcloud-director/util"
)
// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"user": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_USER", nil),
Description: "The user name for VCD API operations.",
},
"password": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_PASSWORD", nil),
Description: "The user password for VCD API operations.",
},
"sysorg": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_SYS_ORG", nil),
Description: "The VCD Org for user authentication",
},
"org": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_ORG", nil),
Description: "The VCD Org for API operations",
},
"vdc": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_VDC", nil),
Description: "The VDC for API operations",
},
"url": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_URL", nil),
Description: "The VCD url for VCD API operations.",
},
"max_retry_timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_MAX_RETRY_TIMEOUT", 60),
Description: "Max num seconds to wait for successful response when operating on resources within vCloud (defaults to 60)",
},
"allow_unverified_ssl": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_ALLOW_UNVERIFIED_SSL", false),
Description: "If set, VCDClient will permit unverifiable SSL certificates.",
},
"logging": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_API_LOGGING", false),
Description: "If set, it will enable logging of API requests and responses",
},
"logging_file": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_API_LOGGING_FILE", "go-vcloud-director.log"),
Description: "Defines the full name of the logging file for API calls (requires 'logging')",
},
},
ResourcesMap: map[string]*schema.Resource{
"vcd_network": resourceVcdNetwork(), // DEPRECATED: replaced by vcd_network_routed
"vcd_network_routed": resourceVcdNetworkRouted(),
"vcd_network_direct": resourceVcdNetworkDirect(),
"vcd_network_isolated": resourceVcdNetworkIsolated(),
"vcd_vapp": resourceVcdVApp(),
"vcd_firewall_rules": resourceVcdFirewallRules(),
"vcd_dnat": resourceVcdDNAT(),
"vcd_snat": resourceVcdSNAT(),
"vcd_edgegateway_vpn": resourceVcdEdgeGatewayVpn(),
"vcd_vapp_vm": resourceVcdVAppVm(),
"vcd_org": resourceOrg(),
"vcd_catalog": resourceVcdCatalog(),
"vcd_catalog_item": resourceVcdCatalogItem(),
"vcd_catalog_media": resourceVcdCatalogMedia(),
"vcd_inserted_media": resourceVcdInsertedMedia(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
maxRetryTimeout := d.Get("max_retry_timeout").(int)
// If sysOrg is defined, we use it for authentication.
// Otherwise, we use the default org defined for regular usage
connectOrg := d.Get("sysorg").(string)
if connectOrg == "" {
connectOrg = d.Get("org").(string)
}
config := Config{
User: d.Get("user").(string),
Password: d.Get("password").(string),
SysOrg: connectOrg, // Connection org
Org: d.Get("org").(string), // Default org for operations
Vdc: d.Get("vdc").(string), // Default vdc
Href: d.Get("url").(string),
MaxRetryTimeout: maxRetryTimeout,
InsecureFlag: d.Get("allow_unverified_ssl").(bool),
}
// If the provider includes logging directives,
// it will activate logging from upstream go-vcloud-director
logging := d.Get("logging").(bool)
// Logging is disabled by default.
// If enabled, we set the log file name and invoke the upstream logging set-up
if logging {
loggingFile := d.Get("logging_file").(string)
if loggingFile != "" {
util.EnableLogging = true
util.ApiLogFileName = loggingFile
util.InitLogging()
}
}
return config.Client()
}