Skip to content

Commit

Permalink
Fixed pre-commit issues
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi1693 committed Apr 26, 2022
1 parent fab4190 commit 087d837
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 45 deletions.
84 changes: 53 additions & 31 deletions src/saltext/vmware/modules/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ def __virtual__():
return __virtualname__


def list_(
service_instance=None,
datacenter_name=None,
cluster_name=None,
host_name=None
):
def list_(service_instance=None, datacenter_name=None, cluster_name=None, host_name=None):
"""
Returns virtual machines.
Expand All @@ -49,28 +44,32 @@ def list_(
"""
log.debug("Running vmware_vm.list")
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)
return utils_vm.list_vms(
service_instance=service_instance,
host_name=host_name,
cluster_name=cluster_name,
datacenter_name=datacenter_name
datacenter_name=datacenter_name,
)


def list_templates(service_instance=None):
def list_templates(service_instance=None, credential_name=None):
"""
Returns virtual machines tempates.
service_instance
(optional) The Service Instance from which to obtain managed object references.
"""
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)
return utils_vm.list_vm_templates(service_instance)


def path(vm_name, service_instance=None):
def path(vm_name, service_instance=None, credential_name=None):
"""
Returns specified virtual machine path.
Expand All @@ -81,7 +80,9 @@ def path(vm_name, service_instance=None):
The Service Instance from which to obtain managed object references.
"""
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)
vm_ref = utils_common.get_mor_by_property(
service_instance,
vim.VirtualMachine,
Expand All @@ -90,7 +91,7 @@ def path(vm_name, service_instance=None):
return utils_common.get_path(vm_ref, service_instance)


def _deploy_ovf(name, host_name, ovf, service_instance=None):
def _deploy_ovf(name, host_name, ovf, service_instance=None, credential_name=None):
"""
Helper fuctions that takes in a OVF file to create a virtual machine.
Expand All @@ -109,7 +110,9 @@ def _deploy_ovf(name, host_name, ovf, service_instance=None):
The Service Instance from which to obtain managed object references.
"""
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)

vms = list_(service_instance)
if name in vms:
Expand Down Expand Up @@ -138,7 +141,7 @@ def _deploy_ovf(name, host_name, ovf, service_instance=None):
return vm_ref


def deploy_ovf(vm_name, host_name, ovf_path, service_instance=None):
def deploy_ovf(vm_name, host_name, ovf_path, service_instance=None, credential_name=None):
"""
Deploy a virtual machine from an OVF
Expand All @@ -155,11 +158,11 @@ def deploy_ovf(vm_name, host_name, ovf_path, service_instance=None):
(optional) The Service Instance from which to obtain managed object references.
"""
ovf = utils_vm.read_ovf_file(ovf_path)
_deploy_ovf(vm_name, host_name, ovf, service_instance)
_deploy_ovf(vm_name, host_name, ovf, service_instance, credential_name)
return {"deployed": True}


def deploy_ova(vm_name, host_name, ova_path, service_instance=None):
def deploy_ova(vm_name, host_name, ova_path, service_instance=None, credential_name=None):
"""
Deploy a virtual machine from an OVA
Expand All @@ -176,11 +179,11 @@ def deploy_ova(vm_name, host_name, ova_path, service_instance=None):
(optional) The Service Instance from which to obtain managed object references.
"""
ovf = utils_vm.read_ovf_from_ova(ova_path)
_deploy_ovf(vm_name, host_name, ovf, service_instance)
_deploy_ovf(vm_name, host_name, ovf, service_instance, credential_name)
return {"deployed": True}


def deploy_template(vm_name, template_name, host_name, service_instance=None):
def deploy_template(vm_name, template_name, host_name, service_instance=None, credential_name=None):
"""
Deploy a virtual machine from a template virtual machine.
Expand All @@ -197,7 +200,9 @@ def deploy_template(vm_name, template_name, host_name, service_instance=None):
(optional) The Service Instance from which to obtain managed object references.
"""
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)

vms = list_(service_instance)
if vm_name in vms:
Expand All @@ -220,7 +225,7 @@ def deploy_template(vm_name, template_name, host_name, service_instance=None):
return {"deployed": True}


