def test_query_list(self):
        resources = []
        resources_count = 10

        with self._create_provider() as provider:
            for i in range(resources_count):
                resource_id = f'/resources/foo/bar/resource-{i}'
                account_id = randint(1, 100)
                resource_name = f'resource {resource_id}'

                resource_dict = {
                    "id": resource_id,
                    "providerType": "Azure",
                    "name": resource_name,
                    "accountId": account_id,
                    "location": "eastus2",
                    "type": "vm"
                }
                resource = Resource(resource_dict)
                provider.write(resource.to_dict())
                resources.append(resource)

            results = provider.query_list()
            self.assertGreaterEqual(len(results), resources_count)

            for resource in resources:
                provider.delete(None, resource.id)
Ejemplo n.º 2
0
    def get_resources(self, filter: ResourceFilter = None):
        """Get list of AzureResources from service.

        :param filter: Filter object to filter resources
        :return: List of AzureResource objects
        """
        return [Resource(resource) for resource in self.resources]
Ejemplo n.º 3
0
    def execute(self, resource: Resource, tags: dict, overwrite=False):
        """Execute tagging of resource.

        :param resource: Resource to tag
        :param tags: tags to apply
        :param overwrite: True if overwrite of existing tags is desired,
            default False
        """
        # Store tags written during this single execution
        local_written = 0

        for tag_key, tag_value in tags.items():
            if not overwrite and tag_key in resource.tags:
                logging.info(
                    f"Skipped tagging {resource.id} with tag {tag_key}"
                    " since it already exists.")
                self._tags_skipped += 1
                continue

            resource.tags[tag_key] = tag_value
            local_written += 1
            self._tags_written += 1

        # Only save if needed
        if local_written > 0:
            self._resource_service.update_resource(resource)
            logging.info(f"Wrote {self._tags_written} tags to {resource.id}.")
    def test_crud_mysql_resources(self):
        resource_id = f'/resources/foo/bar/{randint(1,1000)}'
        account_id = randint(1, 100)
        resource_name = f'resource {resource_id}'

        resource_dict = {
            "id": resource_id,
            "providerType": "Azure",
            "name": resource_name,
            "accountId": account_id,
            "location": "eastus2",
            "type": "vm"
        }
        resource = Resource(resource_dict)

        with self._create_provider() as provider:
            # Insert the resource
            provider.write(resource.to_dict())

            # Query the resource
            query_result = provider.query(None, resource.id)
            self.assertIsNotNone(query_result)

            # Update the resource
            resource.name = resource.name + " udpated"
            provider.write(resource.to_dict())

            # Re-Query the resource
            query_result = provider.query(None, resource.id)
            self.assertEqual(query_result["name"], resource.name)

            #
            query_results = provider.query_list()
            self.assertGreater(len(query_results), 0)

            # Delete the resource
            provider.delete(None, resource.id)

            # Re-Query the resource
            query_result = provider.query(None, resource.id)
            self.assertIsNone(query_result)
    def test_write_with_resource(self, mock_post):
        provider = self._create_provider()

        resource_json = {
            "id": "/subscriptions/foo/resourcegroups/bar/Microsoft.Test/resources/baz",
            "accountId": "abcxyz123",
            "name": "Test Resource",
            "type": "Microsoft.Test",
            "location": "westus",
            "tags": {
                "a": "1",
                "b": "2"
            }
        }

        resource = Resource(resource_json)

        expected_url = f"{TestRestResourceStorage.test_url}?AccountId=abcxyz123&ResourceType=Microsoft_Test&Region=westus"
        expected_entry = resource.to_normalized_dict()

        provider.write_entries([resource])
        mock_post.assert_called_once_with(expected_url, json=[expected_entry])
Ejemplo n.º 6
0
    def setUp(self):
        os.environ["QUEUE_TYPE"] = "simulator"
        os.environ["TAG_UPDATES_QUEUE_NAME"] = "test-queue-name"
        self._queue = QueueFactory.create("test-queue-name")

        resource_json = {
            "id": '/resources/type1/resource1',
            "accountId": "account1",
            "type": "Microsoft.Storage/virtualMachine",
            "name": "resource1",
            "providerType": "simulator",
            "location": "location1"
        }
        self._resource = Resource(resource_json)
Ejemplo n.º 7
0
    def write(self, resource: Resource):
        if resource is None:
            raise ValueError("resource is required")

        # Query string params are required in addition to the POST body
        entry = resource.to_normalized_dict()

        query = urllib.parse.urlencode({
            'AccountId': entry["OwnerId"],
            'ResourceType': entry["ResourceType"],
            'Region': entry["Region"]
        })

        post_url = f'{self._url}?{query}'
        RequestHelper.post(post_url, json=[entry])
Ejemplo n.º 8
0
def _parse_resources(message):
    """Parse message from queue as JSON of resources.

    :param message: JSON of resources
    :return: Deserialized list of Resource objects
    """
    resource_list = message.get_json()

    # Convert message into a list if it isn"t already
    if not isinstance(resource_list, list):
        resource_list = [resource_list]

    logging.info(f"Found {len(resource_list)} resources to process")

    resource_list = [Resource(resource) for resource in resource_list]

    return resource_list
Ejemplo n.º 9
0
    def process_queue_message(message):
        """Apply tags to resources specified by message.

        :param message: Payload of resources
        :return: Tags written, tags skipped
        """
        msg_json = message.get_json()
        table_storage = ResourceStorageFactory.create()

        resource = Resource(msg_json["resource"])
        resource_service = ResourceServiceFactory.create(
            resource.provider_type, resource.account_id)

        tag_processor = ResourceTagProcessor(resource_service)

        tag_processor.execute(resource, msg_json["tags"])
        table_storage.write(resource)

        return tag_processor.tags_written, tag_processor.tags_skipped
Ejemplo n.º 10
0
    def query_list(self):
        """Get all resources in storage.

        :return: List of Resource objects
        """
        return [Resource(resource) for resource in self._resources]