blob: c4048cfb75593e63a40eb49ecb945fa13b631a1a [file] [log] [blame]
Ales Komarek49a37292016-08-31 16:18:31 +02001# -*- coding: utf-8 -*-
2'''
3Module for handling gerrit calls.
4
5:optdepends: - gerritlib Python adapter
6:configuration: This module is not usable until the following are specified
7 either in a pillar or in the minion's config file::
8
9 gerrit.host: localhost
10 gerrit.user: admin
11 gerrit.key: |
12 -----BEGIN RSA PRIVATE KEY-----
13 MIIEowIBAAKCAQEAs0Y8mxS3dfs5zG8Du5vdBkfOCOng1IEUmFZIirJ8oBgJOd54
14 ...
15 jvMXms60iD/A5OpG33LWHNNzQBP486SxG75LB+Xs5sp5j2/b7VF5LJLhpGiJv9Mk
16 ydbuy8iuuvali2uF133kAlLqnrWfVTYQQI1OfW5glOv1L6kv94dU
17 -----END RSA PRIVATE KEY-----
18
19'''
20
21from __future__ import absolute_import
22
23import logging
24import os
25
26LOG = logging.getLogger(__name__)
27
28# Import third party libs
29HAS_GERRIT = False
30try:
31 from gerritlib import gerrit
32 HAS_GERRIT = True
33except ImportError:
34 pass
35
36
37def __virtual__():
38 '''
39 Only load this module if gerrit
40 is installed on this minion.
41 '''
42 if HAS_GERRIT:
43 return 'gerrit'
44 return False
45
46__opts__ = {}
47
48
49def auth(**connection_args):
50 '''
51 Set up gerrit credentials
52
53 Only intended to be used within gerrit-enabled modules
54 '''
55
56 prefix = "gerrit"
57
58 # look in connection_args first, then default to config file
59 def get(key, default=None):
60 return connection_args.get('connection_' + key,
61 __salt__['config.get'](prefix, {})).get(key, default)
62
63 host = get('host', 'localhost')
64 user = get('user', 'admin')
65 keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa')
66
67 gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile)
68 return gerrit_client
69
70
71def project_create(name, **kwargs):
72 '''
73 Create a gerrit project
74
75 :param name: new project name
76
77 CLI Examples:
78
79 .. code-block:: bash
80
81 salt '*' gerrit.project_create namespace/nova description='nova project'
82
83 '''
84 ret = {}
85 gerrit_client = auth(**kwargs)
86
87 project = project_get(name, **kwargs)
88
89 if project and not "Error" in project:
90 LOG.debug("Project {0} exists".format(name))
91 return project
92
93 new = gerrit_client.createProject(name)
94 return project_get(name, **kwargs)
95
96def project_get(name, **kwargs):
97 '''
98 Return a specific project
99
100 CLI Examples:
101
102 .. code-block:: bash
103
104 salt '*' gerrit.project_get projectname
105 '''
106 gerrit_client = auth(**kwargs)
107 ret = {}
108
109 projects = gerrit_client.listProjects()
110 if not name in projects:
111 return {'Error': 'Error in retrieving project'}
112 ret[name] = {'name': name}
113 return ret
114
115
116def project_list(**connection_args):
117 '''
118 Return a list of available projects
119
120 CLI Example:
121
122 .. code-block:: bash
123
124 salt '*' gerrit.project_list
125 '''
126 gerrit_client = auth(**connection_args)
127 ret = {}
128
129 projects = gerrit_client.listProjects()
130
131 for project in projects:
132 ret[project] = {
133 'name': project
134 }
135 return ret
136
137
138def query(change, **kwargs):
139 '''
140 Query gerrit
141
142 :param change: Query content
143
144 CLI Examples:
145
146 .. code-block:: bash
147
148 salt '*' gerrit.query 'status:open project:tools/gerrit limit:2'
149
150 '''
151 ret = {}
152 gerrit_client = auth(**kwargs)
153 msg = gerrit_client.query(change)
154 ret['query'] = msg
155 return ret