def _get(self, model_instance): """ Returns data from a store and returns back a model. :param model_instance: Model instance to search and return :type model_instance: commissaire.model.Model :returns: The model instance :rtype: commissaire.model.Model :raises StorageLookupError: if data lookup fails """ try: key = self._format_key(model_instance) etcd_resp = self._store.get(key) json_value = json.loads(etcd_resp.value) # XXX Temporary hack to create Host and HostCreds models # from the same etcd key. if isinstance(model_instance, models.HostCreds): new_json_value = {'address': json_value.get('address')} if 'ssh_priv_key' in json_value: new_json_value['ssh_priv_key'] = json_value['ssh_priv_key'] if 'remote_user' in json_value: new_json_value['remote_user'] = json_value['remote_user'] json_value = new_json_value return model_instance.new(**json_value) except etcd.EtcdKeyNotFound as error: raise StorageLookupError(str(error), model_instance)
def _delete(self, model_instance): """ Deletes a serialized SecretModel string from Custodia. :param model_instance: SecretModel instance to delete. :type model_instance: commissaire.model.SecretModel :raises StorageLookupError: if data lookup fails (404 Not Found) :raises requests.HTTPError: if the request fails (other than 404) """ url = self._build_key_url(model_instance) try: response = self.session.request('DELETE', url, timeout=self.CUSTODIA_TIMEOUT) response.raise_for_status() except requests.HTTPError as error: # XXX bool(response) defers to response.ok, which is a misfeature. # Have to explicitly test "if response is None" to know if the # object is there. have_response = response is not None if have_response and error.response.status_code == 404: raise StorageLookupError(str(error), model_instance) else: raise error
def _get(self, model_instance): """ Retrieves a serialized SecretModel string from Custodia and constructs a model instance. :param model_instance: SecretModel instance to search and get. :type model_instance: commissaire.model.SecretModel :returns: The saved model instance. :rtype: commissaire.model.SecretModel :raises StorageLookupError: if data lookup fails (404 Not Found) :raises requests.HTTPError: if the request fails (other than 404) """ headers = {'Accept': 'application/octet-stream'} url = self._build_key_url(model_instance) try: response = self.session.request('GET', url, headers=headers, timeout=self.CUSTODIA_TIMEOUT) response.raise_for_status() return model_instance.new(**response.json()) except requests.HTTPError as error: # XXX bool(response) defers to response.ok, which is a misfeature. # Have to explicitly test "if response is None" to know if the # object is there. have_response = response is not None if have_response and error.response.status_code == 404: raise StorageLookupError(str(error), model_instance) else: raise error
def _delete(self, model_instance): """ Deletes data from a store. :param model_instance: Model instance to delete :type model_instance: commissaire.model.Model :raises StorageLookupError: if data lookup fails """ try: key = self._format_key(model_instance) self._store.delete(key) except etcd.EtcdKeyNotFound as error: raise StorageLookupError(str(error), model_instance)
def _get(self, model_instance): """ Returns data from a store and returns back a model. :param model_instance: Model instance to search and return :type model_instance: commissaire.model.Model :returns: The model instance :rtype: commissaire.model.Model :raises StorageLookupError: if data lookup fails """ try: key = self._format_key(model_instance) etcd_resp = self._store.get(key) return model_instance.new(**json.loads(etcd_resp.value)) except etcd.EtcdKeyNotFound as error: raise StorageLookupError(str(error), model_instance)
def _delete(self, model_instance): """ Deletes data from a store. :param model_instance: Model instance to delete :type model_instance: commissaire.model.Model :raises StorageLookupError: if data lookup fails """ try: key = self._format_key(model_instance) etcd_resp = self._store.delete(key) model_type = type(model_instance) model_dict = json.loads(etcd_resp._prev_node.value) model_instance = model_type.new(**model_dict) self.notify.deleted(model_instance) except etcd.EtcdKeyNotFound as error: raise StorageLookupError(str(error), model_instance)