API for dashboard entries

- Dashboard entry DB model and DB migration logic added,
 - Handler, json schema and validation added,
 - Serializer added,
 - Dashboard entires info added into statistics,
 - Public url for Dashboard entry creation.

Implements blueprint post-deployment-dashboard
Change-Id: I5e613bb35366227d1265fbc698e2efeddf931cc7
This commit is contained in:
Vladimir Sharshov (warpc) 2015-07-08 20:29:51 +03:00
parent 9c177c3404
commit fe5552b012
15 changed files with 560 additions and 1 deletions

View File

@ -0,0 +1,101 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# 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.
from nailgun.api.v1.handlers import base
from nailgun.api.v1.handlers.base import content
from nailgun.api.v1.validators import dashboard_entry
from nailgun.errors import errors
from nailgun import objects
class DashboardEntryHandler(base.SingleHandler):
validator = dashboard_entry.DashboardEntryValidator
single = objects.DashboardEntry
def GET(self, cluster_id, obj_id):
self.get_object_or_404(objects.Cluster, cluster_id)
obj = self.get_object_or_404(self.single, obj_id)
return self.single.to_json(obj)
@content
def PUT(self, cluster_id, obj_id):
""":returns: JSONized REST object.
:http: * 200 (OK)
* 404 (object not found in db)
"""
obj = self.get_object_or_404(self.single, obj_id)
data = self.checked_data(
self.validator.validate_update,
instance=obj
)
self.single.update(obj, data)
return self.single.to_json(obj)
def PATCH(self, cluster_id, obj_id):
""":returns: JSONized REST object.
:http: * 200 (OK)
* 404 (object not found in db)
"""
return self.PUT(cluster_id, obj_id)
@content
def DELETE(self, cluster_id, obj_id):
""":returns: JSONized REST object.
:http: * 200 (OK)
* 404 (object not found in db)
"""
d_e = self.get_object_or_404(self.single, obj_id)
self.single.delete(d_e)
raise self.http(204)
class DashboardEntryCollectionHandler(base.CollectionHandler):
collection = objects.DashboardEntryCollection
validator = dashboard_entry.DashboardEntryValidator
@content
def GET(self, cluster_id):
""":returns: Collection of JSONized DashboardEntry objects.
:http: * 200 (OK)
* 404 (cluster not found in db)
"""
self.get_object_or_404(objects.Cluster, cluster_id)
return self.collection.to_json(
self.collection.get_by_cluster_id(cluster_id)
)
@content
def POST(self, cluster_id):
""":returns: JSONized REST object.
:http: * 201 (object successfully created)
* 400 (invalid object data specified)
"""
data = self.checked_data()
try:
new_obj = self.collection.create_with_cluster_id(data, cluster_id)
except errors.CannotCreate as exc:
raise self.http(400, exc.message)
raise self.http(201, self.collection.single.to_json(new_obj))

View File

