blob: 38e921964406bd234f29485bfc1d87f4c72e1e4c [file] [log] [blame]
Jakub Josef8e7385e2016-12-07 21:20:34 +01001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josef8e7385e2016-12-07 21:20:34 +01003logger = logging.getLogger(__name__)
4
Ilya Kharin3d8bffe2017-06-22 17:40:31 +04005def __virtual__():
6 '''
7 Only load if jenkins_common module exist.
8 '''
9 if 'jenkins_common.call_groovy_script' not in __salt__:
10 return (
11 False,
12 'The jenkins_credentials state module cannot be loaded: '
13 'jenkins_common not found')
14 return True
15
16
Yuriy Taraday2e21e3c2018-02-06 20:17:58 +040017def present(name, scope, username, password="", desc="", key=None, secret=None, filename=None, content=None):
Jakub Josef8e7385e2016-12-07 21:20:34 +010018 """
19 Main jenkins credentials state method
20
21 :param name: credential name
22 :param scope: credential scope
Yuriy Taraday2e21e3c2018-02-06 20:17:58 +040023 :param username: username (optional)
Jakub Josef8e7385e2016-12-07 21:20:34 +010024 :param password: password (optional)
25 :param desc: credential description (optional)
26 :param key: credential key (optional)
Yuriy Taraday2e21e3c2018-02-06 20:17:58 +040027 :param filename: file name for file credential (optional)
28 :param content: file content for file credential (optional)
Jakub Josef8e7385e2016-12-07 21:20:34 +010029 :returns: salt-specified state dict
30 """
Yuriy Taraday2e21e3c2018-02-06 20:17:58 +040031 template = __salt__['jenkins_common.load_template'](
32 'salt://jenkins/files/groovy/credential.template',
33 __env__)
34 clazz = ""
35 if key:
36 clazz = "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
37 params = 'CredentialsScope.{}, "{}", "{}", new com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(key.trim()), "{}", "{}"'.format(
38 scope, name, username, password if password else "", desc if desc else "")
39 elif secret:
40 clazz = "org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl"
41 params = 'CredentialsScope.{}, "{}", "{}"'.format(
42 scope, name, desc if desc else "")
43 elif filename:
44 clazz = "org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl"
45 params = 'CredentialsScope.{}, "{}", "{}", "{}", SecretBytes.fromBytes("""{}""".getBytes())'.format(
46 scope, name, desc if desc else "", filename, content.encode('unicode_escape'))
Jakub Josef8e7385e2016-12-07 21:20:34 +010047 else:
Yuriy Taraday2e21e3c2018-02-06 20:17:58 +040048 clazz = "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
49 params = 'CredentialsScope.{}, "{}", "{}", "{}", "{}"'.format(
50 scope, name, desc if desc else "", username, password)
51 return __salt__['jenkins_common.api_call'](name, template,
52 ["CREATED", "EXISTS"],
53 {
54 "scope": scope,
55 "name": name,
56 "username": username if username else "",
57 "password": password if password else "",
58 "clazz": clazz,
59 "params": params,
60 "key": key if key else "",
61 "desc": desc if desc else "",
62 "secret": secret if secret else "",
63 "fileName": filename if filename else "",
64 "content": content.encode('unicode_escape') if content else ""
65 },
66 "Credentials")