Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/data-sources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `role` (String)
- `role` (String) Role assigned to the user or group. Valid roles are `admin` and `use`.


<a id="nestedatt--acl--users"></a>
Expand All @@ -95,7 +95,7 @@ Read-Only:
Read-Only:

- `id` (String)
- `role` (String)
- `role` (String) Role assigned to the user or group. Valid roles are `admin` and `use`.



Expand Down
4 changes: 2 additions & 2 deletions docs/resources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Required:
Required:

- `id` (String)
- `role` (String)
- `role` (String) Role assigned to the user or group. Valid roles are `admin` and `use`.


<a id="nestedatt--acl--users"></a>
Expand All @@ -113,7 +113,7 @@ Required:
Required:

- `id` (String)
- `role` (String)
- `role` (String) Role assigned to the user or group. Valid roles are `admin` and `use`.



Expand Down
3 changes: 2 additions & 1 deletion internal/provider/template_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ var computedPermissionAttribute = schema.SetNestedAttribute{
Computed: true,
},
"role": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Role assigned to the user or group. Valid roles are `admin` and `use`.",
Computed: true,
},
},
},
Expand Down
11 changes: 10 additions & 1 deletion internal/provider/template_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ var permissionAttribute = schema.SetNestedAttribute{
Required: true,
},
"role": schema.StringAttribute{
Required: true,
MarkdownDescription: "Role assigned to the user or group. Valid roles are `admin` and `use`.",
Required: true,
Validators: []validator.String{
templateACLRoleValidator,
},
},
},
},
Expand Down Expand Up @@ -1179,6 +1183,11 @@ var weekValidator = setvalidator.ValueStringsAre(
stringvalidator.OneOf("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"),
)

var templateACLRoleValidator = stringvalidator.OneOf(
string(codersdk.TemplateRoleAdmin),
string(codersdk.TemplateRoleUse),
)

func uploadDirectory(ctx context.Context, client *codersdk.Client, logger slog.Logger, directory string) (*codersdk.UploadResponse, error) {
pipeReader, pipeWriter := io.Pipe()
go func() {
Expand Down
58 changes: 58 additions & 0 deletions internal/provider/template_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ func mustVariablesToSet(vars []Variable) types.Set {
return s
}

func TestTemplateACLRoleValidator(t *testing.T) {
t.Parallel()

for _, role := range []string{"admin", "use"} {
t.Run(role, func(t *testing.T) {
t.Parallel()
var resp validator.StringResponse
templateACLRoleValidator.ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(role),
}, &resp)
require.False(t, resp.Diagnostics.HasError())
})
}

t.Run("invalid", func(t *testing.T) {
t.Parallel()
var resp validator.StringResponse
templateACLRoleValidator.ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue("owner"),
}, &resp)
require.True(t, resp.Diagnostics.HasError())
})
}

func TestAccTemplateResource(t *testing.T) {
t.Parallel()
if os.Getenv("TF_ACC") == "" {
Expand Down Expand Up @@ -681,6 +705,40 @@ func TestAccTemplateResource(t *testing.T) {
},
})
})

t.Run("InvalidACLRole", func(t *testing.T) {
cfg1 := testAccTemplateResourceConfig{
URL: client.URL.String(),
Token: client.SessionToken(),
Name: ptr.Ref("example-template"),
Versions: ptr.Ref([]testAccTemplateVersionConfig{
{
Directory: &exTemplateOne,
Active: ptr.Ref(true),
},
}),
ACL: testAccTemplateACLConfig{
GroupACL: []testAccTemplateKeyValueConfig{
{
Key: ptr.Ref(firstUser.OrganizationIDs[0].String()),
Value: ptr.Ref("owner"),
},
},
},
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IsUnitTest: true,
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: cfg1.String(t),
ExpectError: regexp.MustCompile(`value must be one of: \["admin" "use"\]`),
},
},
})
})
}

// TestAccTemplateResourceOptionalVersions covers PLAT-288: `versions` is
Expand Down