@ -38,6 +38,11 @@ from nailgun.api.v1.handlers.cluster import VmwareAttributesDefaultsHandler
from nailgun.api.v1.handlers.cluster import VmwareAttributesHandler
from nailgun.api.v1.handlers.component import ComponentCollectionHandler
from nailgun.api.v1.handlers.dashboard_entry \
import DashboardEntryCollectionHandler
from nailgun.api.v1.handlers.dashboard_entry \
import DashboardEntryHandler
from nailgun.api.v1.handlers.logs import LogEntryCollectionHandler
from nailgun.api.v1.handlers.logs import LogPackageDefaultConfig
from nailgun.api.v1.handlers.logs import LogPackageHandler
@ -212,6 +217,11 @@ urls = (
r'/clusters/(?P<cluster_id>\d+)/vmware_attributes/defaults/?$',
VmwareAttributesDefaultsHandler,
r'/clusters/(?P<cluster_id>\d+)/dashboard_entries/?$',
DashboardEntryCollectionHandler,
r'/clusters/(?P<cluster_id>\d+)/dashboard_entries/(?P<obj_id>\d+)/?$',
DashboardEntryHandler,
r'/nodegroups/?$',
NodeGroupCollectionHandler,
r'/nodegroups/(?P<obj_id>\d+)/?$',
@ -367,5 +377,6 @@ def public_urls():
return {
r'/nodes/?$': ['POST'],
r'/nodes/agent/?$': ['PUT'],
r'/version/?$': ['GET']
r'/version/?$': ['GET'],
r'/clusters/(?P<cluster_id>\d+)/dashboard_entries/?$': ['POST']
}

View File

@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# 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.
from nailgun.api.v1.validators.base import BasicValidator
from nailgun.api.v1.validators.json_schema import dashboard_entry
class DashboardEntryValidator(BasicValidator):
collection_schema = dashboard_entry.DASHBOARD_ENTRIES_SCHEMA
@classmethod
def validate(cls, data):
parsed = super(DashboardEntryValidator, cls).validate(data)
cls.validate_schema(parsed, dashboard_entry.DASHBOARD_ENTRY_SCHEMA)
return parsed
@classmethod
def validate_update(cls, data, instance):
parsed = super(DashboardEntryValidator, cls).validate(data)
cls.validate_schema(parsed,
dashboard_entry.DASHBOARD_ENTRY_UPDATE_SCHEMA)
return parsed
@classmethod
def validate_create(cls, data):
return cls.validate(data)

View File

@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# 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.
DASHBOARD_ENTRY_SCHEMA = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'properties': {
'id': {'type': 'integer'},
'title': {'type': 'string', 'maxLength': 50, 'minLength': 1},
'url': {'type': 'string'},
'description': {'type': 'string'}
},
'required': ['title', 'url']
}
DASHBOARD_ENTRIES_SCHEMA = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'array',
'items': DASHBOARD_ENTRY_SCHEMA}
DASHBOARD_ENTRY_UPDATE_SCHEMA = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'properties': {
'title': {'type': 'string', 'maxLength': 50, 'minLength': 1},
'url': {'type': 'string'},
'description': {'type': 'string'}
}
}

View File

