Convert server_usage v3 plugin to v2.1 API

Changes required to have v3 plugin natively support the V2.1
API.

The parameters is changed from os-server-usage to OS-SRV-USG

Partially implements blueprint v2-on-v3-api
Co-Authored-By: Chris Yeoh <cyeoh@au1.ibm.com>

Change-Id: I4a4ebe533f8910a29b54d6ec9554cc151af492d1
This commit is contained in:
jichenjc 2014-07-24 05:40:49 +08:00 committed by Chris Yeoh
parent 10a6c6cb0b
commit 0af17f3514
11 changed files with 44 additions and 149 deletions

View File

@ -63,8 +63,8 @@
"id": 1
}
],
"os-server-usage:launched_at": "2013-09-23T13:37:00.880302",
"os-server-usage:terminated_at": null,
"OS-SRV-USG:launched_at": "2013-09-23T13:37:00.880302",
"OS-SRV-USG:terminated_at": null,
"progress": 0,
"os-security-groups:security_groups": [
{

View File

@ -64,8 +64,8 @@
"id": 1
}
],
"os-server-usage:launched_at": "2013-09-23T13:53:12.774549",
"os-server-usage:terminated_at": null,
"OS-SRV-USG:launched_at": "2013-09-23T13:53:12.774549",
"OS-SRV-USG:terminated_at": null,
"progress": 0,
"os-security-groups:security_groups": [
{

View File

@ -46,8 +46,8 @@
"My Server Name": "Apache1"
},
"name": "new-server-test",
"os-server-usage:launched_at": "2013-08-15T08:12:40.108903",
"os-server-usage:terminated_at": null,
"OS-SRV-USG:launched_at": "2013-08-15T08:12:40.108903",
"OS-SRV-USG:terminated_at": null,
"progress": 0,
"status": "ACTIVE",
"tenant_id": "openstack",

View File

@ -47,8 +47,8 @@
"My Server Name": "Apache1"
},
"name": "new-server-test",
"os-server-usage:launched_at": "2013-08-15T12:04:05.368766",
"os-server-usage:terminated_at": null,
"OS-SRV-USG:launched_at": "2013-08-15T12:04:05.368766",
"OS-SRV-USG:terminated_at": null,
"progress": 0,
"status": "ACTIVE",
"tenant_id": "openstack",

View File

@ -22,6 +22,8 @@ LOG = logging.getLogger(__name__)
ALIAS = "os-server-usage"
authorize = extensions.soft_extension_authorizer('compute', 'v3:' + ALIAS)
resp_topic = "OS-SRV-USG"
class ServerUsageController(wsgi.Controller):
def __init__(self, *args, **kwargs):
@ -30,7 +32,7 @@ class ServerUsageController(wsgi.Controller):
def _extend_server(self, server, instance):
for k in ['launched_at', 'terminated_at']:
key = "%s:%s" % (ServerUsage.alias, k)
key = "%s:%s" % (resp_topic, k)
# NOTE(danms): Historically, this timestamp has been generated
# merely by grabbing str(datetime) of a TZ-naive object. The
# only way we can keep that with instance objects is to strip

View File

