Ejemplo n.º 1
0
    def test_resolve_with_implicit_stack_reference(self,
                                                   mock_get_output_value):
        stack = MagicMock(spec=Stack)
        stack.dependencies = []
        stack.project_code = "project-code"
        stack.name = "account/dev/stack"
        stack._connection_manager = MagicMock(spec=ConnectionManager)

        dependency = MagicMock()
        dependency.name = "account/dev/vpc"
        dependency.profile = 'dependency_profile'
        dependency.region = 'dependency_region'

        mock_get_output_value.return_value = "output_value"

        stack_output_resolver = StackOutput("account/dev/vpc.yaml::VpcId",
                                            stack)

        stack_output_resolver.setup()
        assert stack.dependencies == ["account/dev/vpc.yaml"]

        stack.dependencies = [dependency]
        result = stack_output_resolver.resolve()
        assert result == "output_value"
        mock_get_output_value.assert_called_once_with(
            "project-code-account-dev-vpc",
            "VpcId",
            profile='dependency_profile',
            region='dependency_region')
Ejemplo n.º 2
0
    def test_resolver_with_existing_dependencies(self, mock_get_output_value):
        stack = MagicMock(spec=Stack)
        stack.dependencies = ["existing"]
        stack.project_code = "project-code"
        stack.template.connection_manager = MagicMock(spec=ConnectionManager)

        dependency = MagicMock()
        dependency.name = "account/dev/vpc"
        dependency.profile = 'dependency_profile'
        dependency.region = 'dependency_region'

        mock_get_output_value.return_value = "output_value"

        stack_output_resolver = StackOutput("account/dev/vpc::VpcId", stack)

        stack_output_resolver.setup()
        assert stack.dependencies == ["existing", "account/dev/vpc"]

        stack.dependencies = [MagicMock(), dependency]
        result = stack_output_resolver.resolve()
        assert result == "output_value"
        mock_get_output_value.assert_called_once_with(
            "project-code-account-dev-vpc",
            "VpcId",
            profile='dependency_profile',
            region='dependency_region')
Ejemplo n.º 3
0
    def test_resolve_with_implicit_stack_reference_top_level(
            self, mock_get_output_value):
        stack = MagicMock(spec=Stack)
        stack.dependencies = []
        stack.project_code = "project-code"
        stack.name = "stack"
        stack._connection_manager = MagicMock(spec=ConnectionManager)

        dependency = MagicMock()
        dependency.project_code = "meh"
        dependency.name = "vpc"
        dependency.profile = "dependency_profile"
        dependency.region = "dependency_region"
        dependency.iam_role = "dependency_iam_role"

        mock_get_output_value.return_value = "output_value"

        stack_output_resolver = StackOutput("vpc.yaml::VpcId", stack)

        stack_output_resolver.setup()
        assert stack.dependencies == ["vpc.yaml"]

        stack.dependencies = [dependency]
        result = stack_output_resolver.resolve()
        assert result == "output_value"
        mock_get_output_value.assert_called_once_with(
            "meh-vpc",
            "VpcId",
            profile="dependency_profile",
            region="dependency_region",
            iam_role="dependency_iam_role")
Ejemplo n.º 4
0
    def test_resolver_with_existing_dependencies(self, mock_get_output_value):
        stack = MagicMock(spec=Stack)
        stack.dependencies = ["existing"]
        stack.project_code = "project-code"
        stack.connection_manager = MagicMock(spec=ConnectionManager)
        stack_output_resolver = StackOutput("account/dev/vpc::VpcId", stack)
        mock_get_output_value.return_value = "output_value"

        result = stack_output_resolver.resolve()
        assert result == "output_value"
        mock_get_output_value.assert_called_once_with(
            "project-code-account-dev-vpc", "VpcId")
        assert stack.dependencies == ["existing", "account/dev/vpc"]
 def determine_stack_output(self, arg: str):
     return StackOutput(
         argument=arg,
         connection_manager=self.connection_manager,
         environment_config=self.environment_config,
         stack_config=self.stack_config,
     ).resolve()
Ejemplo n.º 6
0
    def run(self):
        """
        Removes all objects from a bucket
        Usage: !s3_empty_bucket stack_name::output_name
        :return:
        """
        try:
            bucket_name = StackOutput(
                argument=self.argument,
                connection_manager=self.connection_manager,
                environment_config=self.environment_config,
                stack_config=self.stack_config,
            ).resolve()

            print(f"[{self.NAME}] Emptying bucket: {bucket_name}")
            s3_client = self.connection_manager.boto_session.client('s3')
            paginator = s3_client.get_paginator('list_object_versions')
            bucket_iterator = paginator.paginate(Bucket=bucket_name)
            for page in bucket_iterator:
                for v in page['Versions']:
                    v_id = v['VersionId']
                    v_key = v['Key']
                    print(f"Deleting '{v_key}', '{v_id}")
                    s3_client.delete_object(Bucket=bucket_name,
                                            Key=v_key,
                                            VersionId=v_id)
        except Exception as e:
            print(f'[{self.NAME}] Error: {e}')
