/
provider.go
171 lines (143 loc) · 5.32 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
// Copyright (c) Unikraft GmbH
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"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"
kraftcloud "sdk.kraft.cloud"
"sdk.kraft.cloud/client"
)
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &KraftCloudProvider{
version: version,
}
}
}
// KraftCloudProvider defines the provider implementation.
type KraftCloudProvider 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
}
// Ensure KraftCloudProvider satisfies various provider interfaces.
var _ provider.Provider = &KraftCloudProvider{}
// KraftCloudModel describes the provider data model.
type KraftCloudModel struct {
Metro types.String `tfsdk:"metro"`
Token types.String `tfsdk:"token"`
}
// Metadata implements provider.Provider.
func (p *KraftCloudProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "kraftcloud"
resp.Version = p.version
}
// Schema implements provider.Provider.
func (p *KraftCloudProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Manage unikernel instances on KraftCloud.",
Attributes: map[string]schema.Attribute{
"metro": schema.StringAttribute{
MarkdownDescription: "API metro",
Optional: true,
},
"token": schema.StringAttribute{
MarkdownDescription: "API token",
Optional: true,
Sensitive: true,
},
},
}
}
// Configure implements provider.Provider.
func (p *KraftCloudProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data KraftCloudModel
// Retrieve provider data from configuration
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// If a configuration value was provided for any of the attributes, it must
// be a known value (either literal, or already resolved by Terraform).
if data.Metro.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("metro"),
"Unknown KraftCloud API Metro",
"The provider cannot create the KraftCloud API client as there is an unknown configuration value for the KraftCloud API metro. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the KRAFTCLOUD_METRO environment variable.",
)
}
if data.Token.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("token"),
"Unknown KraftCloud API Token",
"The provider cannot create the KraftCloud API client as there is an unknown configuration value for the KraftCloud API token. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the KRAFTCLOUD_TOKEN environment variable.",
)
}
if resp.Diagnostics.HasError() {
return
}
// Consider values from environment variables, but override with explicit
// configuration values when provided.
metro := os.Getenv("KRAFTCLOUD_METRO")
if metro == "" {
metro = client.DefaultMetro
}
if !data.Metro.IsNull() {
metro = data.Metro.ValueString()
}
token := os.Getenv("KRAFTCLOUD_TOKEN")
if !data.Token.IsNull() {
token = data.Token.ValueString()
}
// If any of the expected configurations are still missing at this point,
// fail the provider's configuration phase.
if metro == "" {
resp.Diagnostics.AddAttributeError(
path.Root("metro"),
"Missing KraftCloud API Metro",
"The provider cannot create the KraftCloud API client as there is a missing or empty configuration value for the KraftCloud API metro. "+
"Set the metro value in the configuration or use the KRAFTCLOUD_METRO environment variable.",
)
}
if token == "" {
resp.Diagnostics.AddAttributeError(
path.Root("token"),
"Missing KraftCloud API Token",
"The provider cannot create the KraftCloud API client as there is a missing or empty configuration value for the KraftCloud API token. "+
"Set the token value in the configuration or use the KRAFTCLOUD_TOKEN environment variable.",
)
}
if resp.Diagnostics.HasError() {
return
}
// Client configuration for data sources and resources
client := kraftcloud.NewClient(
kraftcloud.WithDefaultMetro(metro),
kraftcloud.WithToken(token),
)
resp.DataSourceData = client.Instances()
resp.ResourceData = client.Instances()
}
// Resources describes the provider data model.
func (p *KraftCloudProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewInstanceResource,
}
}
// DataSources describes the provider data model.
func (p *KraftCloudProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewInstanceDataSource,
NewInstancesDataSource,
}
}