def create_from_dashboard(self, dashboard):
        # TODO: REFACTOR+MERGE with create_from_analysis after testing to be the same
        logger.info(
            f"requesting quicksight create_template id {self.id} from dashboard"
        )
        quicksight_client = get_quicksight_client()

        analysis_source_entity = SourceEntity(
            self.data_sets,
            dashboard.arn,
            self.config_data,
            source_entity_type="SourceAnalysis")

        response = quicksight_client.create_template(
            AwsAccountId=self.aws_account_id,
            TemplateId=self.id,
            Name=self.name,
            Permissions=self._get_permissions(),
            SourceEntity=analysis_source_entity.get_source_entity(),
            VersionDescription="1",
        )
        logger.info(
            f"finished quicksight create_template id:{self.id} from analysis, response: {response}"
        )

        self.arn = response["Arn"]
        return response
    def create_from_analysis(self, analysis):
        logger.info(
            f"requesting quicksight create_template id {self.id} from analysis"
        )
        quicksight_client = get_quicksight_client()

        analysis_source_entity = SourceEntity(
            self.data_sets,
            analysis.arn,
            self.config_data,
            source_entity_type="SourceAnalysis")

        response = quicksight_client.create_template(
            AwsAccountId=self.aws_account_id,
            TemplateId=self.id,
            Name=self.name,
            Permissions=self._get_permissions(),
            SourceEntity=analysis_source_entity.get_source_entity(),
            VersionDescription="1",
        )
        logger.info(
            f"finished quicksight create_template id:{self.id} from analysis "
            f"response: {response}")

        self.arn = response["Arn"]
        return response
def test_source_entity_get_map_invalid_sub_type(quicksight_application_stub,
                                                mininmal_data_sets_stub):
    template_arn_param = template_arn
    mock_data_set_references = {
        "MainInjectError": {
            "SourceEntity": {
                "SourceTemplate": {
                    "DataSetReferences": [
                        {
                            "DataSetPlaceholder":
                            "sentiment",
                            "DataSetArn":
                            "arn:{Aws.PARTITION}:quicksight:{Aws.REGION}:{Aws.ACCOUNT_ID}:dataset/sentiment",
                        },
                        {
                            "DataSetPlaceholder":
                            "topic",
                            "DataSetArn":
                            "arn:{Aws.PARTITION}:quicksight:{Aws.REGION}:{Aws.ACCOUNT_ID}:dataset/topic",
                        },
                        {
                            "DataSetPlaceholder":
                            "text",
                            "DataSetArn":
                            "arn:{Aws.PARTITION}:quicksight:{Aws.REGION}:{Aws.ACCOUNT_ID}:dataset/text",
                        },
                        {
                            "DataSetPlaceholder":
                            "image-text",
                            "DataSetArn":
                            "arn:{Aws.PARTITION}:quicksight:{Aws.REGION}:{Aws.ACCOUNT_ID}:dataset/image-text",
                        },
                        {
                            "DataSetPlaceholder":
                            "image-moderation-label",
                            "DataSetArn":
                            "arn:{Aws.PARTITION}:quicksight:{Aws.REGION}:{Aws.ACCOUNT_ID}:dataset/image-moderation-label",
                        },
                    ],
                    "Arn":
                    "{self.source_template_arn}",
                }
            }
        }
    }
    obj = SourceEntity(
        data_sets=mininmal_data_sets_stub.data_sets_stub,
        source_obj_arn=template_arn_param,
        config_data=mock_data_set_references,
        source_entity_type="SourceTemplate",
    )

    source_entity = None
    with pytest.raises(ValueError):
        # Function under test
        source_entity = obj.get_source_entity()

    assert not source_entity
Esempio n. 4
0
    def __init__(
        self, quicksight_application=None, data_sets=None, quicksight_template_arn=None, data_source=None, props=None
    ):
        super().__init__(quicksight_application=quicksight_application, type="analysis", props=props)
        self.use_props(props)

        self.data_sets = data_sets
        self.data_source = data_source
        self.quicksight_template_arn = quicksight_template_arn

        self.config_data = dict()
        self._load_config(self.type, ["main"], self.config_data)
        self.source_entity = SourceEntity(
            data_sets, quicksight_template_arn, self.config_data, source_entity_type="SourceTemplate"
        )
    def __init__(self,
                 quicksight_application=None,
                 data_sets=None,
                 quicksight_template_arn=None,
                 props=None):
        super().__init__(quicksight_application=quicksight_application,
                         props=props)
        self.type = 'dashboard'
        self.use_props(props)

        self.data_sets = data_sets
        self.quicksight_template_arn = quicksight_template_arn

        self.config_data = dict()
        self._load_config(self.type, ['main'], self.config_data)
        self.source_entity = SourceEntity(data_sets,
                                          quicksight_template_arn,
                                          self.config_data,
                                          source_entity_type='SourceTemplate')
def test_source_entity_init_invalid(quicksight_application_stub,
                                    mininmal_data_sets_stub):
    template_arn_param = template_arn

    obj = None
    with pytest.raises(ValueError):
        # Function under test
        obj = SourceEntity(
            data_sets=mininmal_data_sets_stub.data_sets_stub,
            source_obj_arn=template_arn_param,
            config_data=None,
            source_entity_type="InjectErrorInvalidSource",
        )

    assert not obj