@ -57,12 +57,13 @@ def fake_compute_get_all(*args, **kwargs):
db_list, fields)
class ServerUsageTest(test.TestCase):
class ServerUsageTestV21(test.TestCase):
content_type = 'application/json'
prefix = 'OS-SRV-USG:'
_prefix = "/v3"
def setUp(self):
super(ServerUsageTest, self).setUp()
super(ServerUsageTestV21, self).setUp()
fakes.stub_out_nw_api(self.stubs)
self.stubs.Set(compute.api.API, 'get', fake_compute_get)
self.stubs.Set(compute.api.API, 'get_all', fake_compute_get_all)
@ -76,9 +77,12 @@ class ServerUsageTest(test.TestCase):
def _make_request(self, url):
req = fakes.HTTPRequest.blank(url)
req.accept = self.content_type
res = req.get_response(fakes.wsgi_app(init_only=('servers',)))
res = req.get_response(self._get_app())
return res
def _get_app(self):
return fakes.wsgi_app_v3(init_only=('servers', 'os-server-usage'))
def _get_server(self, body):
return jsonutils.loads(body).get('server')
@ -96,7 +100,7 @@ class ServerUsageTest(test.TestCase):
terminated_at)
def test_show(self):
url = '/v2/fake/servers/%s' % UUID3
url = self._prefix + ('/servers/%s' % UUID3)
res = self._make_request(url)
self.assertEqual(res.status_int, 200)
@ -107,7 +111,7 @@ class ServerUsageTest(test.TestCase):
terminated_at=DATE2)
def test_detail(self):
url = '/v2/fake/servers/detail'
url = self._prefix + '/servers/detail'
res = self._make_request(url)
self.assertEqual(res.status_int, 200)
@ -125,13 +129,27 @@ class ServerUsageTest(test.TestCase):
raise exception.InstanceNotFound(instance_id='fake')
self.stubs.Set(compute.api.API, 'get', fake_compute_get)
url = '/v2/fake/servers/70f6db34-de8d-4fbd-aafb-4065bdfa6115'
url = self._prefix + '/servers/70f6db34-de8d-4fbd-aafb-4065bdfa6115'
res = self._make_request(url)
self.assertEqual(res.status_int, 404)
class ServerUsageXmlTest(ServerUsageTest):
class ServerUsageTestV20(ServerUsageTestV21):
_prefix = "/v2/fake"
def setUp(self):
super(ServerUsageTestV20, self).setUp()
self.flags(
osapi_compute_extension=[
'nova.api.openstack.compute.contrib.select_extensions'],
osapi_compute_ext_list=['Server_usage'])
def _get_app(self):
return fakes.wsgi_app(init_only=('servers',))
class ServerUsageXmlTest(ServerUsageTestV20):
content_type = 'application/xml'
prefix = '{%s}' % server_usage.Server_usage.namespace

View File

