Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CredentialsById.update to create/update credential by id #809

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions jenkinsapi/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,53 @@ def __getitem__(self, credential_id):

raise KeyError('Credential with credential_id "%s" not found'
% credential_id)

def update(self, credential):
"""
Creates or updates credential in Jenkins. The credential.credential_id
must be set.

If credential.credential_id already exists - this method is going to
update existing Credential

:param Credential credential: The credential to create/update.
"""
if not credential.credential_id:
raise JenkinsAPIException('Credential Id must be set before '
'creating/update credential.')
cred_id = credential.credential_id
if cred_id not in self:
params = credential.get_attributes()
url = (
'%s/createCredentials'
% self.baseurl
)
try:
self.jenkins.requester.post_and_confirm_status(
url, params={}, data=urlencode(params)
)
except JenkinsAPIException as jae:
raise JenkinsAPIException('Latest version of Credentials '
'plugin is required to be able '
'to create credentials. '
'Original exception: %s' % str(jae))
else:
params = credential.get_attributes_xml()
url = (
'%s/credential/%s/config.xml'
% (self.baseurl, cred_id)
)
try:
self.jenkins.requester.post_xml_and_confirm_status(
url, params={}, data=params
)
except JenkinsAPIException as jae:
raise JenkinsAPIException('Latest version of Credentials '
'plugin is required to be able '
'to update credentials. '
'Original exception: %s' % str(jae))

self.poll()
self.credentials = self._data['credentials']
if cred_id not in self:
raise JenkinsAPIException('Problem creating/updating credential.')