Ejemplo n.º 1
0
    def update_account_alias(self):
        """
        Update the account alias.

        Args:
            None
        Returns:
            (String, String) Account ID, Account Alias

        """
        account_id, account_alias = get_account_alias_from_role_arn(
            self._role_arn)
        with AccountAliasAccessor(account_id, self._schema) as alias_accessor:
            alias_accessor.set_account_alias(account_alias)

        accounts = get_account_names_by_organization(self._role_arn)
        for account in accounts:
            acct_id = account.get('id')
            acct_alias = account.get('name')
            if acct_id and acct_alias:
                with AccountAliasAccessor(acct_id,
                                          self._schema) as alias_accessor:
                    alias_accessor.set_account_alias(acct_alias)

        return account_id, account_alias
Ejemplo n.º 2
0
    def test_update_account_via_orgs(self, mock_get_alias,
                                     mock_get_account_names):
        """Test update alias with org api response."""
        alias = "hccm-alias"
        mock_get_alias.return_value = (self.account_id, alias)
        member_account_id = "1234598760"
        member_account_name = "hccm-member"
        account_names = [
            {
                "id": self.account_id,
                "name": alias
            },
            {
                "id": member_account_id,
                "name": member_account_name
            },
        ]
        mock_get_account_names.return_value = account_names
        role_arn = f"arn:aws:iam::{self.account_id}:role/CostManagement"
        accessor = AWSAccountAlias(role_arn, "acct10001")
        accessor.update_account_alias()

        db_access = AccountAliasAccessor(self.account_id, "acct10001")
        self.assertEqual(db_access._obj.account_id, self.account_id)
        self.assertEqual(db_access._obj.account_alias, alias)

        member_db_access = AccountAliasAccessor(member_account_id, "acct10001")
        self.assertEqual(member_db_access._obj.account_id, member_account_id)
        self.assertEqual(member_db_access._obj.account_alias,
                         member_account_name)
Ejemplo n.º 3
0
    def test_update_account_via_orgs(self, mock_get_alias,
                                     mock_get_account_names):
        """Test update alias with org api response."""
        alias = 'hccm-alias'
        mock_get_alias.return_value = (self.account_id, alias)
        member_account_id = '1234598760'
        member_account_name = 'hccm-member'
        account_names = [{
            'id': self.account_id,
            'name': alias
        }, {
            'id': member_account_id,
            'name': member_account_name
        }]
        mock_get_account_names.return_value = account_names
        role_arn = 'arn:aws:iam::{}:role/CostManagement'.format(
            self.account_id)
        accessor = AWSAccountAlias(role_arn, 'acct10001')
        accessor.update_account_alias()

        db_access = AccountAliasAccessor(self.account_id, 'acct10001')
        self.assertEqual(db_access._obj.account_id, self.account_id)
        self.assertEqual(db_access._obj.account_alias, alias)

        member_db_access = AccountAliasAccessor(member_account_id, 'acct10001')
        self.assertEqual(member_db_access._obj.account_id, member_account_id)
        self.assertEqual(member_db_access._obj.account_alias,
                         member_account_name)
 def test_initializer(self):
     """Test Initializer."""
     accessor = AccountAliasAccessor(self.account_id, self.schema)
     with schema_context(self.schema):
         obj = accessor._get_db_obj_query().first()
     self.assertEqual(obj.account_id, self.account_id)
     self.assertEqual(obj.account_alias, self.account_id)
