Skip to content
Permalink
Browse files

Added more pipeline job creation parameters.

  • Loading branch information...
dihedron committed Jun 3, 2017
1 parent fcedcdf commit db0c25d2e412984b2ea030a2bf4f0c84010a7031
Showing with 166 additions and 119 deletions.
  1. +45 −3 config_xml.go
  2. +117 −114 resource_jenkins_job.go
  3. +4 −2 util.go
@@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
)

func createXML(d *schema.ResourceData) string {
func createConfigXML(d *schema.ResourceData) string {
var buffer bytes.Buffer
buffer.WriteString("<?xml version='1.0' encoding='UTF-8'?>\n")
buffer.WriteString("<flow-definition plugin=\"workflow-job@2.11\">\n")
@@ -40,13 +40,55 @@ func createXML(d *schema.ResourceData) string {

if value, ok := d.GetOk("github_project"); ok {
value := value.([]interface{})[0].(map[string]interface{})
log.Printf("[DEBUG] jenkins_pipeline::xml - type of value: %T", value)
buffer.WriteString(" <com.coravy.hudson.plugins.github.GithubProjectProperty plugin=\"github@1.27.0\">\n")
buffer.WriteString(fmt.Sprintf(" <projectUrl>%s</projectUrl>>\n", value["project_url"].(string)))
buffer.WriteString(fmt.Sprintf(" <projectUrl>%s</projectUrl>\n", value["project_url"].(string)))
buffer.WriteString(fmt.Sprintf(" <displayName>%s</displayName>\n", value["display_name"].(string)))
buffer.WriteString(" </com.coravy.hudson.plugins.github.GithubProjectProperty>\n")
}

// TODO: parameters

if value, ok := d.GetOk("throttle_builds"); ok {
value := value.([]interface{})[0].(map[string]interface{})
buffer.WriteString(" <jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl plugin=\"branch-api@2.0.9\">\n")
buffer.WriteString(fmt.Sprintf(" <durationName>%s</durationName>\n", value["period"].(string)))
buffer.WriteString(fmt.Sprintf(" <count>%d</count>\n", value["rate"].(int)))
buffer.WriteString(" </jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl>\n")
}

if value, ok := d.GetOk("build_after"); ok {
value := value.([]interface{})[0].(map[string]interface{})
log.Printf("[DEBUG] jenkins_pipeline::xml - type of value: %T", value)
buffer.WriteString(" <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>\n")
buffer.WriteString(" <triggers>\n")
buffer.WriteString(" <jenkins.triggers.ReverseBuildTrigger>\n")
buffer.WriteString(" <spec></spec>\n")
buffer.WriteString(fmt.Sprintf(" <upstreamProjects>%s</upstreamProjects>\n", value["projects"].(string)))
buffer.WriteString(" <threshold>\n")
switch value["threshold"] {
case "success":
buffer.WriteString(" <name>SUCCESS</name>\n")
buffer.WriteString(" <ordinal>0</ordinal>\n")
buffer.WriteString(" <color>BLUE</color>\n")
buffer.WriteString(" <completeBuild>true</completeBuild>\n")
case "unstable":
buffer.WriteString(" <name>UNSTABLE</name>\n")
buffer.WriteString(" <ordinal>1</ordinal>\n")
buffer.WriteString(" <color>YELLOW</color>\n")
buffer.WriteString(" <completeBuild>true</completeBuild>\n")
case "failure":
buffer.WriteString(" <name>FAILURE</name>\n")
buffer.WriteString(" <ordinal>2</ordinal>\n")
buffer.WriteString(" <color>RED</color>\n")
buffer.WriteString(" <completeBuild>true</completeBuild>\n")
}
buffer.WriteString(" </threshold>\n")
buffer.WriteString(" </jenkins.triggers.ReverseBuildTrigger>\n")
// TODO: whatever's left to add here...
buffer.WriteString(" </triggers>\n")
buffer.WriteString(" </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>\n")
}

buffer.WriteString(" </properties>\n")
buffer.WriteString(" <definition class=\"org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition\" plugin=\"workflow-cps@2.32\">\n")
buffer.WriteString(" <script></script>\n")
@@ -243,87 +243,89 @@ func resourceJenkinsJob() *schema.Resource {
},
},
},
"throttle_builds": {
Type: schema.TypeList,
Description: "Enforces a minimum time between builds based on the desired maximum rate.",
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rate": &schema.Schema{
Type: schema.TypeInt,
Description: "The maximum number of builds allowed within the specified time period.",
Optional: true,
ForceNew: true, // TODO:remove
Default: 1,
},
"period": &schema.Schema{
Type: schema.TypeString,
Description: "The time period within which the rate will be enforced (e.g. 2 builds per hour).",
Required: true,
ForceNew: true, // TODO:remove
ValidateFunc: validateAllowedStringsCaseInsensitive([]string{
"hour", "day", "week", "month", "year",
}),
},
*/
"throttle_builds": {
Type: schema.TypeList,
Description: "Enforces a minimum time between builds based on the desired maximum rate.",
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rate": &schema.Schema{
Type: schema.TypeInt,
Description: "The maximum number of builds allowed within the specified time period.",
Optional: true,
ForceNew: true, // TODO:remove
Default: 1,
},
"period": &schema.Schema{
Type: schema.TypeString,
Description: "The time period within which the rate will be enforced (e.g. 2 builds per hour).",
Required: true,
ForceNew: true, // TODO:remove
ValidateFunc: validateAllowedStringsCaseInsensitive([]string{
"hour", "day", "week", "month", "year",
}),
},
//
// goes into:
// <jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl plugin="branch-api@2.0.9">
// <durationName>week</durationName>
// <count>2</count>
// </jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl>
//
},
//
// goes into:
// <jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl plugin="branch-api@2.0.9">
// <durationName>week</durationName>
// <count>2</count>
// </jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl>
//
},
"build_after": {
Type: schema.TypeList,
Description: "The trigger so that when some other projects finish building, a new build is scheduled.",
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"projects": &schema.Schema{
Type: schema.TypeString,
Description: "The names of the projects to watch.",
Required: true,
ForceNew: true, // TODO:remove
},
"threshold": &schema.Schema{
Type: schema.TypeString,
Description: "Condition under which the build is triggered (stable only, even unstable, even failed).",
Required: true,
ForceNew: true, // TODO:remove
ValidateFunc: validateAllowedStringsCaseInsensitive([]string{
"success", "unstable", "failure",
}),
},
},

