/
provider.go
218 lines (184 loc) · 7.05 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package provider
import (
"context"
"crypto/tls"
"fmt"
"os"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Ensure ProdvanaProvider satisfies various provider interfaces.
var _ provider.Provider = &ProdvanaProvider{}
// ProdvanaProvider defines the provider implementation.
type ProdvanaProvider 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
}
// ProdvanaProviderModel describes the provider data model.
type ProdvanaProviderModel struct {
OrgSlug types.String `tfsdk:"org_slug"`
ApiToken types.String `tfsdk:"api_token"`
BaseDomain types.String `tfsdk:"base_domain"`
}
type AuthToken struct {
Token string
}
func (t AuthToken) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error) {
return map[string]string{
"authorization": "Bearer " + t.Token,
}, nil
}
func (AuthToken) RequireTransportSecurity() bool {
return true
}
func (p *ProdvanaProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "prodvana"
resp.Version = p.version
}
func (p *ProdvanaProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"org_slug": schema.StringAttribute{
MarkdownDescription: "Prodvana organization to authenticate with (you can find this in your Org's url: <org>.prodvana.io)",
// Optional because we support passing as an environment variable, see Configure
Optional: true,
},
"api_token": schema.StringAttribute{
MarkdownDescription: "An API token generated with permissions to this organization.",
Sensitive: true,
// Optional because we support passing as an environment variable, see Configure
Optional: true,
},
"base_domain": schema.StringAttribute{
MarkdownDescription: "(Internal Only) The base domain to connect to, the default is runprodvana.com -- only change this if you know what you're doing.",
Optional: true,
},
},
}
}
func (p *ProdvanaProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data ProdvanaProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if data.OrgSlug.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("org_slug"),
"Unknown Prodvana Org Slug",
"The provider cannot create a Prodvana API client as there is an unknown configuration value for the org_slug."+
"Either target apply the source of the value first, set the value statically in the configuration, or use the PVN_ORG_SLUG environment variable.",
)
}
if data.ApiToken.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("api_token"),
"Unknown Prodvana API Token",
"The provider cannot create a Prodvana API client as there is an unknown configuration value for the api_token."+
"Either target apply the source of the value first, set the value statically in the configuration, or use the PVN_API_TOKEN environment variable.",
)
}
if data.BaseDomain.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("base_domain"),
"Unknown Prodvana Base Domain",
"The provider cannot create a Prodvana API client as there is an unknown configuration value for the base_domain."+
"Either target apply the source of the value first, set the value statically in the configuration, or use the PVN_BASE_DOMAIN environment variable.",
)
}
if resp.Diagnostics.HasError() {
return
}
// Default values to environment variables, but override
// with Terraform configuration value if set.
orgSlug := os.Getenv("PVN_ORG_SLUG")
apiToken := os.Getenv("PVN_API_TOKEN")
baseDomain := os.Getenv("PVN_BASE_DOMAIN")
if !data.OrgSlug.IsNull() {
orgSlug = data.OrgSlug.ValueString()
}
if !data.ApiToken.IsNull() {
apiToken = data.ApiToken.ValueString()
}
if !data.BaseDomain.IsNull() {
baseDomain = data.BaseDomain.ValueString()
}
// If any of the expected configurations are missing, return
// errors with provider-specific guidance.
if orgSlug == "" {
resp.Diagnostics.AddAttributeError(
path.Root("org_slug"),
"Missing Prodvana Org Slug",
"The provider cannot create a Prodvana API client as there is an unknown configuration value for the org_slug."+
"Either target apply the source of the value first, set the value statically in the configuration, or use the PVN_ORG_SLUG environment variable."+
"If either is already set, ensure the value is not empty.",
)
}
if apiToken == "" {
resp.Diagnostics.AddAttributeError(
path.Root("api_token"),
"Missing Prodvana API Token",
"The provider cannot create a Prodvana API client as there is an unknown configuration value for the api_token."+
"Either target apply the source of the value first, set the value statically in the configuration, or use the PVN_API_TOKEN environment variable.",
)
}
if baseDomain == "" {
baseDomain = "runprodvana.com"
}
if resp.Diagnostics.HasError() {
return
}
ctx = tflog.SetField(ctx, "prodvana_org_slug", orgSlug)
ctx = tflog.SetField(ctx, "prodvana_api_token", apiToken)
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "prodvana_api_token")
tflog.Debug(ctx, "Creating Prodvana client")
domain := fmt.Sprintf("%s.grpc.%s", orgSlug, baseDomain)
cred := credentials.NewTLS(&tls.Config{ServerName: domain})
options := []grpc.DialOption{
grpc.WithTransportCredentials(cred),
grpc.WithPerRPCCredentials(AuthToken{Token: apiToken}),
}
conn, err := grpc.Dial(domain+":443", options...)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Prodvana API Client",
"An unexpected error occurred when creating the Prodvana API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"Prodvana Client Error: "+err.Error(),
)
return
}
resp.DataSourceData = conn
resp.ResourceData = conn
}
func (p *ProdvanaProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewApplicationResource,
NewReleaseChannelResource,
NewK8sRuntimeResource,
NewRuntimeLinkResource,
NewManagedK8sRuntimeResource,
NewContainerRegistryResource,
NewECRRegistryResource,
}
}
func (p *ProdvanaProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewApplicationDataSource,
NewReleaseChannelDataSource,
NewK8sRuntimeDataSource,
}
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &ProdvanaProvider{
version: version,
}
}
}