Adapt dashboard to new cloudkitty client

Since https://review.openstack.org/#/c/568537/ has been merged,
the bindings to CloudKitty's client have changed.

Change-Id: I1b8f04bfc3384193acf9d672d99f2ca0f301e14d
This commit is contained in:
Luka Peschke 2018-07-05 11:45:06 +02:00
parent 91b85be715
commit 15e65aec62
13 changed files with 184 additions and 158 deletions

View File

@ -17,32 +17,39 @@ import collections
from django.conf import settings
from horizon.utils.memoized import memoized # noqa
from keystoneauth1.identity.v3 import Token
from cloudkittyclient import client as ck_client
from openstack_dashboard.api import base
from cloudkittydashboard import utils
@memoized
def cloudkittyclient(request):
"""Initialization of Cloudkitty client."""
endpoint = base.url_for(request, 'rating')
insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
return ck_client.Client('1', endpoint,
token=(lambda: request.user.token.id),
insecure=insecure,
cacert=cacert)
auth_url = getattr(settings, 'OPENSTACK_KEYSTONE_URL', None)
auth = Token(
auth_url,
token=request.user.token.id,
project_id=request.user.project_id,
domain_id=request.user.domain_id,
)
return ck_client.Client(
'1',
auth=auth,
cert=cacert)
def identify(what, name=False, key=None):
if isinstance(what, collections.Iterable):
for i in what:
i.id = getattr(i, key or "%s_id" % i.key)
if name:
i.name = getattr(i, key or "%s_id" % i.key)
i['id'] = i.get(key or "%s_id" % i['key'])
if name and not i.get('name'):
i['name'] = i.get(key or "%s_id" % i['key'])
what = [utils.TemplatizableDict(i) for i in what]
else:
what.id = getattr(what, key or "%s_id" % what.key)
if name:
what.name = getattr(what, key or "%s_id" % what.key)
what['id'] = what.get(key or "%s_id" % what['key'])
if name and not i.get('name'):
what['name'] = what.get(key or "%s_id" % what['key'])
what = utils.TemplatizableDict(what)
return what

View File

