-
Notifications
You must be signed in to change notification settings - Fork 25
/
_cmd_vswitch.py
173 lines (137 loc) · 5.89 KB
/
_cmd_vswitch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Copyright 2016-2017 IBM Corp. 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.
from __future__ import absolute_import
import click
import zhmcclient
from .zhmccli import cli
from ._helper import print_properties, print_resources, \
options_to_properties, original_options, COMMAND_OPTIONS_METAVAR, \
raise_click_exception
from ._cmd_cpc import find_cpc
def find_vswitch(cmd_ctx, client, cpc_name, vswitch_name):
"""
Find a virtual switch by name and return its resource object.
"""
cpc = find_cpc(cmd_ctx, client, cpc_name)
# The CPC must be in DPM mode. We don't check that because it would
# cause a GET to the CPC resource that we otherwise don't need.
try:
vswitch = cpc.virtual_switches.find(name=vswitch_name)
except zhmcclient.Error as exc:
raise_click_exception(exc, cmd_ctx.error_format)
return vswitch
@cli.group('vswitch', options_metavar=COMMAND_OPTIONS_METAVAR)
def vswitch_group():
"""
Command group for managing virtual switches.
In addition to the command-specific options shown in this help text, the
general options (see 'zhmc --help') can also be specified right after the
'zhmc' command name.
"""
@vswitch_group.command('list', options_metavar=COMMAND_OPTIONS_METAVAR)
@click.argument('CPC', type=str, metavar='CPC')
@click.option('--adapter', is_flag=True, required=False,
help='Show additional properties for the backing adapter.')
@click.option('--uri', is_flag=True, required=False,
help='Show additional properties for the resource URI.')
@click.pass_obj
def vswitch_list(cmd_ctx, cpc, **options):
"""
List the virtual switches in a CPC.
In addition to the command-specific options shown in this help text, the
general options (see 'zhmc --help') can also be specified right after the
'zhmc' command name.
"""
cmd_ctx.execute_cmd(lambda: cmd_vswitch_list(cmd_ctx, cpc, options))
@vswitch_group.command('show', options_metavar=COMMAND_OPTIONS_METAVAR)
@click.argument('CPC', type=str, metavar='CPC')
@click.argument('VSWITCH', type=str, metavar='VSWITCH')
@click.pass_obj
def vswitch_show(cmd_ctx, cpc, vswitch):
"""
Show the details of a virtual switch.
In addition to the command-specific options shown in this help text, the
general options (see 'zhmc --help') can also be specified right after the
'zhmc' command name.
"""
cmd_ctx.execute_cmd(lambda: cmd_vswitch_show(cmd_ctx, cpc, vswitch))
@vswitch_group.command('update', options_metavar=COMMAND_OPTIONS_METAVAR)
@click.argument('CPC', type=str, metavar='CPC')
@click.argument('VSWITCH', type=str, metavar='VSWITCH')
@click.option('--name', type=str, required=False,
help='The new name of the virtual switch.')
@click.option('--description', type=str, required=False,
help='The new description of the virtual switch.')
@click.pass_obj
def vswitch_update(cmd_ctx, cpc, vswitch, **options):
"""
Update the properties of a virtual switch.
Only the properties will be changed for which a corresponding option is
specified, so the default for all options is not to change properties.
In addition to the command-specific options shown in this help text, the
general options (see 'zhmc --help') can also be specified right after the
'zhmc' command name.
"""
cmd_ctx.execute_cmd(lambda: cmd_vswitch_update(cmd_ctx, cpc, vswitch,
options))
def cmd_vswitch_list(cmd_ctx, cpc_name, options):
client = zhmcclient.Client(cmd_ctx.session)
cpc = find_cpc(cmd_ctx, client, cpc_name)
try:
vswitches = cpc.virtual_switches.list()
except zhmcclient.Error as exc:
raise_click_exception(exc, cmd_ctx.error_format)
show_list = [
'name',
]
if options['adapter']:
show_list.extend([
'type', # part of list()
'port', # needs to be retrieved
])
if options['uri']:
show_list.extend([
'object-uri',
])
cmd_ctx.spinner.stop()
print_resources(vswitches, cmd_ctx.output_format, show_list)
def cmd_vswitch_show(cmd_ctx, cpc_name, vswitch_name):
client = zhmcclient.Client(cmd_ctx.session)
vswitch = find_vswitch(cmd_ctx, client, cpc_name, vswitch_name)
try:
vswitch.pull_full_properties()
except zhmcclient.Error as exc:
raise_click_exception(exc, cmd_ctx.error_format)
cmd_ctx.spinner.stop()
print_properties(vswitch.properties, cmd_ctx.output_format)
def cmd_vswitch_update(cmd_ctx, cpc_name, vswitch_name, options):
client = zhmcclient.Client(cmd_ctx.session)
vswitch = find_vswitch(cmd_ctx, client, cpc_name, vswitch_name)
options = original_options(options)
properties = options_to_properties(options)
if not properties:
cmd_ctx.spinner.stop()
click.echo("No properties specified for updating virtual switch %s." %
vswitch_name)
return
try:
vswitch.update_properties(properties)
except zhmcclient.Error as exc:
raise_click_exception(exc, cmd_ctx.error_format)
cmd_ctx.spinner.stop()
if 'name' in properties and properties['name'] != vswitch_name:
click.echo("Virtual switch %s has been renamed to %s and was "
"updated." % (vswitch_name, properties['name']))
else:
click.echo("Virtual switch %s has been updated." % vswitch_name)