class Dashboard(QuickSightResource):
    def __init__(self,
                 quicksight_application=None,
                 data_sets=None,
                 quicksight_template_arn=None,
                 props=None):
        super().__init__(quicksight_application=quicksight_application,
                         props=props)
        self.type = 'dashboard'
        self.use_props(props)

        self.data_sets = data_sets
        self.quicksight_template_arn = quicksight_template_arn

        self.config_data = dict()
        self._load_config(self.type, ['main'], self.config_data)
        self.source_entity = SourceEntity(data_sets,
                                          quicksight_template_arn,
                                          self.config_data,
                                          source_entity_type='SourceTemplate')

    def create(self):
        logger.info(f"requesting quicksight create_dashboard: {self.id}")
        quicksight_client = get_quicksight_client()

        response = quicksight_client.create_dashboard(
            AwsAccountId=self.aws_account_id,
            DashboardId=self.id,
            Name=self.name,
            Permissions=self._get_permissions(),
            SourceEntity=self._get_source_entity(),
            DashboardPublishOptions=self._get_dashboard_publish_options())
        logger.info(f"finished quicksight create_dashboard for id:{self.id} "
                    f"response: {response}")

        self.arn = response['Arn']
        return response

    def delete(self):
        logger.info(f"requesting quicksight delete_dashboard id:{self.id}")
        quicksight_client = get_quicksight_client()

        response = quicksight_client.delete_dashboard(
            AwsAccountId=self.aws_account_id, DashboardId=self.id)
        logger.info(f"finished quicksight delete_dashboard for id:{self.id} "
                    f"response: {response}")

        return response

    def _get_dashboard_publish_options(self):
        dashboard_publish_options = {
            "AdHocFilteringOption": {
                "AvailabilityStatus": "ENABLED"
            },
            "ExportToCSVOption": {
                "AvailabilityStatus": "ENABLED"
            },
            "SheetControlsOption": {
                "VisibilityState": "EXPANDED"
            }
        }
        return dashboard_publish_options

    def _get_permissions(self):
        # The principal is the owner of the resource and create the resources and is given full actions for the type
        permissions = [{
            "Principal":
            self.principal_arn,
            "Actions": [
                "quicksight:DescribeDashboard",
                "quicksight:ListDashboardVersions",
                "quicksight:UpdateDashboardPermissions",
                "quicksight:QueryDashboard", "quicksight:UpdateDashboard",
                "quicksight:DeleteDashboard",
                "quicksight:DescribeDashboardPermissions",
                "quicksight:UpdateDashboardPublishedVersion"
            ]
        }]
        return permissions

    def _get_source_entity(self):
        return self.source_entity.get_source_entity()
class Analysis(QuickSightResource):
    def __init__(self,
                 quicksight_application=None,
                 data_sets=None,
                 quicksight_template_arn=None,
                 data_source=None,
                 props=None):
        super().__init__(quicksight_application=quicksight_application,
                         props=props)
        self.type = 'analysis'
        self.use_props(props)

        self.data_sets = data_sets
        self.data_source = data_source
        self.quicksight_template_arn = quicksight_template_arn

        self.config_data = dict()
        self._load_config(self.type, ['main'], self.config_data)
        self.source_entity = SourceEntity(data_sets,
                                          quicksight_template_arn,
                                          self.config_data,
                                          source_entity_type='SourceTemplate')

    @retry(retry=retry_if_exception_type(QuickSightFailure),
           stop=stop_after_attempt(3))
    def create(self):
        logger.info(f"requesting quicksight create_analysis: {self.id}")
        quicksight_client = get_quicksight_client()

        try:
            response = quicksight_client.create_analysis(
                AwsAccountId=self.aws_account_id,
                AnalysisId=self.id,
                Name=self.name,
                Permissions=self._get_permissions(),
                SourceEntity=self._get_source_entity())
            logger.info(
                f"finished quicksight create_analysis for id:{self.id} "
                f"response: {response}")
        except quicksight_client.exceptions.ResourceExistsException:
            response = quicksight_client.describe_analysis(
                AwsAccountId=self.aws_account_id, AnalysisId=self.id)
            response = response["Analysis"]
        except quicksight_client.exceptions.InvalidParameterValueException as exc:
            logger.error(str(exc))
            raise QuickSightFailure()

        self.arn = response['Arn']
        return response

    def delete(self):
        logger.info(f"requesting quicksight delete_analysis id:{self.id}")
        quicksight_client = get_quicksight_client()

        response = quicksight_client.delete_analysis(
            AwsAccountId=self.aws_account_id, AnalysisId=self.id)
        logger.info(f"finished quicksight delete_analysis for id:{self.id} "
                    f"response: {response}")
        return response

    def _get_permissions(self):
        # The principal is the owner of the resource and create the resources and is given full actions for the type
        permissions = [{
            "Principal":
            self.principal_arn,
            "Actions": [
                'quicksight:RestoreAnalysis',
                'quicksight:UpdateAnalysisPermissions',
                'quicksight:DeleteAnalysis', 'quicksight:QueryAnalysis',
                'quicksight:DescribeAnalysisPermissions',
                'quicksight:DescribeAnalysis', 'quicksight:UpdateAnalysis'
            ]
        }]
        return permissions

    def _get_source_entity(self):
        return self.source_entity.get_source_entity()