1
0
mirror of https://github.com/vmware/vsphere-automation-sdk-python.git synced 2024-11-23 18:10:00 -05:00
vsphere-automation-sdk-python/samples/vsphere/contentlibrary/isomount/iso_mount.py

106 lines
3.9 KiB
Python
Raw Normal View History

2016-10-26 19:08:23 -04:00
#!/usr/bin/env python
"""
* *******************************************************
* Copyright VMware, Inc. 2016. All Rights Reserved.
* *******************************************************
*
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
"""
__author__ = 'VMware, Inc.'
__copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.'
__vcenter_version__ = '6.5+'
from samples.vsphere.common.sample_base import SampleBase
from samples.vsphere.contentlibrary.lib.cls_api_client import ClsApiClient
from samples.vsphere.contentlibrary.lib.cls_api_helper import ClsApiHelper
2016-10-26 19:08:23 -04:00
from samples.vsphere.vcenter.helper.vm_helper import get_vm
2016-10-26 19:08:23 -04:00
2017-02-24 03:41:48 -05:00
2016-10-26 19:08:23 -04:00
class IsoMount(SampleBase):
"""
Demonstrates the content library ISO item mount and
unmount workflow via the mount and unmount APIs from the
ISO service.
"""
ISO_FILENAME = 'test.iso'
def __init__(self):
SampleBase.__init__(self, self.__doc__)
self.servicemanager = None
self.client = None
self.datastore_name = None
self.lib_name = "iso-demo-lib"
self.local_library = None
self.iso_item_name = "iso-demo-lib-item"
self.vm_name = None
def _options(self):
self.argparser.add_argument('-datastorename',
'--datastorename',
help='The name of a datastore (of type vmfs) that is'
' acceassible to the vm specified with --vmname.')
self.argparser.add_argument('-vmname',
'--vmname',
help='The name of the vm where iso will be mounted. '
'The vm needs to be already created on the vCenter')
def _setup(self):
self.datastore_name = self.args.datastorename
assert self.datastore_name is not None
self.vm_name = self.args.vmname
assert self.vm_name is not None
self.servicemanager = self.get_service_manager()
self.client = ClsApiClient(self.servicemanager)
self.helper = ClsApiHelper(self.client, self.skip_verification)
def _execute(self):
2017-02-24 03:41:48 -05:00
storage_backings = self.helper.create_storage_backings(
self.servicemanager, self.datastore_name)
2016-10-26 19:08:23 -04:00
2017-02-24 03:41:48 -05:00
library_id = self.helper.create_local_library(storage_backings,
self.lib_name)
2016-10-26 19:08:23 -04:00
self.local_library = self.client.local_library_service.get(library_id)
2017-02-24 03:41:48 -05:00
library_item_id = self.helper.create_iso_library_item(library_id,
self.iso_item_name,
self.ISO_FILENAME)
2016-10-26 19:08:23 -04:00
vm_id = get_vm(self.servicemanager.stub_config, self.vm_name)
assert vm_id is not None
# Mount the iso item as a CDROM device
device_id = self.client.iso_service.mount(library_item_id, vm_id)
assert device_id is not None
2017-02-24 03:41:48 -05:00
print('Mounted library item {0} on vm {1} at device {2}'.
format(self.iso_item_name, self.vm_name, device_id))
2016-10-26 19:08:23 -04:00
# Unmount the CDROM
self.client.iso_service.unmount(vm_id, device_id)
2017-02-24 03:41:48 -05:00
print('Unmounted library item {0} from vm {1} mounted at device {2}'.
format(self.iso_item_name, self.vm_name, device_id))
2016-10-26 19:08:23 -04:00
def _cleanup(self):
if self.local_library:
2017-02-24 03:41:48 -05:00
self.client.local_library_service.delete(
library_id=self.local_library.id)
print('Deleted Library Id: {0}'.format(self.local_library.id))
2016-10-26 19:08:23 -04:00
def main():
iso_mount_sample = IsoMount()
iso_mount_sample.main()
if __name__ == '__main__':
main()