<commit_before># Copyright (c) 2014 Rackspace, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

from oslo_log import log

from poppy.manager import base
from poppy.notification.mailgun import driver as n_driver
from poppy.provider.akamai.background_jobs.check_cert_status_and_update \
    import check_cert_status_and_update_flow
from poppy.provider.akamai.background_jobs.update_property import \
    update_property_flow
from poppy.provider.akamai import driver as a_driver

LOG = log.getLogger(__name__)


class BackgroundJobController(base.BackgroundJobController):

    def __init__(self, manager):
        super(BackgroundJobController, self).__init__(manager)
        self.distributed_task_controller = (
            self._driver.distributed_task.services_controller)
        self.akamai_san_cert_suffix = self.driver.conf[
            a_driver.AKAMAI_GROUP].akamai_https_access_url_suffix
        self.akamai_san_cert_cname_list = self.driver.conf[
            a_driver.AKAMAI_GROUP].san_cert_cnames
        self.notify_email_list = self.driver.conf[
            n_driver.MAIL_NOTIFICATION_GROUP].recipients

    def post_job(self, job_type, kwargs):
        queue_data = []

        run_list = []
        ignore_list = []
        if job_type == "akamai_check_and_update_cert_status":
            # this task will consume the san mapping queue
            if 'akamai' in self._driver.providers:
                akamai_driver = self._driver.providers['akamai'].obj
                queue_data += akamai_driver.san_mapping_queue.traverse_queue(
                    consume=True
                )

                run_list += queue_data
                for cert_dict in queue_data:
                    try:
                        cert_dict = json.loads(cert_dict)
                        LOG.info(
                            'Starting to check status on domain: {0},'
                            'for project_id: {1}'
                            'flavor_id: {2}, cert_type: {3}'.format(
                                cert_dict.get("domain_name"),
                                cert_dict.get("project_id"),
                                cert_dict.get("flavor_id"),
                                cert_dict.get("cert_type")
                            )
                        )
                        t_kwargs = {
                            "cert_obj_json": json.dumps(cert_dict),
                            "project_id": kwargs.get("project_id")
                        }
                        self.distributed_task_controller.submit_task(
                            check_cert_status_and_update_flow.
                            check_cert_status_and_update_flow,
                            **t_kwargs
                        )
                    except Exception as e:
                        akamai_driver.san_mapping_queue.enqueue_san_mapping(
                            json.dumps(cert_dict)
                        )
                        run_list.remove(cert_dict)
                        cert_dict['error_message'] = str(e.message)
                        ignore_list.append(cert_dict)
                        LOG.exception(e)

            return run_list, ignore_list
        elif job_type == "akamai_update_papi_property_for_mod_san":
            # this task will leave the san mapping queue intact
            if 'akamai' in self._driver.providers:
                akamai_driver = self._driver.providers['akamai'].obj
                queue_data += akamai_driver.san_mapping_queue.traverse_queue()

            cname_host_info_list = []

            run_list += queue_data
            for cert_dict in queue_data:
                try:
                    cert_dict = json.loads(cert_dict)

                    domain_name = cert_dict["domain_name"]
                    san_cert = (
                        cert_dict["cert_details"]
                        ["Akamai"]["extra_info"]["san cert"]
                    )
                    LOG.info(
                        "{0}: {1} to {2}, on property: {3}".format(
                            kwargs.get("action", 'add'),
                            domain_name,
                            san_cert,
                            kwargs.get(
                                "property_spec",
                                'akamai_https_san_config_numbers'
                            )
                        )
                    )

                    # Note(tonytan4ever): Put this check here so erroneous san
                    # cert params will not pass. Support occasionally put in
                    # the ending "edgekey.net"
                    # (e.g: securexxx.san1.abc.com.edgekey.net), this check
                    # will effectively error that out
                    if san_cert not in self.akamai_san_cert_cname_list:
                        raise ValueError(
                            "Not A valid san cert cname: {0}, "
                            "valid san cert cnames are: {1}".format(
                                san_cert,
                                self.akamai_san_cert_cname_list
                            )
                        )
                    cname_host_info_list.append({
                        "cnameFrom": domain_name,
                        "cnameTo": '.'.join(
                            [san_cert, self.akamai_san_cert_suffix]
                        ),
                        "cnameType": "EDGE_HOSTNAME"
                    })
                except Exception as e:
                    run_list.remove(cert_dict)
                    cert_dict['error_message'] = str(e.message)
                    ignore_list.append(cert_dict)
                    LOG.exception(e)

            update_info_list = json.dumps([
                (
                    kwargs.get("action", 'add'),
                    cname_host_info_list
                )
            ])

            t_kwargs = {
                "property_spec": kwargs.get(
                    "property_spec",
                    'akamai_https_san_config_numbers'
                ),<commit_msg>Append to run_list, ignore_list during task exec<commit_after># Copyright (c) 2014 Rackspace, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

from oslo_log import log

from poppy.manager import base
from poppy.notification.mailgun import driver as n_driver
from poppy.provider.akamai.background_jobs.check_cert_status_and_update \
    import check_cert_status_and_update_flow
from poppy.provider.akamai.background_jobs.update_property import \
    update_property_flow
from poppy.provider.akamai import driver as a_driver

LOG = log.getLogger(__name__)


class BackgroundJobController(base.BackgroundJobController):

    def __init__(self, manager):
        super(BackgroundJobController, self).__init__(manager)
        self.distributed_task_controller = (
            self._driver.distributed_task.services_controller)
        self.akamai_san_cert_suffix = self.driver.conf[
            a_driver.AKAMAI_GROUP].akamai_https_access_url_suffix
        self.akamai_san_cert_cname_list = self.driver.conf[
            a_driver.AKAMAI_GROUP].san_cert_cnames
        self.notify_email_list = self.driver.conf[
            n_driver.MAIL_NOTIFICATION_GROUP].recipients

    def post_job(self, job_type, kwargs):
        queue_data = []

        run_list = []
        ignore_list = []
        if job_type == "akamai_check_and_update_cert_status":
            # this task will consume the san mapping queue
            if 'akamai' in self._driver.providers:
                akamai_driver = self._driver.providers['akamai'].obj
                queue_data += akamai_driver.san_mapping_queue.traverse_queue(
                    consume=True
                )

                for cert_dict in queue_data:
                    try:
                        cert_dict = json.loads(cert_dict)
                        LOG.info(
                            'Starting to check status on domain: {0},'
                            'for project_id: {1}'
                            'flavor_id: {2}, cert_type: {3}'.format(
                                cert_dict.get("domain_name"),
                                cert_dict.get("project_id"),
                                cert_dict.get("flavor_id"),
                                cert_dict.get("cert_type")
                            )
                        )
                        t_kwargs = {
                            "cert_obj_json": json.dumps(cert_dict),
                            "project_id": kwargs.get("project_id")
                        }
                        self.distributed_task_controller.submit_task(
                            check_cert_status_and_update_flow.
                            check_cert_status_and_update_flow,
                            **t_kwargs
                        )
                        run_list.append(cert_dict)
                    except Exception as e:
                        try:
                            akamai_driver.san_mapping_queue.\
                                enqueue_san_mapping(json.dumps(cert_dict))
                        except Exception as e:
                            akamai_driver.san_mapping_queue.\
                                enqueue_san_mapping(cert_dict)

                        cert_dict['error_message'] = str(e.message)
                        ignore_list.append(cert_dict)
                        LOG.exception(e)

            return run_list, ignore_list
        elif job_type == "akamai_update_papi_property_for_mod_san":
            # this task will leave the san mapping queue intact
            if 'akamai' in self._driver.providers:
                akamai_driver = self._driver.providers['akamai'].obj
                queue_data += akamai_driver.san_mapping_queue.traverse_queue()

            cname_host_info_list = []

            for cert_dict in queue_data:
                try:
                    cert_dict = json.loads(cert_dict)

                    domain_name = cert_dict["domain_name"]
                    san_cert = (
                        cert_dict["cert_details"]
                        ["Akamai"]["extra_info"]["san cert"]
                    )
                    LOG.info(
                        "{0}: {1} to {2}, on property: {3}".format(
                            kwargs.get("action", 'add'),
                            domain_name,
                            san_cert,
                            kwargs.get(
                                "property_spec",
                                'akamai_https_san_config_numbers'
                            )
                        )
                    )

                    # Note(tonytan4ever): Put this check here so erroneous san
                    # cert params will not pass. Support occasionally put in
                    # the ending "edgekey.net"
                    # (e.g: securexxx.san1.abc.com.edgekey.net), this check
                    # will effectively error that out
                    if san_cert not in self.akamai_san_cert_cname_list:
                        raise ValueError(
                            "Not A valid san cert cname: {0}, "
                            "valid san cert cnames are: {1}".format(
                                san_cert,
                                self.akamai_san_cert_cname_list
                            )
                        )
                    cname_host_info_list.append({
                        "cnameFrom": domain_name,
                        "cnameTo": '.'.join(
                            [san_cert, self.akamai_san_cert_suffix]
                        ),
                        "cnameType": "EDGE_HOSTNAME"
                    })
                    run_list.append(cert_dict)
                except Exception as e:
                    cert_dict['error_message'] = str(e.message)
                    ignore_list.append(cert_dict)
                    LOG.exception(e)

            update_info_list = json.dumps([
                (
                    kwargs.get("action", 'add'),
                    cname_host_info_list
                )
            ])

            t_kwargs = {
                "property_spec": kwargs.get(
                    "property_spec",
                    'akamai_https_san_config_numbers'
                ),
{'fraction_non_alphanumeric': 0.07146598817630771, 'fraction_numerical': 0.002993339818902941, 'mean_word_length': 1.2977991746905089, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6874207', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3307, 'n_words': 693}