def _build_ase_deployment_properties(name,
                                     location,
                                     subnet_id,
                                     virtual_ip_type=None,
                                     front_end_scale_factor=None,
                                     front_end_sku=None,
                                     tags=None,
                                     kind='ASEv2',
                                     os_preference=None,
                                     zone_redundant=None):
    # InternalLoadBalancingMode Enum: None 0, Web 1, Publishing 2.
    # External: 0 (None), Internal: 3 (Web + Publishing)
    ilb_mode = 3 if virtual_ip_type == 'Internal' else 0
    ase_properties = {
        'name': name,
        'location': location,
        'InternalLoadBalancingMode': ilb_mode,
        'virtualNetwork': {
            'id': subnet_id
        }
    }
    if front_end_scale_factor:
        ase_properties['frontEndScaleFactor'] = front_end_scale_factor
    if front_end_sku:
        worker_sku = _map_worker_sku(front_end_sku)
        ase_properties['multiSize'] = worker_sku
    if os_preference:
        ase_properties['osPreference'] = os_preference
    if zone_redundant:
        ase_properties['zoneRedundant'] = zone_redundant

    ase_resource = {
        'name': name,
        'type': 'Microsoft.Web/hostingEnvironments',
        'location': location,
        'apiVersion': '2019-08-01',
        'kind': kind,
        'tags': tags,
        'properties': ase_properties
    }

    deployment_template = ArmTemplateBuilder()
    deployment_template.add_resource(ase_resource)
    template = deployment_template.build()
    parameters = deployment_template.build_parameters()

    deploymentProperties = DeploymentProperties(template=template,
                                                parameters=parameters,
                                                mode='Incremental')
    deployment = Deployment(properties=deploymentProperties)
    return deployment
