Implemented existence checking in user state.
diff --git a/_modules/jenkins_common.py b/_modules/jenkins_common.py
index e4ae5d8..493aebe 100644
--- a/_modules/jenkins_common.py
+++ b/_modules/jenkins_common.py
@@ -6,12 +6,18 @@
logger = logging.getLogger(__name__)
-def call_groovy_script(script, props):
+def call_groovy_script(script, props, username=None, password=None, success_status_codes=[200]):
"""
Common method for call Jenkins groovy script API
- :param script groovy script template
- :param props groovy script properties
+ :param script: groovy script template
+ :param props: groovy script properties
+ :param username: jenkins username (optional,
+ if missing creds from sall will be used)
+ :param password: jenkins password (optional,
+ if missing creds from sall will be used)
+ :param success_status_codes: success response status code
+ (optional) in some cases we want to declare error call as success
:returns: HTTP dict {status,code,msg}
"""
ret = {
@@ -20,6 +26,11 @@
"msg": ""
}
jenkins_url, jenkins_user, jenkins_password = get_jenkins_auth()
+ if username:
+ jenkins_user = username
+ if password:
+ jenkins_password = password
+
if not jenkins_url:
raise SaltInvocationError('No Jenkins URL found.')
@@ -33,7 +44,7 @@
auth=(jenkins_user, jenkins_password),
data=req_data)
ret["code"] = req.status_code
- if req.status_code == 200:
+ if req.status_code in success_status_codes:
ret["status"] = "SUCCESS"
ret["msg"] = req.text
logger.debug("Jenkins script API call success: %s", ret)
@@ -72,15 +83,15 @@
auth=(jenkins_user, jenkins_password) if jenkins_user else None)
if tokenReq.status_code == 200:
return tokenReq.json()
- elif tokenReq.status_code == 404:
- # 404 means CSRF security is disabled, so api crumb is not necessary
+ elif tokenReq.status_code in [404, 401]:
+ # 404 means CSRF security is disabled, so api crumb is not necessary,
+ # 401 means unauthorized
return None
else:
raise Exception("Cannot obtain Jenkins API crumb. Status code: %s. Text: %s" %
(tokenReq.status_code, tokenReq.text))
-
def get_jenkins_auth():
"""
Get jenkins params from salt