Ejemplo n.º 5
0
    def create_ocpawscostlineitem_project_daily_summary(
            self, account_id, schema):
        """Create an ocpawscostlineitem_project_daily_summary object for test."""
        table_name = AWS_CUR_TABLE_MAP['ocp_on_aws_project_daily_summary']
        data = self.create_columns_for_table(table_name)
        row = self.db_accessor.create_db_object(table_name, data)

        accessor = AccountAliasAccessor(account_id, schema)
        with schema_context(schema):
            account_alias = accessor._get_db_obj_query().first()

            row.account_alias = account_alias
            row.cost_entry_bill = self.create_cost_entry_bill()
            row.namespace = self.fake.pystr()[:8]
            row.pod = self.fake.pystr()[:8]
            row.node = self.fake.pystr()[:8]
            row.usage_start = self.stringify_datetime(
                self.fake.past_datetime())
            row.usage_end = self.stringify_datetime(self.fake.past_datetime())
            row.product_code = self.fake.pystr()[:8]
            row.usage_account_id = self.fake.pystr()[:8]

            row.save()

        return row
    def test_set_account_alias(self):
        """Test alias setter."""
        with schema_context(self.schema):
            AWSAccountAlias.objects.create(account_id=self.account_id,
                                           account_alias=self.account_id)

        alias_name = "test-alias"
        accessor = AccountAliasAccessor(self.account_id, self.schema)

        accessor.set_account_alias(alias_name)
        with schema_context(self.schema):
            obj = accessor._get_db_obj_query().first()
        self.assertEqual(obj.account_id, self.account_id)
        self.assertEqual(obj.account_alias, alias_name)
Ejemplo n.º 7
0
    def test_update_account_alias_with_alias(self, mock_get_alias, mock_get_account_names):
        """Test updating alias."""
        alias = 'hccm-alias'
        mock_get_alias.return_value = (self.account_id, alias)
        role_arn = 'arn:aws:iam::{}:role/CostManagement'.format(self.account_id)
        accessor = AWSAccountAlias(role_arn, 'acct10001')
        accessor.update_account_alias()

        db_access = AccountAliasAccessor(self.account_id, 'acct10001')
        self.assertEqual(db_access._obj.account_id, self.account_id)
        self.assertEqual(db_access._obj.account_alias, alias)

        mock_get_alias.return_value = (self.account_id, None)
        accessor.update_account_alias()
        db_access = AccountAliasAccessor(self.account_id, 'acct10001')
        self.assertIsNone(db_access._obj.account_alias)
Ejemplo n.º 8
0
    def create_ocpawscostlineitem_project_daily_summary(
            self, account_id, schema):
        """Create an ocpawscostlineitem_project_daily_summary object for test."""
        table_name = AWS_CUR_TABLE_MAP["ocp_on_aws_project_daily_summary"]
        data = self.create_columns_for_table(table_name)

        with AccountAliasAccessor(account_id, schema) as accessor:
            account_alias = accessor._get_db_obj_query().first()

            data = {
                "account_alias_id": account_alias.id,
                "cost_entry_bill":
                self.create_cost_entry_bill(str(uuid.uuid4())),
                "namespace": self.fake.pystr()[:8],
                "pod": self.fake.pystr()[:8],
                "node": self.fake.pystr()[:8],
                "usage_start":
                self.make_datetime_aware(self.fake.past_datetime()),
                "usage_end":
                self.make_datetime_aware(self.fake.past_datetime()),
                "product_code": self.fake.pystr()[:8],
                "usage_account_id": self.fake.pystr()[:8],
            }
        with OCPReportDBAccessor(self.schema) as accessor:
            return accessor.create_db_object(table_name, data)
Ejemplo n.º 9
0
    def test_update_account_alias_no_alias(self, mock_get_alias):
        """Test updating alias when none is set."""
        mock_get_alias.return_value = (self.account_id, None)
        role_arn = 'arn:aws:iam::{}:role/CostManagement'.format(self.account_id)
        accessor = AWSAccountAlias(role_arn, 'acct10001')
        accessor.update_account_alias()

        db_access = AccountAliasAccessor(self.account_id, 'acct10001')
        self.assertEqual(db_access._obj.account_id, self.account_id)
        self.assertIsNone(db_access._obj.account_alias)
Ejemplo n.º 10
0
    def update_account_alias(self):
        """
        Update the account alias.

        Args:
            None
        Returns:
            (String, String) Account ID, Account Alias
        """
        account_id, account_alias = get_account_alias_from_role_arn(
            self._role_arn)
        with AccountAliasAccessor(account_id, self._schema) as alias_accessor:
            alias_accessor.set_account_alias(account_alias)
            alias_accessor.commit()

        return account_id, account_alias
    def test_add_account_alias(self):
        """Test Add."""
        with schema_context(self.schema):
            AWSAccountAlias.objects.create(account_id=self.account_id,
                                           account_alias=self.account_id)
        accessor = AccountAliasAccessor(self.account_id, self.schema)

        account_id = str(uuid.uuid4())

        accessor.add(account_id)
        new_accessor = AccountAliasAccessor(account_id, self.schema)
        with schema_context(self.schema):
            obj = new_accessor._get_db_obj_query().first()

        self.assertEqual(obj.account_id, account_id)
        self.assertEqual(obj.account_alias, account_id)
