From 1368205c0e5404b9e7ef6482da8789d456a90eed Mon Sep 17 00:00:00 2001 From: Mike Dorman Date: Thu, 27 Oct 2016 09:45:35 -0600 Subject: [PATCH] Add support for credentials job parameter type Adds support for the 'credentials' parameter type for job definitions. Requires the Jenkins Credentials Plugin. Change-Id: I5e410668caed9e95bed7afb02341a456d2ece5e3 Story: 2000761 --- jenkins_jobs/modules/parameters.py | 55 +++++++++++++++++++ .../fixtures/credentials-param001.xml | 16 ++++++ .../fixtures/credentials-param001.yaml | 6 ++ 3 files changed, 77 insertions(+) create mode 100644 tests/parameters/fixtures/credentials-param001.xml create mode 100644 tests/parameters/fixtures/credentials-param001.yaml diff --git a/jenkins_jobs/modules/parameters.py b/jenkins_jobs/modules/parameters.py index 5a4f8603c..843cd9c67 100644 --- a/jenkins_jobs/modules/parameters.py +++ b/jenkins_jobs/modules/parameters.py @@ -285,6 +285,61 @@ def choice_param(registry, xml_parent, data): XML.SubElement(a, 'string').text = choice +def credentials_param(registry, xml_parent, data): + """yaml: credentials + A credentials selection parameter. Requires the Jenkins + :jenkins-wiki:`Credentials Plugin + `. + + :arg str name: the name of the parameter + :arg str type: credential type (optional, default 'any') + + :Allowed Values: * **any** Any credential type (default) + * **usernamepassword** Username with password + * **sshkey** SSH Username with private key + * **secretfile** Secret file + * **secrettext** Secret text + * **certificate** Certificate + + :arg bool required: whether this parameter is required (optional, default + false) + :arg string default: default credentials ID (optional) + :arg str description: a description of the parameter (optional) + + Example:: + + .. literalinclude:: \ + /../../tests/parameters/fixtures/credentials-param001.yaml + :language: yaml + + """ + cred_impl_types = { + 'any': 'com.cloudbees.plugins.credentials.common.StandardCredentials', + 'usernamepassword': 'com.cloudbees.plugins.credentials.impl.' + + 'UsernamePasswordCredentialsImpl', + 'sshkey': 'com.cloudbees.jenkins.plugins.sshcredentials.impl.' + + 'BasicSSHUserPrivateKey', + 'secretfile': 'org.jenkinsci.plugins.plaincredentials.impl.' + + 'FileCredentialsImpl', + 'secrettext': 'org.jenkinsci.plugins.plaincredentials.impl.' + + 'StringCredentialsImpl', + 'certificate': 'com.cloudbees.plugins.credentials.impl.' + + 'CertificateCredentialsImpl' + } + + cred_type = data.get('type', 'any').lower() + if cred_type not in cred_impl_types: + raise InvalidAttributeError('type', cred_type, cred_impl_types.keys()) + + pdef = base_param(registry, xml_parent, data, False, + 'com.cloudbees.plugins.credentials.' + + 'CredentialsParameterDefinition') + XML.SubElement(pdef, 'defaultValue').text = data.get('default', '') + XML.SubElement(pdef, 'credentialType').text = cred_impl_types[cred_type] + XML.SubElement(pdef, 'required').text = str(data.get('required', + False)).lower() + + def run_param(registry, xml_parent, data): """yaml: run A run parameter. diff --git a/tests/parameters/fixtures/credentials-param001.xml b/tests/parameters/fixtures/credentials-param001.xml new file mode 100644 index 000000000..e9345743e --- /dev/null +++ b/tests/parameters/fixtures/credentials-param001.xml @@ -0,0 +1,16 @@ + + + + + + + OS_CREDENTIALS + Test credentials + default-credentials-id + com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl + false + + + + + diff --git a/tests/parameters/fixtures/credentials-param001.yaml b/tests/parameters/fixtures/credentials-param001.yaml new file mode 100644 index 000000000..cf1fe5cd7 --- /dev/null +++ b/tests/parameters/fixtures/credentials-param001.yaml @@ -0,0 +1,6 @@ +parameters: + - credentials: + name: OS_CREDENTIALS + type: usernamepassword + default: "default-credentials-id" + description: "Test credentials"