diff --git a/SoftLayer/CLI/image/export.py b/SoftLayer/CLI/image/export.py index 327cef475..375de7842 100644 --- a/SoftLayer/CLI/image/export.py +++ b/SoftLayer/CLI/image/export.py @@ -12,17 +12,25 @@ @click.command() @click.argument('identifier') @click.argument('uri') +@click.option('--ibm-api-key', + default=None, + help="The IBM Cloud API Key with access to IBM Cloud Object " + "Storage instance. For help creating this key see " + "https://console.bluemix.net/docs/services/cloud-object-" + "storage/iam/users-serviceids.html#serviceidapikeys") @environment.pass_env -def cli(env, identifier, uri): +def cli(env, identifier, uri, ibm_api_key): """Export an image to object storage. The URI for an object storage object (.vhd/.iso file) of the format: swift://@// + or cos://// if using IBM Cloud + Object Storage """ image_mgr = SoftLayer.ImageManager(env.client) image_id = helpers.resolve_id(image_mgr.resolve_ids, identifier, 'image') - result = image_mgr.export_image_to_uri(image_id, uri) + result = image_mgr.export_image_to_uri(image_id, uri, ibm_api_key) if not result: raise exceptions.CLIAbort("Failed to export Image") diff --git a/SoftLayer/CLI/image/import.py b/SoftLayer/CLI/image/import.py index 03ec25acb..525564416 100644 --- a/SoftLayer/CLI/image/import.py +++ b/SoftLayer/CLI/image/import.py @@ -16,15 +16,44 @@ default="", help="The note to be applied to the imported template") @click.option('--os-code', - default="", help="The referenceCode of the operating system software" - " description for the imported VHD") + " description for the imported VHD, ISO, or RAW image") +@click.option('--ibm-api-key', + default=None, + help="The IBM Cloud API Key with access to IBM Cloud Object " + "Storage instance and IBM KeyProtect instance. For help " + "creating this key see https://console.bluemix.net/docs/" + "services/cloud-object-storage/iam/users-serviceids.html" + "#serviceidapikeys") +@click.option('--root-key-id', + default=None, + help="ID of the root key in Key Protect") +@click.option('--wrapped-dek', + default=None, + help="Wrapped Data Encryption Key provided by IBM KeyProtect. " + "For more info see https://console.bluemix.net/docs/" + "services/key-protect/wrap-keys.html#wrap-keys") +@click.option('--kp-id', + default=None, + help="ID of the IBM Key Protect Instance") +@click.option('--cloud-init', + is_flag=True, + help="Specifies if image is cloud-init") +@click.option('--byol', + is_flag=True, + help="Specifies if image is bring your own license") +@click.option('--is-encrypted', + is_flag=True, + help="Specifies if image is encrypted") @environment.pass_env -def cli(env, name, note, os_code, uri): +def cli(env, name, note, os_code, uri, ibm_api_key, root_key_id, wrapped_dek, + kp_id, cloud_init, byol, is_encrypted): """Import an image. The URI for an object storage object (.vhd/.iso file) of the format: swift://@// + or cos://// if using IBM Cloud + Object Storage """ image_mgr = SoftLayer.ImageManager(env.client) @@ -33,6 +62,13 @@ def cli(env, name, note, os_code, uri): note=note, os_code=os_code, uri=uri, + ibm_api_key=ibm_api_key, + root_key_id=root_key_id, + wrapped_dek=wrapped_dek, + kp_id=kp_id, + cloud_init=cloud_init, + byol=byol, + is_encrypted=is_encrypted ) if not result: diff --git a/SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py b/SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py index ee58ad762..785ed3b05 100644 --- a/SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py +++ b/SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py @@ -29,5 +29,11 @@ 'id': 100, 'name': 'test_image', }] - +createFromIcos = [{ + 'createDate': '2013-12-05T21:53:03-06:00', + 'globalIdentifier': '0B5DEAF4-643D-46CA-A695-CECBE8832C9D', + 'id': 100, + 'name': 'test_image', +}] copyToExternalSource = True +copyToIcos = True diff --git a/SoftLayer/managers/image.py b/SoftLayer/managers/image.py index 81eee9282..abd60ba8a 100644 --- a/SoftLayer/managers/image.py +++ b/SoftLayer/managers/image.py @@ -120,28 +120,68 @@ def edit(self, image_id, name=None, note=None, tag=None): return bool(name or note or tag) - def import_image_from_uri(self, name, uri, os_code=None, note=None): + def import_image_from_uri(self, name, uri, os_code=None, note=None, + ibm_api_key=None, root_key_id=None, + wrapped_dek=None, kp_id=None, cloud_init=False, + byol=False, is_encrypted=False): """Import a new image from object storage. :param string name: Name of the new image :param string uri: The URI for an object storage object (.vhd/.iso file) of the format: swift://@// + or (.vhd/.iso/.raw file) of the format: + cos://// if using IBM Cloud + Object Storage :param string os_code: The reference code of the operating system :param string note: Note to add to the image + :param string ibm_api_key: Ibm Api Key needed to communicate with ICOS + and Key Protect + :param string root_key_id: ID of the root key in Key Protect + :param string wrapped_dek: Wrapped Data Encryption Key provided by + IBM KeyProtect + :param string kp_id: ID of the IBM Key Protect Instance + :param boolean cloud_init: Specifies if image is cloud-init + :param boolean byol: Specifies if image is bring your own license + :param boolean is_encrypted: Specifies if image is encrypted """ - return self.vgbdtg.createFromExternalSource({ - 'name': name, - 'note': note, - 'operatingSystemReferenceCode': os_code, - 'uri': uri, - }) - - def export_image_to_uri(self, image_id, uri): + if 'cos://' in uri: + return self.vgbdtg.createFromIcos({ + 'name': name, + 'note': note, + 'operatingSystemReferenceCode': os_code, + 'uri': uri, + 'ibmApiKey': ibm_api_key, + 'rootKeyId': root_key_id, + 'wrappedDek': wrapped_dek, + 'keyProtectId': kp_id, + 'cloudInit': cloud_init, + 'byol': byol, + 'isEncrypted': is_encrypted + }) + else: + return self.vgbdtg.createFromExternalSource({ + 'name': name, + 'note': note, + 'operatingSystemReferenceCode': os_code, + 'uri': uri, + }) + + def export_image_to_uri(self, image_id, uri, ibm_api_key=None): """Export image into the given object storage :param int image_id: The ID of the image :param string uri: The URI for object storage of the format swift://@// + or cos://// if using IBM Cloud + Object Storage + :param string ibm_api_key: Ibm Api Key needed to communicate with IBM + Cloud Object Storage """ - return self.vgbdtg.copyToExternalSource({'uri': uri}, id=image_id) + if 'cos://' in uri: + return self.vgbdtg.copyToIcos({ + 'uri': uri, + 'ibmApiKey': ibm_api_key + }, id=image_id) + else: + return self.vgbdtg.copyToExternalSource({'uri': uri}, id=image_id) diff --git a/tests/managers/image_tests.py b/tests/managers/image_tests.py index 5347d4e71..50a081988 100644 --- a/tests/managers/image_tests.py +++ b/tests/managers/image_tests.py @@ -145,6 +145,36 @@ def test_import_image(self): 'uri': 'someuri', 'operatingSystemReferenceCode': 'UBUNTU_LATEST'},)) + def test_import_image_cos(self): + self.image.import_image_from_uri(name='test_image', + note='testimage', + uri='cos://some_uri', + os_code='UBUNTU_LATEST', + ibm_api_key='some_ibm_key', + root_key_id='some_root_key_id', + wrapped_dek='some_dek', + kp_id='some_id', + cloud_init=False, + byol=False, + is_encrypted=False + ) + + self.assert_called_with( + IMAGE_SERVICE, + 'createFromIcos', + args=({'name': 'test_image', + 'note': 'testimage', + 'operatingSystemReferenceCode': 'UBUNTU_LATEST', + 'uri': 'cos://some_uri', + 'ibmApiKey': 'some_ibm_key', + 'rootKeyId': 'some_root_key_id', + 'wrappedDek': 'some_dek', + 'keyProtectId': 'some_id', + 'cloudInit': False, + 'byol': False, + 'isEncrypted': False + },)) + def test_export_image(self): self.image.export_image_to_uri(1234, 'someuri') @@ -153,3 +183,14 @@ def test_export_image(self): 'copyToExternalSource', args=({'uri': 'someuri'},), identifier=1234) + + def test_export_image_cos(self): + self.image.export_image_to_uri(1234, + 'cos://someuri', + ibm_api_key='someApiKey') + + self.assert_called_with( + IMAGE_SERVICE, + 'copyToIcos', + args=({'uri': 'cos://someuri', 'ibmApiKey': 'someApiKey'},), + identifier=1234)