Ejemplo n.º 7
0
 def get_stack_output(self, stack_name):
     return StackOutput(
         argument=stack_name,
         connection_manager=self.connection_manager,
         environment_config=self.environment_config,
         stack_config=self.stack_config,
     ).resolve()
Ejemplo n.º 8
0
    def test_resolve_with_implicit_stack_reference(self,
                                                   mock_get_output_value):
        stack = MagicMock(spec=Stack)
        stack.dependencies = []
        stack.project_code = "project-code"
        stack.name = "account/dev/stack"
        stack.connection_manager = MagicMock(spec=ConnectionManager)
        stack_output_resolver = StackOutput("vpc::VpcId", stack)
        mock_get_output_value.return_value = "output_value"

        stack_output_resolver.setup()
        result = stack_output_resolver.resolve()

        assert result == "output_value"
        mock_get_output_value.assert_called_once_with(
            "project-code-account-dev-vpc", "VpcId")
        assert stack.dependencies == ["account/dev/vpc"]
Ejemplo n.º 9
0
    def test_resolve(self, mock_get_output_value):
        mock_environment_config = MagicMock(spec=Config)
        mock_stack_config = MagicMock(spec=Config)
        mock_environment_config.__getitem__.return_value = "project-code"
        mock_stack_config.__getitem__.return_value = []

        stack_output_resolver = StackOutput(
            environment_config=mock_environment_config,
            stack_config=mock_stack_config,
            connection_manager=sentinel.connection_manager,
            argument="account/dev/vpc::VpcId"
        )

        mock_get_output_value.return_value = "output_value"

        stack_output_resolver.resolve()

        mock_get_output_value.assert_called_once_with(
            "project-code-account-dev-vpc", "VpcId"
        )
Ejemplo n.º 10
0
 def run(self):
     try:
         bucket_name = StackOutput(argument=self.argument,
                              connection_manager=self.connection_manager,
                              environment_config=self.environment_config,
                              stack_config=self.stack_config,
                              ).resolve()
         s3 = self.connection_manager.boto_session.resource('s3')
         bucket = s3.Bucket(bucket_name)
         print(f"Deleting bucket: {bucket_name}")
         bucket.delete()
     except Exception as e:
         print(e)
 def run(self):
     """
     Deletes a bucket
     Usage: !s3_delete_bucket stack_name::output_name
     :return:
     """
     try:
         bucket_name = StackOutput(
             argument=self.argument,
             connection_manager=self.connection_manager,
             environment_config=self.environment_config,
             stack_config=self.stack_config,
         ).resolve()
         s3 = self.connection_manager.boto_session.resource('s3')
         bucket = s3.Bucket(bucket_name)
         print(f"[{self.NAME}]: Deleting bucket: {bucket_name}")
         bucket.delete()
     except Exception as e:
         print(e)
    def run(self):
        """
        Removes all objects from a bucket
        Usage: !s3_empty_bucket stack_name::output_name
        :return:
        """
        try:
            bucket_name = StackOutput(argument=self.argument,
                                      connection_manager=self.connection_manager,
                                      environment_config=self.environment_config,
                                      stack_config=self.stack_config,
                                      ).resolve()

            print(f"[{self.NAME}] Emptying bucket: {bucket_name}")
            s3 = self.connection_manager.boto_session.resource('s3')
            bucket = s3.Bucket(bucket_name)
            s3_client = self.connection_manager.boto_session.client('s3')
            resp = s3_client.list_object_versions(Bucket=bucket_name)

            del_markers = {}
            # All delete markers that are latest version, i.e., should be deleted
            if resp.get('DeleteMarkers'):
                del_markers = {item['Key']: item['VersionId'] for item in resp['DeleteMarkers'] if item['IsLatest'] == True}

            # all objects must be deleted
            for item in resp['Versions']:
                self.del_obj_list[item['Key']].append(item['VersionId'])

            # Remove old versions of object by VersionId
            for del_item in self.del_obj_list:
                print(f'[{self.NAME}] Deleting: {del_item}')
                rm_obj = bucket.Object(del_item)

                for del_id in self.del_obj_list[del_item]:
                    rm_obj.delete(VersionId=del_id)

                # Remove delete marker
                rm_obj.delete(VersionId=del_markers[del_item])

            bucket.objects.all().delete()
        except Exception as e:
            print(f'[{self.NAME}] Error: {e}')
Ejemplo n.º 13
0
 def run(self):
     """
     Uploads a file to s3
     Usage: !s3_upload_file file_name bucket_key stack_name::output_name
     :return:
     """
     try:
         cwd = os.getcwd()
         print(cwd)
         file_name, bucket_key, stack_param = self.argument.split(' ', 2)
         bucket_name = StackOutput(
             argument=stack_param,
             connection_manager=self.connection_manager,
             environment_config=self.environment_config,
             stack_config=self.stack_config,
         ).resolve()
         print(
             f"[{self.NAME}] Copying {file_name} to bucket: s3://{bucket_name}/{bucket_key}"
         )
         s3 = self.connection_manager.boto_session.resource('s3')
         bucket = s3.Bucket(bucket_name)
         bucket.upload_file(file_name, bucket_key)
     except Exception as e:
         print(e)