/usr/lib/python2.7/dist-packages/gnocchiclient/v1/resource.py is in python-gnocchiclient 7.0.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | #
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
from gnocchiclient import utils
from gnocchiclient.v1 import base
class ResourceManager(base.Manager):
url = "v1/resource/"
def list(self, resource_type="generic", details=False, history=False,
limit=None, marker=None, sorts=None):
"""List resources
:param resource_type: Type of the resource
:type resource_type: str
:param details: Show all attributes of resources
:type details: bool
:param history: Show the history of resources
:type history: bool
:param limit: maximum number of resources to return
:type limit: int
:param marker: the last item of the previous page; we return the next
results after this value.
:type marker: str
:param sorts: list of resource attributes to order by. (example
["user_id:desc-nullslast", "project_id:asc"]
:type sorts: list of str
"""
params = utils.build_pagination_options(
details, history, limit, marker, sorts)
url = "%s%s?%s" % (self.url, resource_type,
utils.dict_to_querystring(params))
return self._get(url).json()
def get(self, resource_type, resource_id, history=False):
"""Get a resource
:param resource_type: Type of the resource
:type resource_type: str
:param resource_id: ID of the resource
:type resource_id: str
:param history: Show the history of the resource
:type history: bool
"""
history = "/history" if history else ""
url = self.url + "%s/%s%s" % (resource_type, resource_id, history)
return self._get(url).json()
def history(self, resource_type, resource_id, details=False,
limit=None, marker=None, sorts=None):
"""Get a resource
:param resource_type: Type of the resource
:type resource_type: str
:param resource_id: ID of the resource
:type resource_id: str
:param details: Show all attributes of resources
:type details: bool
:param limit: maximum number of resources to return
:type limit: int
:param marker: the last item of the previous page; we returns the next
results after this value.
:type marker: str
:param sorts: list of resource attributes to order by. (example
["user_id:desc-nullslast", "project_id:asc"]
:type sorts: list of str
"""
params = utils.build_pagination_options(details, False, limit, marker,
sorts)
url = "%s%s/%s/history?%s" % (self.url, resource_type, resource_id,
utils.dict_to_querystring(params))
return self._get(url).json()
def create(self, resource_type, resource):
"""Create a resource
:param resource_type: Type of the resource
:type resource_type: str
:param resource: Attribute of the resource
:type resource: dict
"""
return self._post(
self.url + resource_type,
headers={'Content-Type': "application/json"},
data=json.dumps(resource)).json()
def update(self, resource_type, resource_id, resource):
"""Update a resource
:param resource_type: Type of the resource
:type resource_type: str
:param resource_id: ID of the resource
:type resource_id: str
:param resource: Attribute of the resource
:type resource: dict
"""
return self._patch(
self.url + resource_type + "/" + resource_id,
headers={'Content-Type': "application/json"},
data=json.dumps(resource)).json()
def delete(self, resource_id):
"""Delete a resource
:param resource_id: ID of the resource
:type resource_id: str
"""
self._delete(self.url + "generic/" + resource_id)
def batch_delete(self, query, resource_type="generic"):
"""Delete a batch of resources based on attribute values
:param resource_type: Type of the resource
:type resource_type: str
"""
if isinstance(query, dict):
return self._delete(
self.url + resource_type + "/",
headers={'Content-Type': "application/json"},
data=json.dumps(query)).json()
return self._delete(
self.url + resource_type + "/?filter=" + query,
headers={'Content-Type': "application/json"}).json()
def search(self, resource_type="generic", query=None, details=False,
history=False, limit=None, marker=None, sorts=None):
"""List resources
:param resource_type: Type of the resource
:type resource_type: str
:param query: The query dictionary
:type query: dict
:param details: Show all attributes of resources
:type details: bool
:param history: Show the history of resources
:type history: bool
:param limit: maximum number of resources to return
:type limit: int
:param marker: the last item of the previous page; we returns the next
results after this value.
:type marker: str
:param sorts: list of resource attributes to order by. (example
["user_id:desc-nullslast", "project_id:asc"]
:type sorts: list of str
See Gnocchi REST API documentation for the format
of *query dictionary*
http://gnocchi.xyz/rest.html#searching-for-resources
"""
query = query or {}
params = utils.build_pagination_options(
details, history, limit, marker, sorts)
url = "v1/search/resource/%s?%%s" % resource_type
if isinstance(query, dict):
return self._post(
url % utils.dict_to_querystring(params),
headers={'Content-Type': "application/json"},
data=json.dumps(query)).json()
params['filter'] = query
return self._post(
url % utils.dict_to_querystring(params),
headers={'Content-Type': "application/json"}).json()
|