Python azure.mgmt.storage.StorageManagementClient() Examples

The following are 22 code examples of azure.mgmt.storage.StorageManagementClient(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module azure.mgmt.storage , or try the search function .
Example #1
Source File: infra.py    From whoville with Apache License 2.0 8 votes vote down vote up
def create_azure_session(token, service):
    assert service in ['compute', 'network', 'security', 'storage', 'resource']
    assert isinstance(token, ServicePrincipalCredentials)
    platform = config.profile.get('platform')
    if 'subscription' in platform and platform['subscription']:
        sub_id = platform['subscription']
    else:
        raise ValueError("Subscription ID not in Azure Platform Definition")
    if service == 'compute':
        from azure.mgmt.compute import ComputeManagementClient
        return ComputeManagementClient(token, sub_id)
    if service == 'network':
        from azure.mgmt.network import NetworkManagementClient
        return NetworkManagementClient(token, sub_id)
    if service == 'storage':
        from azure.mgmt.storage import StorageManagementClient
        return StorageManagementClient(token, sub_id)
    if service == 'resource':
        from azure.mgmt.resource import ResourceManagementClient
        return ResourceManagementClient(token, sub_id) 
Example #2
Source File: account_setup.py    From aztk with MIT License 6 votes vote down vote up
def create_storage_account(credentials, subscription_id, **kwargs):
    """
        Create a Storage account
        :param credentials: msrestazure.azure_active_directory.AdalAuthentication
        :param subscription_id: str
        :param **resource_group: str
        :param **storage_account: str
        :param **region: str
    """
    storage_management_client = StorageManagementClient(credentials, subscription_id)
    storage_account = storage_management_client.storage_accounts.create(
        resource_group_name=kwargs.get("resource_group", DefaultSettings.resource_group),
        account_name=kwargs.get("storage_account", DefaultSettings.storage_account),
        parameters=StorageAccountCreateParameters(
            sku=Sku(SkuName.standard_lrs), kind=Kind.storage, location=kwargs.get('region', DefaultSettings.region)))
    return storage_account.result().id 
Example #3
Source File: azure_resource.py    From Particle-Cloud-Framework with Apache License 2.0 6 votes vote down vote up
def storage_client(self):
        """
        Uses client from cli so that users can use az login to get their credentials

        Returns:
             Storage Client
        """
        if not self.client:
            resource_group = self.desired_state_definition.get("resource_group")
            storage_account = self.desired_state_definition.get("storage_account")
            if resource_group and storage_account:
                client = get_client_from_cli_profile(StorageManagementClient)
                storage_keys = client.storage_accounts.list_keys(resource_group, storage_account)
                storage_keys = {v.key_name: v.value for v in storage_keys.keys}

                self.client = CloudStorageAccount(storage_account, storage_keys['key1'])
            else:
                raise Exception("azure_resource.resource_group and azure_resource.storage_account must be defined")
        return self.client 
Example #4
Source File: storageaccounts.py    From ScoutSuite with GNU General Public License v2.0 5 votes vote down vote up
def get_client(self, subscription_id: str):
        return StorageManagementClient(self.credentials.get_credentials('arm'),
                                       subscription_id=subscription_id) 
Example #5
Source File: sync.py    From cloudbolt-forge with Apache License 2.0 5 votes vote down vote up
def _get_client(handler):
    """
    Get the clients using newer methods from the CloudBolt main repo if this CB is running
    a version greater than 9.2.2. These internal methods implicitly take care of much of the other
    features in CloudBolt such as proxy and ssl verification.
    Otherwise, manually instantiate clients without support for those other CloudBolt settings.
    """
    import settings
    from common.methods import is_version_newer

    cb_version = settings.VERSION_INFO["VERSION"]
    if is_version_newer(cb_version, "9.2.2"):
        wrapper = handler.get_api_wrapper()
        storage_client = wrapper.storage_client
    else:
        # TODO: Remove once versions <= 9.2.2 are no longer supported.
        credentials = ServicePrincipalCredentials(
            client_id=handler.client_id, secret=handler.secret, tenant=handler.tenant_id
        )
        storage_client = storage.StorageManagementClient(
            credentials, handler.serviceaccount
        )

    set_progress("Connection to Azure established")

    return storage_client 
Example #6
Source File: create.py    From cloudbolt-forge with Apache License 2.0 5 votes vote down vote up
def _get_client(handler):
    """
    Get the clients using newer methods from the CloudBolt main repo if this CB is running
    a version greater than 9.2.2. These internal methods implicitly take care of much of the other
    features in CloudBolt such as proxy and ssl verification.
    Otherwise, manually instantiate clients without support for those other CloudBolt settings.
    """
    import settings
    from common.methods import is_version_newer

    cb_version = settings.VERSION_INFO["VERSION"]
    if is_version_newer(cb_version, "9.2.2"):
        wrapper = handler.get_api_wrapper()
        storage_client = wrapper.storage_client
    else:
        # TODO: Remove once versions <= 9.2.2 are no longer supported.
        credentials = ServicePrincipalCredentials(
            client_id=handler.client_id, secret=handler.secret, tenant=handler.tenant_id
        )
        storage_client = storage.StorageManagementClient(
            credentials, handler.serviceaccount
        )

    set_progress("Connection to Azure established")

    return storage_client 
Example #7
Source File: delete.py    From cloudbolt-forge with Apache License 2.0 5 votes vote down vote up
def _get_client(handler):
    """
    Get the clients using newer methods from the CloudBolt main repo if this CB is running
    a version greater than 9.2.2. These internal methods implicitly take care of much of the other
    features in CloudBolt such as proxy and ssl verification.
    Otherwise, manually instantiate clients without support for those other CloudBolt settings.
    """
    import settings
    from common.methods import is_version_newer

    cb_version = settings.VERSION_INFO["VERSION"]
    if is_version_newer(cb_version, "9.2.2"):
        wrapper = handler.get_api_wrapper()
        storage_client = wrapper.storage_client
    else:
        # TODO: Remove once versions <= 9.2.2 are no longer supported.
        credentials = ServicePrincipalCredentials(
            client_id=handler.client_id, secret=handler.secret, tenant=handler.tenant_id
        )
        storage_client = storage.StorageManagementClient(
            credentials, handler.serviceaccount
        )

    set_progress("Connection to Azure established")

    return storage_client 
Example #8
Source File: sync.py    From cloudbolt-forge with Apache License 2.0 5 votes vote down vote up
def discover_resources(**kwargs):
    discovered_azure_sql = []
    for handler in AzureARMHandler.objects.all():
        set_progress('Connecting to Azure storage \
        files for handler: {}'.format(handler))
        credentials = ServicePrincipalCredentials(
            client_id=handler.client_id,
            secret=handler.secret,
            tenant=handler.tenant_id
        )
        azure_client = storage.StorageManagementClient(
            credentials, handler.serviceaccount)
        azure_resources_client = resources.ResourceManagementClient(
            credentials, handler.serviceaccount)

        for resource_group in azure_resources_client.resource_groups.list():
            try:
                for st in azure_client.storage_accounts.list_by_resource_group(resource_group.name)._get_next().json()['value']:
                    res = azure_client.storage_accounts.list_keys(
                        resource_group.name, st['name'])
                    keys = res.keys
                    file_service = FileService(
                        account_name=st['name'], account_key=keys[1].value)
                    for share in file_service.list_shares():
                        for file in file_service.list_directories_and_files(share_name=share.name).items:
                            if type(file) is File:
                                discovered_azure_sql.append(
                                    {
                                        'name': share.name + '-' + file.name,
                                        'azure_storage_file_name': file.name,
                                        'azure_storage_file_share_name': share.name,
                                        'azure_storage_account_name': st['name'],
                                        'azure_account_key': keys[0].value,
                                        'azure_account_key_fallback': keys[1].value
                                    }
                                )
            except:
                continue

    return discovered_azure_sql 
Example #9
Source File: create.py    From cloudbolt-forge with Apache License 2.0 5 votes vote down vote up
def generate_options_for_storage_account(server=None, **kwargs):
    discovered_az_stores = []
    for handler in AzureARMHandler.objects.all():
        set_progress('Connecting to Azure Storage \
        for handler: {}'.format(handler))
        credentials = ServicePrincipalCredentials(
            client_id=handler.client_id,
            secret=handler.secret,
            tenant=handler.tenant_id
        )
        azure_client = storage.StorageManagementClient(credentials, handler.serviceaccount)
        set_progress("Connection to Azure established")
        for st in azure_client.storage_accounts.list():
            discovered_az_stores.append(st.name)
    return discovered_az_stores 
Example #10
Source File: monitor.py    From pan-fca with Apache License 2.0 5 votes vote down vote up
def __init__(self, cred, subs_id, my_storage_rg, vmss_rg_name, vmss_name, storage, pan_handle, logger=None):
        self.credentials = cred
        self.subscription_id = subs_id
        self.logger = logger
        self.hub_name = vmss_rg_name
        self.storage_name = storage
        self.panorama_handler = pan_handle
        self.vmss_table_name = re.sub(self.ALPHANUM, '', vmss_name + 'vmsstable')
        self.vmss_rg_name = vmss_rg_name

        try:
            self.resource_client = ResourceManagementClient(cred, subs_id)
            self.compute_client = ComputeManagementClient(cred, subs_id)
            self.network_client = NetworkManagementClient(cred, subs_id)
            self.store_client = StorageManagementClient(cred, subs_id)
            store_keys = self.store_client.storage_accounts.list_keys(my_storage_rg, storage).keys[0].value
            self.table_service = TableService(account_name=storage,
                                              account_key=store_keys)
        except Exception as e:
            self.logger.error("Getting Azure Infra handlers failed %s" % str(e))
            raise e


        rg_list = self.resource_client.resource_groups.list()
        self.managed_spokes = []
        self.managed_spokes.append(vmss_rg_name)
        self.new_spokes = [] 
Example #11
Source File: msazure.py    From wrapanapi with MIT License 5 votes vote down vote up
def storage_client(self):
        return StorageManagementClient(self.credentials, self.subscription_id) 
Example #12
Source File: azure_api.py    From aztk with MIT License 5 votes vote down vote up
def make_table_service(secrets):
    if secrets.shared_key:
        table_service = TableService(
            account_name=secrets.shared_key.storage_account_name,
            account_key=secrets.shared_key.storage_account_key,
            endpoint_suffix=secrets.shared_key.storage_account_suffix,
        )
    else:
        arm_credentials = ServicePrincipalCredentials(
            client_id=secrets.service_principal.client_id,
            secret=secrets.service_principal.credential,
            tenant=secrets.service_principal.tenant_id,
            resource="https://management.core.windows.net/",
        )
        match = RESOURCE_ID_PATTERN.match(secrets.service_principal.storage_account_resource_id)
        accountname = match.group("account")
        subscription = match.group("subscription")
        resourcegroup = match.group("resourcegroup")
        mgmt_client = StorageManagementClient(arm_credentials, subscription)
        key = (retry_function(
            mgmt_client.storage_accounts.list_keys,
            10,
            1,
            Exception,
            resource_group_name=resourcegroup,
            account_name=accountname,
        ).keys[0].value)
        table_service = TableService(account_name=accountname, account_key=key)

    return table_service 
Example #13
Source File: azure_api.py    From aztk with MIT License 5 votes vote down vote up
def make_blob_client(secrets):
    """
        Creates a blob client object
        :param str storage_account_key: storage account key
        :param str storage_account_name: storage account name
        :param str storage_account_suffix: storage account suffix
    """

    if secrets.shared_key:
        # Set up SharedKeyCredentials
        blob_client = blob.BlockBlobService(
            account_name=secrets.shared_key.storage_account_name,
            account_key=secrets.shared_key.storage_account_key,
            endpoint_suffix=secrets.shared_key.storage_account_suffix,
        )
    else:
        # Set up ServicePrincipalCredentials
        arm_credentials = ServicePrincipalCredentials(
            client_id=secrets.service_principal.client_id,
            secret=secrets.service_principal.credential,
            tenant=secrets.service_principal.tenant_id,
            resource="https://management.core.windows.net/",
        )
        match = RESOURCE_ID_PATTERN.match(secrets.service_principal.storage_account_resource_id)
        accountname = match.group("account")
        subscription = match.group("subscription")
        resourcegroup = match.group("resourcegroup")
        mgmt_client = StorageManagementClient(arm_credentials, subscription)
        key = (retry_function(
            mgmt_client.storage_accounts.list_keys,
            10,
            1,
            Exception,
            resource_group_name=resourcegroup,
            account_name=accountname,
        ).keys[0].value)
        storage_client = CloudStorageAccount(accountname, key)
        blob_client = storage_client.create_block_blob_service()

    return blob_client 
Example #14
Source File: config.py    From aztk with MIT License 5 votes vote down vote up
def get_blob_client() -> blob.BlockBlobService:
    if not storage_resource_id:
        return blob.BlockBlobService(
            account_name=storage_account_name, account_key=storage_account_key, endpoint_suffix=storage_account_suffix)
    else:
        credentials = ServicePrincipalCredentials(
            client_id=client_id, secret=credential, tenant=tenant_id, resource="https://management.core.windows.net/")
        m = RESOURCE_ID_PATTERN.match(storage_resource_id)
        accountname = m.group("account")
        subscription = m.group("subscription")
        resourcegroup = m.group("resourcegroup")
        mgmt_client = StorageManagementClient(credentials, subscription)
        key = (mgmt_client.storage_accounts.list_keys(resource_group_name=resourcegroup, account_name=accountname)
               .keys[0].value)
        storage_client = CloudStorageAccount(accountname, key)
        return storage_client.create_block_blob_service() 
Example #15
Source File: meta_lib.py    From incubator-dlab with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        os.environ['AZURE_AUTH_LOCATION'] = '/root/azure_auth.json'
        self.compute_client = get_client_from_auth_file(ComputeManagementClient)
        self.resource_client = get_client_from_auth_file(ResourceManagementClient)
        self.network_client = get_client_from_auth_file(NetworkManagementClient)
        self.storage_client = get_client_from_auth_file(StorageManagementClient)
        self.datalake_client = get_client_from_auth_file(DataLakeStoreAccountManagementClient)
        self.authorization_client = get_client_from_auth_file(AuthorizationManagementClient)
        self.sp_creds = json.loads(open(os.environ['AZURE_AUTH_LOCATION']).read())
        self.dl_filesystem_creds = lib.auth(tenant_id=json.dumps(self.sp_creds['tenantId']).replace('"', ''),
                                            client_secret=json.dumps(self.sp_creds['clientSecret']).replace('"', ''),
                                            client_id=json.dumps(self.sp_creds['clientId']).replace('"', ''),
                                            resource='https://datalake.azure.net/') 
Example #16
Source File: actions_lib.py    From incubator-dlab with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        os.environ['AZURE_AUTH_LOCATION'] = '/root/azure_auth.json'
        self.compute_client = get_client_from_auth_file(ComputeManagementClient)
        self.resource_client = get_client_from_auth_file(ResourceManagementClient)
        self.network_client = get_client_from_auth_file(NetworkManagementClient)
        self.storage_client = get_client_from_auth_file(StorageManagementClient)
        self.datalake_client = get_client_from_auth_file(DataLakeStoreAccountManagementClient)
        self.authorization_client = get_client_from_auth_file(AuthorizationManagementClient)
        self.sp_creds = json.loads(open(os.environ['AZURE_AUTH_LOCATION']).read())
        self.dl_filesystem_creds = lib.auth(tenant_id=json.dumps(self.sp_creds['tenantId']).replace('"', ''),
                                            client_secret=json.dumps(self.sp_creds['clientSecret']).replace('"', ''),
                                            client_id=json.dumps(self.sp_creds['clientId']).replace('"', ''),
                                            resource='https://datalake.azure.net/') 
Example #17
Source File: tests_client.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_cloud_storage_account(self, _):
        """Test the cloud_storage_account method."""
        subscription_id = FAKE.uuid4()
        resource_group_name = FAKE.word()
        storage_account_name = FAKE.word()
        obj = AzureClientFactory(
            subscription_id=subscription_id,
            tenant_id=FAKE.uuid4(),
            client_id=FAKE.uuid4(),
            client_secret=FAKE.word(),
            cloud=random.choice(self.clouds),
        )
        with patch.object(StorageManagementClient, "storage_accounts", return_value=None):
            cloud_account = obj.cloud_storage_account(resource_group_name, storage_account_name)
            self.assertTrue(isinstance(cloud_account, BlobServiceClient)) 
Example #18
Source File: tests_client.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_storage_client(self, _):
        """Test the storage_client property."""
        obj = AzureClientFactory(
            subscription_id=FAKE.uuid4(),
            tenant_id=FAKE.uuid4(),
            client_id=FAKE.uuid4(),
            client_secret=FAKE.word(),
            cloud=random.choice(self.clouds),
        )
        self.assertTrue(isinstance(obj.storage_client, StorageManagementClient)) 
Example #19
Source File: client.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def storage_client(self):
        """Get storage client with subscription and credentials."""
        return StorageManagementClient(self.credentials, self.subscription_id) 
Example #20
Source File: Azure.py    From im with GNU General Public License v3.0 5 votes vote down vote up
def get_storage_account(group_name, storage_name, credentials, subscription_id):
        """
        Get the Storage Account named storage_name in group_name, if it not exists return None
        """
        try:
            storage_client = StorageManagementClient(credentials, subscription_id)
            return storage_client.storage_accounts.get_properties(group_name, storage_name)
        except CloudError as cex:
            if cex.status_code == 404:
                return None
            else:
                raise cex 
Example #21
Source File: storage.py    From seismic-deeplearning with MIT License 4 votes vote down vote up
def create_premium_storage(
    profile_credentials, subscription_id, location, resource_group_name, storage_name,
):
    """Create premium blob storage

    Args:
        profile_credentials : credentials from Azure login (see example below for details)
        subscription_id (str): subscription you wish to use
        location (str): location you wish the strage to be created in
        resource_group_name (str): the name of the resource group you want the storage to be created under
        storage_name (str): the name of the storage account

    Raises:
        Exception: [description]

    Returns:
        [type]: [description]

    Example:
        >>> from azure.common.credentials import get_cli_profile
        >>> profile = get_cli_profile()
        >>> profile.set_active_subscription("YOUR-ACCOUNT")
        >>> cred, subscription_id, _ = profile.get_login_credentials()
        >>> storage = create_premium_storage(cred, subscription_id, "eastus", "testrg", "teststr", wait=False)
    """
    storage_client = StorageManagementClient(profile_credentials, subscription_id)
    create_resource_group(profile_credentials, subscription_id, location, resource_group_name)
    if storage_client.storage_accounts.check_name_availability(storage_name).name_available == False:
        storage_account = storage_client.storage_accounts.get_properties(resource_group_name, storage_name)
    else:
        storage_async_operation = storage_client.storage_accounts.create(
            resource_group_name,
            storage_name,
            StorageAccountCreateParameters(
                sku=Sku(name=SkuName.premium_lrs), kind=Kind.block_blob_storage, location="eastus",
            ),
        )
        storage_account = storage_async_operation.result()

    if "Succeeded" not in storage_account.provisioning_state:
        raise StorageAccountCreateFailure(
            f"Storage account not created successfully | State {storage_account.provisioning_state}"
        )

    storage_keys = storage_client.storage_accounts.list_keys(resource_group_name, storage_name)
    storage_keys = {v.key_name: v.value for v in storage_keys.keys}

    return storage_account, storage_keys 
Example #22
Source File: sync.py    From cloudbolt-forge with Apache License 2.0 4 votes vote down vote up
def discover_resources(**kwargs):
    discovered_azure_queues = []
    for handler in AzureARMHandler.objects.all():
        set_progress(
            "Connecting to Azure sql \
        DB for handler: {}".format(
                handler
            )
        )
        credentials = ServicePrincipalCredentials(
            client_id=handler.client_id, secret=handler.secret, tenant=handler.tenant_id
        )
        azure_client = storage.StorageManagementClient(
            credentials, handler.serviceaccount
        )
        azure_resources_client = resources.ResourceManagementClient(
            credentials, handler.serviceaccount
        )

        for resource_group in azure_resources_client.resource_groups.list():
            try:
                for st in (
                    azure_client.storage_accounts.list_by_resource_group(
                        resource_group.name
                    )
                    ._get_next()
                    .json()["value"]
                ):
                    try:
                        res = azure_client.storage_accounts.list_keys(
                            resource_group.name, st["name"]
                        )
                        keys = res.keys
                        for queue in QueueService(
                            account_name=st["name"], account_key=keys[1].value
                        ).list_queues():
                            discovered_azure_queues.append(
                                {
                                    "name": queue.name,
                                    "azure_queue_name": "Azure queues - " + queue.name,
                                    "resource_group_name": resource_group.name,
                                    "azure_rh_id": handler.id,
                                    "azure_storage_account_name": st["name"],
                                    "azure_account_key": keys[0].value,
                                    "azure_account_key_fallback": keys[1].value,
                                }
                            )
                    except:  # noqa: E722
                        continue
            except CloudError as e:
                set_progress("Azure CloudError: {}".format(e))
                continue

    return discovered_azure_queues