diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c1dbe9..3f08bfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,20 @@ # Changelog All notable changes to this project will be documented in this file. + +## Development + +- Added new function `powercli::hosts_status` to retrieve the current connection status + for ESXi hosts in vCenter. + + Contributed by Nick Maludy (@nmaludy) + ## v0.1.2 (2020-11-13) - Simplified dvs_add_hosts resource. It will no longer take into consideration defaults and other complexities. These complexities are to be handed within the puppet profile which calls this resource. Contributed by Greg Perry (@gperry2011) -## Development - - Converted from Travis to GitHub Actions Contributed by Nick Maludy (@nmaludy) diff --git a/lib/puppet/functions/powercli/hosts_status.rb b/lib/puppet/functions/powercli/hosts_status.rb new file mode 100644 index 0000000..cfd2f47 --- /dev/null +++ b/lib/puppet/functions/powercli/hosts_status.rb @@ -0,0 +1,41 @@ +require 'rbvmomi' + +# Returns a hash of hosts along with their connection status +# Note: this uses PowerCLI under the hood, so it should be run as a deferred function +Puppet::Functions.create_function(:'powercli::hosts_status') do + # @param hosts Explicit list of hosts to retrieve status for. + # If not specified all hosts that exist in the vCenter will be returned + # @return Hash where the key is the ESXi hostname, the value is the connection status for + # that host, either 'online' or 'offline'. + # Hosts that have a connection status of 'Disconnected' or 'NotResponding' will be + # marked as 'offline', otherwise they will be returned as 'online'. + dispatch :hosts_status do + required_param 'Powercli::Connection', :connection + optional_param 'Array[String]', :hosts + return_type "Hash[String, Enum['online', 'offline']]" + end + + def hosts_status(connection, hosts = nil) + credentials = { + host: connection['server'], + user: connection['username'], + password: connection['password'], + insecure: true + } + vim = RbVmomi::VIM.connect(credentials) + + # recursive find/grep to find all ESX hosts + # Type list: https://code.vmware.com/apis/196/vsphere + query = { + container: vim.rootFolder, + type: ['HostSystem'], + recursive: true + } + host_list = vim.serviceContent.viewManager.CreateContainerView(query).view + results = {} + host_list.map do |host| + results[host.name] = host.runtime.connectionState + end + results + end +end diff --git a/lib/puppet/provider/powercli_esx_ntp/powercli.rb b/lib/puppet/provider/powercli_esx_ntp/powercli.rb index b6f756d..7802667 100644 --- a/lib/puppet/provider/powercli_esx_ntp/powercli.rb +++ b/lib/puppet/provider/powercli_esx_ntp/powercli.rb @@ -1,5 +1,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'powercli')) -require 'ruby-pwsh' Puppet::Type.type(:powercli_esx_ntp).provide(:api, parent: Puppet::Provider::PowerCLI) do commands powershell: 'powershell.exe' diff --git a/lib/puppet/provider/powercli_esx_syslog/powercli.rb b/lib/puppet/provider/powercli_esx_syslog/powercli.rb index 0291f10..414e8da 100644 --- a/lib/puppet/provider/powercli_esx_syslog/powercli.rb +++ b/lib/puppet/provider/powercli_esx_syslog/powercli.rb @@ -1,5 +1,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'powercli')) -require 'ruby-pwsh' require 'uri' Puppet::Type.type(:powercli_esx_syslog).provide(:api, parent: Puppet::Provider::PowerCLI) do diff --git a/lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb b/lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb index 60857fa..05c1f9b 100644 --- a/lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb +++ b/lib/puppet/provider/powercli_esx_vs_portgroup/powercli.rb @@ -1,5 +1,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'powercli')) -require 'ruby-pwsh' Puppet::Type.type(:powercli_esx_vs_portgroup).provide(:api, parent: Puppet::Provider::PowerCLI) do commands powershell: 'powershell.exe' diff --git a/lib/puppet_x/encore/powercli/helper.rb b/lib/puppet_x/encore/powercli/helper.rb index bd99169..ea48afa 100644 --- a/lib/puppet_x/encore/powercli/helper.rb +++ b/lib/puppet_x/encore/powercli/helper.rb @@ -1,4 +1,5 @@ require 'puppet_x' +require 'ruby-pwsh' require 'singleton' module PuppetX::PowerCLI diff --git a/manifests/init.pp b/manifests/init.pp index 3fef8e4..d0d3596 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -32,7 +32,6 @@ Optional[Powercli::Connection] $vcenter_connection = undef, Optional[Hash] $config = $powercli::params::config, ) inherits powercli::params { - pspackageprovider { 'Nuget': ensure => 'present', }