Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/vm filter #322

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: 3.7
- name: Set Cache Key
Expand All @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@v2

- name: Set up Python 3.7 For Nox
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: 3.7

Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down Expand Up @@ -192,7 +192,7 @@ jobs:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down Expand Up @@ -315,7 +315,7 @@ jobs:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down
21 changes: 19 additions & 2 deletions src/saltext/vmware/modules/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@ def __virtual__():
return __virtualname__


def list_(service_instance=None, profile=None):
def list_(
service_instance=None, datacenter_name=None, cluster_name=None, host_name=None, profile=None
):
"""
Returns virtual machines.

datacenter_name
Filter by this datacenter name (required when cluster is specified)

cluster_name
Filter by this cluster name (optional)

host_name
Filter by this host name (optional)

service_instance
(optional) The Service Instance from which to obtain managed object references.

Expand All @@ -43,10 +54,16 @@ def list_(service_instance=None, profile=None):

salt '*' vmware_vm.list
"""
log.debug("Running vmware_vm.list")
service_instance = service_instance or connect.get_service_instance(
config=__opts__, profile=profile
)
return utils_vm.list_vms(service_instance)
return utils_vm.list_vms(
service_instance=service_instance,
host_name=host_name,
cluster_name=cluster_name,
datacenter_name=datacenter_name,
)


def list_templates(service_instance=None, profile=None):
Expand Down
56 changes: 53 additions & 3 deletions src/saltext/vmware/utils/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,68 @@ def unregister_vm(vm_ref):
raise salt.exceptions.VMwareRuntimeError(exc.msg)


def list_vms(service_instance):
def list_vms(
service_instance,
datacenter_name=None,
cluster_name=None,
host_name=None,
):
"""
Returns a list of VMs associated with a given service instance.

service_instance
The Service Instance Object from which to obtain VMs.

datacenter_name
The datacenter name. Default is None.

cluster_name
The cluster name - used to restrict the VMs retrieved. Only used if
the datacenter is set. This argument is optional.

host_name
The host_name to be retrieved. Default is None.
"""
properties = ["name", "config"]

if cluster_name and not datacenter_name:
raise salt.exceptions.ArgumentValueError(
"Must specify the datacenter when specifying the cluster"
)

if not datacenter_name:
# Assume the root folder is the starting point
start_point = utils_common.get_root_folder(service_instance)
else:
start_point = utils_datacenter.get_datacenter(service_instance, datacenter_name)
if cluster_name:
# Retrieval to test if cluster exists. Cluster existence only makes
# sense if the datacenter has been specified
properties.append("parent")

if 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,
)

items = []
vms = utils_common.get_mors_with_properties(service_instance, vim.VirtualMachine)
for vm in vms:
if not vm["config"].template:
items.append(vm["name"])
if host_name:
if host == vm["runtime.host"]:
items.append(vm["name"])
else:
items.append(vm["name"])
return items


Expand Down
5 changes: 4 additions & 1 deletion tests/integration/modules/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def test_vm_list(integration_test_config, patch_salt_globals_vm):
"""
Test vm list_
"""
all = virtual_machine.list_()
all = virtual_machine.list_(
datacenter_name="Datacenter",
cluster_name="Cluster",
)
assert all == integration_test_config["virtual_machines"]


Expand Down
Loading