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 @@ -10,9 +10,10 @@
from knack.log import get_logger
from azure.core.paging import ItemPaged
from azure.cli.core.commands import LongRunningOperation, _is_poller
from azure.cli.core.util import CLIError
from azure.cli.core.azclierror import ValidationError
from azure.cli.core.azclierror import RequiredArgumentMissingError, InvalidArgumentValueError
from azure.mgmt.resource.resources.models import ResourceGroup
from msrestazure.tools import parse_resource_id
from msrestazure.azure_exceptions import CloudError
from ._client_factory import resource_client_factory, cf_mysql_flexible_location_capabilities, cf_postgres_flexible_location_capabilities
from .flexible_server_custom_common import firewall_rule_create_func
logger = get_logger(__name__)
Expand Down Expand Up @@ -122,8 +123,8 @@ def parse_public_access_input(public_access):
elif len(parsed_input) == 2:
return parsed_input[0], parsed_input[1]
else:
raise CLIError('incorrect usage: --public-access. Acceptable values are \'all\', \'none\',\'<startIP>\' and \'<startIP>-<destinationIP>\' '
'where startIP and destinationIP ranges from 0.0.0.0 to 255.255.255.255')
raise InvalidArgumentValueError('incorrect usage: --public-access. Acceptable values are \'all\', \'none\',\'<startIP>\' and \'<startIP>-<destinationIP>\' '
'where startIP and destinationIP ranges from 0.0.0.0 to 255.255.255.255')


def server_list_custom_func(client, resource_group_name=None):
Expand Down Expand Up @@ -208,7 +209,7 @@ def get_mysql_list_skus_info(cmd, location):
def _parse_list_skus(result, database_engine):
result = _get_list_from_paged_response(result)
if not result:
raise CLIError("No available SKUs in this location")
raise InvalidArgumentValueError("No available SKUs in this location")

tiers = result[0].supported_flexible_server_editions
tiers_dict = {}
Expand Down Expand Up @@ -297,11 +298,45 @@ def get_current_time():
return datetime.utcnow().replace(tzinfo=dt.timezone.utc, microsecond=0).isoformat()


def change_str_to_datetime(date_str):
for fmt in ("%Y-%m-%dT%H:%M:%S+00:00", "%Y-%m-%dT%H:%M:%S.%f+00:00"):
try:
return datetime.strptime(date_str, fmt)
except ValueError:
pass
def get_id_components(rid):
parsed_rid = parse_resource_id(rid)
subscription = parsed_rid['subscription']
resource_group = parsed_rid['resource_group']
vnet_name = parsed_rid['name']
subnet_name = parsed_rid['child_name_1'] if 'child_name_1' in parsed_rid else None

raise ValidationError("The format of restore time should be %Y-%m-%dT%H:%M:%S+00:00")
return subscription, resource_group, vnet_name, subnet_name


def check_existence(resource_client, value, resource_group, provider_namespace, resource_type,
parent_name=None, parent_type=None):

parent_path = ''
if parent_name and parent_type:
parent_path = '{}/{}'.format(parent_type, parent_name)

api_version = _resolve_api_version(resource_client, provider_namespace, resource_type, parent_path)

try:
resource_client.resources.get(resource_group, provider_namespace, parent_path, resource_type, value, api_version)
except CloudError:
return False
return True


def _resolve_api_version(client, provider_namespace, resource_type, parent_path):
provider = client.providers.get(provider_namespace)

# If available, we will use parent resource's api-version
resource_type_str = (parent_path.split('/')[0] if parent_path else resource_type)