"build_after": {
Type: schema.TypeList,
Description: "The trigger so that when some other projects finish building, a new build is scheduled.",
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"projects": &schema.Schema{
Type: schema.TypeString,
Description: "The names of the projects to watch.",
Required: true,
ForceNew: true, // TODO:remove
},
"threshold": &schema.Schema{
Type: schema.TypeString,
Description: "Condition under which the build is triggered (stable only, even unstable, even failed).",
Required: true,
ForceNew: true, // TODO:remove
ValidateFunc: validateAllowedStringsCaseInsensitive([]string{
"success", "unstable", "failure",
}),
},
//
// goes into:
// <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
// <triggers>
// <jenkins.triggers.ReverseBuildTrigger>
// <spec></spec>
// <upstreamProjects>UPSTREAM_PROJECT</upstreamProjects>
// <threshold>
// <name>SUCCESS|UNSTABLE|FAILURE</name>
// <ordinal>0|1|2</ordinal>
// <color>BLUE|YELLOW|RED</color>
// <completeBuild>true</completeBuild>
// </threshold>
// </jenkins.triggers.ReverseBuildTrigger>
// [...]
// </triggers>
// </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
//
},
//
// goes into:
// <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
// <triggers>
// <jenkins.triggers.ReverseBuildTrigger>
// <spec></spec>
// <upstreamProjects>UPSTREAM_PROJECT</upstreamProjects>
// <threshold>
// <name>SUCCESS|UNSTABLE|FAILURE</name>
// <ordinal>0|1|2</ordinal>
// <color>BLUE|YELLOW|RED</color>
// <completeBuild>true</completeBuild>
// </threshold>
// </jenkins.triggers.ReverseBuildTrigger>
// [...]
// </triggers>
// </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
//
},
},
/*
"periodic_build_schedule": &schema.Schema{
Type: schema.TypeList,
Description: "Determines the schedule of periodic builds in a cron-like format.",
@@ -345,44 +347,45 @@ func resourceJenkinsJob() *schema.Resource {
// </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
//
},
"github_hook_trigger": &schema.Schema{
Type: schema.TypeBool,
Description: "Upon a PUSH request from the GitHub SCM hook, Jenkins will trigger Git polling.",
Optional: true,
ForceNew: true, // TODO:remove
Default: false,
// goes into:
// <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
// <triggers>
// [...]
// <com.cloudbees.jenkins.GitHubPushTrigger plugin="github@1.27.0">
// <spec></spec>
// </com.cloudbees.jenkins.GitHubPushTrigger>
// </triggers>
// </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
//
},
"scm_poll_schedule": &schema.Schema{
Type: schema.TypeList,
Description: "Determines the schedule of SCM polling in a cron-like format.",
Optional: true,
ForceNew: true, // TODO:remove
//
// goes into:
// <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
// <triggers>
// [...]
// <hudson.triggers.SCMTrigger>
// <spec># once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday.
//45 9-16/2 * * 1-5
//# once in every two hours slot between 9 AM and 5 PM every weekday (perhaps at 10:38 AM, 12:38 PM, 2:38 PM, 4:38 PM)
//H H(9-16)/2 * * 1-5</spec>
// <ignorePostCommitHooks>false|true</ignorePostCommitHooks>
// </hudson.triggers.SCMTrigger>
// </triggers>
// </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
//
},
"github_hook_trigger": &schema.Schema{
Type: schema.TypeBool,
Description: "Upon a PUSH request from the GitHub SCM hook, Jenkins will trigger Git polling.",
Optional: true,
ForceNew: true, // TODO:remove
Default: false,
// goes into:
// <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
// <triggers>
// [...]
// <com.cloudbees.jenkins.GitHubPushTrigger plugin="github@1.27.0">
// <spec></spec>
// </com.cloudbees.jenkins.GitHubPushTrigger>
// </triggers>
// </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
//
},
"scm_poll_schedule": &schema.Schema{
Type: schema.TypeList,
Description: "Determines the schedule of SCM polling in a cron-like format.",
Optional: true,
ForceNew: true, // TODO:remove
//
// goes into:
// <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
// <triggers>
// [...]
// <hudson.triggers.SCMTrigger>
// <spec># once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday.
//45 9-16/2 * * 1-5
//# once in every two hours slot between 9 AM and 5 PM every weekday (perhaps at 10:38 AM, 12:38 PM, 2:38 PM, 4:38 PM)
//H H(9-16)/2 * * 1-5</spec>
// <ignorePostCommitHooks>false|true</ignorePostCommitHooks>
// </hudson.triggers.SCMTrigger>
// </triggers>
// </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
//
},
*/
},
}
@@ -416,7 +419,7 @@ func resourceJenkinsJobCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*jenkins.Jenkins)

name := d.Get("name").(string)
xml := createXML(d)
xml := createConfigXML(d)
job, err := client.CreateJob(xml, name)

log.Printf("[DEBUG] jenkins_pipeline::create - job %q created", name)
@@ -2,15 +2,17 @@ package main

import (
"fmt"
"log"
"strings"

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

func validateAllowedStringsCaseInsensitive(ss []string) schema.SchemaValidateFunc {
log.Printf("[DEBUG] jenkins_pipeline::validate - length of %v: %d", ss, len(ss))
ll := make([]string, 0, len(ss))
for i, s := range ss {
ll[i] = strings.ToLower(s)
for _, s := range ss {
ll = append(ll, strings.ToLower(s))
}
return func(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToLower(v.(string))

0 comments on commit db0c25d

Please sign in to comment.
You can’t perform that action at this time.