<commit_before>"""
This plugin is to synchronize Azure Web App resources

It should be placed in Orchestration Actions at the
Post Sync VMs Hookpoint.

Additionally, this action should have its Resource Technology limited to Azure

"""
if __name__ == "__main__":
    import os
    import sys
    import django

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
    sys.path.append("/opt/cloudbolt")
    django.setup()

from common.methods import set_progress
from infrastructure.models import CustomField
from jobs.models import Job
from azure.mgmt.web import WebSiteManagementClient
from resourcehandlers.azure_arm.models import AzureARMHandler
from resourcehandlers.azure_arm.azure_wrapper import configure_arm_client

from resources.models import ResourceType, Resource
from servicecatalog.models import ServiceBlueprint
from accounts.models import Group

from utilities.logger import ThreadLogger


logger = ThreadLogger(__name__)


def run(job, **kwargs):
    # Connect to Azure Management Service
    set_progress("Connecting To Azure Management Service...")
    azure = AzureARMHandler.objects.first()

    wrapper = azure.get_api_wrapper()
    resource_client = wrapper.resource_client
    web_client = configure_arm_client(wrapper, WebSiteManagementClient)
    set_progress("Successfully Connected To Azure Management Service!")

    # Generate custom fields
    CustomField.objects.get_or_create(
        name="web_app_id",
        label="Azure Web App ID",
        type="STR",
        description="Used by the Azure Web App resource & blueprint.",
    )

    # Generate custom fields
    CustomField.objects.get_or_create(
        name="web_app_location",
        label="Azure Web App Location",
        type="STR",
        description="Used by the Azure Web App resource & blueprint.",
    )

    # Generate custom fields
    CustomField.objects.get_or_create(
        name="web_app_default_host_name",
        label="Azure Web App Host Name",
        type="URL",
        description="Used by the Azure Web App resource & blueprint.",<commit_msg>Support backwards compatibility of Azure blueprints<commit_after>"""
This plugin is to synchronize Azure Web App resources

It should be placed in Orchestration Actions at the
Post Sync VMs Hookpoint.

Additionally, this action should have its Resource Technology limited to Azure

"""
if __name__ == "__main__":
    import os
    import sys
    import django

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
    sys.path.append("/opt/cloudbolt")
    django.setup()

from common.methods import set_progress
from infrastructure.models import CustomField
from jobs.models import Job
from resourcehandlers.azure_arm.models import AzureARMHandler
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.web import WebSiteManagementClient

from resources.models import ResourceType, Resource
from servicecatalog.models import ServiceBlueprint
from accounts.models import Group

from utilities.logger import ThreadLogger


logger = ThreadLogger(__name__)


def _get_clients(handler):
    """
    Get the clients using newer methods from the CloudBolt main repo if this CB is running
    a version greater than 9.2. These internal methods implicitly take care of much of the other
    features in CloudBolt such as proxy and ssl verification.
    Otherwise, manually instantiate clients without support for those other CloudBolt settings.
    :param handler:
    :return:
    """
    import settings
    from common.methods import is_version_newer

    set_progress("Connecting To Azure...")

    cb_version = settings.VERSION_INFO["VERSION"]
    if is_version_newer(cb_version, "9.2"):
        from resourcehandlers.azure_arm.azure_wrapper import configure_arm_client

        wrapper = handler.get_api_wrapper()
        web_client = configure_arm_client(wrapper, WebSiteManagementClient)
        resource_client = wrapper.resource_client
    else:
        # TODO: Remove once versions <= 9.2 are no longer supported.
        credentials = ServicePrincipalCredentials(
            client_id=handler.client_id,
            secret=handler.secret,
            tenant=handler.tenant_id,
        )
        web_client = WebSiteManagementClient(credentials, handler.serviceaccount)
        resource_client = ResourceManagementClient(credentials, handler.serviceaccount)

    set_progress("Connection to Azure established")

    return web_client, resource_client


def run(job, **kwargs):
    # Connect to Azure Management Service
    azure = AzureARMHandler.objects.first()
    web_client, resource_client = _get_clients(azure)

    # Generate custom fields
    CustomField.objects.get_or_create(
        name="web_app_id",
        label="Azure Web App ID",
        type="STR",
        description="Used by the Azure Web App resource & blueprint.",
    )

    # Generate custom fields
    CustomField.objects.get_or_create(
        name="web_app_location",
        label="Azure Web App Location",
        type="STR",
        description="Used by the Azure Web App resource & blueprint.",
    )

    # Generate custom fields
    CustomField.objects.get_or_create(
        name="web_app_default_host_name",
        label="Azure Web App Host Name",
        type="URL",
        description="Used by the Azure Web App resource & blueprint.",
{'fraction_non_alphanumeric': 0.07026325546903968, 'fraction_numerical': 0.0011123470522803114, 'mean_word_length': 3.753303964757709, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6436492', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1378, 'n_words': 449}