Esempio n. 2
0
def create_domain(
        cmd,
        resource_group_name,
        hostname,
        contact_info,
        privacy=True,
        auto_renew=True,  # pylint: disable=too-many-locals
        accept_terms=False,
        tags=None,
        dryrun=False,
        no_wait=False):
    from azure.cli.core.commands.arm import ArmTemplateBuilder
    from azure.cli.command_modules.appservice._template_builder import (
        build_dns_zone, build_domain)
    from datetime import datetime
    import socket
    import json

    tags = tags or {}

    if not accept_terms and not dryrun:
        raise CLIError(
            "To purchase and create your custom domain '{}', you must view the terms and conditions "
            "using the command `az appservice domain show-terms`, and accept these terms and "
            "conditions using the --accept-terms flag".format(hostname))

    try:
        contact_info = json.loads(contact_info)
    except Exception:
        raise CLIError(
            'Unable to load contact info. Please verify the path to your contact info file, '
            'and that the format matches the sample found at the following link: '
            'https://github.com/AzureAppServiceCLI/appservice_domains_templates'
            '/blob/master/contact_info.json')
    contact_info = verify_contact_info_and_format(contact_info)

    current_time = str(datetime.utcnow()).replace('+00:00', 'Z')
    local_ip_address = ''
    try:
        local_ip_address = socket.gethostbyname(socket.gethostname())
    except:
        raise CLIError("Unable to get IP address")

    web_client = web_client_factory(cmd.cli_ctx)
    hostname_availability = web_client.domains.check_availability(
        name=hostname)

    if dryrun:
        logger.warning(
            "Custom domain will be purchased with the below configuration. Re-run command "
            "without the --dryrun flag to purchase & create the custom domain")
        dry_run_params = contact_info.copy()
        dry_run_params.update({
            "hostname":
            hostname,
            "resource_group_name":
            resource_group_name,
            "privacy":
            bool(privacy),
            "auto_renew":
            bool(auto_renew),
            "accept_terms":
            bool(accept_terms),
            "hostname_available":
            bool(hostname_availability.available),
            "price":
            "$11.99 USD" if hostname_availability.available else "N/A"
        })
        dry_run_str = r""" {
                    "hostname" : "%(hostname)s",
                    "resource_group" : "%(resource_group_name)s",
                    "contact_info": {
                            "address1": "%(address1)s",
                            "address2": "%(address2)s",
                            "city": "%(city)s",
                            "country": "%(country)s",
                            "postal_code": "%(postal_code)s",
                            "state": "%(state)s",
                            "email": "%(email)s",
                            "fax": "%(fax)s",
                            "job_title": "%(job_title)s",
                            "name_first": "%(name_first)s",
                            "name_last": "%(name_last)s",
                            "name_middle": "%(name_middle)s",
                            "organization": "%(organization)s",
                            "phone": "%(phone)s"
                        },
                    "privacy": "%(privacy)s",
                    "auto_renew": "%(auto_renew)s",
                    "accepted_hostname_purchase_terms": "%(accept_terms)s",
                    "hostname_available": "%(hostname_available)s",
                    "price": "%(price)s"
                    }
                    """ % dry_run_params
        return json.loads(dry_run_str)

    if not hostname_availability.available:
        raise CLIError(
            "Custom domain name '{}' is not available. Please try again "
            "with a new hostname.".format(hostname))

    tld = '.'.join(hostname.split('.')[1:])
    agreements = web_client.top_level_domains.list_agreements(
        name=tld, include_privacy=privacy)
    agreement_keys = [agreement.agreement_key for agreement in agreements]

    dns_zone_id = "[resourceId('Microsoft.Network/dnszones', '{}')]".format(
        hostname)

    master_template = ArmTemplateBuilder()
    dns_zone_resource = build_dns_zone(hostname)
    domain_resource = build_domain(domain_name=hostname,
                                   local_ip_address=local_ip_address,
                                   current_time=current_time,
                                   address1=contact_info['address1'],
                                   address2=contact_info['address2'],
                                   city=contact_info['city'],
                                   country=contact_info['country'],
                                   postal_code=contact_info['postal_code'],
                                   state=contact_info['state'],
                                   email=contact_info['email'],
                                   fax=contact_info['fax'],
                                   job_title=contact_info['job_title'],
                                   name_first=contact_info['name_first'],
                                   name_last=contact_info['name_last'],
                                   name_middle=contact_info['name_middle'],
                                   organization=contact_info['organization'],
                                   phone=contact_info['phone'],
                                   dns_zone_id=dns_zone_id,
                                   privacy=privacy,
                                   auto_renew=auto_renew,
                                   agreement_keys=agreement_keys,
                                   tags=tags,
                                   dependencies=[dns_zone_id])

    master_template.add_resource(dns_zone_resource)
    master_template.add_resource(domain_resource)

    template = master_template.build()

    # deploy ARM template
    deployment_name = 'domain_deploy_' + random_string(32)
    client = get_mgmt_service_client(
        cmd.cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES).deployments
    DeploymentProperties = cmd.get_models(
        'DeploymentProperties',
        resource_type=ResourceType.MGMT_RESOURCE_RESOURCES)
    properties = DeploymentProperties(template=template,
                                      parameters={},
                                      mode='incremental')

    if cmd.supported_api_version(
            min_api='2019-10-01',
            resource_type=ResourceType.MGMT_RESOURCE_RESOURCES):
        Deployment = cmd.get_models(
            'Deployment', resource_type=ResourceType.MGMT_RESOURCE_RESOURCES)
        deployment = Deployment(properties=properties)

        deployment_result = DeploymentOutputLongRunningOperation(cmd.cli_ctx)(
            sdk_no_wait(no_wait, client.create_or_update, resource_group_name,
                        deployment_name, deployment))
    else:
        deployment_result = DeploymentOutputLongRunningOperation(cmd.cli_ctx)(
            sdk_no_wait(no_wait, client.create_or_update, resource_group_name,
                        deployment_name, properties))

    return deployment_result