Skip to content

Commit 4aa400f

Browse files
committed
maybe
1 parent 05655ef commit 4aa400f

23 files changed

+287
-178
lines changed

internal/provider/logger.go renamed to internal/logger.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package provider
1+
package internal
22

33
import (
44
"context"
@@ -7,19 +7,19 @@ import (
77
"github.com/hashicorp/terraform-plugin-log/tflog"
88
)
99

10-
var _ slog.Sink = &tfLogSink{}
10+
var _ slog.Sink = &tflogSink{}
1111

12-
type tfLogSink struct {
13-
tfCtx context.Context
12+
type tflogSink struct {
13+
ctx context.Context
1414
}
1515

16-
func newTFLogSink(tfCtx context.Context) *tfLogSink {
17-
return &tfLogSink{
18-
tfCtx: tfCtx,
16+
func NewLogSink(ctx context.Context) slog.Sink {
17+
return &tflogSink{
18+
ctx: ctx,
1919
}
2020
}
2121

22-
func (s *tfLogSink) LogEntry(ctx context.Context, e slog.SinkEntry) {
22+
func (s *tflogSink) LogEntry(ctx context.Context, e slog.SinkEntry) {
2323
var logFn func(ctx context.Context, msg string, additionalFields ...map[string]interface{})
2424
switch e.Level {
2525
case slog.LevelDebug:
@@ -31,10 +31,10 @@ func (s *tfLogSink) LogEntry(ctx context.Context, e slog.SinkEntry) {
3131
default:
3232
logFn = tflog.Error
3333
}
34-
logFn(s.tfCtx, e.Message, mapToFields(e.Fields))
34+
logFn(s.ctx, e.Message, mapToFields(e.Fields))
3535
}
3636

37-
func (s *tfLogSink) Sync() {}
37+
func (s *tflogSink) Sync() {}
3838

3939
func mapToFields(m slog.Map) map[string]interface{} {
4040
fields := make(map[string]interface{}, len(m))

internal/provider/group_data_source.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/coder/coder/v2/codersdk"
8+
"github.com/coder/terraform-provider-coderd/internal"
89
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
910
"github.com/hashicorp/terraform-plugin-framework/datasource"
1011
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -28,9 +29,9 @@ type GroupDataSource struct {
2829
// GroupDataSourceModel describes the data source data model.
2930
type GroupDataSourceModel struct {
3031
// ID or name and organization ID must be set
31-
ID UUID `tfsdk:"id"`
32-
Name types.String `tfsdk:"name"`
33-
OrganizationID UUID `tfsdk:"organization_id"`
32+
ID internal.UUID `tfsdk:"id"`
33+
Name types.String `tfsdk:"name"`
34+
OrganizationID internal.UUID `tfsdk:"organization_id"`
3435

3536
DisplayName types.String `tfsdk:"display_name"`
3637
AvatarURL types.String `tfsdk:"avatar_url"`
@@ -40,14 +41,14 @@ type GroupDataSourceModel struct {
4041
}
4142

4243
type Member struct {
43-
ID UUID `tfsdk:"id"`
44-
Username types.String `tfsdk:"username"`
45-
Email types.String `tfsdk:"email"`
46-
CreatedAt types.Int64 `tfsdk:"created_at"`
47-
LastSeenAt types.Int64 `tfsdk:"last_seen_at"`
48-
Status types.String `tfsdk:"status"`
49-
LoginType types.String `tfsdk:"login_type"`
50-
ThemePreference types.String `tfsdk:"theme_preference"`
44+
ID internal.UUID `tfsdk:"id"`
45+
Username types.String `tfsdk:"username"`
46+
Email types.String `tfsdk:"email"`
47+
CreatedAt types.Int64 `tfsdk:"created_at"`
48+
LastSeenAt types.Int64 `tfsdk:"last_seen_at"`
49+
Status types.String `tfsdk:"status"`
50+
LoginType types.String `tfsdk:"login_type"`
51+
ThemePreference types.String `tfsdk:"theme_preference"`
5152
}
5253

5354
func (d *GroupDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
@@ -63,7 +64,7 @@ func (d *GroupDataSource) Schema(ctx context.Context, req datasource.SchemaReque
6364
MarkdownDescription: "The ID of the group to retrieve. This field will be populated if a name and organization ID is supplied.",
6465
Optional: true,
6566
Computed: true,
66-
CustomType: UUIDType,
67+
CustomType: internal.UUIDType,
6768
Validators: []validator.String{
6869
stringvalidator.AtLeastOneOf(path.Expressions{
6970
path.MatchRoot("name"),
@@ -78,7 +79,7 @@ func (d *GroupDataSource) Schema(ctx context.Context, req datasource.SchemaReque
7879
},
7980
"organization_id": schema.StringAttribute{
8081
MarkdownDescription: "The organization ID that the group belongs to. This field will be populated if an ID is supplied. Defaults to the provider default organization ID.",
81-
CustomType: UUIDType,
82+
CustomType: internal.UUIDType,
8283
Optional: true,
8384
Computed: true,
8485
},
@@ -102,7 +103,7 @@ func (d *GroupDataSource) Schema(ctx context.Context, req datasource.SchemaReque
102103
NestedObject: schema.NestedAttributeObject{
103104
Attributes: map[string]schema.Attribute{
104105
"id": schema.StringAttribute{
105-
CustomType: UUIDType,
106+
CustomType: internal.UUIDType,
106107
Computed: true,
107108
},
108109
"username": schema.StringAttribute{
@@ -176,7 +177,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
176177
client := d.data.Client
177178

178179
if data.OrganizationID.IsNull() {
179-
data.OrganizationID = UUIDValue(d.data.DefaultOrganizationID)
180+
data.OrganizationID = internal.UUIDValue(d.data.DefaultOrganizationID)
180181
}
181182

182183
var (
@@ -187,7 +188,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
187188
groupID := data.ID.ValueUUID()
188189
group, err = client.Group(ctx, groupID)
189190
if err != nil {
190-
if isNotFound(err) {
191+
if internal.IsNotFound(err) {
191192
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String()))
192193
resp.State.RemoveResource(ctx)
193194
return
@@ -196,19 +197,19 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
196197
return
197198
}
198199
data.Name = types.StringValue(group.Name)
199-
data.OrganizationID = UUIDValue(group.OrganizationID)
200+
data.OrganizationID = internal.UUIDValue(group.OrganizationID)
200201
} else {
201202
group, err = client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
202203
if err != nil {
203-
if isNotFound(err) {
204+
if internal.IsNotFound(err) {
204205
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with name %s not found in organization with ID %s. Marking as deleted.", data.Name.ValueString(), data.OrganizationID.ValueString()))
205206
resp.State.RemoveResource(ctx)
206207
return
207208
}
208209
resp.Diagnostics.AddError("Failed to get group by name and org ID", err.Error())
209210
return
210211
}
211-
data.ID = UUIDValue(group.ID)
212+
data.ID = internal.UUIDValue(group.ID)
212213
}
213214

214215
data.DisplayName = types.StringValue(group.DisplayName)
@@ -217,7 +218,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
217218
members := make([]Member, 0, len(group.Members))
218219
for _, member := range group.Members {
219220
members = append(members, Member{
220-
ID: UUIDValue(member.ID),
221+
ID: internal.UUIDValue(member.ID),
221222
Username: types.StringValue(member.Username),
222223
Email: types.StringValue(member.Email),
223224
CreatedAt: types.Int64Value(member.CreatedAt.Unix()),

internal/provider/group_data_source_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/coder/coder/v2/coderd/util/ptr"
1212
"github.com/coder/coder/v2/codersdk"
1313
"github.com/coder/terraform-provider-coderd/integration"
14+
"github.com/coder/terraform-provider-coderd/internal"
1415
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1516
"github.com/stretchr/testify/require"
1617
)
@@ -188,7 +189,7 @@ data "coderd_group" "test" {
188189
`
189190

190191
funcMap := template.FuncMap{
191-
"orNull": PrintOrNull,
192+
"orNull": internal.PrintOrNull,
192193
}
193194

194195
buf := strings.Builder{}

internal/provider/group_resource.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/coder/coder/v2/codersdk"
9+
"github.com/coder/terraform-provider-coderd/internal"
910
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
1011
"github.com/google/uuid"
1112
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -37,14 +38,14 @@ type GroupResource struct {
3738

3839
// GroupResourceModel describes the resource data model.
3940
type GroupResourceModel struct {
40-
ID UUID `tfsdk:"id"`
41-
42-
Name types.String `tfsdk:"name"`
43-
DisplayName types.String `tfsdk:"display_name"`
44-
AvatarURL types.String `tfsdk:"avatar_url"`
45-
QuotaAllowance types.Int32 `tfsdk:"quota_allowance"`
46-
OrganizationID UUID `tfsdk:"organization_id"`
47-
Members types.Set `tfsdk:"members"`
41+
ID internal.UUID `tfsdk:"id"`
42+
43+
Name types.String `tfsdk:"name"`
44+
DisplayName types.String `tfsdk:"display_name"`
45+
AvatarURL types.String `tfsdk:"avatar_url"`
46+
QuotaAllowance types.Int32 `tfsdk:"quota_allowance"`
47+
OrganizationID internal.UUID `tfsdk:"organization_id"`
48+
Members types.Set `tfsdk:"members"`
4849
}
4950

5051
func CheckGroupEntitlements(ctx context.Context, features map[codersdk.FeatureName]codersdk.Feature) (diags diag.Diagnostics) {
@@ -67,7 +68,7 @@ func (r *GroupResource) Schema(ctx context.Context, req resource.SchemaRequest,
6768
Attributes: map[string]schema.Attribute{
6869
"id": schema.StringAttribute{
6970
MarkdownDescription: "Group ID.",
70-
CustomType: UUIDType,
71+
CustomType: internal.UUIDType,
7172
Computed: true,
7273
PlanModifiers: []planmodifier.String{
7374
stringplanmodifier.UseStateForUnknown(),
@@ -104,7 +105,7 @@ func (r *GroupResource) Schema(ctx context.Context, req resource.SchemaRequest,
104105
},
105106
"organization_id": schema.StringAttribute{
106107
MarkdownDescription: "The organization ID that the group belongs to. Defaults to the provider default organization ID.",
107-
CustomType: UUIDType,
108+
CustomType: internal.UUIDType,
108109
Optional: true,
109110
Computed: true,
110111
PlanModifiers: []planmodifier.String{
@@ -113,7 +114,7 @@ func (r *GroupResource) Schema(ctx context.Context, req resource.SchemaRequest,
113114
},
114115
"members": schema.SetAttribute{
115116
MarkdownDescription: "Members of the group, by ID. If `null`, members will not be added or removed by Terraform. To have a group resource with unmanaged members, but be able to read the members in Terraform, use `data.coderd_group`",
116-
ElementType: UUIDType,
117+
ElementType: internal.UUIDType,
117118
Optional: true,
118119
},
119120
},
@@ -141,9 +142,8 @@ func (r *GroupResource) Configure(ctx context.Context, req resource.ConfigureReq
141142
}
142143

143144
func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
144-
var data GroupResourceModel
145-
146145
// Read Terraform plan data into the model
146+
var data GroupResourceModel
147147
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
148148

149149
if resp.Diagnostics.HasError() {
@@ -158,7 +158,7 @@ func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest,
158158
client := r.data.Client
159159

160160
if data.OrganizationID.IsUnknown() {
161-
data.OrganizationID = UUIDValue(r.data.DefaultOrganizationID)
161+
data.OrganizationID = internal.UUIDValue(r.data.DefaultOrganizationID)
162162
}
163163

164164
orgID := data.OrganizationID.ValueUUID()
@@ -177,7 +177,7 @@ func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest,
177177
tflog.Info(ctx, "successfully created group", map[string]any{
178178
"id": group.ID.String(),
179179
})
180-
data.ID = UUIDValue(group.ID)
180+
data.ID = internal.UUIDValue(group.ID)
181181
data.DisplayName = types.StringValue(group.DisplayName)
182182

183183
tflog.Info(ctx, "setting group members")
@@ -217,7 +217,7 @@ func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp
217217

218218
group, err := client.Group(ctx, groupID)
219219
if err != nil {
220-
if isNotFound(err) {
220+
if internal.IsNotFound(err) {
221221
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String()))
222222
resp.State.RemoveResource(ctx)
223223
return
@@ -230,13 +230,13 @@ func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp
230230
data.DisplayName = types.StringValue(group.DisplayName)
231231
data.AvatarURL = types.StringValue(group.AvatarURL)
232232
data.QuotaAllowance = types.Int32Value(int32(group.QuotaAllowance))
233-
data.OrganizationID = UUIDValue(group.OrganizationID)
233+
data.OrganizationID = internal.UUIDValue(group.OrganizationID)
234234
if !data.Members.IsNull() {
235235
members := make([]attr.Value, 0, len(group.Members))
236236
for _, member := range group.Members {
237-
members = append(members, UUIDValue(member.ID))
237+
members = append(members, internal.UUIDValue(member.ID))
238238
}
239-
data.Members = types.SetValueMust(UUIDType, members)
239+
data.Members = types.SetValueMust(internal.UUIDType, members)
240240
}
241241

242242
// Save updated data into Terraform state
@@ -255,7 +255,7 @@ func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest,
255255

256256
client := r.data.Client
257257
if data.OrganizationID.IsUnknown() {
258-
data.OrganizationID = UUIDValue(r.data.DefaultOrganizationID)
258+
data.OrganizationID = internal.UUIDValue(r.data.DefaultOrganizationID)
259259
}
260260
groupID := data.ID.ValueUUID()
261261

@@ -267,7 +267,7 @@ func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest,
267267
var add []string
268268
var remove []string
269269
if !data.Members.IsNull() {
270-
var plannedMembers []UUID
270+
var plannedMembers []internal.UUID
271271
resp.Diagnostics.Append(
272272
data.Members.ElementsAs(ctx, &plannedMembers, false)...,
273273
)

internal/provider/group_resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ resource "coderd_group" "test" {
195195
}
196196
`
197197
funcMap := template.FuncMap{
198-
"orNull": PrintOrNull,
198+
"orNull": printOrNull,
199199
}
200200

201201
buf := strings.Builder{}

internal/provider/license_resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ resource "coderd_license" "test" {
6161
}
6262
`
6363
funcMap := template.FuncMap{
64-
"orNull": PrintOrNull,
64+
"orNull": printOrNull,
6565
}
6666

6767
buf := strings.Builder{}

0 commit comments

Comments
 (0)