Made a common helper function - convert_mapping_to_xml

This will allow other modules to convert a mapping set to xml easily
with defaults so other methods don't have to create their own.

Includes behaviour to selectively omit or include empty tags.

Behaviour for default value:
  None - omit this tag if users didn't put anything
  "" - create an empty element if users didn't put anything

This is applied to the existing openshift plugin functions in
refactoring as Jenkins actually retains an empty XML tag when the
optional 'auth-token' setting is missing from the input job definition.
Which results in modifying the default test's output for the openshift
plugin.

Change-Id: I935a70ad191bc0f3f3dbd571b2b1cf7468106697
This commit is contained in:
Ari LiVigni 2015-10-08 15:55:28 -04:00 committed by Darragh Bailey
parent 556deebe25
commit 101f74645d
11 changed files with 33 additions and 30 deletions

View File

@ -46,6 +46,7 @@ from jenkins_jobs.modules.helpers import cloudformation_stack
from jenkins_jobs.modules.helpers import config_file_provider_builder
from jenkins_jobs.modules.helpers import config_file_provider_settings
from jenkins_jobs.modules.helpers import copyartifact_build_selector
from jenkins_jobs.modules.helpers import convert_mapping_to_xml
from jenkins_jobs.errors import (JenkinsJobsException,
MissingAttributeError,
InvalidAttributeError)
@ -2407,21 +2408,6 @@ def cloudformation(parser, xml_parent, data):
region_dict)
def _openshift_common(osb, data, mapping):
for elem in mapping:
(optname, xmlname, val) = elem
val = data.get(optname, val)
# Skip adding xml entry if default is empty string
# and no value given
if not val and elem[2] is '':
continue
if str(val).lower() == 'true' or str(val).lower() == 'false':
val = str(val).lower()
xe = XML.SubElement(osb, xmlname)
xe.text = str(val)
def openshift_build_verify(parser, xml_parent, data):
"""yaml: openshift-build-verify
Performs the equivalent of an 'oc get builds` command invocation for the
@ -2465,7 +2451,7 @@ def openshift_build_verify(parser, xml_parent, data):
("auth-token", 'authToken', ''),
]
_openshift_common(osb, data, mapping)
convert_mapping_to_xml(osb, data, mapping)
def openshift_builder(parser, xml_parent, data):
@ -2510,7 +2496,7 @@ def openshift_builder(parser, xml_parent, data):
("follow-log", 'followLog', 'true'),
]
_openshift_common(osb, data, mapping)
convert_mapping_to_xml(osb, data, mapping)
def openshift_dep_verify(parser, xml_parent, data):
@ -2560,7 +2546,7 @@ def openshift_dep_verify(parser, xml_parent, data):
("auth-token", 'authToken', ''),
]
_openshift_common(osb, data, mapping)
convert_mapping_to_xml(osb, data, mapping)
def openshift_deployer(parser, xml_parent, data):
@ -2604,7 +2590,7 @@ def openshift_deployer(parser, xml_parent, data):
("auth-token", 'authToken', ''),
]
_openshift_common(osb, data, mapping)
convert_mapping_to_xml(osb, data, mapping)
def openshift_img_tagger(parser, xml_parent, data):
@ -2653,7 +2639,7 @@ def openshift_img_tagger(parser, xml_parent, data):
("auth-token", 'authToken', ''),
]
_openshift_common(osb, data, mapping)
convert_mapping_to_xml(osb, data, mapping)
def openshift_scaler(parser, xml_parent, data):
@ -2698,7 +2684,7 @@ def openshift_scaler(parser, xml_parent, data):
("auth-token", 'authToken', ''),
]
_openshift_common(osb, data, mapping)
convert_mapping_to_xml(osb, data, mapping)
def openshift_svc_verify(parser, xml_parent, data):
@ -2741,4 +2727,4 @@ def openshift_svc_verify(parser, xml_parent, data):
("auth-token", 'authToken', ''),
]
_openshift_common(osb, data, mapping)
convert_mapping_to_xml(osb, data, mapping)

View File

@ -395,3 +395,18 @@ def artifactory_repository(xml_parent, data, target):
'deploy-snapshot-repo-key', '')
XML.SubElement(xml_parent, 'dynamicMode').text = str(
data.get('deploy-dynamic-mode', False)).lower()
def convert_mapping_to_xml(parent, data, mapping):
for elem in mapping:
(optname, xmlname, val) = elem
val = data.get(optname, val)
# if no value is provided then continue else leave it
# up to the user if they want to use an empty XML tag
if val is None:
continue
if str(val).lower() == 'true' or str(val).lower() == 'false':
val = str(val).lower()
xe = XML.SubElement(parent, xmlname)
xe.text = str(val)

View File

@ -39,6 +39,7 @@ Example of an empty ``scm``:
import logging
import xml.etree.ElementTree as XML
import jenkins_jobs.modules.base
from jenkins_jobs.modules.helpers import convert_mapping_to_xml
from jenkins_jobs.errors import (InvalidAttributeError,
JenkinsJobsException,
MissingAttributeError)
@ -1096,14 +1097,7 @@ def openshift_img_streams(parser, xml_parent, data):
("auth-token", 'authToken', ''),
]
for elem in mapping:
(optname, xmlname, val) = elem
val = data.get(optname, val)
# Skip adding xml entry if default is empty string and no value given
if not val and elem[2] is '':
continue
xe = XML.SubElement(scm, xmlname)
xe.text = str(val)
convert_mapping_to_xml(scm, data, mapping)
class SCM(jenkins_jobs.modules.base.Base):

View File

@ -5,6 +5,7 @@
<apiURL>https://openshift.default.svc.cluster.local</apiURL>
<bldCfg>frontend</bldCfg>
<namespace>test</namespace>
<authToken/>
</com.openshift.openshiftjenkinsbuildutils.OpenShiftBuildVerifier>
</builders>
</project>

View File

@ -5,6 +5,7 @@
<apiURL>https://openshift.default.svc.cluster.local</apiURL>
<bldCfg>frontend</bldCfg>
<namespace>test</namespace>
<authToken/>
<followLog>true</followLog>
</com.openshift.openshiftjenkinsbuildutils.OpenShiftBuilder>
</builders>

View File

@ -6,6 +6,7 @@
<depCfg>frontend</depCfg>
<namespace>test</namespace>
<replicaCount>0</replicaCount>
<authToken/>
</com.openshift.openshiftjenkinsbuildutils.OpenShiftDeploymentVerifier>
</builders>
</project>

View File

@ -5,6 +5,7 @@
<apiURL>https://openshift.default.svc.cluster.local</apiURL>
<depCfg>frontend</depCfg>
<namespace>test</namespace>
<authToken/>
</com.openshift.openshiftjenkinsbuildutils.OpenShiftDeployer>
</builders>
</project>

View File

@ -6,6 +6,7 @@
<testTag>origin-nodejs-sample:latest</testTag>
<prodTag>origin-nodejs-sample:prod</prodTag>
<namespace>test</namespace>
<authToken/>
</com.openshift.openshiftjenkinsbuildutils.OpenShiftImageTagger>
</builders>
</project>

View File

@ -6,6 +6,7 @@
<depCfg>frontend</depCfg>
<namespace>test</namespace>
<replicaCount>0</replicaCount>
<authToken/>
</com.openshift.openshiftjenkinsbuildutils.OpenShiftScaler>
</builders>
</project>

View File

@ -5,6 +5,7 @@
<apiURL>https://openshift.default.svc.cluster.local</apiURL>
<svcName>frontend</svcName>
<namespace>test</namespace>
<authToken/>
</com.openshift.openshiftjenkinsbuildutils.OpenShiftServiceVerifier>
</builders>
</project>

View File

@ -5,5 +5,6 @@
<tag>latest</tag>
<apiURL>https://openshift.default.svc.cluster.local</apiURL>
<namespace>test</namespace>
<authToken/>
</scm>
</project>