def test_create_storage(self):
        ctx = self.mock_ctx('testcreatestorage')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN test create storage")
    
        status_code = storage.create(ctx=ctx)
        ctx.logger.info("status_code : " + str(status_code))
        self.assertTrue(bool((status_code == 200) | (status_code == 202)))
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "storage",constants.SUCCEEDED, timeout=600)
        ctx.logger.info("Storage Account Created")

        keys = storage.get_storage_keys(ctx)
        self.assertIsNotNone(keys)
        self.assertEqual(len(keys), 2)
        ctx.logger.info("Key 1: {}, key 2: {}".format(keys[0], keys[1]))

        self.assertEqual(200, storage.delete(ctx=ctx))
        ctx.logger.info("Checking Storage Account deleted")
        current_ctx.set(ctx=ctx)
        self.assertRaises(utils.WindowsAzureError,
            storage.get_provisioning_state,
            ctx=ctx
        )
        ctx.logger.info("Storage Account Deleted")

        ctx.logger.info("END test create storage")
Ejemplo n.º 2
0
def _get_datadisks_from_storage(ctx):
    """Helper to retrieve the list of the existing disks within a storage account.
    The storage account used here is the one present in the runtime_properties
    of the disk.

    :param ctx: The Cloudify ctx context.
    :rtype: List
    """
    vm_name = ctx.instance.runtime_properties[constants.COMPUTE_KEY]
    storage_account = \
        ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY]
    key = storage.get_storage_keys(ctx)[0]
    version = "2015-02-21"
    timestamp = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
    container = "{}-vhds".format(vm_name)
    string_to_sign = ("GET\n" +
				      "\n" +
				      "\n" + 
				      "\n" +
				      "\n" +
				      "\n" + 
				      "\n" +
				      "\n" +
				      "\n" +
				      "\n" +
				      "\n" + 
				      "\n" +
				      "x-ms-date:{}\n" +
				      "x-ms-version:{}\n" +
				      "/{}/{}\n" +
				      "comp:list\n" +
				      "restype:container" 
				      ).format(timestamp, version, storage_account, container)

    signed_string = hmac.new(key=base64.b64decode(key), 
                             msg=unicode(string_to_sign, "utf-8"), 
                             digestmod=hashlib.sha256
                             )

    header = {'x-ms-date': timestamp,
		      'x-ms-version': version,
		      'Authorization': 'SharedKey {}:{}'.format(
                        storage_account,
                        base64.b64encode(signed_string.digest())
                        )
		     }

    response = requests.get(("https://{}.blob.core.windows.net/{}?" +
                             "restype=container&comp=list").format(
                                                    storage_account,
                                                    container
                                                    ),
                            headers=header
                            )
    
    if not re.match(r'(^2+)', '{}'.format(response.status_code)):
        raise utils.WindowsAzureError(
                    response.status_code,
                    response.text
                    )

    xml_list_blob = ET.fromstring(response.text)
    xml_blobs_name = xml_list_blob.findall("./Blobs/Blob/Name")

    blobs_name = []
    for blob in xml_blobs_name:
        blobs_name.append(blob.text)

    ctx.logger.debug("Blobs names: {} in {}/{}".format(
                            blobs_name,
                            storage_account,
                            container
                            )
                     )
    return blobs_name
Ejemplo n.º 3
0
def delete(**_):
    """Delete a data disk.

    :param ctx: The Cloudify ctx context.
    """
    vm_name = ctx.instance.runtime_properties[constants.COMPUTE_KEY]
    storage_account = \
        ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY]
    disks = ctx.node.properties[constants.DISKS_KEY]
    key = storage.get_storage_keys(ctx)[0]
    timestamp = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
    container = "{}-vhds".format(vm_name)
    version = "2015-02-21"

    for disk in disks:
        if disk[constants.DELETABLE_KEY]:
            string_to_sign = ("DELETE\n" +
				              "\n" +
				              "\n" + 
				              "\n" +
				              "\n" +
				              "\n" + 
				              "\n" +
				              "\n" +
				              "\n" +
				              "\n" +
				              "\n" + 
				              "\n" +
				              "x-ms-date:{}\n" +
				              "x-ms-version:{}\n" +
				              "/{}/{}/{}.vhd"
				              ).format(timestamp,
                                       version,
                                       storage_account, 
                                       container,
                                       disk['name']
                                       )

            signed_string = hmac.new(key=base64.b64decode(key), 
                                     msg=unicode(string_to_sign, "utf-8"), 
                                     digestmod=hashlib.sha256
                                    )
            header = {'x-ms-date': timestamp,
		              'x-ms-version': version,
		              'Authorization': 'SharedKey {}:{}'.format(
                            storage_account,
                            base64.b64encode(signed_string.digest())
                            )
		            }

            response = requests.delete(("https://{}.blob.core.windows.net/" +
                                       "{}/{}.vhd").format(storage_account,
                                                           container,
                                                           disk['name']
                                                          ),
                                        headers=header
                                       )

            if not re.match(r'(^2+)', '{}'.format(response.status_code)):
                raise utils.WindowsAzureError(
                        response.status_code,
                        response.text
                        )

            ctx.logger.info("Disk has been successfully deleted.")
        else:
            ctx.logger.info(("Disk will not be deleted" + 
                            "thanks to deletable property."))