@ -1,125 +0,0 @@
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
# 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.
import datetime
from nova import compute
from nova import db
from nova import exception
from nova import objects
from nova.objects import instance as instance_obj
from nova.openstack.common import jsonutils
from nova.openstack.common import timeutils
from nova import test
from nova.tests.api.openstack import fakes
from nova.tests import fake_instance
UUID1 = '00000000-0000-0000-0000-000000000001'
UUID2 = '00000000-0000-0000-0000-000000000002'
UUID3 = '00000000-0000-0000-0000-000000000003'
DATE1 = datetime.datetime(year=2013, month=4, day=5, hour=12)
DATE2 = datetime.datetime(year=2013, month=4, day=5, hour=13)
DATE3 = datetime.datetime(year=2013, month=4, day=5, hour=14)
def fake_compute_get(*args, **kwargs):
inst = fakes.stub_instance(1, uuid=UUID3, launched_at=DATE1,
terminated_at=DATE2)
return fake_instance.fake_instance_obj(args[1], **inst)
def fake_compute_get_all(*args, **kwargs):
db_list = [
fakes.stub_instance(2, uuid=UUID1, launched_at=DATE2,
terminated_at=DATE3),
fakes.stub_instance(3, uuid=UUID2, launched_at=DATE1,
terminated_at=DATE3),
]
fields = instance_obj.INSTANCE_DEFAULT_FIELDS
return instance_obj._make_instance_list(args[1],
objects.InstanceList(),
db_list, fields)
class ServerUsageTest(test.TestCase):
content_type = 'application/json'
prefix = 'os-server-usage:'
def setUp(self):
super(ServerUsageTest, self).setUp()
fakes.stub_out_nw_api(self.stubs)
self.stubs.Set(compute.api.API, 'get', fake_compute_get)
self.stubs.Set(compute.api.API, 'get_all', fake_compute_get_all)
return_server = fakes.fake_instance_get()
self.stubs.Set(db, 'instance_get_by_uuid', return_server)
def _make_request(self, url):
req = fakes.HTTPRequestV3.blank(url)
req.accept = self.content_type
res = req.get_response(fakes.wsgi_app_v3(init_only=(
'servers', 'os-server-usage')))
return res
def _get_server(self, body):
return jsonutils.loads(body).get('server')
def _get_servers(self, body):
return jsonutils.loads(body).get('servers')
def assertServerUsage(self, server, launched_at, terminated_at):
resp_launched_at = timeutils.parse_isotime(
server.get('%slaunched_at' % self.prefix))
self.assertEqual(timeutils.normalize_time(resp_launched_at),
launched_at)
resp_terminated_at = timeutils.parse_isotime(
server.get('%sterminated_at' % self.prefix))
self.assertEqual(timeutils.normalize_time(resp_terminated_at),
terminated_at)
def test_show(self):
url = '/v3/servers/%s' % UUID3
res = self._make_request(url)
self.assertEqual(res.status_int, 200)
now = timeutils.utcnow()
timeutils.set_time_override(now)
self.assertServerUsage(self._get_server(res.body),
launched_at=DATE1,
terminated_at=DATE2)
def test_detail(self):
url = '/v3/servers/detail'
res = self._make_request(url)
self.assertEqual(res.status_int, 200)
servers = self._get_servers(res.body)
self.assertServerUsage(servers[0],
launched_at=DATE2,
terminated_at=DATE3)
self.assertServerUsage(servers[1],
launched_at=DATE1,
terminated_at=DATE3)
def test_no_instance_passthrough_404(self):
def fake_compute_get(*args, **kwargs):
raise exception.InstanceNotFound(instance_id='fake')
self.stubs.Set(compute.api.API, 'get', fake_compute_get)
url = '/v3/servers/70f6db34-de8d-4fbd-aafb-4065bdfa6115'
res = self._make_request(url)
self.assertEqual(res.status_int, 404)

View File

@ -59,8 +59,8 @@
"OS-EXT-STS:vm_state": "active",
"os-extended-volumes:volumes_attached": [],
"os-pci:pci_devices": [{"id": 1}],
"os-server-usage:launched_at": "%(strtime)s",
"os-server-usage:terminated_at": null,
"OS-SRV-USG:launched_at": "%(strtime)s",
"OS-SRV-USG:terminated_at": null,
"progress": 0,
"os-security-groups:security_groups": [
{

View File

@ -60,8 +60,8 @@
"OS-EXT-STS:vm_state": "active",
"os-extended-volumes:volumes_attached": [],
"os-pci:pci_devices": [{"id": 1}],
"os-server-usage:launched_at": "%(strtime)s",
"os-server-usage:terminated_at": null,
"OS-SRV-USG:launched_at": "%(strtime)s",
"OS-SRV-USG:terminated_at": null,
"progress": 0,
"os-security-groups:security_groups": [
{

View File

@ -1,7 +1,7 @@
{
"server": {
"os-server-usage:launched_at": "%(strtime)s",
"os-server-usage:terminated_at": null,
"OS-SRV-USG:launched_at": "%(strtime)s",
"OS-SRV-USG:terminated_at": null,
"addresses": {
"private": [
{

View File

@ -3,7 +3,7 @@
{
"status": "ACTIVE",
"created": "%(isotime)s",
"os-server-usage:launched_at": "%(strtime)s",
"OS-SRV-USG:launched_at": "%(strtime)s",
"user_id": "fake",
"addresses": {
"private": [
@ -38,7 +38,7 @@
]
},
"id": "%(uuid)s",
"os-server-usage:terminated_at": null,
"OS-SRV-USG:terminated_at": null,
"tenant_id": "openstack",
"progress": 0,
"flavor": {