Esempio n. 1
0
    def __init__(self, group_name, location):
        self.location = location

        self.subscription_id = os.environ.get("SUBSCRIPTION_ID", None)

        # Use 2019-07-01 api version to test create VM
        self.compute_client = mgmt_compute.ComputeManagementClient(
            credential=DefaultAzureCredential(),
            subscription_id=self.subscription_id,
            api_version="2019-07-01")
        self.network_client = mgmt_network.NetworkManagementClient(
            credential=DefaultAzureCredential(),
            subscription_id=self.subscription_id)
        self.resource_client = mgmt_resource.ResourceManagementClient(
            credential=DefaultAzureCredential(),
            subscription_id=self.subscription_id)

        self.group = self.resource_client.resource_groups.create_or_update(
            group_name,
            # model style
            resource_models.ResourceGroup(location=self.location)

            # json style
            # {'location': self.location}
        )
Esempio n. 2
0
    async def init_group(self):
        print("Init Group: {}".format(self.group_name))
        self.group = await self.resource_client.resource_groups.create_or_update(
            self.group_name,
            # model style
            resource_models.ResourceGroup(location=self.location)

            # json style
            # {'location': self.location}
        )
Esempio n. 3
0
def create_resource_group(resource_client, resource_group, location):
    # type: (azure.mgmt.resource.resources.ResourceManagementClient,
    #        str, str) -> None
    """Create a resource group if it doesn't exist
    :param azure.mgmt.resource.resources.ResourceManagementClient
        resource_client: resource client
    :param str resource_group: resource group name
    :param str location: location
    """
    # check if resource group exists
    exists = resource_client.resource_groups.check_existence(resource_group)
    # create resource group if it doesn't exist
    if not exists:
        logger.info('creating resource group: {}'.format(resource_group))
        resource_client.resource_groups.create_or_update(
            resource_group_name=resource_group,
            parameters=rgmodels.ResourceGroup(location=location, ))
    else:
        logger.debug('resource group {} exists'.format(resource_group))
Esempio n. 4
0
    def setUpClass(cls):
        if identity is None:
            raise unittest.SkipTest('missing azure-identity library')

        if resource is None or resource_models is None:
            raise unittest.SkipTest('missing azure-mgmt-resource library')

        if storage is None or storage_models is None:
            raise unittest.SkipTest('missing azure-mgmt-storage library')

        config = {
            key: os.getenv(key)
            for key in (
                'AZURE_TENANT_ID',
                'AZURE_SUBSCRIPTION_ID',
                'AZURE_CLIENT_ID',
                'AZURE_CLIENT_SECRET',
            )
        }

        for key, value in config.items():
            if not value:
                raise unittest.SkipTest('missing environment variable %s' % key)

        credentials = identity.ClientSecretCredential(
            tenant_id=config['AZURE_TENANT_ID'],
            client_id=config['AZURE_CLIENT_ID'],
            client_secret=config['AZURE_CLIENT_SECRET'],
        )

        resource_client = resource.ResourceManagementClient(
            credentials,
            config['AZURE_SUBSCRIPTION_ID'],
        )

        storage_client = storage.StorageManagementClient(
            credentials,
            config['AZURE_SUBSCRIPTION_ID'],
        )

        location = os.getenv('AZURE_LOCATION', DEFAULT_AZURE_LOCATION)
        name = RESOURCE_GROUP_NAME_PREFIX
        name += random_string(MAX_STORAGE_ACCOUNT_NAME_LENGTH - len(name))
        timeout = float(os.getenv('AZURE_TIMEOUT_SECONDS', DEFAULT_TIMEOUT_SECONDS))

        # We clean up any left over resource groups from previous runs on setUpClass. If tests on
        # CI get terminated non-gracefully, old resources will be left laying around and we want
        # to clean those up to ensure we dont hit any limits.
        # To avoid deleting groups from concurrent runs, we only delete resources older than a
        # couple (6) of hours
        print("Checking and cleaning up any old stray resource groups...")

        resource_groups = resource_client.resource_groups.list()
        now_ts = int(time.time())
        delete_threshold_ts = now_ts - int(datetime.timedelta(hours=6).total_seconds())

        for resource_group in resource_groups:
            resource_create_ts = int(resource_group.tags.get('create_ts', now_ts))

            if resource_group.name.startswith(RESOURCE_GROUP_NAME_PREFIX) and \
               resource_group.location.lower() == location.lower() and \
               'test' in resource_group.tags and resource_create_ts <= delete_threshold_ts:
                assert resource_group.name.startswith(RESOURCE_GROUP_NAME_PREFIX)
                print("Deleting old stray resource group: %s..." % (resource_group.name))

                try:
                    resource_client.resource_groups.begin_delete(resource_group.name)
                except Exception as e:
                    print("Failed to delete resource group: %s" % (str(e)), file=sys.stderr)

        group = resource_client.resource_groups.create_or_update(
            resource_group_name=name,
            parameters=resource_models.ResourceGroup(
                location=location,
                tags={
                    'test': cls.__name__,
                    'create_ts': str(now_ts),
                    'gh_run_id': os.getenv('GITHUB_RUN_ID', 'unknown'),
                    'gh_job_id': os.getenv('GITHUB_JOB_ID', 'unknown'),
                    'gh_sha': os.getenv('GITHUB_SHA', 'unknown'),
                    'gh_ref': os.getenv('GITHUB_REF', 'unknown'),
                },
            ),
            timeout=timeout,
        )

        cls.addClassCleanup(lambda: resource_client.resource_groups
                            .begin_delete(group.name)
                            .result(timeout))

        account = storage_client.storage_accounts.begin_create(
            resource_group_name=group.name,
            account_name=name,
            parameters=storage_models.StorageAccountCreateParameters(
                sku=storage_models.Sku(name=storage_models.SkuName.STANDARD_LRS),
                access_tier=cls.access_tier,
                kind=cls.kind,
                location=location,
            ),
        ).result(timeout)

        keys = storage_client.storage_accounts.list_keys(
            resource_group_name=group.name,
            account_name=account.name,
            timeout=timeout,
        )

        cls.account = account.name
        cls.secret = keys.keys[0].value
