-
Notifications
You must be signed in to change notification settings - Fork 7
/
provider.go
100 lines (90 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
package main
import (
"context"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
hcx "github.com/adeleporte/terraform-provider-hcx/hcx"
)
// Provider -
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"hcx": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HCX_URL", nil),
},
"username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HCX_USER", nil),
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("HCX_PASSWORD", nil),
},
"admin_username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HCX_ADMIN_USER", nil),
},
"admin_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("HCX_ADMIN_PASSWORD", nil),
},
"vmc_token": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("VMC_API_TOKEN", nil),
},
},
ResourcesMap: map[string]*schema.Resource{
"hcx_site_pairing": resourceSitePairing(),
"hcx_network_profile": resourceNetworkProfile(),
"hcx_compute_profile": resourceComputeProfile(),
"hcx_service_mesh": resourceServiceMesh(),
"hcx_l2_extension": resourceL2Extension(),
"hcx_vcenter": resourcevCenter(),
"hcx_sso": resourceSSO(),
"hcx_activation": resourceActivation(),
"hcx_rolemapping": resourceRoleMapping(),
"hcx_location": resourceLocation(),
"hcx_vmc": resourceVmc(),
},
DataSourcesMap: map[string]*schema.Resource{
"hcx_network_backing": dataSourceNetworkBacking(),
"hcx_compute_profile": dataSourceComputeProfile(),
},
ConfigureContextFunc: providerConfigure,
}
}
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
var diags diag.Diagnostics
hcxurl := d.Get("hcx").(string)
username := d.Get("username").(string)
password := d.Get("password").(string)
adminusername := d.Get("admin_username").(string)
adminpassword := d.Get("admin_password").(string)
vmc_token := d.Get("vmc_token").(string)
c, err := hcx.NewClient(&hcxurl, &username, &password, &adminusername, &adminpassword, &vmc_token)
//c := &http.Client{Timeout: 10 * time.Second}
if err != nil {
return nil, diag.FromErr(err)
}
if hcxurl == "" {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "No HCX Url provided",
Detail: "Only hcx_vmc resource will be manageable",
AttributePath: cty.Path{cty.GetAttrStep{Name: "hcx"}},
})
}
c.Token = vmc_token
return c, diags
}