I probably also have some wonky entries related to setting credentials for each folder/connection entry to parent but that is working (after a bit of trouble I had there too).
import subprocess
import json
def get_instances(region = "", profile = ""):
cmd = "aws ec2 describe-instances --output json"
if region != "":
cmd += " --region " + region
if profile != "":
cmd += " --profile " + profile
aws = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
(response_json, err) = aws.communicate()
exit_code = aws.wait()
response = json.loads(response_json)
windows_connections = []
linux_connections = []
for reservation in response.get("Reservations", None):
for instance in reservation.get("Instances", None):
instance_id = instance.get("InstanceId", "")
platform = instance.get("Platform", "")
is_windows = platform.lower() == "windows"
private_ip_address = instance.get("PrivateIpAddress", "")
tags = instance.get("Tags")
name = instance_id
if tags is not None:
for tag in tags:
if tag.get("Key", "").lower() == "name":
tagValue = tag.get("Value", "")
if tagValue.lower() != "":
name = tagValue
break
computer_name = private_ip_address
connection = {
"ID": instance_id,
"Name": name,
"ComputerName": computer_name,
"CredentialFromParent": True,
"CredentialMode": 1
}
if not is_windows:
connection["Type"] = "TerminalConnection"
connection["TerminalConnectionType"] = "SSH"
linux_connections.append(connection)
else:
connection["Type"] = "RemoteDesktopConnection"
connection["RedirectDrives"] = True
windows_connections.append(connection)
folders = [
{
"Type": "Folder",
"Name": "Windows Instances",
"Objects": windows_connections,
"CredentialFromParent": True,
"CredentialMode": 1
},
{
"Type": "Folder",
"Name": "Linux Instances",
"Objects": linux_connections,
"CredentialFromParent": True,
"CredentialMode": 1
}
]
store = {
"Objects": folders
}
store_json = json.dumps(store)
return store_json
print(get_instances("$CustomProperty.Region$", "$CustomProperty.Profile$"))
Let me know if any other detail is required.