-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprovider.go
181 lines (157 loc) · 5.08 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
package provider
import (
"context"
"net/url"
"os"
"strings"
"cdr.dev/slog"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/function"
"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"
"github.com/coder/coder/v2/codersdk"
)
// Ensure CoderdProvider satisfies various provider interfaces.
var _ provider.Provider = &CoderdProvider{}
var _ provider.ProviderWithFunctions = &CoderdProvider{}
// CoderdProvider defines the provider implementation.
type CoderdProvider 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
}
type CoderdProviderData struct {
Client *codersdk.Client
Features map[codersdk.FeatureName]codersdk.Feature
}
// CoderdProviderModel describes the provider data model.
type CoderdProviderModel struct {
URL types.String `tfsdk:"url"`
Token types.String `tfsdk:"token"`
}
func (p *CoderdProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "coderd"
resp.Version = p.version
}
func (p *CoderdProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `
The coderd provider can be used to manage resources on a Coder deployment. The provider exposes resources and data sources for users, groups, templates, and workspace proxies.
~> **Warning**
This provider is only compatible with Coder version [2.10.1](https://github.com/coder/coder/releases/tag/v2.10.1) and later.
`,
Attributes: map[string]schema.Attribute{
"url": schema.StringAttribute{
MarkdownDescription: "URL to the Coder deployment. Defaults to `$CODER_URL`.",
Optional: true,
},
"token": schema.StringAttribute{
MarkdownDescription: "API token for communicating with the deployment. Most resource types require elevated permissions. Defaults to `$CODER_SESSION_TOKEN`.",
Optional: true,
},
},
}
}
func (p *CoderdProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data CoderdProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
if data.URL.ValueString() == "" {
urlEnv, ok := os.LookupEnv("CODER_URL")
if !ok {
resp.Diagnostics.AddError("url", "url or $CODER_URL is required")
return
}
data.URL = types.StringValue(urlEnv)
}
if data.Token.ValueString() == "" {
tokenEnv, ok := os.LookupEnv("CODER_SESSION_TOKEN")
if !ok {
resp.Diagnostics.AddError("token", "token or $CODER_SESSION_TOKEN is required")
return
}
data.Token = types.StringValue(tokenEnv)
}
url, err := url.Parse(data.URL.ValueString())
if err != nil {
resp.Diagnostics.AddError("url", "url is not a valid URL: "+err.Error())
return
}
client := codersdk.New(url)
client.SetLogger(slog.Make(tfslog{}).Leveled(slog.LevelDebug))
client.SetSessionToken(data.Token.ValueString())
entitlements, err := client.Entitlements(ctx)
if err != nil {
resp.Diagnostics.AddError("Client Error", "failed to get deployment entitlements: "+err.Error())
}
providerData := &CoderdProviderData{
Client: client,
Features: entitlements.Features,
}
resp.DataSourceData = providerData
resp.ResourceData = providerData
}
func (p *CoderdProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewUserResource,
NewGroupResource,
NewTemplateResource,
NewWorkspaceProxyResource,
NewLicenseResource,
}
}
func (p *CoderdProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewGroupDataSource,
NewUserDataSource,
NewOrganizationDataSource,
NewTemplateDataSource,
}
}
func (p *CoderdProvider) Functions(ctx context.Context) []func() function.Function {
return []func() function.Function{}
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &CoderdProvider{
version: version,
}
}
}
// tfslog redirects slog entries to tflog.
type tfslog struct{}
var _ slog.Sink = tfslog{}
// LogEntry implements slog.Sink.
func (t tfslog) LogEntry(ctx context.Context, e slog.SinkEntry) {
m := map[string]any{
"time": e.Time.Unix(),
"func": e.Func,
"file": e.File,
"line": e.Line,
}
for _, f := range e.Fields {
m[f.Name] = f.Value
}
msg := e.Message
if len(e.LoggerNames) > 0 {
msg = "[" + strings.Join(e.LoggerNames, ".") + "] " + msg
}
switch e.Level {
case slog.LevelDebug:
tflog.Debug(ctx, msg, m)
case slog.LevelInfo:
tflog.Info(ctx, msg, m)
case slog.LevelWarn:
tflog.Warn(ctx, msg, m)
case slog.LevelError, slog.LevelFatal:
tflog.Error(ctx, msg, m)
}
}
// Sync implements slog.Sink.
func (t tfslog) Sync() {}