-
Notifications
You must be signed in to change notification settings - Fork 6
Add third party plugins #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: chameleoncloud/xena
Are you sure you want to change the base?
Changes from 1 commit
276ab48
34f7da3
7db8a66
54ceb91
dfa7da4
cc6016f
aeb9e59
853574d
e754baa
35e2a2e
97f91eb
b98e8ef
29d3f4b
3d1c210
2e4b883
1c208f1
cc43b3e
5d740ab
b671d4a
b11f78d
ef50627
844b0c6
cc52958
19b897b
f21a5ee
972b2db
1138230
d3480ab
bb34404
fd65787
9bdf28b
8e6acf7
9e5b632
ea556b5
7503f18
83d6bdd
216b203
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,18 @@ | |
from oslo_utils import strutils | ||
from oslo_config import cfg | ||
from oslo_serialization import jsonutils | ||
from oslo_policy.policy import DocumentedRuleDefault | ||
|
||
from blazar import status | ||
from blazar import policy | ||
from blazar.api.v1 import validation | ||
from blazar.api.v1 import utils as api_utils | ||
from blazar.db import api as db_api | ||
from blazar.db import exceptions as db_ex | ||
from blazar.db import utils as db_utils | ||
from blazar.manager import exceptions as manager_ex | ||
from blazar.plugins import monitor | ||
from blazar.policies import base | ||
from blazar.utils import plugins as plugins_utils | ||
from blazar.utils import trusts | ||
from blazar.utils.openstack import keystone | ||
|
@@ -140,7 +144,7 @@ def matching_resources( | |
shuffle(not_allocated_resource_ids) | ||
return not_allocated_resource_ids[:int(min_resources)] | ||
else: | ||
raise NotEnoughResourcesAvailable() | ||
raise manager_ex.NotEnoughResourcesAvailable() | ||
|
||
def allocation_candidates(self, values): | ||
return self.matching_resources( | ||
|
@@ -271,23 +275,29 @@ def _update_allocations(self, dates_before, dates_after, reservation_id, | |
min_resources = min_resources - kept_resources \ | ||
if (min_resources - kept_resources) > 0 else 0 | ||
max_resources = max_resources - kept_resources | ||
# TODO func does not exist | ||
resource_ids = self._matching_resources( | ||
resource_properties, | ||
str(min_resources) + '-' + str(max_resources), | ||
dates_after['start_date'], dates_after['end_date'], | ||
lease['project_id']) | ||
if len(resource_ids) >= min_resources: | ||
for resource_id in resource_ids: | ||
db_api.resource_allocation_create( | ||
{'resource_id': resource_id, | ||
'reservation_id': reservation_id}) | ||
# TODO call allocate() here? | ||
self.update_new_resources() | ||
else: | ||
raise manager_ex.NotEnoughResourcesAvailable() | ||
|
||
for allocation in allocs_to_remove: | ||
db_api.resource_allocation_destroy(allocation['id']) | ||
|
||
def update_new_resources(self, resource_reservation, resource_ids): | ||
for resource_id in resource_ids: | ||
db_api.resource_allocation_create( | ||
{'resource_id': resource_id, | ||
'reservation_id': reservation_id}) | ||
new_resource = db_api.resource_get(resource_id) | ||
new_resourcess.append(new_resource['hypervisor_hostname']) | ||
# TODO call allocate() here? | ||
|
||
def _convert_int_param(self, param, name): | ||
""" | ||
Checks that the parameter is present and can be converted to int. | ||
|
@@ -444,7 +454,11 @@ def notification_callback(self, event_type, payload): | |
def get_notification_event_types(self): | ||
return [] | ||
|
||
def reallocate(self, allocation): | ||
return True | ||
|
||
def api_list(self): | ||
policy.check_enforcement(self.resource_type(), "get") | ||
raw_resource_list = db_api.resource_list(self.resource_type()) | ||
resource_list = [] | ||
for resource in raw_resource_list: | ||
|
@@ -453,6 +467,7 @@ def api_list(self): | |
|
||
@trusts.use_trust_auth() | ||
def api_create(self, data): | ||
policy.check_enforcement(self.resource_type(), "post") | ||
create_data = data["data"] | ||
trust_id = data["trust_id"] | ||
create_data["trust_id"] = trust_id | ||
|
@@ -465,13 +480,15 @@ def api_create(self, data): | |
return resource | ||
|
||
def api_get(self, resource_id): | ||
policy.check_enforcement(self.resource_type(), "get") | ||
resource = self.get(resource_id) | ||
if resource is None: | ||
raise manager_ex.ResourceNotFound( | ||
resource=resource_id, resource_type=self.resource_type()) | ||
return resource | ||
|
||
def api_update(self, resource_id, data): | ||
policy.check_enforcement(self.resource_type(), "put") | ||
extras = data["extras"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's a good idea. I guess this would be the difference between not including it in the API request and it being empty. Either way, returning None is fine. |
||
data = data["data"] | ||
if not data and not extras: | ||
|
@@ -487,6 +504,7 @@ def api_update(self, resource_id, data): | |
return db_api.resource_get(self.resource_type(), resource_id) | ||
|
||
def api_delete(self, resource_id): | ||
policy.check_enforcement(self.resource_type(), "delete") | ||
resource = db_api.resource_get(self.resource_type(), resource_id) | ||
if resource is None: | ||
raise manager_ex.ResourceNotFound( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This exception appears to come from |
||
|
@@ -509,10 +527,11 @@ def api_delete(self, resource_id): | |
) | ||
|
||
def api_list_allocations(self, query): | ||
policy.check_enforcement(self.resource_type(), "get_allocations") | ||
resource_id_list = [ | ||
r['id'] for r in db_api.resource_list(self.resource_type())] | ||
options = self.get_query_options(query, QUERY_TYPE_ALLOCATION) | ||
options['detail'] = False # TODO use conf | ||
options['detail'] = False | ||
resource_allocations = self.query_resource_allocations( | ||
resource_id_list, **options) | ||
|
||
|
@@ -521,19 +540,42 @@ def api_list_allocations(self, query): | |
for resource, allocs in resource_allocations.items() | ||
] | ||
|
||
def api_get_allocations(self, resource_id, query): | ||
def get_allocations(self, resource_id, query, detail=False): | ||
options = self.get_query_options(query, QUERY_TYPE_ALLOCATION) | ||
options['detail'] = False | ||
resource_allocations = self.query_resource_allocations( | ||
[resource_id], **options) | ||
allocs = resource_allocations.get(resource_id, []) | ||
return {"resource_id": resource_id, "reservations": allocs} | ||
|
||
# TODO | ||
def api_get_allocations(self, resource_id, query): | ||
policy.check_enforcement(self.resource_type(), "get_allocations") | ||
return self.get_allocations(resource_id, query) | ||
|
||
def api_reallocate(self, resource_id, data): | ||
raise Exception("unimplemented") | ||
policy.check_enforcement(self.resource_type(), "reallocate") | ||
allocations = self.get_allocations(resource_id, data, detail=True) | ||
|
||
for alloc in allocations['reservations']: | ||
reservation_flags = {} | ||
resource_allocation = db_api.resource_allocation_get_all_by_values( | ||
resource_id=resource_id, | ||
reservation_id=alloc['id'])[0] | ||
|
||
if self.reallocate(resource_allocation): | ||
if alloc['status'] == status.reservation.ACTIVE: | ||
reservation_flags.update(dict(resources_changed=True)) | ||
db_api.lease_update(alloc['lease_id'], dict(degraded=True)) | ||
else: | ||
reservation_flags.update(dict(missing_resources=True)) | ||
db_api.lease_update(alloc['lease_id'], dict(degraded=True)) | ||
|
||
db_api.reservation_update(alloc['id'], reservation_flags) | ||
|
||
return self.get_allocations(resource_id, data) | ||
|
||
def api_list_resource_properties(self, query): | ||
policy.check_enforcement(self.resource_type(), "get_resource_properties") | ||
detail = False if not query else query.get('detail', False) | ||
resource_properties = collections.defaultdict(list) | ||
|
||
|
@@ -553,6 +595,7 @@ def api_list_resource_properties(self, query): | |
return resource_properties | ||
|
||
def api_update_resource_property(self, property_name, data): | ||
policy.check_enforcement(self.resource_type(), "patch_resource_properties") | ||
return db_api.resource_property_update( | ||
self.resource_type(), property_name, data) | ||
|
||
|
@@ -639,7 +682,6 @@ def query_resource_allocations(self, resources, lease_id=None, | |
resource_allocations = {d: [] for d in resources} | ||
|
||
for reservation in reservations: | ||
# TODO detail config? | ||
if detail: | ||
del reservation['project_id'] | ||
del reservation['lease_name'] | ||
|
@@ -653,6 +695,109 @@ def query_resource_allocations(self, resources, lease_id=None, | |
|
||
return resource_allocations | ||
|
||
def get_policy(self): | ||
policy_root = f'blazar:{self.resource_type()}:%s' | ||
resource_policy = [ | ||
DocumentedRuleDefault( | ||
name=policy_root % 'get', | ||
check_str=base.RULE_ADMIN, | ||
description='Policy rule for List/Show Resource(s) API.', | ||
operations=[ | ||
{ | ||
'path': '/{api_version}/'+self.resource_type(), | ||
'method': 'GET' | ||
}, | ||
{ | ||
'path': '/{api_version}/'+self.resource_type(), | ||
'method': 'GET' | ||
} | ||
] | ||
), | ||
DocumentedRuleDefault( | ||
name=policy_root % 'post', | ||
check_str=base.RULE_ADMIN, | ||
description='Policy rule for Create Resource API.', | ||
operations=[ | ||
{ | ||
'path': '/{api_version}/'+self.resource_type(), | ||
'method': 'POST' | ||
} | ||
] | ||
), | ||
DocumentedRuleDefault( | ||
name=policy_root % 'put', | ||
check_str=base.RULE_ADMIN, | ||
description='Policy rule for Update Resource API.', | ||
operations=[ | ||
{ | ||
'path': '/{api_version}/'+self.resource_type()+'/{resource_id}', | ||
'method': 'PUT' | ||
} | ||
] | ||
), | ||
DocumentedRuleDefault( | ||
name=policy_root % 'delete', | ||
check_str=base.RULE_ADMIN, | ||
description='Policy rule for Delete Resource API.', | ||
operations=[ | ||
{ | ||
'path': '/{api_version}/'+self.resource_type()+'/{resource_id}', | ||
'method': 'DELETE' | ||
} | ||
] | ||
), | ||
DocumentedRuleDefault( | ||
name=policy_root % 'get_allocations', | ||
check_str=base.RULE_ADMIN, | ||
description='Policy rule for List/Get Resource(s) Allocations API.', | ||
operations=[ | ||
{ | ||
'path': '/{api_version}/'+self.resource_type()+'/allocations', | ||
'method': 'GET' | ||
}, | ||
{ | ||
'path': '/{api_version}/'+self.resource_type()+'/{resource_id}/allocation', | ||
'method': 'GET' | ||
} | ||
] | ||
), | ||
DocumentedRuleDefault( | ||
name=policy_root % 'reallocate', | ||
check_str=base.RULE_ADMIN, | ||
description='Policy rule for Reallocate Resource API.', | ||
operations=[ | ||
{ | ||
'path': '/{api_version}/'+self.resource_type()+'/{resource_id}/allocation', | ||
'method': 'PUT' | ||
} | ||
] | ||
), | ||
DocumentedRuleDefault( | ||
name=policy_root % 'get_resource_properties', | ||
check_str=base.RULE_ADMIN, | ||
description='Policy rule for Resource Properties API.', | ||
operations=[ | ||
{ | ||
'path': '/{api_version}/'+self.resource_type()+'/resource_properties', | ||
'method': 'GET' | ||
} | ||
] | ||
), | ||
DocumentedRuleDefault( | ||
name=policy_root % 'patch_resource_properties', | ||
check_str=base.RULE_ADMIN, | ||
description='Policy rule for Resource Properties API.', | ||
operations=[ | ||
{ | ||
'path': ('/{api_version}/'+self.resource_type()+'/resource_properties/' | ||
'{property_name}'), | ||
'method': 'PATCH' | ||
} | ||
] | ||
), | ||
] | ||
return resource_policy | ||
|
||
|
||
class ResourceMonitorPlugin(monitor.GeneralMonitorPlugin): | ||
def __new__(cls, plugin, *args, **kwargs): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exception appears to come from
plugin_ex
, notmanager_ex
.