rt = [t for t in provider.resource_types # pylint: disable=no-member
if t.resource_type.lower() == resource_type_str.lower()]
if not rt:
raise InvalidArgumentValueError('Resource type {} not found.'.format(resource_type_str))
if len(rt) == 1 and rt[0].api_versions:
npv = [v for v in rt[0].api_versions if 'preview' not in v.lower()]
return npv[0] if npv else rt[0].api_versions[0]
raise RequiredArgumentMissingError(
'API version is required and could not be resolved for resource {}'
.format(resource_type))
2 changes: 1 addition & 1 deletion src/azure-cli/azure/cli/command_modules/rdbms/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def _flexible_server_params(command_group):
c.argument('server_name', options_list=['--name', '-n'], arg_type=overriding_none_arg_type,
help='The name of the new server that is created by the restore command.')
c.argument('restore_point_in_time', options_list=['--restore-time'], default=get_current_time(),
help='The point in time in UTC to restore from (ISO8601 format), e.g., 2017-04-26T02:10:00+08:00')
help='The point in time in UTC to restore from (ISO8601 format), e.g., 2017-04-26T02:10:00+00:00')
if command_group == 'postgres':
c.argument('source_server', options_list=['--source-server'],
help='The name of the source server to restore from.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# --------------------------------------------------------------------------------------------

# pylint: disable=unused-argument, line-too-long
import datetime as dt
from datetime import datetime
from msrestazure.azure_exceptions import CloudError
from msrestazure.tools import resource_id, is_valid_resource_id, parse_resource_id # pylint: disable=import-error
from knack.log import get_logger
Expand All @@ -21,7 +19,7 @@
parse_public_access_input, generate_password, parse_maintenance_window, get_mysql_list_skus_info, \
DEFAULT_LOCATION_MySQL
from .flexible_server_custom_common import user_confirmation
from .flexible_server_virtual_network import create_vnet, prepare_vnet
from .flexible_server_virtual_network import prepare_private_network
from .validators import mysql_arguments_validator

logger = get_logger(__name__)
Expand Down Expand Up @@ -52,16 +50,6 @@ def flexible_server_create(cmd, client, resource_group_name=None, server_name=No
raise CLIError("Incorrect usage : A combination of the parameters --subnet "
"and --public_access is invalid. Use either one of them.")

# When address space parameters are passed, the only valid combination is : --vnet, --subnet, --vnet-address-prefix, --subnet-address-prefix
# pylint: disable=too-many-boolean-expressions
if (vnet_address_prefix is not None) or (subnet_address_prefix is not None):
if (((vnet_address_prefix is not None) and (subnet_address_prefix is None)) or
((vnet_address_prefix is None) and (subnet_address_prefix is not None)) or
((vnet_address_prefix is not None) and (subnet_address_prefix is not None) and
((vnet_resource_id is None) or (subnet_arm_resource_id is None)))):
raise CLIError("Incorrect usage : "
"--vnet, --subnet, --vnet-address-prefix, --subnet-address-prefix must be supplied together.")

server_result = firewall_id = subnet_id = None

# Populate desired parameters
Expand All @@ -70,16 +58,17 @@ def flexible_server_create(cmd, client, resource_group_name=None, server_name=No
server_name = server_name.lower()

# Handle Vnet scenario
if (subnet_arm_resource_id is not None) or (vnet_resource_id is not None):
subnet_id = prepare_vnet(cmd, server_name, vnet_resource_id, subnet_arm_resource_id, resource_group_name,
location, DELEGATION_SERVICE_NAME, vnet_address_prefix, subnet_address_prefix)
delegated_subnet_arguments = mysql_flexibleservers.models.DelegatedSubnetArguments(
subnet_arm_resource_id=subnet_id)
elif public_access is None and subnet_arm_resource_id is None and vnet_resource_id is None:
subnet_id = create_vnet(cmd, server_name, location, resource_group_name,
DELEGATION_SERVICE_NAME)
delegated_subnet_arguments = mysql_flexibleservers.models.DelegatedSubnetArguments(
subnet_arm_resource_id=subnet_id)
if public_access is None:
subnet_id = prepare_private_network(cmd,
resource_group_name,
server_name,
vnet=vnet_resource_id,
subnet=subnet_arm_resource_id,
location=location,
delegation_service_name=DELEGATION_SERVICE_NAME,
vnet_address_pref=vnet_address_prefix,
subnet_address_pref=subnet_address_prefix)
delegated_subnet_arguments = mysql_flexibleservers.models.DelegatedSubnetArguments(subnet_arm_resource_id=subnet_id)
else:
delegated_subnet_arguments = None

Expand Down Expand Up @@ -144,12 +133,6 @@ def flexible_server_restore(cmd, client, resource_group_name, server_name, sourc
else:
raise ValueError('The provided source-server {} is invalid.'.format(source_server))

try:
restore_point_in_time = datetime.strptime(restore_point_in_time, "%Y-%m-%dT%H:%M:%S.%f+00:00")
except ValueError:
restore_point_in_time = datetime.strptime(restore_point_in_time, "%Y-%m-%dT%H:%M:%S+00:00")
restore_point_in_time = restore_point_in_time.replace(tzinfo=dt.timezone.utc)

parameters = mysql_flexibleservers.models.Server(
source_server_id=source_server,
restore_point_in_time=restore_point_in_time,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# --------------------------------------------------------------------------------------------

# pylint: disable=unused-argument, line-too-long
import datetime as dt
from msrestazure.azure_exceptions import CloudError
from msrestazure.tools import resource_id, is_valid_resource_id, parse_resource_id # pylint: disable=import-error
from knack.log import get_logger
Expand All @@ -18,8 +17,8 @@
from .flexible_server_custom_common import user_confirmation
from ._flexible_server_util import generate_missing_parameters, resolve_poller, create_firewall_rule, \
parse_public_access_input, generate_password, parse_maintenance_window, get_postgres_list_skus_info, \
DEFAULT_LOCATION_PG, change_str_to_datetime
from .flexible_server_virtual_network import create_vnet, prepare_vnet
DEFAULT_LOCATION_PG
from .flexible_server_virtual_network import prepare_private_network
from .validators import pg_arguments_validator


Expand Down Expand Up @@ -57,16 +56,6 @@ def flexible_server_create(cmd, client,
raise CLIError("Incorrect usage : A combination of the parameters --subnet "
"and --public_access is invalid. Use either one of them.")

# When address space parameters are passed, the only valid combination is : --vnet, --subnet, --vnet-address-prefix, --subnet-address-prefix
# pylint: disable=too-many-boolean-expressions
if (vnet_address_prefix is not None) or (subnet_address_prefix is not None):
if (((vnet_address_prefix is not None) and (subnet_address_prefix is None)) or
((vnet_address_prefix is None) and (subnet_address_prefix is not None)) or
((vnet_address_prefix is not None) and (subnet_address_prefix is not None) and
((vnet_resource_id is None) or (subnet_arm_resource_id is None)))):
raise CLIError("Incorrect usage : "
"--vnet, --subnet, --vnet-address-prefix, --subnet-address-prefix must be supplied together.")

server_result = firewall_id = subnet_id = None

# Populate desired parameters
Expand All @@ -75,15 +64,17 @@ def flexible_server_create(cmd, client,
server_name = server_name.lower()

# Handle Vnet scenario
if (subnet_arm_resource_id is not None) or (vnet_resource_id is not None):
subnet_id = prepare_vnet(cmd, server_name, vnet_resource_id, subnet_arm_resource_id, resource_group_name, location, DELEGATION_SERVICE_NAME, vnet_address_prefix, subnet_address_prefix)
delegated_subnet_arguments = postgresql_flexibleservers.models.ServerPropertiesDelegatedSubnetArguments(
subnet_arm_resource_id=subnet_id)
elif public_access is None and subnet_arm_resource_id is None and vnet_resource_id is None:
subnet_id = create_vnet(cmd, server_name, location, resource_group_name,
DELEGATION_SERVICE_NAME)
delegated_subnet_arguments = postgresql_flexibleservers.models.ServerPropertiesDelegatedSubnetArguments(
subnet_arm_resource_id=subnet_id)
if public_access is None:
subnet_id = prepare_private_network(cmd,
resource_group_name,
server_name,
vnet=vnet_resource_id,
subnet=subnet_arm_resource_id,
location=location,
delegation_service_name=DELEGATION_SERVICE_NAME,
vnet_address_pref=vnet_address_prefix,
subnet_address_pref=subnet_address_prefix)
delegated_subnet_arguments = postgresql_flexibleservers.models.ServerPropertiesDelegatedSubnetArguments(subnet_arm_resource_id=subnet_id)
else:
delegated_subnet_arguments = None

Expand Down Expand Up @@ -148,9 +139,6 @@ def flexible_server_restore(cmd, client,
else:
source_server_id = source_server

restore_point_in_time = change_str_to_datetime(restore_point_in_time)
restore_point_in_time = restore_point_in_time.replace(tzinfo=dt.timezone.utc)

parameters = postgresql_flexibleservers.models.Server(
point_in_time_utc=restore_point_in_time,
source_server_name=source_server, # this should be the source server name, not id
Expand Down
Loading