Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import azure.cli.command_modules.rdbms._helptext_mysql # pylint: disable=unused-import


# pylint: disable=import-outside-toplevel
class RdbmsCommandsLoader(AzCommandsLoader):

def __init__(self, cli_ctx=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint: disable=unused-argument, line-too-long
# pylint: disable=unused-argument, line-too-long, import-outside-toplevel, raise-missing-from
import datetime as dt
from datetime import datetime
import random
Expand Down
3 changes: 2 additions & 1 deletion src/azure-cli/azure/cli/command_modules/rdbms/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
logger = get_logger(__name__)


class RdbmsArgumentContext(AzArgumentContext): # pylint: disable=too-few-public-methods
# pylint: disable=too-few-public-methods, import-outside-toplevel
class RdbmsArgumentContext(AzArgumentContext):

def __init__(self, command_loader, scope, **kwargs): # pylint: disable=unused-argument
super().__init__(command_loader, scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint: disable=unused-argument, line-too-long
# pylint: disable=unused-argument, line-too-long, import-outside-toplevel

from msrestazure.tools import is_valid_resource_id, parse_resource_id, is_valid_resource_name, resource_id # pylint: disable=import-error
from knack.log import get_logger
from azure.cli.core.profiles import ResourceType
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import CLIError
from azure.cli.core.azclierror import ValidationError
from azure.mgmt.privatedns.models import VirtualNetworkLink
from azure.mgmt.privatedns.models import PrivateZone
from azure.mgmt.privatedns.models import SubResource
from azure.mgmt.privatedns.models import VirtualNetworkLink
from ._client_factory import resource_client_factory, network_client_factory, private_dns_client_factory, private_dns_link_client_factory, cf_postgres_flexible_private_dns_zone_suffix_operations
from ._flexible_server_util import get_id_components, check_existence

Expand All @@ -22,7 +22,7 @@
DEFAULT_SUBNET_ADDRESS_PREFIX = '10.0.0.0/24'


# pylint: disable=too-many-locals, too-many-statements, too-many-branches
# pylint: disable=too-many-locals, too-many-statements, too-many-branches, import-outside-toplevel
def prepare_private_network(cmd, resource_group_name, server_name, vnet, subnet, location, delegation_service_name, vnet_address_pref, subnet_address_pref):

nw_client = network_client_factory(cmd.cli_ctx)
Expand Down Expand Up @@ -66,7 +66,7 @@ def prepare_private_network(cmd, resource_group_name, server_name, vnet, subnet,
location, server_name, vnet_address_pref, subnet_address_pref)

else:
raise ValidationError("If you pass both --vnet and --subnet, consider passing names instead of IDs. If you want to use exising subnet, please provide subnet Id (not vnet Id).")
raise ValidationError("If you pass both --vnet and --subnet, consider passing names instead of IDs. If you want to use an existing subnet, please provide the subnet Id only (not vnet Id).")

elif subnet is None and vnet is None:
subnet_result = _create_vnet_subnet_delegation(cmd, nw_client, resource_client, delegation_service_name, resource_group_name, 'Vnet' + server_name[6:], 'Subnet' + server_name[6:],
Expand Down Expand Up @@ -121,7 +121,7 @@ def _create_vnet_subnet_delegation(cmd, nw_client, resource_client, delegation_s
prefixes = vnet.address_space.address_prefixes
subnet_exist = check_existence(resource_client, subnet_name, resource_group, 'Microsoft.Network', 'subnets', parent_name=vnet_name, parent_type='virtualNetworks')
if not subnet_exist and vnet_address_pref not in prefixes:
logger.warning('Adding address prefix %s to Vnet %s', vnet_address_pref, vnet_name)
logger.warning('The default/input address prefix does not exist in the Vnet. Adding address prefix %s to Vnet %s.', vnet_address_pref, vnet_name)
nw_client.virtual_networks.begin_create_or_update(resource_group, vnet_name,
VirtualNetwork(location=location,
address_space=AddressSpace(
Expand All @@ -131,18 +131,16 @@ def _create_vnet_subnet_delegation(cmd, nw_client, resource_client, delegation_s


def _create_subnet_delegation(cmd, nw_client, resource_client, delegation_service_name, resource_group, vnet_name, subnet_name, location, server_name, subnet_address_pref):
Delegation, Subnet, ServiceEndpoint = cmd.get_models('Delegation', 'Subnet', 'ServiceEndpointPropertiesFormat', resource_type=ResourceType.MGMT_NETWORK)
Delegation, Subnet = cmd.get_models('Delegation', 'Subnet', resource_type=ResourceType.MGMT_NETWORK)
delegation = Delegation(name=delegation_service_name, service_name=delegation_service_name)
service_endpoint = ServiceEndpoint(service='Microsoft.Storage')

# subnet exist
# subnet not exist
if not check_existence(resource_client, subnet_name, resource_group, 'Microsoft.Network', 'subnets', parent_name=vnet_name, parent_type='virtualNetworks'):
subnet_result = Subnet(
name=subnet_name,
location=location,
address_prefix=subnet_address_pref,
delegations=[delegation],
service_endpoints=[service_endpoint])
delegations=[delegation])

vnet = nw_client.virtual_networks.get(resource_group, vnet_name)
vnet_subnet_prefixes = [subnet.address_prefix for subnet in vnet.subnets]
Expand All @@ -152,6 +150,7 @@ def _create_subnet_delegation(cmd, nw_client, resource_client, delegation_servic
logger.warning('Creating new Subnet "%s" in resource group "%s"', subnet_name, resource_group)
subnet = nw_client.subnets.begin_create_or_update(resource_group, vnet_name, subnet_name,
subnet_result).result()
# subnet exist
else:
subnet = nw_client.subnets.get(resource_group, vnet_name, subnet_name)
logger.warning('Using existing Subnet "%s" in resource group "%s"', subnet_name, resource_group)
Expand All @@ -161,16 +160,14 @@ def _create_subnet_delegation(cmd, nw_client, resource_client, delegation_servic
# Add Delegation if not delegated already
if not subnet.delegations:
logger.warning('Adding "%s" delegation to the existing subnet %s.', delegation_service_name, subnet_name)
subnet.delegations = [delegation]
subnet = nw_client.subnets.begin_create_or_update(resource_group, vnet_name, subnet_name, subnet).result()
else:
for delgtn in subnet.delegations:
if delgtn.service_name != delegation_service_name:
raise CLIError("Can not use subnet with existing delegations other than {}".format(
delegation_service_name))

subnet.service_endpoints = [service_endpoint]
subnet.delegations = [delegation]
subnet = nw_client.subnets.begin_create_or_update(resource_group, vnet_name, subnet_name, subnet).result()

return subnet


Expand All @@ -191,49 +188,66 @@ def prepare_private_dns_zone(cmd, database_engine, resource_group, server_name,
nw_client = network_client_factory(cmd.cli_ctx, subscription_id=vnet_sub)
vnet = nw_client.virtual_networks.get(vnet_rg, vnet_name)

dns_rg = None
if private_dns_zone is None:
private_dns_zone = server_name + '.' + private_dns_zone_suffix

if 'private' in private_dns_zone_suffix:
private_dns_zone = server_name + '.' + private_dns_zone_suffix
else:
private_dns_zone = server_name + '.private.' + private_dns_zone_suffix
# resource ID input => check subscription and change client
elif not _check_if_resource_name(private_dns_zone) and is_valid_resource_id(private_dns_zone):
subscription, resource_group, private_dns_zone, _ = get_id_components(private_dns_zone)
subscription, dns_rg, private_dns_zone, _ = get_id_components(private_dns_zone)
if private_dns_zone[-len(private_dns_zone_suffix):] != private_dns_zone_suffix:
raise ValidationError('The suffix for the private DNS zone should be "{}"'.format(private_dns_zone_suffix))
raise ValidationError('The suffix of the private DNS zone should be "{}"'.format(private_dns_zone_suffix))

if subscription != get_subscription_id(cmd.cli_ctx):
logger.warning('The provided private DNS zone ID is in different subscription from the server')
resource_client = resource_client_factory(cmd.cli_ctx, subscription_id=subscription)
private_dns_client = private_dns_client_factory(cmd.cli_ctx, subscription_id=subscription)
private_dns_link_client = private_dns_link_client_factory(cmd.cli_ctx, subscription_id=subscription)
_resource_group_verify_and_create(resource_client, resource_group, location)

_resource_group_verify_and_create(resource_client, dns_rg, location)
# check Invalid resource ID or Name format
elif _check_if_resource_name(private_dns_zone) and not is_valid_resource_name(private_dns_zone) \
or not _check_if_resource_name(private_dns_zone) and not is_valid_resource_id(private_dns_zone):
raise ValidationError("Check if the private dns zone name or id is in correct format.")

raise ValidationError("Check if the private dns zone name or Id is in correct format.")
# Invalid resource name suffix check
elif _check_if_resource_name(private_dns_zone) and private_dns_zone[-len(private_dns_zone_suffix):] != private_dns_zone_suffix:
raise ValidationError('The suffix for the private DNS zone should be "{}"'.format(private_dns_zone_suffix))
raise ValidationError('The suffix of the private DNS zone should be in "{}" format'.format(private_dns_zone_suffix))

link = VirtualNetworkLink(location='global', virtual_network=SubResource(id=vnet.id))
link.registration_enabled = True

if not check_existence(resource_client, private_dns_zone, resource_group, 'Microsoft.Network', 'privateDnsZones'):
logger.warning('Creating a private dns zone %s..', private_dns_zone)
private_zone = private_dns_client.begin_create_or_update(resource_group_name=resource_group,
# check existence DNS zone and change resource group
zone_exist_flag = False
if dns_rg is not None and check_existence(resource_client, private_dns_zone, dns_rg, 'Microsoft.Network', 'privateDnsZones'):
zone_exist_flag = True
elif dns_rg is None and check_existence(resource_client, private_dns_zone, resource_group, 'Microsoft.Network', 'privateDnsZones'):
zone_exist_flag = True
dns_rg = resource_group
elif dns_rg is None and check_existence(resource_client, private_dns_zone, vnet_rg, 'Microsoft.Network', 'privateDnsZones'):
zone_exist_flag = True
dns_rg = vnet_rg
else:
dns_rg = vnet_rg

# create DNS zone if not exist
if not zone_exist_flag:
logger.warning('Creating a private dns zone %s in resource group "%s"', private_dns_zone, dns_rg)
private_zone = private_dns_client.begin_create_or_update(resource_group_name=dns_rg,
private_zone_name=private_dns_zone,
parameters=PrivateZone(location='global'),
if_none_match='*').result()

private_dns_link_client.begin_create_or_update(resource_group_name=resource_group,
private_dns_link_client.begin_create_or_update(resource_group_name=dns_rg,
private_zone_name=private_dns_zone,
virtual_network_link_name=vnet_name + '-link',
parameters=link, if_none_match='*').result()
else:
logger.warning('Using the existing private dns zone %s', private_dns_zone)
private_zone = private_dns_client.get(resource_group_name=resource_group,
private_zone_name=private_dns_zone)
# private dns zone link list
logger.warning('Using the existing private dns zone %s in resource group "%s"', private_dns_zone, dns_rg)

virtual_links = private_dns_link_client.list(resource_group_name=resource_group,
private_zone = private_dns_client.get(resource_group_name=dns_rg,
private_zone_name=private_dns_zone)
virtual_links = private_dns_link_client.list(resource_group_name=dns_rg,
private_zone_name=private_dns_zone)

link_exist_flag = False
Expand All @@ -243,7 +257,7 @@ def prepare_private_dns_zone(cmd, database_engine, resource_group, server_name,
break

if not link_exist_flag:
private_dns_link_client.begin_create_or_update(resource_group_name=resource_group,
private_dns_link_client.begin_create_or_update(resource_group_name=dns_rg,
private_zone_name=private_dns_zone,
virtual_network_link_name=vnet_name + '-link',
parameters=link, if_none_match='*').result()
Expand Down

Large diffs are not rendered by default.

Loading