|
| 1 | +import base64 |
| 2 | + |
| 3 | +from .. import utils |
| 4 | + |
| 5 | + |
| 6 | +class SecretApiMixin(object): |
| 7 | + @utils.minimum_version('1.25') |
| 8 | + def create_secret(self, name, data, labels=None): |
| 9 | + """ |
| 10 | + Create a secret |
| 11 | +
|
| 12 | + Args: |
| 13 | + name (string): Name of the secret |
| 14 | + data (bytes): Secret data to be stored |
| 15 | + labels (dict): A mapping of labels to assign to the secret |
| 16 | +
|
| 17 | + Returns (dict): ID of the newly created secret |
| 18 | + """ |
| 19 | + if not isinstance(data, bytes): |
| 20 | + data = data.encode('utf-8') |
| 21 | + |
| 22 | + data = base64.b64encode(data) |
| 23 | + body = { |
| 24 | + 'Data': data, |
| 25 | + 'Name': name, |
| 26 | + 'Labels': labels |
| 27 | + } |
| 28 | + |
| 29 | + url = self._url('/secrets/create') |
| 30 | + return self._result( |
| 31 | + self._post_json(url, data=body), True |
| 32 | + ) |
| 33 | + |
| 34 | + @utils.minimum_version('1.25') |
| 35 | + @utils.check_resource |
| 36 | + def inspect_secret(self, id): |
| 37 | + """ |
| 38 | + Retrieve secret metadata |
| 39 | +
|
| 40 | + Args: |
| 41 | + id (string): Full ID of the secret to remove |
| 42 | +
|
| 43 | + Returns (dict): A dictionary of metadata |
| 44 | +
|
| 45 | + Raises: |
| 46 | + :py:class:`docker.errors.NotFound` |
| 47 | + if no secret with that ID exists |
| 48 | + """ |
| 49 | + url = self._url('/secrets/{0}', id) |
| 50 | + return self._result(self._get(url), True) |
| 51 | + |
| 52 | + @utils.minimum_version('1.25') |
| 53 | + @utils.check_resource |
| 54 | + def remove_secret(self, id): |
| 55 | + """ |
| 56 | + Remove a secret |
| 57 | +
|
| 58 | + Args: |
| 59 | + id (string): Full ID of the secret to remove |
| 60 | +
|
| 61 | + Returns (boolean): True if successful |
| 62 | +
|
| 63 | + Raises: |
| 64 | + :py:class:`docker.errors.NotFound` |
| 65 | + if no secret with that ID exists |
| 66 | + """ |
| 67 | + url = self._url('/secrets/{0}', id) |
| 68 | + res = self._delete(url) |
| 69 | + self._raise_for_status(res) |
| 70 | + return True |
| 71 | + |
| 72 | + @utils.minimum_version('1.25') |
| 73 | + def secrets(self, filters=None): |
| 74 | + """ |
| 75 | + List secrets |
| 76 | +
|
| 77 | + Args: |
| 78 | + filters (dict): A map of filters to process on the secrets |
| 79 | + list. Available filters: ``names`` |
| 80 | +
|
| 81 | + Returns (list): A list of secrets |
| 82 | + """ |
| 83 | + url = self._url('/secrets') |
| 84 | + params = {} |
| 85 | + if filters: |
| 86 | + params['filters'] = utils.convert_filters(filters) |
| 87 | + return self._result(self._get(url, params=params), True) |
0 commit comments