Ejemplo n.º 12
0
    def create_awscostentrylineitem_daily_summary(self, account_id, schema):
        """Create an ocpawscostlineitem_project_daily_summary( object for test."""
        table_name = AWS_CUR_TABLE_MAP['line_item_daily_summary']
        data = self.create_columns_for_table(table_name)

        with AccountAliasAccessor(account_id, schema) as accessor:
            account_alias = accessor._get_db_obj_query().first()
            data = {
                'account_alias_id': account_alias.id,
                'cost_entry_bill': self.create_cost_entry_bill(),
                'usage_start':
                self.make_datetime_aware(self.fake.past_datetime()),
                'product_code': self.fake.pystr()[:8],
                'usage_account_id': self.fake.pystr()[:8]
            }

        with OCPReportDBAccessor(self.schema, self.column_map) as accessor:
            return accessor.create_db_object(table_name, data)
Ejemplo n.º 13
0
    def create_awscostentrylineitem_daily_summary(self, account_id, schema, cost_entry_bill, usage_date=None):
        """Create reporting_awscostentrylineitem_daily_summary object for test."""
        table_name = AWS_CUR_TABLE_MAP["line_item_daily_summary"]
        data = self.create_columns_for_table(table_name)
        if usage_date is None:
            usage_date = self.fake.past_datetime()

        with AccountAliasAccessor(account_id, schema) as accessor:
            account_alias = accessor._get_db_obj_query().first()
            data = {
                "account_alias_id": account_alias.id,
                "cost_entry_bill": cost_entry_bill,
                "usage_start": self.make_datetime_aware(usage_date),
                "product_code": self.fake.pystr()[:8],
                "usage_account_id": self.fake.pystr()[:8],
            }

        with OCPReportDBAccessor(self.schema) as accessor:
            obj = accessor.create_db_object(table_name, data)
        return obj
Ejemplo n.º 14
0
 def setUp(self):
     """Setup test cases."""
     self.account_id = '123456789'
     self.schema = 'acct10001'
     self.accessor = AccountAliasAccessor(self.account_id, self.schema)
     self.accessor.commit()
Ejemplo n.º 15
0
class AccountAliasAccessorTest(MasuTestCase):
    """Test Cases for the AuthDBAccessor object."""
    def setUp(self):
        """Setup test cases."""
        self.account_id = '123456789'
        self.schema = 'acct10001'
        self.accessor = AccountAliasAccessor(self.account_id, self.schema)
        self.accessor.commit()

    def tearDown(self):
        """Tear down test case."""
        self.accessor._session.delete(self.accessor._obj)
        self.accessor.commit()
        self.accessor.close_session()

    def test_initializer(self):
        """Test Initializer."""
        self.assertIsNotNone(self.accessor._session)
        self.assertTrue(self.accessor.does_db_entry_exist())

        obj = self.accessor._get_db_obj_query().first()
        self.assertEqual(obj.account_id, self.account_id)
        self.assertEqual(obj.account_alias, self.account_id)

    def test_set_account_alias(self):
        """test alias setter."""
        alias_name = 'test-alias'
        self.accessor.set_account_alias(alias_name)
        self.accessor.commit()
        obj = self.accessor._get_db_obj_query().first()
        self.assertEqual(obj.account_id, self.account_id)
        self.assertEqual(obj.account_alias, alias_name)
Ejemplo n.º 16
0
 def tearDown(self):
     """Teardown test case."""
     db_access = AccountAliasAccessor(self.account_id, 'acct10001')
     with schema_context(self.schema):
         db_access._get_db_obj_query().delete()
Ejemplo n.º 17
0
 def tearDown(self):
     """Teardown test case."""
     db_access = AccountAliasAccessor(self.account_id, 'acct10001')
     db_access._get_db_obj_query().delete()
     db_access.get_session().commit()
     db_access.close_session()