Skip to content

Commit

Permalink
Dynamic policies
Browse files Browse the repository at this point in the history
  • Loading branch information
JosepSampe committed Oct 23, 2017
1 parent 42fabb6 commit cc73b6c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
51 changes: 20 additions & 31 deletions api/policies/actors/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ def __init__(self, policy_data, controller_server):
port=self.redis_port,
db=self.redis_db)

self.rule_parsed = rule_parsed
self.action = policy_data['action']
self.filter = policy_data['filter']
self.params = policy_data['parameters']
self.target_id = policy_data['target_id']
self.target_name = policy_data['target_name']
self.object_size = policy_data['object_size']
self.object_tag = policy_data['object_tag']
self.object_type = policy_data['object_type']
self.controller_server = controller_server

self.conditions = rule_parsed.condition_list.asList()
self.conditions = policy_data['condition']
self.observers_values = dict()
self.observers_proxies = dict()
self.token = None
Expand Down Expand Up @@ -97,11 +99,22 @@ def start_rule(self):
metrics necessaries.
"""
logger.info("Rule, Start '" + str(self.id) + "'")
self.check_metrics(self.conditions)
logger.info('Rule, Conditions: ' + str(self.conditions))

# Start Condition checker
# TODO: PASRE CONDITIONS STRING
"""
if not isinstance(condition_list[0], list):
self._add_metric(condition_list[0].lower())
else:
for element in condition_list:
if element is not "OR" and element is not "AND":
self.check_metrics(element)
"""

def _add_metric(self, metric_name):
"""
The `add_metric()` method subscribes the rule to all workload metrics
This method subscribes the rule to the metric_name
that it needs to check the conditions defined in the policy
:param metric_name: The name that identifies the workload metric.
Expand All @@ -116,23 +129,6 @@ def _add_metric(self, metric_name):
self.observers_proxies[metric_name] = observer
self.observers_values[metric_name] = None

def check_metrics(self, condition_list):
"""
The check_metrics method finds in the condition list all the metrics
that it needs to check the conditions, when find some metric that it
needs, call the method add_metric.
:param condition_list: The list of all the conditions.
:type condition_list: **any** List type
"""
logger.info('Rule, Condition: ' + str(condition_list))
if not isinstance(condition_list[0], list):
self._add_metric(condition_list[0].lower())
else:
for element in condition_list:
if element is not "OR" and element is not "AND":
self.check_metrics(element)

def update(self, metric_name, value):
"""
The method update is called by the workloads metrics following the
Expand Down Expand Up @@ -206,16 +202,9 @@ def _do_action(self):

data = dict()

if hasattr(self.rule_parsed.object_list, "object_type"):
data['object_type'] = self.rule_parsed.object_list.object_type.object_value
else:
data['object_type'] = ''

if hasattr(self.rule_parsed.object_list, "object_size"):
data['object_size'] = self.rule_parsed.object_list.object_size.object_value
else:
data['object_size'] = ''

data['object_type'] = self.object_type
data['object_size'] = self.object_size
data['object_tag'] = self.object_tag
data['params'] = self.params

response = requests.put(url, json.dumps(data), headers=headers)
Expand Down
12 changes: 3 additions & 9 deletions api/policies/actors/rule_transient.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,9 @@ def do_action(self, condition_result):
data = dict()
url = os.path.join(self.controller_server, 'filters', self.target_id, "deploy", str(self.filter))

if hasattr(self.rule_parsed.object_list, "object_type"):
data['object_type'] = self.rule_parsed.object_list.object_type.object_value
else:
data['object_type'] = ''

if hasattr(self.rule_parsed.object_list, "object_size"):
data['object_size'] = self.rule_parsed.object_list.object_size.object_value
else:
data['object_size'] = ''
data['object_type'] = self.object_type
data['object_size'] = self.object_size
data['object_tag'] = self.object_tag

data['params'] = self.params

Expand Down
23 changes: 17 additions & 6 deletions api/policies/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,18 @@ def policy_list(request):
project_id = data['project_id']
container = data['container_id']

project_list = get_project_list()
project_name = project_list[project_id]
if project_id == 'global':
project_name = 'Global'
else:
project_list = get_project_list()
project_name = project_list[project_id]

target_id = os.path.join(project_id, container)
target_name = os.path.join(project_name, container)
if container:
target_id = os.path.join(project_id, container)
target_name = os.path.join(project_name, container)
else:
target_id = project_id
target_name = project_name

if data['transient']:
location = settings.RULE_TRANSIENT_MODULE
Expand Down Expand Up @@ -161,7 +168,7 @@ def policy_list(request):
rule_actors[policy_id].start_rule()

try:
r.hmset(key, policy_data)
r.hmset(rule_id, policy_data)
return JSONResponse("Policy inserted correctly", status=status.HTTP_201_CREATED)
except RedisError:
return JSONResponse("Error inserting policy", status=status.HTTP_400_BAD_REQUEST)
Expand Down Expand Up @@ -429,8 +436,10 @@ def deploy_dynamic_policy(r, rule_string, parsed_rule, http_host):
rule_id = 'policy:' + str(policy_id)

if action_info.transient:
transient = True
location = settings.RULE_TRANSIENT_MODULE
else:
transient = False
location = settings.RULE_MODULE
policy_location = os.path.join(settings.PYACTOR_URL, location, str(rule_id))

Expand All @@ -444,6 +453,8 @@ def deploy_dynamic_policy(r, rule_string, parsed_rule, http_host):
if parsed_rule.object_list:
if parsed_rule.object_list.object_type:
object_type = parsed_rule.object_list.object_type.object_value
if parsed_rule.object_list.object_tag:
object_type = parsed_rule.object_list.object_tag.object_value
if parsed_rule.object_list.object_size:
object_size = [parsed_rule.object_list.object_size.operand,
parsed_rule.object_list.object_size.object_value]
Expand All @@ -458,7 +469,7 @@ def deploy_dynamic_policy(r, rule_string, parsed_rule, http_host):
"object_type": object_type,
"object_size": object_size,
"object_tag": object_tag,
"transient": action_info.transient,
"transient": transient,
"policy_location": policy_location,
"alive": True}

Expand Down

0 comments on commit cc73b6c

Please sign in to comment.