def info(vm_name=None, service_instance=None):
def info(vm_name=None, service_instance=None, credential_name=None):
"""
Return basic info about a vSphere VM guest
Expand All @@ -233,7 +238,9 @@ def info(vm_name=None, service_instance=None):
vms = []
info = {}
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)

if vm_name:
vms.append(
Expand Down Expand Up @@ -276,7 +283,7 @@ def info(vm_name=None, service_instance=None):
return info


def power_state(vm_name, state, datacenter_name=None, service_instance=None):
def power_state(vm_name, state, datacenter_name=None, service_instance=None, credential_name=None):
"""
Manages the power state of a virtual machine.
Expand All @@ -294,7 +301,9 @@ def power_state(vm_name, state, datacenter_name=None, service_instance=None):
"""
log.trace(f"Managing power state of virtual machine {vm_name} to {state}")
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)

if datacenter_name:
dc_ref = utils_common.get_mor_by_property(service_instance, vim.Datacenter, datacenter_name)
Expand Down Expand Up @@ -337,6 +346,7 @@ def boot_manager(
retry_delay=0,
efi_secure_boot_enabled=False,
service_instance=None,
credential_name=None,
):
"""
Manage boot option for a virtual machine
Expand All @@ -363,7 +373,9 @@ def boot_manager(
(optional) The Service Instance from which to obtain managed object references.
"""
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)

vm = utils_common.get_mor_by_property(service_instance, vim.VirtualMachine, vm_name)

Expand Down Expand Up @@ -394,6 +406,7 @@ def create_snapshot(
quiesce=False,
datacenter_name=None,
service_instance=None,
credential_name=None,
):
"""
Create snapshot of given vm.
Expand Down Expand Up @@ -421,7 +434,9 @@ def create_snapshot(
"""

if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)

if datacenter_name:
dc_ref = utils_common.get_mor_by_property(service_instance, vim.Datacenter, datacenter_name)
Expand All @@ -446,6 +461,7 @@ def destroy_snapshot(
remove_children=False,
datacenter_name=None,
service_instance=None,
credential_name=None,
):
"""
Destroy snapshot of given vm.
Expand All @@ -469,7 +485,9 @@ def destroy_snapshot(
(optional) The Service Instance from which to obtain managed object references.
"""
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)

if datacenter_name:
dc_ref = utils_common.get_mor_by_property(service_instance, vim.Datacenter, datacenter_name)
Expand All @@ -484,7 +502,7 @@ def destroy_snapshot(
return {"snapshot": "destroyed"}


def snapshot(vm_name, datacenter_name=None, service_instance=None):
def snapshot(vm_name, datacenter_name=None, service_instance=None, credential_name=None):
"""
Return info about a virtual machine snapshots
Expand All @@ -495,7 +513,9 @@ def snapshot(vm_name, datacenter_name=None, service_instance=None):
(optional) The Service Instance from which to obtain managed object references.
"""
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)

if datacenter_name:
dc_ref = utils_common.get_mor_by_property(service_instance, vim.Datacenter, datacenter_name)
Expand All @@ -510,7 +530,7 @@ def snapshot(vm_name, datacenter_name=None, service_instance=None):
return {"snapshots": snapshots}


def relocate(vm_name, new_host_name, datastore_name, service_instance=None):
def relocate(vm_name, new_host_name, datastore_name, service_instance=None, credential_name=None):
"""
Relocates a virtual machine to the location specified.
Expand All @@ -527,7 +547,9 @@ def relocate(vm_name, new_host_name, datastore_name, service_instance=None):
(optional) The Service Instance from which to obtain managed object references.
"""
if service_instance is None:
service_instance = connect.get_service_instance(opts=__opts__, pillar=__pillar__)
service_instance = connect.get_service_instance(
opts=__opts__, pillar=__pillar__, name=credential_name
)
vm_ref = utils_common.get_mor_by_property(service_instance, vim.VirtualMachine, vm_name)
resources = utils_common.deployment_resources(new_host_name, service_instance)
assert isinstance(datastore_name, str)
Expand Down
24 changes: 10 additions & 14 deletions src/saltext/vmware/utils/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ def unregister_vm(vm_ref):


def list_vms(
service_instance,
datacenter_name=None,
cluster_name=None,
host_name=None,
service_instance,
datacenter_name=None,
cluster_name=None,
host_name=None,
):
"""
Returns a list of VMs associated with a given service instance.
Expand Down Expand Up @@ -356,23 +356,19 @@ def list_vms(
properties.append("parent")

if host_name:
host = utils_common.get_mor_by_property(
service_instance,
vim.HostSystem,
host_name
)
host = utils_common.get_mor_by_property(service_instance, vim.HostSystem, host_name)
properties.append("runtime.host")
log.trace("Retrieved host: %s", host)
else:
host = None

# Search for the objects
vms = utils_common.get_mors_with_properties(
service_instance,
vim.VirtualMachine,
container_ref=start_point,
property_list=properties,
)
service_instance,
vim.VirtualMachine,
container_ref=start_point,
property_list=properties,
)

items = []
for vm in vms:
Expand Down

0 comments on commit 087d837

Please sign in to comment.