Esempio n. 5
0
    def setUpClass(cls):
        if identity is None:
            raise unittest.SkipTest('missing azure-identity library')

        if resource is None or resource_models is None:
            raise unittest.SkipTest('missing azure-mgmt-resource library')

        if storage is None or storage_models is None:
            raise unittest.SkipTest('missing azure-mgmt-storage library')

        config = {
            key: os.getenv(key)
            for key in (
                'AZURE_TENANT_ID',
                'AZURE_SUBSCRIPTION_ID',
                'AZURE_CLIENT_ID',
                'AZURE_CLIENT_SECRET',
            )
        }

        for key, value in config.items():
            if not value:
                raise unittest.SkipTest('missing environment variable %s' %
                                        key)

        credentials = identity.ClientSecretCredential(
            tenant_id=config['AZURE_TENANT_ID'],
            client_id=config['AZURE_CLIENT_ID'],
            client_secret=config['AZURE_CLIENT_SECRET'],
        )

        resource_client = resource.ResourceManagementClient(
            credentials,
            config['AZURE_SUBSCRIPTION_ID'],
        )

        storage_client = storage.StorageManagementClient(
            credentials,
            config['AZURE_SUBSCRIPTION_ID'],
        )

        location = os.getenv('AZURE_LOCATION', DEFAULT_AZURE_LOCATION)
        name = 'libcloud'
        name += random_string(MAX_STORAGE_ACCOUNT_NAME_LENGTH - len(name))
        timeout = float(
            os.getenv('AZURE_TIMEOUT_SECONDS', DEFAULT_TIMEOUT_SECONDS))

        group = resource_client.resource_groups.create_or_update(
            resource_group_name=name,
            parameters=resource_models.ResourceGroup(
                location=location,
                tags={
                    'test': cls.__name__,
                    'run': os.getenv('GITHUB_RUN_ID', '-'),
                },
            ),
            timeout=timeout,
        )

        cls.addClassCleanup(lambda: resource_client.resource_groups.
                            begin_delete(group.name).result(timeout))

        account = storage_client.storage_accounts.begin_create(
            resource_group_name=group.name,
            account_name=name,
            parameters=storage_models.StorageAccountCreateParameters(
                sku=storage_models.Sku(
                    name=storage_models.SkuName.STANDARD_LRS),
                access_tier=cls.access_tier,
                kind=cls.kind,
                location=location,
            ),
        ).result(timeout)

        keys = storage_client.storage_accounts.list_keys(
            resource_group_name=group.name,
            account_name=account.name,
            timeout=timeout,
        )

        cls.account = account.name
        cls.secret = keys.keys[0].value