Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def func_args(arg_values, arg_names):
"""
Convert args and arg_names into positional args and keyword args.
"""
posargs = []
kwargs = {}
for i, name in enumerate(arg_names):
value = arg_values[i]
if name is not None:
kwargs[name] = value
else:
posargs.append(value)
return posargs, kwargs
class MyError(Error):
"""
Concrete class to get instances of abstract base class ``Error``.
"""
def __init__(self, *args, **kwargs):
super(MyError, self).__init__(*args, **kwargs)
class TestError(object):
"""
All tests for exception class Error.
Because this is an abstract base class, we use our own derived class
MyError.
"""
logger.addHandler(LOG_HANDLER)
logger.setLevel(logging.DEBUG)
logger = logging.getLogger('zhmcclient.api')
if LOG_HANDLER not in logger.handlers:
logger.addHandler(LOG_HANDLER)
logger.setLevel(logging.DEBUG)
# Creating a session does not interact with the HMC (logon is deferred)
session = zhmcclient.Session(
hd.hmc_host, hd.hmc_userid, hd.hmc_password)
# Check access to the HMC
try:
session.logon()
except zhmcclient.Error as exc:
msg = "Cannot log on to HMC {0} at {1} due to {2}: {3}". \
format(hd.nickname, hd.hmc_host, exc.__class__.__name__, exc)
hd.skip_msg = msg
pytest.skip(msg)
hd.skip_msg = None
session.hmc_definition = hd
yield session
session.logoff()
except zhmcclient.NotFound:
print("Could not find partition %s in CPC %s" % (partname, cpc.name))
sys.exit(1)
print("Accessing status of partition %s ..." % partition.name)
partition_status = partition.get_property('status')
print("Status of partition %s: %s" % (partition.name, partition_status))
if partition_status == 'active':
print("Stopping partition %s asynchronously ..." % partition.name)
job = partition.stop(wait_for_completion=False)
elif partition_status in ('inactive', 'stopped'):
print("Starting partition %s asynchronously ..." % partition.name)
job = partition.start(wait_for_completion=False)
else:
raise zhmcclient.Error("Cannot deal with partition status: %s" % \
partition_status)
print("Waiting for completion of job %s ..." % job.uri)
sys.stdout.flush()
# Just for demo purposes, we show how a loop for processing multiple
# notifications would look like.
while True:
with NOTI_LOCK:
# Wait until listener has a new notification
while not NOTI_DATA:
NOTI_LOCK.wait()
# Process the notification
print("Received notification:")
pprint(NOTI_DATA)
def cmd_port_show(cmd_ctx, cpc_name, adapter_name, port_name):
client = zhmcclient.Client(cmd_ctx.session)
port = find_port(cmd_ctx, client, cpc_name, adapter_name, port_name)
try:
port.pull_full_properties()
except zhmcclient.Error as exc:
raise_click_exception(exc, cmd_ctx.error_format)
cmd_ctx.spinner.stop()
print_properties(port.properties, cmd_ctx.output_format)
def cmd_session_create(cmd_ctx):
"""Create an HMC session."""
session = cmd_ctx.session
try:
# We need to first log off, to make the logon really create a new
# session. If we don't first log off, the session from the
# ZHMC_SESSION_ID env var will be used and no new session be created.
session.logoff(verify=True)
session.logon(verify=True)
except zhmcclient.Error as exc:
raise_click_exception(exc, cmd_ctx.error_format)
cmd_ctx.spinner.stop()
click.echo("export ZHMC_HOST=%s" % session.host)
click.echo("export ZHMC_USERID=%s" % session.userid)
click.echo("export ZHMC_SESSION_ID=%s" % session.session_id)
def raise_click_exception(exc, error_format):
"""
Raise a ClickException with the desired error message format.
Parameters:
exc (exception or string):
The exception or the message.
error_format (string):
The error format (see ``--error-format`` general option).
"""
if error_format == 'def':
if isinstance(exc, zhmcclient.Error):
error_str = exc.str_def()
else:
assert isinstance(exc, six.string_types)
error_str = "classname: None, message: {}".format(exc)
else:
assert error_format == 'msg'
if isinstance(exc, zhmcclient.Error):
error_str = "{}: {}".format(exc.__class__.__name__, exc)
else:
assert isinstance(exc, six.string_types)
error_str = exc
raise click.ClickException(error_str)
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
"CPC %s." %
(vswitch_name, cpc_name),
cmd_ctx.error_format)
properties['virtual-switch-uri'] = vswitch.uri
else:
# The backing adapter port or virtual switch is not being updated.
pass
if not properties:
cmd_ctx.spinner.stop()
click.echo("No properties specified for updating NIC %s." % nic_name)
return
try:
nic.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'] != nic_name:
click.echo("NIC %s has been renamed to %s and was updated." %
(nic_name, properties['name']))
else:
click.echo("NIC %s has been updated." % nic_name)
def find_adapter(cmd_ctx, client, cpc_name, adapter_name):
"""
Find an adapter 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:
adapter = cpc.adapters.find(name=adapter_name)
except zhmcclient.Error as exc:
raise_click_exception(exc, cmd_ctx.error_format)
return adapter
def cmd_metrics_env(cmd_ctx, cpc_name, options):
try:
client = zhmcclient.Client(cmd_ctx.session)
metric_group = 'zcpc-environmentals-and-power'
resource_filter = [
('cpc', cpc_name),
]
print_metric_groups(cmd_ctx, client, metric_group, resource_filter)
except zhmcclient.Error as exc:
raise_click_exception(exc, cmd_ctx.error_format)