|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2016 IBM. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +""" |
| 16 | +API module/class for interacting with a security document in a database. |
| 17 | +""" |
| 18 | +import json |
| 19 | + |
| 20 | +from ._2to3 import url_quote_plus |
| 21 | + |
| 22 | +class SecurityDocument(dict): |
| 23 | + """ |
| 24 | + Encapsulates a JSON security document. A SecurityDocument object is |
| 25 | + instantiated with a reference to a database and used to manipulate security |
| 26 | + document content in a CouchDB or Cloudant database instance. |
| 27 | +
|
| 28 | + In addition to basic read/write operations, a SecurityDocument object also |
| 29 | + provides a convenient context manager. This context manager removes having |
| 30 | + to explicitly :func:`~cloudant.security_document.SecurityDocument.fetch` |
| 31 | + the security document from the remote database before commencing work on it |
| 32 | + as well as explicitly having to |
| 33 | + :func:`~cloudant.security_document.SecurityDocument.save` the security |
| 34 | + document once work is complete. |
| 35 | +
|
| 36 | + For example: |
| 37 | +
|
| 38 | + .. code-block:: python |
| 39 | +
|
| 40 | + # Upon entry into the security document context, fetches the security |
| 41 | + # document from the remote database, if it exists. Upon exit from the |
| 42 | + # context, saves the security document to the remote database with |
| 43 | + # changes made within the context. |
| 44 | + with SecurityDocument(database) as security_document: |
| 45 | + # The security document is fetched from the remote database |
| 46 | + # Changes are made locally |
| 47 | + security_document['Cloudant']['julia'] = ['_reader', '_writer'] |
| 48 | + security_document['Cloudant']['ruby'] = ['_admin', '_replicator'] |
| 49 | + # The security document is saved to the remote database |
| 50 | +
|
| 51 | + :param database: A database instance used by the SecurityDocument. Can be |
| 52 | + either a ``CouchDatabase`` or ``CloudantDatabase`` instance. |
| 53 | + """ |
| 54 | + def __init__(self, database): |
| 55 | + super(SecurityDocument, self).__init__() |
| 56 | + self._client = database.client |
| 57 | + self._database = database |
| 58 | + self._database_host = self._client.server_url |
| 59 | + self._database_name = database.database_name |
| 60 | + self.encoder = self._client.encoder |
| 61 | + |
| 62 | + @property |
| 63 | + def document_url(self): |
| 64 | + """ |
| 65 | + Constructs and returns the security document URL. |
| 66 | +
|
| 67 | + :returns: Security document URL |
| 68 | + """ |
| 69 | + return '/'.join([ |
| 70 | + self._database_host, |
| 71 | + url_quote_plus(self._database_name), |
| 72 | + '_security' |
| 73 | + ]) |
| 74 | + |
| 75 | + @property |
| 76 | + def r_session(self): |
| 77 | + """ |
| 78 | + Returns the Python requests session used by the security document. |
| 79 | +
|
| 80 | + :returns: The Python requests session |
| 81 | + """ |
| 82 | + return self._client.r_session |
| 83 | + |
| 84 | + def json(self): |
| 85 | + """ |
| 86 | + Retrieves the JSON string representation of the current locally cached |
| 87 | + security document object, encoded by the encoder specified in the |
| 88 | + associated client object. |
| 89 | +
|
| 90 | + :returns: Encoded JSON string containing the security document data |
| 91 | + """ |
| 92 | + return json.dumps(dict(self), cls=self.encoder) |
| 93 | + |
| 94 | + def fetch(self): |
| 95 | + """ |
| 96 | + Retrieves the content of the current security document from the remote |
| 97 | + database and populates the locally cached SecurityDocument object with |
| 98 | + that content. A call to fetch will overwrite any dictionary content |
| 99 | + currently in the locally cached SecurityDocument object. |
| 100 | + """ |
| 101 | + resp = self.r_session.get(self.document_url) |
| 102 | + resp.raise_for_status() |
| 103 | + self.clear() |
| 104 | + self.update(resp.json()) |
| 105 | + |
| 106 | + def save(self): |
| 107 | + """ |
| 108 | + Saves changes made to the locally cached SecurityDocument object's data |
| 109 | + structures to the remote database. |
| 110 | + """ |
| 111 | + resp = self.r_session.put( |
| 112 | + self.document_url, |
| 113 | + data=self.json(), |
| 114 | + headers={'Content-Type': 'application/json'} |
| 115 | + ) |
| 116 | + resp.raise_for_status() |
| 117 | + |
| 118 | + def __enter__(self): |
| 119 | + """ |
| 120 | + Supports context like editing of security document fields. |
| 121 | + Handles context entry logic. Executes a |
| 122 | + :func:`~cloudant.security_document.SecurityDocument.fetch` upon entry. |
| 123 | + """ |
| 124 | + self.fetch() |
| 125 | + return self |
| 126 | + |
| 127 | + def __exit__(self, *args): |
| 128 | + """ |
| 129 | + Support context like editing of security document fields. |
| 130 | + Handles context exit logic. Executes a |
| 131 | + :func:`~cloudant.security_document.SecurityDocument.save` upon exit. |
| 132 | + """ |
| 133 | + self.save() |
0 commit comments