Skip to content

Commit

Permalink
add disable_concurrent_builds_job_property (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgramoll committed Aug 20, 2019
1 parent dd3345e commit 3188a76
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ resource "jenkins_job_parameter_definition_choice" "env" {
choices = ["1", "3", "4"]
}
resource "jenkins_job_disable_concurrent_builds_property" "main" {
job = "${jenkins_job.main.name}"
}
```

## Development ##
Expand Down
36 changes: 36 additions & 0 deletions client/job_disable_concurrent_builds_job_property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package client

import (
"encoding/xml"
)

func init() {
propertyUnmarshalFunc["org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty"] = unmarshalJobDisableConcurrentBuildsJobProperty
}

type JobDisableConcurrentBuildsJobProperty struct {
XMLName xml.Name `xml:"org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty"`
Id string `xml:"id,attr,omitempty"`
Plugin string `xml:"plugin,attr,omitempty"`
}

func NewJobDisableConcurrentBuildsJobProperty() *JobDisableConcurrentBuildsJobProperty {
return &JobDisableConcurrentBuildsJobProperty{}
}

func (property *JobDisableConcurrentBuildsJobProperty) GetId() string {
return property.Id
}

func (p *JobDisableConcurrentBuildsJobProperty) SetId(id string) {
p.Id = id
}

func unmarshalJobDisableConcurrentBuildsJobProperty(d *xml.Decoder, start xml.StartElement) (JobProperty, error) {
property := NewJobDisableConcurrentBuildsJobProperty()
err := d.DecodeElement(property, &start)
if err != nil {
return nil, err
}
return property, nil
}
59 changes: 59 additions & 0 deletions client/job_disable_concurrent_builds_job_property_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package client

import (
"encoding/xml"
"testing"

"github.com/sergi/go-diff/diffmatchpatch"
)

var disableConcurrentBuildsJobProperty *JobDisableConcurrentBuildsJobProperty

func init() {
disableConcurrentBuildsJobProperty = NewJobDisableConcurrentBuildsJobProperty()
}

func TestJobDisableConcurrentBuildsJobPropertyDeserialize(t *testing.T) {
var job Job
err := xml.Unmarshal([]byte(expectedDisableConcurrentBuildsJobPropertyJson), &job)
if err != nil {
t.Fatalf("failed to deserialize xml %s", err)
}
if job.Properties == nil || len(*job.Properties.Items) == 0 {
t.Fatalf("failed to deserialize properties")
}
_, ok := (*job.Properties.Items)[0].(*JobDisableConcurrentBuildsJobProperty)
if !ok {
t.Fatalf("failed to deserialize JobDisableConcurrentBuildsJobProperty")
}
}

func TestJobDisableConcurrentBuildsJobPropertySerialize(t *testing.T) {
job := NewJob()
jobDisableConcurrentBuildsJobProperty := NewJobDisableConcurrentBuildsJobProperty()
job.Properties = job.Properties.Append(jobDisableConcurrentBuildsJobProperty)

config := JobConfigFromJob(job)
resultBytes, err := xml.MarshalIndent(config, "", "\t")
if err != nil {
t.Fatalf("failed to serialize xml %s", err)
}
result := string(resultBytes)

if result != expectedDisableConcurrentBuildsJobPropertyJson {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(expectedDisableConcurrentBuildsJobPropertyJson, result, true)
t.Fatalf("job definition not expected: %s", dmp.DiffPrettyText(diffs))
}
}

var expectedDisableConcurrentBuildsJobPropertyJson = `<flow-definition>
<actions></actions>
<description></description>
<keepDependencies>false</keepDependencies>
<properties>
<org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty></org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty>
</properties>
<triggers></triggers>
<disabled>false</disabled>
</flow-definition>`
27 changes: 27 additions & 0 deletions importer/resource_job_disable_concurrent_builds_job_property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"

"github.com/jgramoll/terraform-provider-jenkins/client"
"github.com/jgramoll/terraform-provider-jenkins/provider"
)

func init() {
jobPropertyCodeFuncs["*client.JobDisableConcurrentBuildsJobProperty"] = jobDisableConcurrentBuildsJobPropertyCode
jobPropertyImportScriptFuncs["*client.JobDisableConcurrentBuildsJobProperty"] = jobDisableConcurrentBuildsJobPropertyImportScript
}

func jobDisableConcurrentBuildsJobPropertyCode(propertyIndex string, property client.JobProperty) string {
return fmt.Sprintf(`
resource "jenkins_job_disable_concurrent_builds_property" "property_%v" {
job = "${jenkins_job.main.name}"
}
`, propertyIndex)
}

func jobDisableConcurrentBuildsJobPropertyImportScript(propertyIndex string, jobName string, p client.JobProperty) string {
return fmt.Sprintf(`
terraform import jenkins_job_disable_concurrent_builds_property.property_%v "%v"
`, propertyIndex, provider.ResourceJobPropertyId(jobName, p.GetId()))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"fmt"
"testing"

"github.com/jgramoll/terraform-provider-jenkins/client"
"github.com/jgramoll/terraform-provider-jenkins/provider"
"github.com/sergi/go-diff/diffmatchpatch"
)

func testDisableConcurrentBuildsJobProperty() *client.JobDisableConcurrentBuildsJobProperty {
property := client.NewJobDisableConcurrentBuildsJobProperty()
return property
}

func TestEnsureJobDisableConcurrentBuildsJobProperty(t *testing.T) {
job := client.NewJob()
property := testDisableConcurrentBuildsJobProperty()
job.Properties = job.Properties.Append(property)

if err := ensureJob(job); err != nil {
t.Fatal(err)
}
if property.Id == "" {
t.Fatalf("Did not set Disable Concurrent Builds Property Id")
}
}

func TestJobDisableConcurrentBuildsJobPropertyCode(t *testing.T) {
job := client.NewJob()
property := testDisableConcurrentBuildsJobProperty()
job.Properties = job.Properties.Append(property)

result := jobCode(job)
expected := `resource "jenkins_job" "main" {
name = ""
plugin = ""
disabled = false
}
resource "jenkins_job_disable_concurrent_builds_property" "property_1" {
job = "${jenkins_job.main.name}"
}
`
if result != expected {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(expected, result, false)
t.Fatalf("job terraform code not as expected: %s", dmp.DiffPrettyText(diffs))
}
}

func TestJobDisableConcurrentBuildsJobPropertyImportScript(t *testing.T) {
job := client.NewJob()
job.Id = "id"
job.Name = "name"
property := testDisableConcurrentBuildsJobProperty()
property.Id = "paramPropertyId"
job.Properties = job.Properties.Append(property)

result := jobImportScript(job)
expected := fmt.Sprintf(`terraform init
terraform import jenkins_job.main "name"
terraform import jenkins_job_disable_concurrent_builds_property.property_1 "%v"
`, provider.ResourceJobPropertyId(job.Name, property.Id))

if result != expected {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(expected, result, false)
t.Fatalf("job terraform import script not as expected: %s", dmp.DiffPrettyText(diffs))
}
}
35 changes: 35 additions & 0 deletions provider/job_disable_concurrent_builds_job_property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package provider

import (
"fmt"
"reflect"

"github.com/hashicorp/terraform/helper/schema"
"github.com/jgramoll/terraform-provider-jenkins/client"
)

type jobDisableConcurrentBuildsJobProperty struct{}

func newJobDisableConcurrentBuildsJobProperty() *jobDisableConcurrentBuildsJobProperty {
return &jobDisableConcurrentBuildsJobProperty{}
}

func (p *jobDisableConcurrentBuildsJobProperty) toClientProperty(id string) client.JobProperty {
clientProperty := client.NewJobDisableConcurrentBuildsJobProperty()
clientProperty.Id = id
return clientProperty
}

func (p *jobDisableConcurrentBuildsJobProperty) fromClientJobProperty(clientPropertyInterface client.JobProperty) (jobProperty, error) {
_, ok := clientPropertyInterface.(*client.JobDisableConcurrentBuildsJobProperty)
if !ok {
return nil, fmt.Errorf("Property is not of expected type, expected *client.JobDisableConcurrentBuildsJobProperty, actually %s",
reflect.TypeOf(clientPropertyInterface).String())
}
property := newJobDisableConcurrentBuildsJobProperty()
return property, nil
}

func (p *jobDisableConcurrentBuildsJobProperty) setResourceData(d *schema.ResourceData) error {
return nil
}
34 changes: 34 additions & 0 deletions provider/job_disable_concurrent_builds_job_property_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package provider

import (
"github.com/hashicorp/terraform/helper/schema"
)

func jobDisableConcurrentBuildsJobPropertyResource() *schema.Resource {
newPropertyInterface := func() jobProperty {
return newJobDisableConcurrentBuildsJobProperty()
}
return &schema.Resource{
Create: func(d *schema.ResourceData, m interface{}) error {
return resourceJobPropertyCreate(d, m, newPropertyInterface)
},
Read: func(d *schema.ResourceData, m interface{}) error {
return resourceJobPropertyRead(d, m, newPropertyInterface)
},
Delete: func(d *schema.ResourceData, m interface{}) error {
return resourceJobPropertyDelete(d, m, newPropertyInterface)
},
Importer: &schema.ResourceImporter{
State: resourceJobPropertyImporter,
},

Schema: map[string]*schema.Schema{
"job": &schema.Schema{
Type: schema.TypeString,
Description: "Name of the job",
Required: true,
ForceNew: true,
},
},
}
}
99 changes: 99 additions & 0 deletions provider/job_disable_concurrent_builds_job_property_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package provider

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/jgramoll/terraform-provider-jenkins/client"
)

func TestAccJobDisableConcurrentBuildsJobPropertyBasic(t *testing.T) {
var jobRef client.Job
var properties []client.JobProperty
jobName := fmt.Sprintf("%s/tf-acc-test-%s", jenkinsFolder, acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
jobResourceName := "jenkins_job.main"
property1 := "jenkins_job_disable_concurrent_builds_property.prop_1"
property2 := "jenkins_job_disable_concurrent_builds_property.prop_2"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccJobDisableConcurrentBuildsJobPropertyConfigBasic(jobName, 2),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobExists(jobResourceName, &jobRef),
testAccCheckJobProperties(&jobRef, []string{
property1,
property2,
}, &properties, ensureJobDisableConcurrentBuildsJobProperty),
),
},
{
ResourceName: property1,
ImportStateId: "invalid",
ImportState: true,
ExpectError: regexp.MustCompile("Invalid property id"),
},
{
ResourceName: property1,
ImportState: true,
ImportStateIdFunc: func(*terraform.State) (string, error) {
if len(properties) == 0 {
return "", fmt.Errorf("no properties to import")
}
return ResourceJobPropertyId(jobName, properties[0].GetId()), nil
},
ImportStateVerify: true,
},
{
ResourceName: property2,
ImportState: true,
ImportStateIdFunc: func(*terraform.State) (string, error) {
if len(properties) == 0 {
return "", fmt.Errorf("no properties to import")
}
return ResourceJobPropertyId(jobName, properties[1].GetId()), nil
},
ImportStateVerify: true,
},
{
Config: testAccJobDisableConcurrentBuildsJobPropertyConfigBasic(jobName, 1),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobExists(jobResourceName, &jobRef),
testAccCheckJobProperties(&jobRef, []string{
property1,
}, &properties, ensureJobDisableConcurrentBuildsJobProperty),
),
},
{
Config: testAccJobDisableConcurrentBuildsJobPropertyConfigBasic(jobName, 0),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobExists(jobResourceName, &jobRef),
testAccCheckJobProperties(&jobRef, []string{}, &properties, ensureJobDisableConcurrentBuildsJobProperty),
),
},
},
})
}

func testAccJobDisableConcurrentBuildsJobPropertyConfigBasic(jobName string, count int) string {
properties := ""
for i := 1; i <= count; i++ {
properties += fmt.Sprintf(`
resource "jenkins_job_disable_concurrent_builds_property" "prop_%v" {
job = "${jenkins_job.main.name}"
}`, i)
}

return testAccJobConfigBasic(jobName) + properties
}

func ensureJobDisableConcurrentBuildsJobProperty(propertyInterface client.JobProperty, resource *terraform.ResourceState) error {
_, err := newJobDisableConcurrentBuildsJobProperty().fromClientJobProperty(propertyInterface)
return err
}
2 changes: 2 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func Provider() terraform.ResourceProvider {

"jenkins_job_parameters_definition_property": jobParametersDefinitionPropertyResource(),
"jenkins_job_parameter_definition_choice": jobParameterDefinitionChoiceResource(),

"jenkins_job_disable_concurrent_builds_property": jobDisableConcurrentBuildsJobPropertyResource(),
},

ConfigureFunc: providerConfigure,
Expand Down

0 comments on commit 3188a76

Please sign in to comment.