Skip to content

Instantly share code, notes, and snippets.

@GitarPlayer
Last active March 18, 2024 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitarPlayer/dbffead7a725510cb8744b213110c5ff to your computer and use it in GitHub Desktop.
Save GitarPlayer/dbffead7a725510cb8744b213110c5ff to your computer and use it in GitHub Desktop.
Import item via https to content library with vSphere SDK
import argparse
from com.vmware.content.library_client import Item
from com.vmware.content.library.item_client import UpdateSession
from com.vmware.vapi.std_client import DynamicID
from vmware.vapi.vsphere.client import create_vsphere_client
import requests
import ssl
def main():
parser = argparse.ArgumentParser(description='vSphere automation script.')
parser.add_argument('--vcenter', required=True, help='vCenter IP address or hostname')
parser.add_argument('--username', required=True, help='vCenter username')
parser.add_argument('--password', required=True, help='vCenter password')
parser.add_argument('--library_item_id', required=True, help='Library item ID')
parser.add_argument('--file_url', required=True, help='File URL to upload')
parser.add_argument('--file_name', required=True, help='Name of the file in the content library')
args = parser.parse_args()
# Create a session
session = requests.session()
# Disable certificate verification if your connection is not using SSL
session.verify = False
# Create a vSphere client
vsphere_client = create_vsphere_client(server=args.vcenter, username=args.username, password=args.password, session=session)
# Get the library item
library_item = vsphere_client.content.library.Item
# Create an update session
update_session = vsphere_client.content.library.Item.UpdateSession
session_id = update_session.create(library_item_id=args.library_item_id)
# Create a specification for the file to be added
file_spec = Item.UpdateSession.File.AddSpec(name=args.file_name, source_url=args.file_url)
# Add the file to the update session
update_session.add_file(session_id, file_spec)
# Complete the update session
update_session.complete(session_id)
# Delete the update session
update_session.delete(session_id)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment