Reputation: 1
I need to get a list VMware vCenter VMs based on a wildcard. From what I have been reading is that vCenter does not support wildcards. I cannot pull a full list of VMs because there are over a thousand VMs in my environment.
There is another question similar to mine,"How do i filter using a partial VM name (string) in vmware vSphere client REST API?" from last year but the answer was a C# program that I could not understand since I really don't know C#. Basically the C# program was accessing the search functionality in the UI directly and pulling the data (brilliant!). I got lost in the C# program when the author started working with cookies.
Please excuse my code, I am new to Python and I'm trying to figure out APIs. I am an OS engineer, but I want to get better at programing. :) Here is the code I have so far that think is working. There are a few print statements to dump out the data as the program runs so that I can see the API responses:
import json
from base64 import b64encode
import base64
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from urllib.parse import parse_qs, urlparse
from bs4 import BeautifulSoup
vcip="myvcenter.fqdn.local" # vCenter server ip address/FQDN
vcid = "testuser@testdomain" # TESTING: Username
vcpw = "SeekretPassword" # TESTING: password
def get_loginurl(vcip):
vcurl = requests.get('https://'+vcip+'/ui/login',allow_redirects=True, verify=False)
return vcurl.url
# Encode the username and password.
vccred64enc = base64.urlsafe_b64encode(bytes(f'{vcid}:{vcpw}',
encoding='utf-8')).decode('utf-8')
# Create the authentication header
auth_header = f'Basic {vccred64enc}'
print(auth_header)
print("="*60)
# DEBUG: Display the SAML URL
vcuilogin = get_loginurl(vcip)
print(vcuilogin)
print("="*60)
# DEBUG: Authenticate to the redirected URL
saml1response = requests.post(vcuilogin,
headers={"Authorization": auth_header}, verify=False)
print(saml1response.url)
print("="*60)
# Castle Authorization - idk what this means
headers2 = {
'Authorization': auth_header,
'Content-type': 'application/x-www-form-urlencoded',
'CastleAuthorization=': auth_header
}
# Parse the URL and then separate the query section
samlparsed = urlparse(saml1response.url)
saml_qs_parse = parse_qs(samlparsed.query)
# Convert from a list item to a single string
samlrequest = ''.join(saml_qs_parse['SAMLRequest'])
samlparams = {'SAMLRequest': samlrequest}
buildurl1 = f"https://{samlparsed.netloc}{samlparsed.path}"
response = requests.post(buildurl1, params=samlparams, headers=headers2, verify=False)
# Make some Soup so that we can find the SAMLResponse
soup = BeautifulSoup(response.text, "html.parser")
#Extract the SAMLResponse value
saml_respvalue = soup.find('input', {'name': 'SAMLResponse'})['value']
print(f'SAMLResponse: {saml_respvalue}')
Upvotes: 0
Views: 7493
Reputation: 43
Can't filter by wildcard. What you can do is pull down all VM's per host and filter from there, something like this:
import requests
import json
vcenter_host = 'https://vcenter.example.com'
session = requests.Session()
session.post(f"{vcenter_host}/rest/com/vmware/cis/session",
auth=(username, password), verify=False)
# Reasonable assumption you have less that 1000 hosts
hosts =json.loads(session.get(f"{vcenter_host}/rest/vcenter/hosts").text)
vms = {i['host']: session.get(f"{vcenter_host}/rest/vcenter/vm",
params={'filter.hosts': i['host']})
for i in hosts['value']}
Then use regex module and/or jmespath to finish your filter.
Upvotes: 1
Reputation: 2121
Long story short, the vSphere REST API isn't really meant to be used for server side searches. There is however a pagination feature which will let you page through the 1000+ VMs you have, then filter through them locally on the client side.
One thing that might be worth looking into, the vSphere Automation SDK for Python. There's nothing wrong with the way you're doing it in the sample code, but the SDK was built to work with the REST API service for vSphere and there's some example code that might help you out in the repo as well.
Upvotes: 0