@ -106,9 +106,11 @@ def upgrade():
upgrade_cluster_plugins()
upgrade_add_baremetal_net()
upgrade_with_components()
dashboard_entries_upgrade()
def downgrade():
dashboard_entries_downgrade()
downgrade_with_components()
downgrade_add_baremetal_net()
downgrade_cluster_plugins()
@ -474,3 +476,22 @@ def downgrade_add_baremetal_net():
def downgrade_with_components():
op.drop_column('plugins', 'components_metadata')
op.drop_column('releases', 'components_metadata')
def dashboard_entries_upgrade():
op.create_table(
'dashboard_entries',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('cluster_id', sa.Integer(), nullable=False),
sa.Column('title', sa.Text(), nullable=False),
sa.Column('url', sa.Text(), nullable=False),
sa.Column('description', sa.Text()),
sa.ForeignKeyConstraint(['cluster_id'], ['clusters.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index('dashboard_entries_cluster_id_key',
'dashboard_entries', ['cluster_id'])
def dashboard_entries_downgrade():
op.drop_table('dashboard_entries')

View File

@ -43,6 +43,7 @@ from nailgun.db.sqlalchemy.models.network_config import NeutronConfig
from nailgun.db.sqlalchemy.models.network_config import NovaNetworkConfig
from nailgun.db.sqlalchemy.models.notification import Notification
from nailgun.db.sqlalchemy.models.dashboard_entry import DashboardEntry
from nailgun.db.sqlalchemy.models.task import Task

View File

@ -88,6 +88,8 @@ class Cluster(Base):
nodes = relationship(
"Node", backref="cluster", cascade="delete", order_by='Node.id')
tasks = relationship("Task", backref="cluster", cascade="delete")
dashboard_entries = relationship(
"DashboardEntry", backref="cluster", cascade="delete")
attributes = relationship("Attributes", uselist=False,
backref="cluster", cascade="delete")
changes_list = relationship("ClusterChanges", backref="cluster",

View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# 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.
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import Text
from nailgun.db.sqlalchemy.models.base import Base
class DashboardEntry(Base):
__tablename__ = 'dashboard_entries'
id = Column(Integer, primary_key=True)
cluster_id = Column(Integer, ForeignKey('clusters.id'))
title = Column(Text, nullable=False)
url = Column(Text, nullable=False)
description = Column(Text)

View File

@ -54,3 +54,6 @@ from nailgun.objects.plugin import ClusterPlugins
from nailgun.objects.network_group import NetworkGroup
from nailgun.objects.network_group import NetworkGroupCollection
from nailgun.objects.dashboard_entry import DashboardEntry
from nailgun.objects.dashboard_entry import DashboardEntryCollection

View File

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
# Copyright 2014 Mirantis, Inc.
#
# 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.
from nailgun.db.sqlalchemy.models import dashboard_entry \
as dashboard_entry_db_model
from nailgun.objects import base
from nailgun.objects.serializers import dashboard_entry
class DashboardEntry(base.NailgunObject):
model = dashboard_entry_db_model.DashboardEntry
serializer = dashboard_entry.DashboardEntrySerializer
class DashboardEntryCollection(base.NailgunCollection):
single = DashboardEntry
@classmethod
def get_by_cluster_id(cls, cluster_id):
if cluster_id == '':
return cls.filter_by(None, cluster_id=None)
return cls.filter_by(None, cluster_id=cluster_id)
@classmethod
def create_with_cluster_id(cls, data, cluster_id):
data['cluster_id'] = cluster_id
return cls.create(data)

View File

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# 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.
from nailgun.objects.serializers.base import BasicSerializer
class DashboardEntrySerializer(BasicSerializer):
fields = (
"id",
"title",
"url",
"description"
)

View File

@ -195,6 +195,8 @@ class InstallationInfo(object):
vmware_attributes_editable,
self.vmware_attributes_white_list
),
'dashboard_entries': self.get_dashboard_entries(
cluster.dashboard_entries),
'net_provider': cluster.net_provider,
'fuel_version': cluster.fuel_version,
'is_customized': cluster.is_customized,
@ -304,6 +306,10 @@ class InstallationInfo(object):
groups_info.append(group_info)
return groups_info
def get_dashboard_entries(self, dashboard_entries):
return [{'title': e.title, 'description': e.description, 'id': e.id}
for e in dashboard_entries]
def get_installation_info(self):
clusters_info = self.get_clusters_info()
allocated_nodes_num = sum([c['nodes_num'] for c in clusters_info])

View File

@ -53,11 +53,13 @@ from nailgun.logger import logger
from nailgun.db.sqlalchemy.fixman import load_fake_deployment_tasks
from nailgun.db.sqlalchemy.fixman import load_fixture
from nailgun.db.sqlalchemy.fixman import upload_fixture
from nailgun.db.sqlalchemy.models import DashboardEntry
from nailgun.db.sqlalchemy.models import NodeAttributes
from nailgun.db.sqlalchemy.models import NodeNICInterface
from nailgun.db.sqlalchemy.models import Notification
from nailgun.db.sqlalchemy.models import Task
# here come objects
from nailgun.objects import Cluster
from nailgun.objects import ClusterPlugins
@ -445,6 +447,23 @@ class EnvironmentManager(object):
ClusterPlugins.set_attributes(cluster.id, plugin.id, enabled=True)
return plugin
def create_dashboard_entry(self, **kwargs):
dash_data = {
"title": "title",
"url": "url",
"description": "description",
"cluster_id": None
}
if kwargs:
dash_data.update(kwargs)
dashboard_entry = DashboardEntry()
dashboard_entry.cluster_id = dash_data.get("cluster_id")
for f, v in dash_data.iteritems():
setattr(dashboard_entry, f, v)
self.db.add(dashboard_entry)
self.db.commit()
return dashboard_entry
def default_metadata(self):
item = self.find_item_by_pk_model(
self.read_fixtures(("sample_environment",)),

View File

@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# 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.
from nailgun.test.base import BaseIntegrationTest
from nailgun.utils import reverse
from oslo.serialization import jsonutils
class TestAssignmentHandlers(BaseIntegrationTest):
def test_dashboard_entries_list_empty(self):
cluster = self.env.create(
cluster_kwargs={"api": True},
nodes_kwargs=[{}]
)
resp = self.app.get(
reverse(
'DashboardEntryCollectionHandler',
kwargs={'cluster_id': cluster['id']}
),
headers=self.default_headers
)
self.assertEqual(200, resp.status_code)
self.assertEqual([], resp.json_body)
def test_dashboard_entry_creation(self):
cluster = self.env.create(
cluster_kwargs={"api": True},
nodes_kwargs=[{}]
)
title = 'test title'
url = 'http://test.com/url'
description = 'short description'
resp = self.app.post(
reverse(
'DashboardEntryCollectionHandler',
kwargs={'cluster_id': cluster['id']}
),
params=jsonutils.dumps({
"title": title,
"url": url,
"description": description
}),
headers=self.default_headers
)
self.assertEqual(resp.status_code, 201)
dashboard_entry = self.env.clusters[0].dashboard_entries[0]
self.assertEqual(dashboard_entry.title, title)
self.assertEqual(dashboard_entry.url, url)
self.assertEqual(dashboard_entry.description, description)
def test_dashboard_entry_fail_creation(self):
cluster = self.env.create(
cluster_kwargs={"api": True},
nodes_kwargs=[{}]
)
title = 'test title'
description = 'short description'
resp = self.app.post(
reverse(
'DashboardEntryCollectionHandler',
kwargs={'cluster_id': cluster['id']}
),
jsonutils.dumps({
'title': title,
'description': description
}),
headers=self.default_headers,
expect_errors=True
)
self.assertEqual(resp.status_code, 400)
self.assertEqual(len(self.env.clusters[0].dashboard_entries), 0)

View File

@ -0,0 +1,124 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# 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.
from nailgun.db.sqlalchemy.models.dashboard_entry import DashboardEntry
from nailgun.test.base import BaseIntegrationTest
from nailgun.utils import reverse
from oslo.serialization import jsonutils
class TestHandlers(BaseIntegrationTest):
def test_dashboard_entry_update(self):
cluster = self.env.create_cluster(api=False)
dashboard_entry = self.env \
.create_dashboard_entry(cluster_id=cluster.id)
dashboard_entry_update = {
'title': 'new title 2',
'description': 'new description 2'
}
resp = self.app.put(
reverse(
'DashboardEntryHandler',
kwargs={'cluster_id': cluster['id'],
'obj_id': dashboard_entry.id}
),
jsonutils.dumps(dashboard_entry_update),
headers=self.default_headers
)
self.assertEqual(dashboard_entry.id, resp.json_body['id'])
self.assertEqual('new title 2', resp.json_body['title'])
self.assertEqual('new description 2', resp.json_body['description'])
self.assertEqual(dashboard_entry.url, resp.json_body['url'])
def test_dashboard_entry_get_with_cluster(self):
cluster = self.env.create_cluster(api=False)
dashboard_entry = self.env \
.create_dashboard_entry(cluster_id=cluster.id)
resp = self.app.get(
reverse(
'DashboardEntryHandler',
kwargs={'cluster_id': cluster['id'],
'obj_id': dashboard_entry.id}
),
headers=self.default_headers
)
self.assertEqual(200, resp.status_code)
self.assertEqual(dashboard_entry.id, resp.json_body['id'])
self.assertEqual(dashboard_entry.title, resp.json_body['title'])
self.assertEqual(dashboard_entry.url, resp.json_body['url'])
self.assertEqual(dashboard_entry.description,
resp.json_body['description'])
def test_dashboard_entry_not_found(self):
cluster = self.env.create_cluster(api=False)
dashboard_entry = self.env \
.create_dashboard_entry(cluster_id=cluster.id)
resp = self.app.get(
reverse(
'DashboardEntryHandler',
kwargs={'cluster_id': cluster['id'],
'obj_id': dashboard_entry.id + 1}
),
headers=self.default_headers,
expect_errors=True
)
self.assertEqual(404, resp.status_code)
def test_dashboard_entry_delete(self):
cluster = self.env.create_cluster(api=False)
dashboard_entry = self.env \
.create_dashboard_entry(cluster_id=cluster.id)
resp = self.app.delete(
reverse(
'DashboardEntryHandler',
kwargs={'cluster_id': cluster['id'],
'obj_id': dashboard_entry.id}
),
headers=self.default_headers,
)
self.assertEqual(204, resp.status_code)
d_e_query = self.db.query(DashboardEntry) \
.filter_by(cluster_id=cluster.id)
self.assertEquals(d_e_query.count(), 0)
def test_dashboard_entry_patch(self):
cluster = self.env.create_cluster(api=False)
dashboard_entry = self.env \
.create_dashboard_entry(cluster_id=cluster.id)
dashboard_entry_update = {
'title': 'new title 3',
'description': 'new description 3'
}
resp = self.app.patch(
reverse(
'DashboardEntryHandler',
kwargs={'cluster_id': cluster['id'],
'obj_id': dashboard_entry.id}
),
jsonutils.dumps(dashboard_entry_update),
headers=self.default_headers
)
self.assertEqual(dashboard_entry.id, resp.json_body['id'])
self.assertEqual('new title 3', resp.json_body['title'])
self.assertEqual('new description 3', resp.json_body['description'])
self.assertEqual(dashboard_entry.url, resp.json_body['url'])