This example shows how to upload an ISO image file from the local system to a library item.

This example uses the steps that are described in the Upload a File from a Local System to a Library Item procedure.

from com.vmware.content import library_client
from com.vmware.content.library import item_client
from com.vmware.content.library.item import update_session_client
import uuid
import os
import httplib
import urlparse


# 1 - Create an instance of the ItemModel class and specify the item settings.
item_model = library_client.ItemModel()
item_model.name = ’ESXi patches’
item_model.description = ’ESXi 5.5 security patches as of 2/3/2015’
item_model.type = ’iso’
item_model.library_id = my_library_id
idem_token = str(uuid.uuid4())
item_stub = library_client.Item(my_stub_config)
item_id = item_stub.create(create_spec=item_model, 
client_token=idem_token)

# 2 - Create an UpdateSessionModel instance to track the changes you make to the item.
update_session_model = item_client.UpdateSessionModel()
update_session_model.library_item_id = item_id

# 3 - Create an update session from the model.
idem_token = str(uuid.uuid4())
update_session_stub = update_session_client.UpdateSession(my_stub_config)
session_id = update_session_stub.create(create_spec=update_session_model
client_token=idem_token)

try :
  # 4 - Create a new AddSpec instance to describe the properties of the file to be uploaded.
  file_spec = update_session_client.AddSpec()
  file_spec.name = ’ESXi patch as of 10/2/2015’
  file_spec.source_type = update_session_client.File.SourceType.PUSH

  # 5 - Link the ISO file spec to the update session.
  update_file_stub = update_session_stub.File(my_stub_config)
  file_info = update_file_stub.File.add(update_session_id=session_id, 
                                        file_spec=file_spec)
  # 6 - Use HTTP library to upload the file to the library item.
  upload_uri = file_info.upload_endpoint.uri  
  file_name = “/updates/esxi/esxi_patch.iso”
  host = urlparse.urlsplit(upload_uri)
  connection = httplib.HTTPConnection(host.netloc)
  with open(file_name, “rb”) as f :
  connection.request(“PUT”, upload_uri, f)

  # 7 - Commit the updates.
  library_item_service.UpdateSession.complete(session_id)
  
  finally :
  # 8 - Delete the session.
  library_item_service.UpdateSession.delete(session_id)