本文整理汇总了Python中mimic.util.helper.invalid_resource函数的典型用法代码示例。如果您正苦于以下问题:Python invalid_resource函数的具体用法?Python invalid_resource怎么用?Python invalid_resource使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了invalid_resource函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_node
def add_node(self, node_list, lb_id, current_timestamp):
"""
Returns the canned response for add nodes
"""
if lb_id in self.lbs:
_verify_and_update_lb_state(self, lb_id, False, current_timestamp)
if self.lbs[lb_id]["status"] != "ACTIVE":
resource = invalid_resource(
"Load Balancer '{0}' has a status of {1} and is considered "
"immutable.".format(lb_id, self.lbs[lb_id]["status"]), 422)
return (resource, 422)
nodes = _format_nodes_on_lb(node_list)
if self.lbs[lb_id].get("nodes"):
for existing_node in self.lbs[lb_id]["nodes"]:
for new_node in node_list:
if (existing_node["address"] == new_node["address"] and
existing_node["port"] == new_node["port"]):
resource = invalid_resource(
"Duplicate nodes detected. One or more nodes "
"already configured on load balancer.", 413)
return (resource, 413)
self.lbs[lb_id]["nodes"] = self.lbs[lb_id]["nodes"] + nodes
else:
self.lbs[lb_id]["nodes"] = nodes
self.lbs[lb_id]["nodeCount"] = len(self.lbs[lb_id]["nodes"])
_verify_and_update_lb_state(self, lb_id,
current_timestamp=current_timestamp)
return {"nodes": nodes}, 202
return not_found_response("loadbalancer"), 404
示例2: del_load_balancer
def del_load_balancer(self, lb_id, current_timestamp):
"""
Returns response for a load balancer
is in building status for 20
seconds and response code 202, and adds the new lb to ``self.lbs``.
A loadbalancer, on delete, goes into PENDING-DELETE and remains in DELETED
status until a nightly job(maybe?)
"""
if lb_id in self.lbs:
if self.lbs[lb_id]["status"] == "PENDING-DELETE":
msg = ("Must provide valid load balancers: {0} are immutable and "
"could not be processed.".format(lb_id))
# Dont doubt this to be 422, it is 400!
return invalid_resource(msg, 400), 400
_verify_and_update_lb_state(self, lb_id, True, current_timestamp)
if any([self.lbs[lb_id]["status"] == "ACTIVE",
self.lbs[lb_id]["status"] == "ERROR",
self.lbs[lb_id]["status"] == "PENDING-UPDATE"]):
del self.lbs[lb_id]
return EMPTY_RESPONSE, 202
if self.lbs[lb_id]["status"] == "PENDING-DELETE":
return EMPTY_RESPONSE, 202
if self.lbs[lb_id]["status"] == "DELETED":
_verify_and_update_lb_state(self, lb_id,
current_timestamp=current_timestamp)
msg = "Must provide valid load balancers: {0} could not be found.".format(lb_id)
# Dont doubt this to be 422, it is 400!
return invalid_resource(msg, 400), 400
return not_found_response("loadbalancer"), 404
示例3: add_node
def add_node(self, node_list, lb_id):
"""
Add one or more nodes to a load balancer. Fails if one or more of the
nodes provided has the same address/port as an existing node. Also
fails if adding the nodes would exceed the maximum number of nodes on
the CLB.
:param list node_list: a `list` of `dict` containing specification for
nodes
:return: a `tuple` of (json response as a dict, http status code)
"""
if lb_id in self.lbs:
current_timestamp = self.clock.seconds()
self._verify_and_update_lb_state(lb_id, False, current_timestamp)
if self.lbs[lb_id]["status"] != "ACTIVE":
return considered_immutable_error(
self.lbs[lb_id]["status"], lb_id)
nodes = [Node.from_json(blob) for blob in node_list]
for existing_node in self.lbs[lb_id].nodes:
for new_node in nodes:
if existing_node.same_as(new_node):
resource = invalid_resource(
"Duplicate nodes detected. One or more nodes "
"already configured on load balancer.", 413)
return (resource, 413)
# If there were no duplicates
new_nodeCount = len(self.lbs[lb_id].nodes) + len(nodes)
if new_nodeCount <= self.node_limit:
self.lbs[lb_id].nodes = self.lbs[lb_id].nodes + nodes
else:
resource = invalid_resource(
"Nodes must not exceed {0} "
"per load balancer.".format(self.node_limit), 413)
return (resource, 413)
# Node status will be OFFLINE if health monitor is enabled in CLB
# ONLINE if health monitor is disabled
if self.lbs[lb_id].health_monitor != {}:
for node in nodes:
node.status = "OFFLINE"
self._add_node_created_feeds(nodes)
self._verify_and_update_lb_state(
lb_id, current_timestamp=current_timestamp)
return {"nodes": [node.as_json() for node in nodes]}, 202
return not_found_response("loadbalancer"), 404
示例4: get_token_and_service_catalog
def get_token_and_service_catalog(self, request):
"""
Return a service catalog consisting of all plugin endpoints and an api
token.
"""
try:
content = json.loads(request.content.read())
except ValueError:
request.setResponseCode(400)
return json.dumps(invalid_resource("Invalid JSON request body"))
tenant_id = (content['auth'].get('tenantName', None) or
content['auth'].get('tenantId', None))
if content['auth'].get('passwordCredentials'):
username = content['auth']['passwordCredentials']['username']
password = content['auth']['passwordCredentials']['password']
session = self.core.sessions.session_for_username_password(
username, password, tenant_id)
elif content['auth'].get('RAX-KSKEY:apiKeyCredentials'):
username = content['auth']['RAX-KSKEY:apiKeyCredentials']['username']
api_key = content['auth']['RAX-KSKEY:apiKeyCredentials']['apiKey']
session = self.core.sessions.session_for_api_key(
username, api_key, tenant_id)
elif content['auth'].get('token') and tenant_id:
session = self.core.sessions.session_for_token(
content['auth']['token']['id'], tenant_id)
else:
request.setResponseCode(400)
return json.dumps(invalid_resource("Invalid JSON request body"))
request.setResponseCode(200)
prefix_map = {
# map of entry to URI prefix for that entry
}
def lookup(entry):
return prefix_map[entry]
return json.dumps(
get_token(
session.tenant_id,
entry_generator=lambda tenant_id:
list(self.core.entries_for_tenant(
tenant_id, prefix_map, base_uri_from_request(request))),
prefix_for_endpoint=lookup,
response_token=session.token,
response_user_id=session.user_id,
response_user_name=session.username,
)
)
示例5: create_server
def create_server(tenant_id, server_info, server_id, compute_uri_prefix):
"""
Canned response for create server and adds the server to the server cache.
"""
status = "ACTIVE"
if 'metadata' in server_info:
if 'create_server_failure' in server_info['metadata']:
dict_meta = json.loads(server_info['metadata']['create_server_failure'])
return invalid_resource(dict_meta['message'], dict_meta['code']), dict_meta['code']
if 'server_building' in server_info['metadata']:
status = "BUILD"
if 'server_error' in server_info['metadata']:
status = "ERROR"
s_cache[server_id] = server_template(
tenant_id, server_info, server_id, status,
compute_uri_prefix=compute_uri_prefix,
)
return {
'server': {"OS-DCF:diskConfig": s_cache[server_id]['OS-DCF:diskConfig'],
"id": s_cache[server_id]['id'],
"links": s_cache[server_id]['links'],
"adminPass": "testpassword"}}, 202
示例6: delete_node
def delete_node(self, lb_id, node_id, current_timestamp):
"""
Determines whether the node to be deleted exists in the session store,
deletes the node, and returns the response code.
"""
if lb_id in self.lbs:
_verify_and_update_lb_state(self, lb_id, False, current_timestamp)
if self.lbs[lb_id]["status"] != "ACTIVE":
# Error message verified as of 2015-04-22
resource = invalid_resource(
"Load Balancer '{0}' has a status of '{1}' and is considered "
"immutable.".format(lb_id, self.lbs[lb_id]["status"]), 422)
return (resource, 422)
_verify_and_update_lb_state(self, lb_id,
current_timestamp=current_timestamp)
if _delete_node(self, lb_id, node_id):
return None, 202
else:
return not_found_response("node"), 404
return not_found_response("loadbalancer"), 404
示例7: get_token_and_service_catalog
def get_token_and_service_catalog(self, request):
"""
Return a service catalog consisting of all plugin endpoints and an api
token.
"""
try:
content = json_from_request(request)
except ValueError:
pass
else:
for cred_type in (PasswordCredentials, APIKeyCredentials,
TokenCredentials):
if cred_type.type_key in content['auth']:
try:
cred = cred_type.from_json(content)
except (KeyError, TypeError):
pass
else:
registry = self.registry_collection.registry_by_event(
authentication)
behavior = registry.behavior_for_attributes(
attr.asdict(cred))
return behavior(self.core, request, cred)
request.setResponseCode(400)
return json.dumps(invalid_resource("Invalid JSON request body"))
示例8: set_attributes
def set_attributes(self, request, tenant_id, clb_id):
"""
Alters the supported attributes of the CLB to supported values. To
return things back to normal, you'll first need to list the CLB to get
any original values yourself.
"""
regional_lbs = self._collection_from_tenant(tenant_id)
if not regional_lbs.lb_in_region(clb_id):
request.setResponseCode(404)
return json.dumps({
"message": "Tenant {0} doesn't own load balancer {1}".format(
tenant_id, clb_id
),
"code": 404,
})
try:
content = json.loads(request.content.read())
except ValueError:
request.setResponseCode(400)
return json.dumps(invalid_resource("Invalid JSON request body"))
try:
regional_lbs.set_attributes(clb_id, content)
except BadKeysError, bke:
request.setResponseCode(400)
return json.dumps({
"message": str(bke),
"code": 400,
})
示例9: delete_node
def delete_node(store, lb_id, node_id, current_timestamp):
"""
Determines whether the node to be deleted exists in mimic store and
returns the response code.
"""
if lb_id in store.lbs:
_verify_and_update_lb_state(store, lb_id, False, current_timestamp)
if store.lbs[lb_id]["status"] != "ACTIVE":
resource = invalid_resource(
"Load Balancer '{0}' has a status of {1} and is considered "
"immutable.".format(lb_id, store.lbs[lb_id]["status"]), 422)
return (resource, 422)
_verify_and_update_lb_state(store, lb_id,
current_timestamp=current_timestamp)
if store.lbs[lb_id].get("nodes"):
for each in store.lbs[lb_id]["nodes"]:
if each["id"] == node_id:
index = store.lbs[lb_id]["nodes"].index(each)
del store.lbs[lb_id]["nodes"][index]
if not store.lbs[lb_id]["nodes"]:
del store.lbs[lb_id]["nodes"]
store.lbs[lb_id].update({"nodeCount": len(store.lbs[lb_id].get("nodes", []))})
return None, 202
return not_found_response("node"), 404
return not_found_response("loadbalancer"), 404
示例10: get_image
def get_image(image_id):
"""
Canned response for get image. The image id provided is substituted in the response,
if not one of the invalid image ids specified in mimic_presets.
"""
if any([image_id in get_presets['servers']['invalid_image_ref'], image_id.endswith('Z')]):
return invalid_resource('Invalid imageRef provided.', 400), 400
return {'image': {'status': 'ACTIVE', 'id': image_id}}, 200
示例11: get_flavor
def get_flavor(flavor_id):
"""
Canned response for get flavor.
The flavor id provided is substituted in the response
"""
if flavor_id in get_presets['servers']['invalid_flavor_ref']:
return invalid_resource('Invalid flavorRef provided.', 400), 400
return {'flavor': {'name': '512MB Standard Instance',
'id': flavor_id}}, 200
示例12: update_node_status
def update_node_status(self, request, tenant_id, clb_id, node_id):
"""
Update given node's status. The request will be like::
{"status": "ONLINE"}
"""
regional_lbs = self._collection_from_tenant(tenant_id)
clb = regional_lbs.lbs.get(clb_id)
if clb is None:
request.setResponseCode(404)
return json.dumps({
"message": "Tenant {0} doesn't own load balancer {1}".format(
tenant_id, clb_id
),
"code": 404,
})
node = next((node for node in clb.nodes if node.id == node_id), None)
if node is None:
request.setResponseCode(404)
return json.dumps({
"message": "Load balancer {1} on tenant {0} does not have node {2}".format(
tenant_id, clb_id, node_id),
"code": 404,
})
try:
content = json_from_request(request)
except ValueError:
request.setResponseCode(400)
return json.dumps(invalid_resource("Invalid JSON request body"))
if content.get("status") not in ("ONLINE", "OFFLINE"):
request.setResponseCode(400)
return json.dumps(invalid_resource(
"status key not found or it must have ONLINE or OFFLINE value"))
node.status = content["status"]
request.setResponseCode(200)
return b""
示例13: add_node_to_load_balancer
def add_node_to_load_balancer(self, request, tenant_id, lb_id):
"""
Return a successful add node response with code 200
"""
try:
content = json.loads(request.content.read())
except ValueError:
request.setResponseCode(400)
return json.dumps(invalid_resource("Invalid JSON request body"))
node_list = content['nodes']
response_data = self.session(tenant_id).add_node(node_list, lb_id)
request.setResponseCode(response_data[1])
return json.dumps(response_data[0])
示例14: update_health_monitor
def update_health_monitor(self, request, tenant_id, lb_id):
"""
Update health monitor setting of given LB.
https://developer.rackspace.com/docs/cloud-load-balancers/v1/developer-guide/#update-health-monitor
"""
try:
content = json_from_request(request)
except ValueError:
request.setResponseCode(400)
return json.dumps(invalid_resource("Invalid JSON request body"))
body, code = self.session(tenant_id).update_health_monitor(
lb_id, content)
request.setResponseCode(code)
return json_dump(body)
示例15: delete_server
def delete_server(server_id):
"""
Returns True if the server was deleted from the cache, else returns false.
"""
if server_id in s_cache:
if 'delete_server_failure' in s_cache[server_id]['metadata']:
del_meta = json.loads(s_cache[server_id]['metadata']['delete_server_failure'])
if del_meta['times'] != 0:
del_meta['times'] = del_meta['times'] - 1
s_cache[server_id]['metadata']['delete_server_failure'] = json.dumps(del_meta)
return invalid_resource('server error', del_meta['code']), del_meta['code']
del s_cache[server_id]
return True, 204
else:
return not_found_response(), 404