Skip to content

Commit db10e24

Browse files
authored
Merge pull request #472 from jeremad/jrm/pages_visibility
feat(project): add the pages_access_level parameter
2 parents dce2bac + 489a6bc commit db10e24

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

docs/resources/project.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ The following arguments are supported:
8282

8383
* `group_with_project_templates_id` - (Optional) For group-level custom templates, specifies ID of group from which all the custom project templates are sourced. Leave empty for instance-level templates. Requires use_custom_template to be true (enterprise edition).
8484

85+
* `pages_access_level` - (Optional) Enable pages access control
86+
Valid values are `disabled`, `private`, `enabled`, `public`.
87+
`private` is the default.
88+
8589
## Attributes Reference
8690

8791
The following additional attributes are exported:

gitlab/resource_gitlab_instance_cluster_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ func testAccCheckGitlabInstanceClusterAttributes(cluster *gitlab.InstanceCluster
217217

218218
func testAccGitlabInstanceClusterConfig(rInt int, managed bool) string {
219219
m := fmt.Sprintf("%t", managed)
220-
fmt.Println(m)
221220

222221
return fmt.Sprintf(`
223222
variable "cert" {

gitlab/resource_gitlab_project.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
268268
Type: schema.TypeInt,
269269
Optional: true,
270270
},
271+
"pages_access_level": {
272+
Type: schema.TypeString,
273+
Optional: true,
274+
Default: "private",
275+
ValidateFunc: validation.StringInSlice([]string{"public", "private", "enabled", "disabled"}, true),
276+
},
271277
}
272278

273279
func resourceGitlabProject() *schema.Resource {
@@ -313,6 +319,7 @@ func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Pro
313319
d.Set("archived", project.Archived)
314320
d.Set("remove_source_branch_after_merge", project.RemoveSourceBranchAfterMerge)
315321
d.Set("packages_enabled", project.PackagesEnabled)
322+
d.Set("pages_access_level", string(project.PagesAccessLevel))
316323
}
317324

318325
func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error {
@@ -378,6 +385,10 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
378385
options.GroupWithProjectTemplatesID = gitlab.Int(v.(int))
379386
}
380387

388+
if v, ok := d.GetOk("pages_access_level"); ok {
389+
options.PagesAccessLevel = stringToAccessControlValue(v.(string))
390+
}
391+
381392
log.Printf("[DEBUG] create gitlab project %q", *options.Name)
382393

383394
project, _, err := client.Projects.CreateProject(options)
@@ -558,6 +569,10 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
558569
options.PackagesEnabled = gitlab.Bool(d.Get("packages_enabled").(bool))
559570
}
560571

572+
if d.HasChange("pages_access_level") {
573+
options.PagesAccessLevel = stringToAccessControlValue(d.Get("pages_access_level").(string))
574+
}
575+
561576
if *options != (gitlab.EditProjectOptions{}) {
562577
log.Printf("[DEBUG] update gitlab project %s", d.Id())
563578
_, _, err := client.Projects.EditProject(d.Id(), options)

gitlab/resource_gitlab_project_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ func TestAccGitlabProject_basic(t *testing.T) {
4141
MergeMethod: gitlab.FastForwardMerge,
4242
OnlyAllowMergeIfPipelineSucceeds: true,
4343
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
44-
Archived: false, // needless, but let's make this explicit
45-
PackagesEnabled: true,
44+
Archived: false, // needless, but let's make this explicit
45+
PackagesEnabled: true,
46+
PagesAccessLevel: gitlab.PublicAccessControl,
4647
}
4748

4849
defaultsMasterBranch = defaults
@@ -82,8 +83,9 @@ func TestAccGitlabProject_basic(t *testing.T) {
8283
MergeMethod: gitlab.FastForwardMerge,
8384
OnlyAllowMergeIfPipelineSucceeds: true,
8485
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
85-
Archived: true,
86-
PackagesEnabled: false,
86+
Archived: true,
87+
PackagesEnabled: false,
88+
PagesAccessLevel: gitlab.DisabledAccessControl,
8789
}, &received),
8890
),
8991
},
@@ -324,7 +326,8 @@ func TestAccGitlabProject_willError(t *testing.T) {
324326
MergeMethod: gitlab.FastForwardMerge,
325327
OnlyAllowMergeIfPipelineSucceeds: true,
326328
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
327-
PackagesEnabled: true,
329+
PackagesEnabled: true,
330+
PagesAccessLevel: gitlab.PublicAccessControl,
328331
}
329332
willError := defaults
330333
willError.TagList = []string{"notatag"}
@@ -423,7 +426,8 @@ func TestAccGitlabProject_transfer(t *testing.T) {
423426
MergeMethod: gitlab.NoFastForwardMerge,
424427
OnlyAllowMergeIfPipelineSucceeds: false,
425428
OnlyAllowMergeIfAllDiscussionsAreResolved: false,
426-
PackagesEnabled: true,
429+
PackagesEnabled: true,
430+
PagesAccessLevel: gitlab.PrivateAccessControl,
427431
}
428432

429433
resource.Test(t, resource.TestCase{
@@ -746,6 +750,7 @@ resource "gitlab_project" "foo" {
746750
merge_method = "ff"
747751
only_allow_merge_if_pipeline_succeeds = true
748752
only_allow_merge_if_all_discussions_are_resolved = true
753+
pages_access_level = "public"
749754
}
750755
`, rInt, rInt, defaultBranchStatement)
751756
}
@@ -803,6 +808,7 @@ resource "gitlab_project" "foo" {
803808
shared_runners_enabled = false
804809
archived = true
805810
packages_enabled = false
811+
pages_access_level = "disabled"
806812
}
807813
`, rInt, rInt)
808814
}

gitlab/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ func stringToMergeMethod(s string) *gitlab.MergeMethodValue {
142142
return &value
143143
}
144144

145+
func stringToAccessControlValue(s string) *gitlab.AccessControlValue {
146+
lookup := map[string]gitlab.AccessControlValue{
147+
"disabled": gitlab.DisabledAccessControl,
148+
"enabled": gitlab.EnabledAccessControl,
149+
"private": gitlab.PrivateAccessControl,
150+
"public": gitlab.PublicAccessControl,
151+
}
152+
153+
value, ok := lookup[s]
154+
if !ok {
155+
return nil
156+
}
157+
return &value
158+
}
159+
145160
var StringIsGitlabVariableName = func(v interface{}, k string) (s []string, es []error) {
146161
value, ok := v.(string)
147162
if !ok {

0 commit comments

Comments
 (0)