Server IP resource

The server IP resource can only list.  The Resource.list method
needed to be overridden because:
* The url path contains the server_id.
* The output is a dict of networks rather than an array.

Also in this change:
* ServerIP objects have sufficient information to stand on their
  own because they contain the server_id and network_label.
* An ips(session) convenience method has been added to the Server.
* The list example has been updated to pass data to path_args so
  the list method and format the url.

The path_args parameter to list may be convenient for other
classes.  I didn't add it to the Resource.list method because
that isn't required for this change.

Change-Id: Iac77aba144d2e5b4ca4cdbf97e534d83519066ef
This commit is contained in:
Terry Howe 2014-08-14 14:33:08 -06:00
parent fa5b9ffdc2
commit b9cc376271
4 changed files with 181 additions and 1 deletions

View File

@ -19,7 +19,10 @@ from examples import session
def run_list(opts):
sess = session.make_session(opts)
cls = common.find_resource_cls(opts)
for obj in cls.list(sess):
path_args = None
if opts.data:
path_args = common.get_data_option(opts)
for obj in cls.list(sess, path_args=path_args):
print(str(obj))
return

View File

@ -11,6 +11,7 @@
# under the License.
from openstack.compute import compute_service
from openstack.compute.v2 import server_ip
from openstack import resource
from openstack import utils
@ -45,6 +46,11 @@ class Server(resource.Resource):
updated = resource.prop('updated')
user_id = resource.prop('user_id')
def ips(self, session):
"""Get server IPs."""
path_args = {'server_id': self.id}
return server_ip.ServerIP.list(session, path_args=path_args)
def action(self, session, body):
"""Preform server actions given the message body."""
url = utils.urljoin(self.base_path, self.id, 'action')

View File

@ -0,0 +1,56 @@
# 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 six
from openstack.compute import compute_service
from openstack import resource
class ServerIP(resource.Resource):
resource_key = 'server_ip'
resources_key = 'server_ips'
base_path = '/servers/%(server_id)s/ips'
service = compute_service.ComputeService()
# capabilities
allow_list = True
# Properties
addr = resource.prop('addr')
network_label = resource.prop('network_label')
server_id = resource.prop('server_id')
version = resource.prop('version')
@property
def id(self):
try:
val = self.addr
except AttributeError:
val = None
return val
@classmethod
def list(cls, session, path_args=None, **params):
url = cls.base_path % path_args
resp = session.get(url, service=cls.service, params=params)
ray = []
for network_label, addresses in six.iteritems(resp.body['addresses']):
for address in addresses:
record = {
'server_id': path_args['server_id'],
'network_label': network_label,
'version': address['version'],
'addr': address['addr'],
}
ray.append(cls.existing(**record))
return ray

View File

@ -0,0 +1,115 @@
# 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 mock
import testtools
from openstack.compute.v2 import server_ip
IDENTIFIER = 'IDENTIFIER'
EXAMPLE = {
'addr': '1',
'network_label': '2',
'server_id': '3',
'version': '4',
}
BODY = {
"addresses": {
"public": [
{
"version": 4,
"addr": "67.23.10.132"
},
{
"version": 6,
"addr": "::babe:67.23.10.132"
},
{
"version": 4,
"addr": "67.23.10.131"
},
{
"version": 6,
"addr": "::babe:4317:0A83"
}
],
"private": [
{
"version": 4,
"addr": "10.176.42.16"
},
{
"version": 6,
"addr": "::babe:10.176.42.16"
}
]
}
}
class TestServerIP(testtools.TestCase):
def test_basic(self):
sot = server_ip.ServerIP()
self.assertEqual('server_ip', sot.resource_key)
self.assertEqual('server_ips', sot.resources_key)
self.assertEqual('/servers/%(server_id)s/ips', sot.base_path)
self.assertEqual('compute', sot.service.service_type)
self.assertFalse(sot.allow_create)
self.assertFalse(sot.allow_retrieve)
self.assertFalse(sot.allow_update)
self.assertFalse(sot.allow_delete)
self.assertTrue(sot.allow_list)
def test_make_it(self):
sot = server_ip.ServerIP(EXAMPLE)
self.assertEqual(EXAMPLE['addr'], sot.id)
self.assertEqual(EXAMPLE['addr'], sot.addr)
self.assertEqual(EXAMPLE['network_label'], sot.network_label)
self.assertEqual(EXAMPLE['server_id'], sot.server_id)
self.assertEqual(EXAMPLE['version'], sot.version)
def test_list(self):
sess = mock.Mock()
resp = mock.Mock()
resp.body = BODY
sess.get = mock.Mock(return_value=resp)
path_args = {'server_id': IDENTIFIER}
caps = server_ip.ServerIP.list(sess, path_args=path_args)
caps = sorted(caps, key=lambda cap: cap.id)
self.assertEqual(6, len(caps))
self.assertEqual('10.176.42.16', caps[0].addr)
self.assertEqual('private', caps[0].network_label)
self.assertEqual(IDENTIFIER, caps[0].server_id)
self.assertEqual(4, caps[0].version)
self.assertEqual('67.23.10.131', caps[1].addr)
self.assertEqual('public', caps[1].network_label)
self.assertEqual(IDENTIFIER, caps[1].server_id)
self.assertEqual(4, caps[1].version)
self.assertEqual('67.23.10.132', caps[2].addr)
self.assertEqual('public', caps[2].network_label)
self.assertEqual(IDENTIFIER, caps[2].server_id)
self.assertEqual(4, caps[2].version)
self.assertEqual('::babe:10.176.42.16', caps[3].addr)
self.assertEqual('private', caps[3].network_label)
self.assertEqual(IDENTIFIER, caps[3].server_id)
self.assertEqual(6, caps[3].version)
self.assertEqual('::babe:4317:0A83', caps[4].addr)
self.assertEqual('public', caps[4].network_label)
self.assertEqual(IDENTIFIER, caps[4].server_id)
self.assertEqual(6, caps[4].version)
self.assertEqual('::babe:67.23.10.132', caps[5].addr)
self.assertEqual('public', caps[5].network_label)
self.assertEqual(IDENTIFIER, caps[5].server_id)
self.assertEqual(6, caps[5].version)