diff --git a/tackerclient/shell.py b/tackerclient/shell.py index 3193b71b..816c35fb 100644 --- a/tackerclient/shell.py +++ b/tackerclient/shell.py @@ -83,6 +83,8 @@ COMMAND_V1 = { 'device-show': device.ShowDevice, 'device-update': device.UpdateDevice, 'device-delete': device.DeleteDevice, + 'interface-attach': device.AttachInterface, + 'interface-detach': device.DetachInterface, } COMMANDS = {'1.0': COMMAND_V1} diff --git a/tackerclient/tacker/v1_0/vm/device.py b/tackerclient/tacker/v1_0/vm/device.py index 55e42757..efb7a8ef 100644 --- a/tackerclient/tacker/v1_0/vm/device.py +++ b/tackerclient/tacker/v1_0/vm/device.py @@ -19,6 +19,9 @@ # # @author: Isaku Yamahata, Intel +import abc +import six + from tackerclient.common import exceptions from tackerclient.openstack.common.gettextutils import _ from tackerclient.tacker import v1_0 as tackerV10 @@ -135,3 +138,45 @@ class DeleteDevice(tackerV10.DeleteCommand): """Delete a given Device.""" resource = _DEVICE + + +@six.add_metaclass(abc.ABCMeta) +class _XtachInterface(tackerV10.UpdateCommand): + resource = _DEVICE + + @abc.abstractmethod + def call_api(self, tacker_client, device_id, body): + pass + + def args2body(self, parsed_args): + body = { + 'port_id': parsed_args.port_id, + } + tackerV10.update_dict(parsed_args, body, []) + return body + + def get_parser(self, prog_name): + parser = super(AttachInterface, self).get_parser(prog_name) + parser.add_argument('port_id', metavar='PORT', + help=_('port to attach/detach')) + self.add_known_arguments(parser) + return parser + + def run(self, parsed_args): + tacker_client = self.get_client() + tacker_client.format = parsed_args.request_format + body = self.args2body(parsed_args) + _id = tackerV10.find_resourceid_by_name_or_id(tacker_client, + self.resource, + parsed_args.id) + self.call_api(tacker_client, _id, body) + + +class AttachInterface(_XtachInterface): + def call_api(self, tacker_client, device_id, body): + return tacker_client.attach_interface(device_id, body) + + +class DetachInterface(_XtachInterface): + def call_api(self, tacker_client, device_id, body): + return tacker_client.detach_interface(device_id, body) diff --git a/tackerclient/v1_0/client.py b/tackerclient/v1_0/client.py index 989c75c8..59871567 100644 --- a/tackerclient/v1_0/client.py +++ b/tackerclient/v1_0/client.py @@ -150,6 +150,8 @@ class Client(object): device_template_path = '/device-templates/%s' devices_path = '/devices' device_path = '/devices/%s' + interface_attach_path = '/devices/%s/attach_interface' + interface_detach_path = '/devices/%s/detach_interface' # API has no way to report plurals, so we have to hard code them EXTED_PLURALS = {} @@ -220,6 +222,14 @@ class Client(object): def delete_device(self, device): return self.delete(self.device_path % device) + @APIParamsCall + def attach_interface(self, device, body=None): + return self.put(self.attach_interface_path % device, body) + + @APIParamsCall + def detach_interface(self, device, body=None): + return self.put(self.detach_interface_path % device, body) + def __init__(self, **kwargs): """Initialize a new client for the Tacker v1.0 API.""" super(Client, self).__init__()