Skip to content

Commit

Permalink
Add display_name support to jenkins_folder resource (taiidani#53)
Browse files Browse the repository at this point in the history
* Add `display_name` support to `jenkins_folder` resource

An optional `display_name` argument can now be set so that you can specify a custom name to display in the Jenkins UI for the folder.

* Truly support `display_name` being optional

If it's not set, then we omit it from the XML payload we generate and submit to the Jenkins API, and Jenkins defaults to using `name`.

And added an additional test case so we cover both setting and not setting `display_name`.

Co-authored-by: Ryan Nixon <r.taiidani@gmail.com>
  • Loading branch information
zahiar and taiidani committed Jul 23, 2021
1 parent 3a471c6 commit 184ba2b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The full canonical folder path, E.G. `/job/parent`.
* `description` - A block of text describing the folder's purpose.
* `display_name` - The name of the folder that is displayed in the UI.
* `template` - A Jenkins-compatible XML template to describe the folder.
1 change: 1 addition & 0 deletions docs/resources/folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ resource "jenkins_folder" "example_child" {
The following arguments are supported:

* `name` - (Required) The name of the folder being created.
* `display_name` - (Optional) The name of the folder to be displayed in the UI.
* `folder` - (Optional) The folder namespace to store the subfolder in. If creating in a nested folder structure you may separate folder names with `/`, such as `parent/child`. This name cannot be changed once the folder has been created, and all parent folders must be created in advance.
* `description` - (Optional) A block of text describing the folder's purpose.
* `security` - (Optional) An optional block defining a project-based authorization strategy, documented below.
Expand Down
5 changes: 5 additions & 0 deletions jenkins/data_source_jenkins_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func dataSourceJenkinsFolder() *schema.Resource {
Required: true,
ValidateDiagFunc: validateJobName,
},
"display_name": {
Type: schema.TypeString,
Description: "The name of the folder to display in the UI.",
Computed: true,
},
"folder": {
Type: schema.TypeString,
Description: "The folder namespace that the folder exists in.",
Expand Down
8 changes: 6 additions & 2 deletions jenkins/data_source_jenkins_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ func TestAccJenkinsFolderDataSource_basic(t *testing.T) {
Config: fmt.Sprintf(`
resource jenkins_folder foo {
name = "tf-acc-test-%s"
display_name = "TF Acceptance Test %s"
description = "Terraform acceptance tests %s"
}
data jenkins_folder foo {
name = jenkins_folder.foo.name
}`, randString, randString),
}`, randString, randString, randString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_folder.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("data.jenkins_folder.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("data.jenkins_folder.foo", "name", "tf-acc-test-"+randString),
resource.TestCheckResourceAttr("data.jenkins_folder.foo", "description", "Terraform acceptance tests "+randString),
resource.TestCheckResourceAttr("data.jenkins_folder.foo", "display_name", "TF Acceptance Test "+randString),
),
},
},
Expand All @@ -51,20 +53,22 @@ func TestAccJenkinsFolderDataSource_nested(t *testing.T) {
resource jenkins_folder sub {
name = "subfolder"
display_name = "TF Acceptance Test %s"
folder = jenkins_folder.foo.id
description = "Terraform acceptance tests %s"
}
data jenkins_folder sub {
name = jenkins_folder.sub.name
folder = jenkins_folder.sub.folder
}`, randString, randString),
}`, randString, randString, randString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_folder.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.sub", "id", "/job/tf-acc-test-"+randString+"/job/subfolder"),
resource.TestCheckResourceAttr("data.jenkins_folder.sub", "name", "subfolder"),
resource.TestCheckResourceAttr("data.jenkins_folder.sub", "folder", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("data.jenkins_folder.sub", "description", "Terraform acceptance tests "+randString),
resource.TestCheckResourceAttr("data.jenkins_folder.sub", "display_name", "TF Acceptance Test "+randString),
),
},
},
Expand Down
1 change: 1 addition & 0 deletions jenkins/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type folder struct {
XMLName xml.Name `xml:"com.cloudbees.hudson.plugins.folder.Folder"`
Description string `xml:"description"`
DisplayName string `xml:"displayName,omitempty"`
Properties folderProperties `xml:"properties"`
FolderViews xmlRawProperty `xml:"folderViews"`
HealthMetrics xmlRawProperty `xml:"healthMetrics"`
Expand Down
6 changes: 6 additions & 0 deletions jenkins/folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Test_parseFolder(t *testing.T) {
def: `<com.cloudbees.hudson.plugins.folder.Folder plugin="cloudbees-folder@6.15">
<actions/>
<description>Example Description</description>
<displayName>Example Display Name</displayName>
<properties>
<com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty>
<inheritanceStrategy class="org.jenkinsci.plugins.matrixauth.inheritance.InheritParentStrategy"/>
Expand Down Expand Up @@ -87,6 +88,7 @@ func Test_parseFolder(t *testing.T) {
want: &folder{
XMLName: xml.Name{Local: "com.cloudbees.hudson.plugins.folder.Folder"},
Description: "Example Description",
DisplayName: "Example Display Name",
Properties: folderProperties{
Security: &folderSecurity{
InheritanceStrategy: folderPermissionInheritanceStrategy{
Expand Down Expand Up @@ -190,6 +192,7 @@ func Test_parseFolder(t *testing.T) {
func Test_folder_Render(t *testing.T) {
type fields struct {
Description string
DisplayName string
Properties folderProperties
}
tests := []struct {
Expand All @@ -202,6 +205,7 @@ func Test_folder_Render(t *testing.T) {
name: "success",
fields: fields{
Description: "Example Description",
DisplayName: "Example Display Name",
Properties: folderProperties{
Security: &folderSecurity{
Permission: []string{"example", "permission"},
Expand Down Expand Up @@ -229,6 +233,7 @@ func Test_folder_Render(t *testing.T) {
},
want: []byte(`<com.cloudbees.hudson.plugins.folder.Folder>
<description>Example Description</description>
<displayName>Example Display Name</displayName>
<properties>
<com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty>
<inheritanceStrategy class="org.jenkinsci.plugins.matrixauth.inheritance.InheritParentStrategy"></inheritanceStrategy>
Expand All @@ -255,6 +260,7 @@ func Test_folder_Render(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
j := &folder{
Description: tt.fields.Description,
DisplayName: tt.fields.DisplayName,
Properties: tt.fields.Properties,
}
got, err := j.Render()
Expand Down
11 changes: 11 additions & 0 deletions jenkins/resource_jenkins_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func resourceJenkinsFolder() *schema.Resource {
ForceNew: true,
ValidateDiagFunc: validateJobName,
},
"display_name": {
Type: schema.TypeString,
Description: "The name of the folder to display in the UI.",
Optional: true,
},
"folder": {
Type: schema.TypeString,
Description: "The folder namespace that the folder will be added to as a subfolder.",
Expand Down Expand Up @@ -83,6 +88,7 @@ func resourceJenkinsFolderCreate(ctx context.Context, d *schema.ResourceData, me

f := folder{
Description: d.Get("description").(string),
DisplayName: d.Get("display_name").(string),
}
f.Properties.Security = expandSecurity(d.Get("security").(*schema.Set).List())

Expand Down Expand Up @@ -143,6 +149,10 @@ func resourceJenkinsFolderRead(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(err)
}

if err := d.Set("display_name", f.DisplayName); err != nil {
return diag.FromErr(err)
}

if err := d.Set("folder", formatFolderID(folders)); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -182,6 +192,7 @@ func resourceJenkinsFolderUpdate(ctx context.Context, d *schema.ResourceData, me

// Then update the values
f.Description = d.Get("description").(string)
f.DisplayName = d.Get("display_name").(string)
f.Properties.Security = expandSecurity(d.Get("security").(*schema.Set).List())

// And send it back to Jenkins
Expand Down
30 changes: 29 additions & 1 deletion jenkins/resource_jenkins_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ func TestAccJenkinsFolder_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_folder.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.foo", "name", "tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.foo", "display_name", ""),
),
},
},
})
}

func TestAccJenkinsFolder_withDisplayName(t *testing.T) {
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckJenkinsFolderDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource jenkins_folder foo {
name = "tf-acc-test-%s"
display_name = "TF Acceptance Test %s"
description = "Terraform acceptance tests %s"
}`, randString, randString, randString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_folder.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.foo", "name", "tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.foo", "display_name", "TF Acceptance Test "+randString),
),
},
},
Expand All @@ -50,15 +76,17 @@ func TestAccJenkinsFolder_nested(t *testing.T) {
resource jenkins_folder sub {
name = "subfolder"
display_name = "TF Acceptance Test %s"
folder = jenkins_folder.foo.id
description = "Terraform acceptance tests ${jenkins_folder.foo.name}"
}`, randString, randString),
}`, randString, randString, randString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_folder.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.foo", "name", "tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.sub", "id", "/job/tf-acc-test-"+randString+"/job/subfolder"),
resource.TestCheckResourceAttr("jenkins_folder.sub", "name", "subfolder"),
resource.TestCheckResourceAttr("jenkins_folder.sub", "folder", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.sub", "display_name", "TF Acceptance Test "+randString),
),
},
},
Expand Down

0 comments on commit 184ba2b

Please sign in to comment.