def get_cloud_storage_account(_ctx=ctx):
    '''Gets the Azure Blob storage service'''
    # Get the storage account
    storage_account = utils.get_parent(_ctx.instance,
                                       rel_type=constants.REL_CONTAINED_IN_SA)
    storage_account_name = utils.get_resource_name(_ctx=storage_account)
    # Get the storage account keys
    keys = StorageAccount(_ctx=storage_account).list_keys()
    if not isinstance(keys, list) or len(keys) < 1:
        raise RecoverableError(
            'StorageAccount reported no usable authentication keys')
    # Get an interface to the Storage Account
    storage_account_key = keys[0].get('key')
    return CloudStorageAccount(account_name=storage_account_name,
                               account_key=storage_account_key)
Beispiel #2
0
def create_file_share(**_):
    '''Creates an Azure File Share'''
    # Get resource config values
    res_cfg = utils.get_resource_config() or dict()
    share_name = ctx.node.properties.get('name')
    metadata = res_cfg.get('metadata')
    quota = res_cfg.get('quota')
    fail_on_exist = res_cfg.get('fail_on_exist', False)
    # Check if invalid external resource
    if ctx.node.properties.get('use_external_resource', False) and \
       not share_name:
        raise NonRecoverableError(
            '"use_external_resource" specified without a resource "name"')
    # Get the storage account
    storage_account = utils.get_parent(ctx.instance,
                                       rel_type=constants.REL_CONTAINED_IN_SA)
    storage_account_name = utils.get_resource_name(_ctx=storage_account)
    # Get the storage account keys
    keys = StorageAccount(_ctx=storage_account).list_keys()
    if not isinstance(keys, list) or len(keys) < 1:
        raise RecoverableError(
            'StorageAccount reported no usable authentication keys')
    # Get an interface to the Storage Account
    storage_account_key = keys[0].get('key')
    storageacct = CloudStorageAccount(account_name=storage_account_name,
                                      account_key=storage_account_key)
    # Get an interface to the File Service
    filesvc = storageacct.create_file_service()
    if ctx.node.properties.get('use_external_resource', False):
        # Attempt to use an existing File Share (if specified)
        ctx.logger.debug(
            'Checking for existing File Share "{0}"'.format(share_name))
        try:
            share = filesvc.get_share_properties(share_name)
            metadata = share.get('metadata', dict())
            quota = share.get('properties', dict()).get('quota')
            created = False
        except Exception as ex:
            ctx.logger.error(
                'File Share "{0}" does not exist and '
                '"use_external_resource" is set to true'.format(share_name))
            raise NonRecoverableError(ex)
    else:
        # Generate a new File Share name if needed
        if not share_name:
            ctx.logger.info('Generating a new File Share name')
            for _ in xrange(0, 10):
                tmpname = file_share_name_generator()
                if not file_share_exists(filesvc, tmpname):
                    share_name = tmpname
                    break
        # Handle name error
        if not share_name:
            raise NonRecoverableError(
                'Error generating a new File Share name. Failed '
                'after 10 tries.')
        # Attempt to create the File Share
        ctx.logger.debug('Creating File Share "{0}"'.format(share_name))
        created = filesvc.create_share(share_name=share_name,
                                       metadata=metadata,
                                       quota=quota,
                                       fail_on_exist=False)
        if not created:
            ctx.logger.warn('File Share already exists')
            if fail_on_exist:
                raise NonRecoverableError(
                    'File Share already exists in the storage account and '
                    '"fail_on_exist" set to True')
    # Set run-time properties
    ctx.instance.runtime_properties['name'] = share_name
    ctx.instance.runtime_properties['quota'] = quota
    ctx.instance.runtime_properties['metadata'] = metadata
    ctx.instance.runtime_properties['created'] = created
    ctx.instance.runtime_properties['storage_account'] = storage_account_name
    ctx.instance.runtime_properties['username'] = storage_account_name
    ctx.instance.runtime_properties['password'] = storage_account_key
    ctx.instance.runtime_properties['uri'] = '{0}.{1}/{2}'.format(
        storage_account_name, constants.CONN_STORAGE_FILE_ENDPOINT, share_name)