Revise policy api

This commit is contained in:
lvdongbing 2016-01-04 22:21:06 +08:00
parent fb6d2cdfd2
commit 6298bf7b09
3 changed files with 72 additions and 10 deletions

View File

@ -111,6 +111,42 @@ class API(wsgi.Router):
action="delete",
conditions={'method': 'DELETE'})
# Policies
policy_resource = policies.create_resource(conf)
policy_path = "/{tenant_id}/policies"
with mapper.submapper(controller=policy_resource,
path_prefix=policy_path) as policy_mapper:
# Policy collection
policy_mapper.connect("policy_index",
"",
action="index",
conditions={'method': 'GET'})
# Policy detail
policy_mapper.connect("policy_get",
"/{policy_id}",
action="get",
conditions={'method': 'GET'})
# Create policy
policy_mapper.connect("policy_create",
"",
action="create",
conditions={'method': 'POST'})
# Update policy
policy_mapper.connect("policy_update",
"/{policy_id}",
action="update",
conditions={'method': 'PUT'})
# Delete policy
policy_mapper.connect("policy_delete",
"/{policy_id}",
action="delete",
conditions={'method': 'DELETE'})
# Events
event_resource = events.create_resource(conf)
event_path = "/{tenant_id}/events"

View File

@ -58,7 +58,6 @@ class PolicyController(object):
"""List summary information for all policies"""
filter_whitelist = {
'name': 'mixed',
'type': 'mixed',
'metadata': 'mixed',
}
param_whitelist = {
@ -83,7 +82,7 @@ class PolicyController(object):
filters = None
policies = self.rpc_client.policy_list(req.context, filters=filters,
**params)
**params)
return {'policies': policies}
@ -91,7 +90,7 @@ class PolicyController(object):
def get(self, req, policy_id):
"""Get detailed information for a policy"""
policy = self.rpc_client.policy_get(req.context,
policy_id)
policy_id)
return {'policy': policy}
@ -101,18 +100,41 @@ class PolicyController(object):
if not validator.is_valid_body(body):
raise exc.HTTPUnprocessableEntity()
policy_data = body.get('policy')
policy_data = body.get('policy', None)
if profile_data is None:
raise exc.HTTPBadRequest(_("Malformed request data, missing "
"'policy' key in request body."))
data = PolicyData(policy_data)
policy = self.rpc_client.policy_create(req.context,
data.name(),
data.spec(),
data.metadata())
data.name(),
data.rules(),
data.metadata())
return {'policy': policy}
@util.policy_enforce
def update(self, req, policy_id, body):
if not validator.is_valid_body(body):
raise exc.HTTPUnprocessableEntity()
policy_data = body.get('policy', None)
if profile_data is None:
raise exc.HTTPBadRequest(_("Malformed request data, missing "
"'policy' key in request body."))
name = policy_data.get(consts.POLICY_NAME, None)
metadata = policy_data.get(consts.POLICY_METADATA, None)
is_default = policy_data.get(consts.POLICY_IS_DEFAULT, None)
policy = self.rpc_client.policy_update(req.context, policy_id, name,
metadata, is_default)
return {'policy': policy}
@util.policy_enforce
def delete(self, req, policy_id):
"""Delete a policy with given policy_id"""
self.rpc_client.delete_policy(req.context, policy_id)
self.rpc_client.policy_delete(req.context, policy_id)
def create_resource(options):

View File

@ -103,7 +103,11 @@ class RuleController(object):
if not validator.is_valid_body(body):
raise exc.HTTPUnprocessableEntity()
rule_data = body.get('rule')
rule_data = body.get('rule', None)
if rule_data is None:
raise exc.HTTPBadRequest(_("Malformed request data, missing "
"'rule' key in request body."))
data = RuleData(rule_data)
rule = self.rpc_client.rule_create(req.context,
data.name(),
@ -114,7 +118,7 @@ class RuleController(object):
@util.policy_enforce
def delete(self, req, rule_id):
"""Delete a rule with given rule_id"""
self.rpc_client.delete_rule(req.context, rule_id)
self.rpc_client.rule_delete(req.context, rule_id)
def create_resource(options):