@ -11,13 +11,13 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from decimal import Decimal
import logging
from django.utils.translation import ugettext_lazy as _
from horizon import forms
from keystoneauth1 import exceptions
from cloudkittyclient.apiclient import exceptions
from cloudkittydashboard.api import cloudkitty as api
from openstack_dashboard import api as api_keystone
@ -58,15 +58,15 @@ class CreateServiceForm(forms.SelfHandlingForm):
service = data['service']
else:
service = data['custom_service']
services_mgr = api.cloudkittyclient(request).hashmap.services
services_mgr = api.cloudkittyclient(request).rating.hashmap
LOG.info('Creating service with name %s' % (service))
return services_mgr.create(name=service)
return services_mgr.create_service(name=service)
def __init__(self, request, *args, **kwargs):
super(CreateServiceForm, self).__init__(request, *args, **kwargs)
services = api.cloudkittyclient(request).service_info.list()
services = api.identify(services)
choices = sorted([(s.service_id, s.service_id) for s in services])
metrics = api.cloudkittyclient(request).info.get_metric()['metrics']
metrics = api.identify(metrics, key='metric_id', name=True)
choices = sorted([(s.metric_id, s.metric_id) for s in metrics])
self.fields['service'].choices = choices
@ -82,24 +82,23 @@ class CreateFieldForm(forms.SelfHandlingForm):
service_id = data['service_id']
field = data['field']
LOG.info('Creating field with name %s' % (field))
fields_mgr = api.cloudkittyclient(request).hashmap.fields
return fields_mgr.create(name=field, service_id=service_id)
fields_mgr = api.cloudkittyclient(request).rating.hashmap
return fields_mgr.create_field(name=field, service_id=service_id)
def __init__(self, request, *args, **kwargs):
super(CreateFieldForm, self).__init__(request, *args, **kwargs)
service_id = kwargs['initial']['service_id']
manager = api.cloudkittyclient(request)
service = manager.hashmap.services.get(service_id=service_id)
self.fields['service_name'].initial = service.name
manager = api.cloudkittyclient(request).rating.hashmap
service = manager.get_service(service_id=service_id)
self.fields['service_name'].initial = service['name']
try:
fields = manager.service_info.get(service_id=service.name)
fields = manager.get_field(service_id=service['name'])['fields']
except exceptions.NotFound:
fields = None
if fields:
fields = api.identify(fields)
choices = sorted([(field, field) for field in fields.metadata])
choices = sorted([(field, field) for field in fields['metadata']])
self.fields['field'] = forms.DynamicChoiceField(
label=_("Field"),
required=True)
@ -116,7 +115,8 @@ class CreateGroupForm(forms.SelfHandlingForm):
def handle(self, request, data):
name = data['name']
LOG.info('Creating group with name %s' % (name))
return api.cloudkittyclient(request).hashmap.groups.create(name=name)
return api.cloudkittyclient(request).rating.hashmap.create_group(
name=name)
class BaseForm(forms.SelfHandlingForm):
@ -134,10 +134,11 @@ class BaseForm(forms.SelfHandlingForm):
def __init__(self, request, *args, **kwargs):
super(BaseForm, self).__init__(request, *args, **kwargs)
self.order_fields(self.fields_order)
groups = api.cloudkittyclient(request).hashmap.groups.list()
groups = api.identify(groups)
choices = [(group.id, group.name) for group in groups]
# self.order_fields(self.fields_order)
groups = api.cloudkittyclient(
request).rating.hashmap.get_group()['groups']
groups = api.identify(groups, key='group_id', name=True)
choices = [(group['id'], group['name']) for group in groups]
choices.insert(0, ('', ' '))
self.fields['group_id'].choices = choices
@ -152,12 +153,12 @@ class BaseThresholdForm(BaseForm):
fields_order = ['level', 'type', 'cost', 'group_id', 'tenant_id']
def handle(self, request, data):
thresholds_mgr = api.cloudkittyclient(request).hashmap.thresholds
thresholds_mgr = api.cloudkittyclient(request).rating.hashmap
threshold = {}
for k, v in data.items():
if v:
threshold[k] = v
return thresholds_mgr.create(**threshold)
threshold[k] = float(v) if isinstance(v, Decimal) else v
return thresholds_mgr.create_threshold(**threshold)
class CreateServiceThresholdForm(BaseThresholdForm):
@ -179,12 +180,12 @@ class CreateFieldThresholdForm(BaseThresholdForm):
class BaseMappingForm(BaseForm):
def handle(self, request, data):
mapping_mgr = api.cloudkittyclient(request).hashmap.mappings
mapping_mgr = api.cloudkittyclient(request).rating.hashmap
mapping = {}
for k, v in data.items():
if v:
mapping[k] = v
return mapping_mgr.create(**mapping)
mapping[k] = float(v) if isinstance(v, Decimal) else v
return mapping_mgr.create_mapping(**mapping)
class CreateFieldMappingForm(BaseMappingForm):
@ -214,13 +215,13 @@ class BaseEditMappingForm(BaseMappingForm):
required=False)
def handle(self, request, data):
mapping_mgr = api.cloudkittyclient(request).hashmap.mappings
mapping_mgr = api.cloudkittyclient(request).rating.hashmap
mapping = {}
for k, v in data.items():
if v:
mapping[k] = v
mapping[k] = float(v) if isinstance(v, Decimal) else v
mapping['mapping_id'] = self.initial['mapping_id']
return mapping_mgr.update(**mapping)
return mapping_mgr.update_mapping(**mapping)
class EditServiceMappingForm(BaseEditMappingForm, CreateServiceMappingForm):
@ -245,13 +246,13 @@ class BaseEditThresholdForm(BaseThresholdForm):
attrs={'readonly': 'readonly'}))
def handle(self, request, data):
threshold_mgr = api.cloudkittyclient(request).hashmap.thresholds
threshold_mgr = api.cloudkittyclient(request).rating.hashmap
threshold = {}
for k, v in data.items():
if v:
threshold[k] = v
threshold[k] = float(v) if isinstance(v, Decimal) else v
threshold['threshold_id'] = self.initial['threshold_id']
return threshold_mgr.update(**threshold)
return threshold_mgr.update_threshold(**threshold)
class EditServiceThresholdForm(BaseEditThresholdForm,

View File

@ -56,7 +56,7 @@ class DeleteService(tables.DeleteAction):
)
def action(self, request, service_id):
api.cloudkittyclient(request).hashmap.services.delete(
api.cloudkittyclient(request).rating.hashmap.delete_service(
service_id=service_id)
@ -116,7 +116,7 @@ class DeleteGroup(tables.DeleteAction):
)
def action(self, request, group_id):
api.cloudkittyclient(request).hashmap.groups.delete(
api.cloudkittyclient(request).rating.hashmap.delete_group(
group_id=group_id)
@ -150,8 +150,8 @@ class GroupsTab(tabs.TableTab):
def get_groups_data(self):
client = api.cloudkittyclient(self.request)
groups = client.hashmap.groups.list()
return api.identify(groups)
groups = client.rating.hashmap.get_group().get('groups', [])
return api.identify(groups, key='group_id')
class CreateServiceThreshold(tables.LinkAction):
@ -204,7 +204,7 @@ class DeleteServiceThreshold(tables.DeleteAction):
)
def action(self, request, threshold_id):
api.cloudkittyclient(request).hashmap.thresholds.delete(
api.cloudkittyclient(request).rating.hashmap.delete_threshold(
threshold_id=threshold_id)
@ -232,7 +232,7 @@ class DeleteFieldThreshold(tables.DeleteAction):
)
def action(self, request, threshold_id):
api.cloudkittyclient(request).hashmap.thresholds.delete(
api.cloudkittyclient(request).rating.hashmap.delete_threshold(
threshold_id=threshold_id)
@ -257,18 +257,18 @@ def get_groupname(datum):
def add_groupname(request, datums):
client = api.cloudkittyclient(request)
groups = client.hashmap.groups.list()
full_groups = OrderedDict([(str(group.group_id), group.name)
groups = client.rating.hashmap.get_group().get('groups', [])
full_groups = OrderedDict([(str(group['group_id']), group['name'])
for group in groups])
for datum in datums:
if datum.group_id:
if datum.group_id in full_groups:
datum.group_name = full_groups[datum.group_id]
if datum.get('group_id'):
if datum['group_id'] in full_groups:
datum['group_name'] = full_groups[datum['group_id']]
else:
group = client.hashmap.groups.get(
group_id=datum.group_id)
datum.group_name = group.name
group = client.rating.hashmap.get_group(
group_id=datum['group_id'])
datum['group_name'] = group['name']
class BaseThresholdsTable(tables.DataTable):
@ -303,10 +303,10 @@ class ServiceThresholdsTab(tabs.TableTab):
def get_service_thresholds_data(self):
client = api.cloudkittyclient(self.request)
thresholds = client.hashmap.thresholds.list(
service_id=self.request.service_id)
thresholds = client.rating.hashmap.get_threshold(
service_id=self.request.service_id).get('thresholds', [])
add_groupname(self.request, thresholds)
return api.identify(thresholds)
return api.identify(thresholds, key='threshold_id', name=True)
class EditFieldThreshold(tables.LinkAction):
@ -343,10 +343,10 @@ class FieldThresholdsTab(tabs.TableTab):
def get_field_thresholds_data(self):
client = api.cloudkittyclient(self.request)
thresholds = client.hashmap.thresholds.list(
field_id=self.request.field_id)
thresholds = client.rating.hashmap.get_threshold(
field_id=self.request.field_id).get('thresholds', [])
add_groupname(self.request, thresholds)
return api.identify(thresholds)
return api.identify(thresholds, key='threshold_id', name=True)
class DeleteField(tables.DeleteAction):
@ -373,7 +373,7 @@ class DeleteField(tables.DeleteAction):
)
def action(self, request, field_id):
api.cloudkittyclient(request).hashmap.fields.delete(
api.cloudkittyclient(request).rating.hashmap.delete_field(
field_id=field_id)
@ -417,8 +417,9 @@ class FieldsTab(tabs.TableTab):
def get_fields_data(self):
client = api.cloudkittyclient(self.request)
fields = client.hashmap.fields.list(service_id=self.request.service_id)
return api.identify(fields)
fields = client.rating.hashmap.get_field(
service_id=self.request.service_id)['fields']
return api.identify(fields, key='field_id')
class DeleteMapping(tables.DeleteAction):
@ -445,7 +446,7 @@ class DeleteMapping(tables.DeleteAction):
)
def action(self, request, mapping_id):
api.cloudkittyclient(request).hashmap.mappings.delete(
api.cloudkittyclient(request).rating.hashmap.delete_mapping(
mapping_id=mapping_id)
@ -536,10 +537,10 @@ class FieldMappingsTab(tabs.TableTab):
def get_mappings_data(self):
client = api.cloudkittyclient(self.request)
mappings = client.hashmap.mappings.list(
field_id=self.request.field_id)
mappings = client.rating.hashmap.get_mapping(
field_id=self.request.field_id).get('mappings', [])
add_groupname(self.request, mappings)
return api.identify(mappings)
return api.identify(mappings, key='mapping_id', name=True)
class MappingsTab(tabs.TableTab):
@ -551,10 +552,10 @@ class MappingsTab(tabs.TableTab):
def get_mappings_data(self):
client = api.cloudkittyclient(self.request)
mappings = client.hashmap.mappings.list(
service_id=self.request.service_id)
mappings = client.rating.hashmap.get_mapping(
service_id=self.request.service_id).get('mappings', [])
add_groupname(self.request, mappings)
return api.identify(mappings)
return api.identify(mappings, key='mapping_id', name=True)
class FieldTabs(tabs.TabGroup):

View File

@ -20,8 +20,9 @@ from horizon import forms
from horizon import tables
from horizon import tabs
from horizon import views
from keystoneauth1 import exceptions
from cloudkittyclient.apiclient import exceptions
# from cloudkittyclient.apiclient import exceptions
from cloudkittydashboard.api import cloudkitty as api
from cloudkittydashboard.dashboards.admin.hashmap import forms as hashmap_forms
from cloudkittydashboard.dashboards.admin.hashmap \
@ -34,19 +35,19 @@ class IndexView(tables.DataTableView):
def get_data(self):
manager = api.cloudkittyclient(self.request)
services = manager.hashmap.services.list()
services = sorted(services, key=lambda service: service.name)
services = manager.rating.hashmap.get_service().get('services', [])
services = sorted(services, key=lambda service: service['name'])
list_services = []
for s in services:
try:
service = manager.service_info.get(service_id=s.name)
unit = service.unit
service = manager.info.get_metric(metric_name=s['name'])
unit = service['unit']
except exceptions.NotFound:
unit = "-"
list_services.append({
"id": s.service_id,
"name": s.name,
"id": s['service_id'],
"name": s['name'],
"unit": unit
})
return list_services
@ -57,24 +58,23 @@ class ServiceView(tabs.TabbedTableView):
template_name = 'admin/hashmap/service_details.html'
def get(self, *args, **kwargs):
service = api.cloudkittyclient(self.request).hashmap.services.get(
service_id=kwargs['service_id']
)
self.request.service_id = service.service_id
self.page_title = "Hashmap Service : %s" % service.name
service = api.cloudkittyclient(
self.request).rating.hashmap.get_service(
service_id=kwargs['service_id'])
self.request.service_id = service['service_id']
self.page_title = "Hashmap Service : %s" % service['name']
return super(ServiceView, self).get(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super(ServiceView, self).get_context_data(**kwargs)
manager = api.cloudkittyclient(self.request)
service = manager.hashmap.services.get(
service_id=kwargs['service_id']
)
config = manager.config.get_config()
service = manager.rating.hashmap.get_service(
service_id=kwargs['service_id'])
config = manager.info.get_config()
period = None
if service.name in config['collect']['services']:
period = config['collect']['period']
if service['name'] in config['metrics'].keys():
period = config.get('period', 3600)
context["service_period"] = period
return context
@ -98,11 +98,10 @@ class FieldView(tabs.TabbedTableView):
template_name = 'admin/hashmap/field_details.html'
def get(self, *args, **kwargs):
field = api.cloudkittyclient(self.request).hashmap.fields.get(
field_id=kwargs['field_id']
)
self.request.field_id = field.field_id
self.page_title = "Hashmap Field : %s" % field.name
field = api.cloudkittyclient(self.request).rating.hashmap.get_field(
field_id=kwargs['field_id'])
self.request.field_id = field['field_id']
self.page_title = "Hashmap Field : %s" % field['name']
return super(FieldView, self).get(*args, **kwargs)
@ -170,9 +169,9 @@ class ServiceMappingEditView(ServiceMappingCreateView):
success_url = 'horizon:admin:hashmap:service_mapping_edit'
def get_initial(self):
out = api.cloudkittyclient(self.request).hashmap.mappings.get(
out = api.cloudkittyclient(self.request).rating.hashmap.get_mapping(
mapping_id=self.kwargs['mapping_id'])
self.initial = out.to_dict()
self.initial = out
return self.initial
def get_context_data(self, **kwargs):
@ -224,9 +223,9 @@ class FieldMappingEditView(FieldMappingCreateView):
submit_url = 'horizon:admin:hashmap:field_mapping_edit'
def get_initial(self):
out = api.cloudkittyclient(self.request).hashmap.mappings.get(
out = api.cloudkittyclient(self.request).rating.hashmap.get_mapping(
mapping_id=self.kwargs['mapping_id'])
self.initial = out.to_dict()
self.initial = out
return self.initial
def get_context_data(self, **kwargs):
@ -309,9 +308,9 @@ class ServiceThresholdEditView(ServiceThresholdCreateView):
submit_url = 'horizon:admin:hashmap:service_threshold_edit'
def get_initial(self):
out = api.cloudkittyclient(self.request).hashmap.thresholds.get(
out = api.cloudkittyclient(self.request).rating.hashmap.get_threshold(
threshold_id=self.kwargs['threshold_id'])
self.initial = out.to_dict()
self.initial = out
return self.initial
def get_context_data(self, **kwargs):
@ -363,9 +362,9 @@ class FieldThresholdEditView(FieldThresholdCreateView):
submit_url = 'horizon:admin:hashmap:field_threshold_edit'
def get_initial(self):
out = api.cloudkittyclient(self.request).hashmap.thresholds.get(
out = api.cloudkittyclient(self.request).rating.hashmap.get_threshold(
threshold_id=self.kwargs['threshold_id'])
self.initial = out.to_dict()
self.initial = out
return self.initial
def get_context_data(self, **kwargs):
@ -386,7 +385,7 @@ class GroupView(tabs.TabbedTableView):
template_name = 'admin/hashmap/group_details.html'
def get(self, *args, **kwargs):
group = api.cloudkittyclient(self.request).hashmap.groups.get(
group = api.cloudkittyclient(self.request).rating.hashmap.get_group(
group_id=kwargs['group_id']
)
self.request.group_id = group.group_id
@ -394,8 +393,7 @@ class GroupView(tabs.TabbedTableView):
return super(GroupView, self).get(*args, **kwargs)
def get_data(self):
out = api.cloudkittyclient(self.request).hashmap.groups.list(
)
out = api.cloudkittyclient(self.request).rating.hashmap.get_group()
return api.identify(out)
@ -408,18 +406,17 @@ class GroupDetailsView(views.APIView):
ck_client = api.cloudkittyclient(self.request)
try:
group = ck_client.hashmap.groups.get(group_id=group_id)
group = ck_client.rating.hashmap.get_group(group_id=group_id)
except Exception:
group = None
try:
mappings = ck_client.hashmap.mappings.findall(group_id=group_id)
mappings = ck_client.rating.hashmap.get_mapping(
group_id=group_id)['mappings']
except Exception:
mappings = []
try:
thresholds = ck_client.hashmap.thresholds.findall(
group_id=group_id)
thresholds = ck_client.rating.hashmap.get_threshold(
group_id=group_id)['thresholds']
except Exception:
thresholds = []
@ -430,7 +427,7 @@ class GroupDetailsView(views.APIView):
for key, value in dict(
mappings=mappings, thresholds=thresholds).items():
for entry in value:
if entry.service_id:
if entry.get('service_id'):
values[key]['services'].append(entry)
else:
values[key]['fields'].append(entry)

View File

@ -27,7 +27,7 @@ class EditPriorityForm(forms.SelfHandlingForm):
def handle(self, request, data):
ck_client = api.cloudkittyclient(request)
return ck_client.modules.update(
return ck_client.rating.update_module(
module_id=self.initial["module_id"],
priority=data["priority"]
)

View File

@ -74,7 +74,7 @@ class ToggleEnabledModule(tables.BatchAction):
)
def allowed(self, request, module=None):
self.enabled = module.enabled
self.enabled = module.get('enabled')
if self.enabled:
self.current_present_action = DISABLE
else:
@ -82,14 +82,12 @@ class ToggleEnabledModule(tables.BatchAction):
return True
def action(self, request, obj_id):
module = api.cloudkittyclient(request).modules.get(module_id=obj_id)
self.enabled = module.enabled
if self.enabled:
self.current_past_action = DISABLE
module.disable()
else:
module.enable()
self.current_past_action = ENABLE
client = api.cloudkittyclient(request)
module = client.rating.get_module(module_id=obj_id)
self.enabled = module.get('enabled', False)
self.current_past_action = DISABLE if self.enabled else ENABLE
client.rating.update_module(module_id=obj_id,
enabled=(not self.enabled))
def get_details_link(datum):

View File

@ -30,11 +30,9 @@ class IndexView(tables.DataTableView):
table_class = admin_tables.ModulesTable
def get_data(self):
# Add data to the context here...
modules = api.identify(
api.cloudkittyclient(self.request).modules.list(),
name=True
)
modules = api.cloudkittyclient(
self.request).rating.get_module()['modules']
modules = api.identify(modules, key='module_id', name=True)
return modules
@ -45,12 +43,13 @@ class ModuleDetailsView(views.APIView):
def get_data(self, request, context, *args, **kwargs):
module_id = kwargs.get("module_id")
try:
module = api.cloudkittyclient(self.request).modules.get(
module = api.cloudkittyclient(self.request).rating.get_module(
module_id=module_id)
context['hotconfig'] = module['hot-config']
context['module'] = module
except Exception:
module = None
context['hotconfig'] = module._info['hot-config']
context['module'] = module
context['hotconfig'] = False
context['module'] = {}
return context
@ -64,9 +63,9 @@ class PriorityModuleEditView(forms.ModalFormView):
template_name = "horizon/common/modal_form.html"
def get_initial(self):
module = api.cloudkittyclient(self.request).modules.get(
module = api.cloudkittyclient(self.request).rating.get_module(
module_id=self.kwargs['module_id'])
self.initial = module.to_dict()
self.initial = module
return self.initial
def get_object_id(self, obj):

View File

@ -93,7 +93,7 @@ class CreateScriptForm(forms.SelfHandlingForm):
name = data['name']
LOG.info('Creating script with name %s' % (name))
ck_client = api.cloudkittyclient(request)
return ck_client.pyscripts.scripts.create(
return ck_client.rating.pyscripts.create_script(
name=name,
data=data['script_data'])
@ -105,10 +105,6 @@ class EditScriptForm(CreateScriptForm):
fields_order = ['script_id', 'name', 'script_source', 'script_upload',
'script_data']
def __init__(self, request, *args, **kwargs):
super(EditScriptForm, self).__init__(request, *args, **kwargs)
self.order_fields(self.fields_order)
class Meta(object):
name = _("Upate Script")
@ -116,6 +112,5 @@ class EditScriptForm(CreateScriptForm):
script_id = self.initial['script_id']
LOG.info('Updating script with id %s' % (script_id))
ck_client = api.cloudkittyclient(request)
return ck_client.pyscripts.scripts.update(script_id=script_id,
name=data['name'],
data=data['script_data'])
return ck_client.rating.pyscripts.update_script(
script_id=script_id, name=data['name'], data=data['script_data'])

View File

@ -71,7 +71,7 @@ class DeletePyScript(tables.DeleteAction):
)
def action(self, request, script_id):
api.cloudkittyclient(request).pyscripts.scripts.delete(
api.cloudkittyclient(request).rating.pyscripts.delete_script(
script_id=script_id)

View File

@ -31,8 +31,9 @@ class IndexView(tables.DataTableView):
template_name = 'admin/pyscripts/pyscripts_list.html'
def get_data(self):
data = api.cloudkittyclient(self.request).pyscripts.scripts.list()
data = api.identify(data, name=False)
data = api.cloudkittyclient(
self.request).rating.pyscripts.list_scripts()['scripts']
data = api.identify(data, key='script_id')
return data
@ -59,9 +60,11 @@ class ScriptUpdateView(forms.ModalFormView):
template_name = 'admin/pyscripts/form.html'
def get_initial(self):
script = api.cloudkittyclient(self.request).pyscripts.scripts.get(
client = api.cloudkittyclient(self.request)
script = client.rating.pyscripts.get_script(
script_id=self.kwargs['script_id'])
self.initial = script.to_dict()
self.initial = script
self.initial['script_data'] = self.initial['data']
return self.initial
@ -83,8 +86,8 @@ class ScriptDetailsView(views.APIView):
def get_data(self, request, context, *args, **kwargs):
script_id = kwargs.get("script_id")
try:
script = api.cloudkittyclient(self.request).pyscripts.scripts.get(
script_id=script_id)
client = api.cloudkittyclient(self.request)
script = client.rating.pyscripts.get_script(script_id=script_id)
except Exception:
script = None
context['script'] = script

View File

@ -29,7 +29,7 @@ class IndexView(views.APIView):
def get_data(self, request, context, *args, **kwargs):
# Add data to the context here...
total = api.cloudkittyclient(request).reports.get_total(
total = api.cloudkittyclient(request).report.get_total(
tenant_id=request.user.tenant_id) or 0.00
context['total'] = total
return context

View File

@ -31,8 +31,8 @@ def _do_this_month(data):
# stacked graphs
start_timestamp = None
end_timestamp = None
for dataframe in data:
begin = dataframe.begin
for dataframe in data.get('dataframes', []):
begin = dataframe['begin']
timestamp = int(time.mktime(
datetime.datetime.strptime(begin[:16],
"%Y-%m-%dT%H:%M").timetuple()))
@ -41,7 +41,7 @@ def _do_this_month(data):
if end_timestamp is None or timestamp > end_timestamp:
end_timestamp = timestamp
for resource in dataframe.resources:
for resource in dataframe['resources']:
service_id = resource['service']
service_data = services.setdefault(
service_id, {'cumulated': 0, 'hourly': {}})
@ -79,7 +79,7 @@ class CostRepartitionTab(tabs.Tab):
begin = "%4d-%02d-01T00:00:00" % (today.year, today.month)
end = "%4d-%02d-%02dT23:59:59" % (today.year, today.month, day_end)
client = api.cloudkittyclient(request)
data = client.storage.dataframes.list(
data = client.storage.get_dataframes(
begin=begin, end=end, tenant_id=request.user.tenant_id)
parsed_data = _do_this_month(data)
return {'repartition_data': parsed_data}

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Objectif Libre
#
# 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.
#
class TemplatizableDict(dict):
"""Class allowing to pass a dict to horizon templates"""
def __getattr__(self, key):
return self[key]
def __setattr__(self, key, val):
self[key] = val