generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider.go
157 lines (131 loc) · 4.79 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package provider
import (
"context"
"crypto/tls"
"net/http"
"os"
"time"
"github.com/TGNThump/terraform-provider-vyos/internal/vyos"
"github.com/foltik/vyos-client-go/client"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure VyOSProvider satisfies various provider interfaces.
var _ provider.Provider = &VyOSProvider{}
var _ provider.ProviderWithMetadata = &VyOSProvider{}
// VyOSProvider defines the provider implementation.
type VyOSProvider struct {
// version is set to the provider version on release, "dev" when the
// provider is built and ran locally, and "test" when running acceptance
// testing.
version string
}
// VyOSProviderModel describes the provider data model.
type VyOSProviderModel struct {
Endpoint types.String `tfsdk:"endpoint"`
ApiKey types.String `tfsdk:"api_key"`
}
func (p *VyOSProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "vyos"
resp.Version = p.version
}
func (p *VyOSProvider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"endpoint": {
MarkdownDescription: "Endpoint of the VyOS HTTP API",
Optional: true,
Type: types.StringType,
},
"api_key": {
MarkdownDescription: "API Key for the VyOS HTTP API",
Optional: true,
Sensitive: true,
Type: types.StringType,
},
},
}, nil
}
func (p *VyOSProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var config VyOSProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
// Configuration values are now available.
if config.Endpoint.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("endpoint"),
"Unknown VyOS API Endpoint",
"The provider cannot create the VyOS API client as there is an unknown configuration value for the VyOS API Endpoint. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the VYOS_ENDPOINT environment variable.",
)
}
if config.ApiKey.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("api_key"),
"Unknown VyOS API key",
"The provider cannot create the VyOS API client as there is an unknown configuration value for the VyOS API ApiKey. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the VYOS_API_KEY environment variable.",
)
}
if resp.Diagnostics.HasError() {
return
}
endpoint := os.Getenv("VYOS_ENDPOINT")
api_key := os.Getenv("VYOS_API_KEY")
if !config.Endpoint.IsNull() {
endpoint = config.Endpoint.ValueString()
}
if !config.ApiKey.IsNull() {
api_key = config.ApiKey.ValueString()
}
if endpoint == "" {
resp.Diagnostics.AddAttributeError(
path.Root("endpoint"),
"Missing VyOS API Endpoint",
"The provider cannot create the VyOS API client as there is an missing or empty value for the VyOS API Endpoint. "+
"Set the username value in the configuration or use the VYOS_ENDPOINT environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if api_key == "" {
resp.Diagnostics.AddAttributeError(
path.Root("api_key"),
"Missing VyOS API key",
"The provider cannot create the VyOS API client as there is an missing or empty value for the VyOS API Key. "+
"Set the username value in the configuration or use the VYOS_API_KEY environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if resp.Diagnostics.HasError() {
return
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
httpClient := &http.Client{Transport: transport, Timeout: 10 * time.Minute}
apiClient := client.NewWithClient(httpClient, endpoint, api_key)
vyosConfig := vyos.New(apiClient)
resp.DataSourceData = vyosConfig
resp.ResourceData = vyosConfig
}
func (p *VyOSProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewConfigResource,
}
}
func (p *VyOSProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{}
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &VyOSProvider{
version: version,
}
}
}