Skip to content

Commit

Permalink
add job_datadog_job_property
Browse files Browse the repository at this point in the history
- cleaned up the job property unmarshal to use map
  • Loading branch information
jgramoll committed Aug 1, 2019
1 parent 3b009a4 commit 2c46df9
Show file tree
Hide file tree
Showing 23 changed files with 337 additions and 82 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,46 @@ provider "jenkins" {
address = "${var.jenkins_address}"
}
resource "jenkins_job" "premerge" {
resource "jenkins_job" "main" {
name = "Premerge checks"
}
resource "jenkins_job_declarative_job_action" premerge {
job = "${jenkins_job.premerge.id}"
resource "jenkins_job_declarative_job_action" "main" {
job = "${jenkins_job.main.id}"
}
resource "jenkins_job_declarative_job_property_tracker_action" premerge {
job = "${jenkins_job.premerge.id}"
resource "jenkins_job_declarative_job_property_tracker_action" "main" {
job = "${jenkins_job.main.id}"
}
resource "jenkins_job_git_scm" "premerge" {
job = "${jenkins_job.premerge.id}"
resource "jenkins_job_git_scm" "main" {
job = "${jenkins_job.main.id}"
config_version = "2"
script_path = "Jenkinsfile.api"
lightweight = false
}
resource "jenkins_job_git_scm_user_remote_config" "premerge" {
scm = "${jenkins_job_git_scm.premerge.id}"
resource "jenkins_job_git_scm_user_remote_config" "main" {
scm = "${jenkins_job_git_scm.main.id}"
refspec = "GERRIT_REFSPEC"
url = "ssh://git.server/git-repo.git"
credentials_id = "123-abc"
}
resource "jenkins_job_git_scm_branch" "premerge" {
scm = "${jenkins_job_git_scm.premerge.id}"
resource "jenkins_job_git_scm_branch" "main" {
scm = "${jenkins_job_git_scm.main.id}"
name = "FETCH_HEAD"
}
resource "jenkins_job_git_scm_clean_before_checkout_extension" "premerge" {
scm = "${jenkins_job_git_scm.premerge.id}"
resource "jenkins_job_git_scm_clean_before_checkout_extension" "main" {
scm = "${jenkins_job_git_scm.main.id}"
}
resource "jenkins_job_build_discarder_property" "main" {
job = "${jenkins_job.premerge.id}"
job = "${jenkins_job.main.id}"
}
resource "jenkins_job_build_discarder_property_log_rotator_strategy" "main" {
Expand All @@ -85,7 +85,7 @@ resource "jenkins_job_build_discarder_property_log_rotator_strategy" "main" {
}
resource "jenkins_job_pipeline_triggers_property" "main" {
job = "${jenkins_job.premerge.id}"
job = "${jenkins_job.main.id}"
}
resource "jenkins_job_gerrit_trigger" "main" {
Expand Down Expand Up @@ -137,15 +137,15 @@ resource "jenkins_job_gerrit_branch" "main" {
pattern = "^(?!refs/meta/config).*$"
}
resource "jenkins_job_datadog_job_property" "main" {
job = "${jenkins_job.main.id}"
}
```

## TODO

1. import resources
1. Refactor unmarshall to use map
1. Refactor types = reflect checks
1. DatadogJobProperty
<!-- <org.datadog.jenkins.plugins.datadog.DatadogJobProperty plugin="datadog@0.7.1">
<emitOnCheckout>false</emitOnCheckout>
</org.datadog.jenkins.plugins.datadog.DatadogJobProperty> -->
1. Fragile TestAccJobBuildDiscarderPropertyStrategyLogRotatorBasic test
15 changes: 13 additions & 2 deletions client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,26 @@ func newJobFromConfigAndDetails(config *jobConfig, details *jobDetails) *Job {
}

func (job *Job) GetProperty(propertyId string) (JobProperty, error) {
properties := *(job.Properties).Items
for _, property := range properties {
for _, property := range *job.Properties.Items {
if property.GetId() == propertyId {
return property, nil
}
}
return nil, ErrJobPropertyNotFound
}

func (job *Job) UpdateProperty(property JobProperty) error {
properties := *job.Properties.Items
propertyId := property.GetId()
for i, oldProp := range properties {
if oldProp.GetId() == propertyId {
properties[i] = property
return nil
}
}
return ErrJobPropertyNotFound
}

func (job *Job) DeleteProperty(propertyId string) error {
properties := *job.Properties.Items
for i, property := range properties {
Expand Down
13 changes: 13 additions & 0 deletions client/job_build_discarder_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package client

import "encoding/xml"

func init() {
propertyUnmarshalFunc["jenkins.model.BuildDiscarderProperty"] = unmarshalBuildDiscarderProperty
}

type JobBuildDiscarderProperty struct {
XMLName xml.Name `xml:"jenkins.model.BuildDiscarderProperty"`
Id string `xml:"id,attr"`
Expand All @@ -18,3 +22,12 @@ func NewJobBuildDiscarderProperty() *JobBuildDiscarderProperty {
func (property *JobBuildDiscarderProperty) GetId() string {
return property.Id
}

func unmarshalBuildDiscarderProperty(d *xml.Decoder, start xml.StartElement) (JobProperty, error) {
property := NewJobBuildDiscarderProperty()
err := d.DecodeElement(property, &start)
if err != nil {
return nil, err
}
return property, nil
}
7 changes: 7 additions & 0 deletions client/job_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func TestJobConfigSerialize(t *testing.T) {
definition.SCM.Branches = definition.SCM.Branches.Append(branchSpec)
job.Definition = definition

datadogJobProperty := NewJobDatadogJobProperty()
datadogJobProperty.Plugin = "datadog@0.7.1"
job.Properties = job.Properties.Append(datadogJobProperty)

gerritBranch := NewJobGerritTriggerBranch()
gerritBranch.CompareType = CompareTypeRegExp
gerritBranch.Pattern = "my-branch"
Expand Down Expand Up @@ -81,6 +85,9 @@ func TestJobConfigSerialize(t *testing.T) {
<description>my-desc</description>
<keepDependencies>false</keepDependencies>
<properties>
<org.datadog.jenkins.plugins.datadog.DatadogJobProperty plugin="datadog@0.7.1">
<emitOnCheckout>false</emitOnCheckout>
</org.datadog.jenkins.plugins.datadog.DatadogJobProperty>
<org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty id="trigger-id">
<triggers>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger plugin="gerrit-trigger@2.29.0">
Expand Down
36 changes: 36 additions & 0 deletions client/job_datadog_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.datadog.jenkins.plugins.datadog.DatadogJobProperty"] = unmarshalJobDatadogJobProperty
}

type JobDatadogJobProperty struct {
XMLName xml.Name `xml:"org.datadog.jenkins.plugins.datadog.DatadogJobProperty"`
Id string `xml:"id,attr,omitempty"`
Plugin string `xml:"plugin,attr,omitempty"`

EmitOnCheckout bool `xml:"emitOnCheckout"`
}

func NewJobDatadogJobProperty() *JobDatadogJobProperty {
return &JobDatadogJobProperty{
EmitOnCheckout: false,
}
}

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

func unmarshalJobDatadogJobProperty(d *xml.Decoder, start xml.StartElement) (JobProperty, error) {
property := NewJobDatadogJobProperty()
err := d.DecodeElement(property, &start)
if err != nil {
return nil, err
}
return property, nil
}
13 changes: 13 additions & 0 deletions client/job_pipeline_triggers_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"errors"
)

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

// ErrJobTriggerNotFound job property not found
var ErrJobTriggerNotFound = errors.New("Could not find job pipeline trigger")

Expand All @@ -24,6 +28,15 @@ func (property *JobPipelineTriggersProperty) GetId() string {
return property.Id
}

func unmarshalPipelineTriggersProperty(d *xml.Decoder, start xml.StartElement) (JobProperty, error) {
property := NewJobPipelineTriggersProperty()
err := d.DecodeElement(property, &start)
if err != nil {
return nil, err
}
return property, nil
}

func (property *JobPipelineTriggersProperty) GetTrigger(triggerId string) (JobTrigger, error) {
triggers := *(property.Triggers).Items
for _, trigger := range triggers {
Expand Down
18 changes: 6 additions & 12 deletions client/job_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"encoding/xml"
)

type PropertyBuilder func(*xml.Decoder, xml.StartElement) (JobProperty, error)

var propertyUnmarshalFunc = map[string]PropertyBuilder{}

type JobProperties struct {
XMLName xml.Name `xml:"properties"`
Items *[]JobProperty `xml:",any"`
Expand Down Expand Up @@ -31,18 +35,8 @@ func (properties *JobProperties) UnmarshalXML(d *xml.Decoder, start xml.StartEle
properties.Items = &[]JobProperty{}
for tok, err = d.Token(); err == nil; tok, err = d.Token() {
if elem, ok := tok.(xml.StartElement); ok {
// TODO use map
switch elem.Name.Local {
case "jenkins.model.BuildDiscarderProperty":
property := NewJobBuildDiscarderProperty()
err := d.DecodeElement(property, &elem)
if err != nil {
return err
}
*properties.Items = append(*(properties).Items, property)
case "org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty":
property := NewJobPipelineTriggersProperty()
err := d.DecodeElement(property, &elem)
if unmarshalXML, ok := propertyUnmarshalFunc[elem.Name.Local]; ok {
property, err := unmarshalXML(d, elem)
if err != nil {
return err
}
Expand Down
10 changes: 7 additions & 3 deletions client/job_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ func TestGetJobDetails(t *testing.T) {
t.Fatalf("Job description should be %v, was %v", "", job.Description)
}
properties := *job.Properties.Items
if len(properties) != 2 {
t.Fatalf("Job should have %v properties, was %v", 2, len(properties))
if len(properties) != 3 {
t.Fatalf("Job should have %v properties, was %v", 3, len(properties))
}
pipelineDiscarderProperty := *properties[1].(*JobBuildDiscarderProperty)
pipelineDiscarderProperty := *properties[2].(*JobBuildDiscarderProperty)
if pipelineDiscarderProperty.Strategy.Item == nil {
t.Fatalf("Job missing discarder strategy")
}
_, ok := properties[1].(*JobDatadogJobProperty)
if !ok {
t.Fatalf("Invalid datadog property, got %s", reflect.TypeOf(properties[2]).String())
}
pipelineTriggersProperty := *properties[0].(*JobPipelineTriggersProperty)
triggers := *pipelineTriggersProperty.Triggers
if len(*triggers.Items) != 1 {
Expand Down
11 changes: 9 additions & 2 deletions provider/job_build_discarder_property.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package provider

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jgramoll/terraform-provider-jenkins/client"
"reflect"
)

type jobBuildDiscarderProperty struct{}
Expand All @@ -17,9 +19,14 @@ func (p *jobBuildDiscarderProperty) toClientProperty(id string) client.JobProper
return clientProperty
}

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

func (p *jobBuildDiscarderProperty) setResourceData(d *schema.ResourceData) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ func jobBuildDiscarderPropertyStrategyLogRotatorResource() *schema.Resource {
Create: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyCreate(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Read: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyRead(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Update: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyUpdate(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Read: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyRead(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Delete: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyDelete(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Expand Down
13 changes: 2 additions & 11 deletions provider/job_build_discarder_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"fmt"
"reflect"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand All @@ -11,10 +10,6 @@ import (
"github.com/jgramoll/terraform-provider-jenkins/client"
)

func init() {
jobPropertyTypes["jenkins_job_build_discarder_property"] = reflect.TypeOf((*client.JobBuildDiscarderProperty)(nil))
}

func TestAccJobBuildDiscarderPropertyBasic(t *testing.T) {
var jobRef client.Job
var properties []client.JobProperty
Expand Down Expand Up @@ -71,10 +66,6 @@ resource "jenkins_job_build_discarder_property" "prop_%v" {
}

func ensureJobBuildDiscarderProperty(propertyInterface client.JobProperty, resource *terraform.ResourceState) error {
_, ok := propertyInterface.(*client.JobBuildDiscarderProperty)
if !ok {
return fmt.Errorf("Property is not of expected type, expected *client.JobBuildDiscarderProperty, actually %s",
reflect.TypeOf(propertyInterface).String())
}
return nil
_, err := newJobBuildDiscarderProperty().fromClientJobProperty(propertyInterface)
return err
}
38 changes: 38 additions & 0 deletions provider/job_datadog_job_property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package provider

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jgramoll/terraform-provider-jenkins/client"
"reflect"
)

type jobDatadogJobProperty struct {
EmitOnCheckout bool `mapstruture:"emit_on_checkout"`
}

func newJobDatadogJobProperty() *jobDatadogJobProperty {
return &jobDatadogJobProperty{}
}

func (p *jobDatadogJobProperty) toClientProperty(id string) client.JobProperty {
clientProperty := client.NewJobDatadogJobProperty()
clientProperty.Id = id
clientProperty.EmitOnCheckout = p.EmitOnCheckout
return clientProperty
}

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

func (p *jobDatadogJobProperty) setResourceData(d *schema.ResourceData) error {
return d.Set("emit_on_checkout", p.EmitOnCheckout)
}
Loading

0 comments on commit 2c46df9

Please sign in to comment.