Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VNET extension #176

Merged
merged 22 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e6b2d2f
VNET extension
rahulgouthamDOTgs May 10, 2018
e25f366
fixing indentation
rahulgouthamDOTgs May 10, 2018
11bc3b1
fixing more flake8 reported issues
rahulgouthamDOTgs May 11, 2018
e06f1c1
Fixing Review comments and formatting issues.
rahulgouthamDOTgs May 11, 2018
f15a7cb
Fixing some more issues with source static and bug fixes
rahulgouthamDOTgs May 11, 2018
bebd263
Merge branch 'master' into master
rahulgouthamDOTgs May 11, 2018
3e1ff25
Renaming _client_factory to _client_vnet_factory
rahulgouthamDOTgs May 11, 2018
8f9dad9
Reverting the last change
rahulgouthamDOTgs May 11, 2018
eb80f8b
Renaming to azext_rdbms_vnet
rahulgouthamDOTgs May 11, 2018
b1babff
More formatting issue fix
rahulgouthamDOTgs May 11, 2018
81a8a29
adding code owners
rahulgouthamDOTgs May 11, 2018
e5ef37e
Adding my email id
rahulgouthamDOTgs May 11, 2018
79d3d6a
Merge branch 'master' into master
rohit-joy May 11, 2018
0cfda9e
Merge branch 'master' into master
rohit-joy May 14, 2018
1bd5236
Merge branch 'master' into master
rohit-joy May 14, 2018
bad76af
Merge branch 'master' into master
rohit-joy May 17, 2018
9876498
Merge branch 'master' into master
rohit-joy May 22, 2018
8504656
Adding extension to index.json
rahulgouthamDOTgs May 22, 2018
6f09d16
Merge branch 'master' of https://github.com/rahulgouthamDOTgs/azure-c…
rahulgouthamDOTgs May 22, 2018
592fba6
Merge branch 'master' into master
rahulgouthamDOTgs May 22, 2018
ec79eeb
fixing index-verify
rahulgouthamDOTgs May 22, 2018
233b695
Merge branch 'master' of https://github.com/rahulgouthamDOTgs/azure-c…
rahulgouthamDOTgs May 22, 2018
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
32 changes: 32 additions & 0 deletions src/rdbms_vnet/azext_rdbms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core import AzCommandsLoader

import azext_rdbms._help # pylint: disable=unused-import


class RdbmsExtCommandsLoader(AzCommandsLoader):

def __init__(self, cli_ctx=None):
from azure.cli.core.commands import CliCommandType
from azext_rdbms._util import RdbmsArgumentContext
rdbms_custom = CliCommandType(operations_tmpl='azext_rdbms.custom#{}')
super(RdbmsExtCommandsLoader, self).__init__(cli_ctx=cli_ctx,
min_profile='2017-03-10-profile',
custom_command_type=rdbms_custom,
argument_context_cls=RdbmsArgumentContext)

def load_command_table(self, args):
from azext_rdbms.commands import load_command_table
load_command_table(self, args)
return self.command_table

def load_arguments(self, command):
from azext_rdbms._params import load_arguments
load_arguments(self, command)


COMMAND_LOADER_CLS = RdbmsExtCommandsLoader
80 changes: 80 additions & 0 deletions src/rdbms_vnet/azext_rdbms/_client_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands.client_factory import get_mgmt_service_client

# CLIENT FACTORIES

RM_URI_OVERRIDE = 'AZURE_CLI_RDBMS_RM_URI'
SUB_ID_OVERRIDE = 'AZURE_CLI_RDBMS_SUB_ID'
CLIENT_ID = 'AZURE_CLIENT_ID'
TENANT_ID = 'AZURE_TENANT_ID'
CLIENT_SECRET = 'AZURE_CLIENT_SECRET'


def get_mysql_management_client(cli_ctx, **_):
from os import getenv
from azext_rdbms.mysql import MySQLManagementClient

# Allow overriding resource manager URI using environment variable
# for testing purposes. Subscription id is also determined by environment
# variable.
rm_uri_override = getenv(RM_URI_OVERRIDE)
if rm_uri_override:
client_id = getenv(CLIENT_ID)
if client_id:
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id=client_id,
secret=getenv(CLIENT_SECRET),
tenant=getenv(TENANT_ID))
else:
from msrest.authentication import Authentication # pylint: disable=import-error
credentials = Authentication()

return MySQLManagementClient(
subscription_id=getenv(SUB_ID_OVERRIDE),
base_url=rm_uri_override,
credentials=credentials)
else:
# Normal production scenario.
return get_mgmt_service_client(cli_ctx, MySQLManagementClient)


def get_postgresql_management_client(cli_ctx, **_):
from os import getenv
from azext_rdbms.postgresql import PostgreSQLManagementClient

# Allow overriding resource manager URI using environment variable
# for testing purposes. Subscription id is also determined by environment
# variable.
rm_uri_override = getenv(RM_URI_OVERRIDE)
if rm_uri_override:
client_id = getenv(CLIENT_ID)
if client_id:
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id=client_id,
secret=getenv(CLIENT_SECRET),
tenant=getenv(TENANT_ID))
else:
from msrest.authentication import Authentication # pylint: disable=import-error
credentials = Authentication()

return PostgreSQLManagementClient(
subscription_id=getenv(SUB_ID_OVERRIDE),
base_url=rm_uri_override,
credentials=credentials)
else:
# Normal production scenario.
return get_mgmt_service_client(cli_ctx, PostgreSQLManagementClient)


def cf_mysql_virtual_network_rules_operations(cli_ctx, _):
return get_mysql_management_client(cli_ctx).virtual_network_rules


def cf_postgres_virtual_network_rules_operations(cli_ctx, _):
return get_postgresql_management_client(cli_ctx).virtual_network_rules
32 changes: 32 additions & 0 deletions src/rdbms_vnet/azext_rdbms/_help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from knack.help_files import helps

# pylint: disable=line-too-long


def add_helps(command_group, server_type):
helps['{} server vnet-rule'.format(command_group)] = """
type: group
short-summary: Manage a server's virtual network rules.
"""
helps['{} server vnet-rule update'.format(command_group)] = """
type: command
short-summary: Update a virtual network rule.
"""
helps['{} server vnet-rule create'.format(command_group)] = """
type: command
short-summary: Create a virtual network rule to allows access to a server.
examples:
- name: Create a virtual network rule by providing the subnet id.
text: az sql server vnet-rule create --subnet /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rgName/providers/Microsoft.Network/virtualNetworks/vnetName/subnets/subnetName
- name: Create a vnet rule by providing the vnet and subnet name. The subnet id is created by taking the resource group name and subscription id of the server.
text: az {} vnet-rule create --subnet subnetName --vnet-name vnetName
""".format(command_group)


add_helps("mysql", "MySQL")
add_helps("postgres", "PostgreSQL")
34 changes: 34 additions & 0 deletions src/rdbms_vnet/azext_rdbms/_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint: disable=line-too-long

from azure.cli.core.commands.parameters import get_three_state_flag, get_resource_name_completion_list, tags_type, get_location_type, get_enum_type # pylint: disable=line-too-long
from azext_rdbms import mysql, postgresql
from azure.cli.command_modules.sql._validators import validate_subnet

from azext_rdbms.validators import configuration_value_validator


def load_arguments(self, _): # pylint: disable=too-many-statements

for scope in ['mysql server vnet-rule', 'postgres server vnet-rule']:
with self.argument_context(scope) as c:
c.argument('server_name', options_list=['--server-name', '-s'])
c.argument('virtual_network_rule_name', options_list=['--name', '-n'], id_part='child_name_1', help='The name of the vnet rule.')
c.argument('virtual_network_subnet_id', options_list=['--subnet'], help='Name or ID of the subnet that allows access to an Azure Postgres Server. If subnet name is provided, --vnet-name must be provided.')
c.argument('ignore_missing_vnet_service_endpoint', options_list=['--ignore-missing-endpoint', '-i'], help='Create vnet rule before virtual network has vnet service endpoint enabled', arg_type=get_three_state_flag())

with self.argument_context('postgres server vnet-rule create') as c:
c.extra('vnet_name', options_list=['--vnet-name'], help='The virtual network name', validator=validate_subnet)

with self.argument_context('postgres server vnet-rule update') as c:
c.extra('vnet_name', options_list=['--vnet-name'], help='The virtual network name', validator=validate_subnet)

with self.argument_context('mysql server vnet-rule create') as c:
c.extra('vnet_name', options_list=['--vnet-name'], help='The virtual network name', validator=validate_subnet)

with self.argument_context('mysql server vnet-rule update') as c:
c.extra('vnet_name', options_list=['--vnet-name'], help='The virtual network name', validator=validate_subnet)
32 changes: 32 additions & 0 deletions src/rdbms_vnet/azext_rdbms/_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import AzArgumentContext


class RdbmsArgumentContext(AzArgumentContext): # pylint: disable=too-few-public-methods

def __init__(self, command_loader, scope, **kwargs): # pylint: disable=unused-argument
super(RdbmsArgumentContext, self).__init__(command_loader, scope)
self.validators = []

def expand(self, dest, model_type, group_name=None, patches=None):
super(RdbmsArgumentContext, self).expand(dest, model_type, group_name, patches)

from knack.arguments import ignore_type

# Remove the validator and store it into a list
arg = self.command_loader.argument_registry.arguments[self.command_scope].get(dest, None)
if not arg: # when the argument context scope is N/A
return

self.validators.append(arg.settings['validator'])
if dest == 'parameters':
from .validators import get_combined_validator
self.argument(dest,
arg_type=ignore_type,
validator=get_combined_validator(self.validators))
else:
self.argument(dest, arg_type=ignore_type, validator=None)
3 changes: 3 additions & 0 deletions src/rdbms_vnet/azext_rdbms/azext_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"azext.minCliCoreVersion": "2.0.24"
}
46 changes: 46 additions & 0 deletions src/rdbms_vnet/azext_rdbms/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import CliCommandType

from azext_rdbms._client_factory import (
cf_mysql_virtual_network_rules_operations,
cf_postgres_virtual_network_rules_operations)


# pylint: disable=too-many-locals, too-many-statements, line-too-long
def load_command_table(self, _):

rdbms_custom = CliCommandType(operations_tmpl='azext_rdbms.custom#{}')

mysql_vnet_sdk = CliCommandType(
operations_tmpl='azext_rdbms.mysql.operations.virtual_network_rules_operations#VirtualNetworkRulesOperations.{}',
client_arg_name='self',
client_factory=cf_mysql_virtual_network_rules_operations
)

postgres_vnet_sdk = CliCommandType(
operations_tmpl='azext_rdbms.postgresql.operations.virtual_network_rules_operations#VirtualNetworkRulesOperations.{}',
client_arg_name='self',
client_factory=cf_postgres_virtual_network_rules_operations
)
with self.command_group('mysql server vnet-rule', mysql_vnet_sdk) as g:
g.command('create', 'create_or_update')
g.command('delete', 'delete', confirmation=True)
g.command('show', 'get')
g.command('list', 'list_by_server')
# g.generic_update_command('update')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the commented lines.

g.generic_update_command('update', getter_name='_custom_vnet_update_get', getter_type=rdbms_custom,
setter_name='_custom_vnet_update_set', setter_type=rdbms_custom, setter_arg_name='parameters')

with self.command_group('postgres server vnet-rule', postgres_vnet_sdk) as g:
g.command('create', 'create_or_update')
g.command('delete', 'delete', confirmation=True)
g.command('show', 'get')
g.command('list', 'list_by_server')
# g.generic_update_command('update')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the commented lines.

g.generic_update_command('update',
getter_name='_custom_vnet_update_get', getter_type=rdbms_custom,
setter_name='_custom_vnet_update_set', setter_type=rdbms_custom, setter_arg_name='parameters')
19 changes: 19 additions & 0 deletions src/rdbms_vnet/azext_rdbms/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint: disable=unused-argument

from azure.cli.core.commands.client_factory import get_subscription_id
from msrestazure.tools import resource_id, is_valid_resource_id, parse_resource_id # pylint: disable=import-error
from azext_rdbms.mysql.operations.servers_operations import ServersOperations
from ._client_factory import get_mysql_management_client, get_postgresql_management_client


def _custom_vnet_update_get(client, resource_group_name, server_name, virtual_network_rule_name):
return client.get(resource_group_name, server_name, virtual_network_rule_name)


def _custom_vnet_update_set(client, resource_group_name, server_name, virtual_network_rule_name, virtual_network_subnet_id, ignore_missing_vnet_service_endpoint=None, parameters=None):
return client.create_or_update(resource_group_name, server_name, virtual_network_rule_name, virtual_network_subnet_id, ignore_missing_vnet_service_endpoint)
18 changes: 18 additions & 0 deletions src/rdbms_vnet/azext_rdbms/mysql/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------

from .my_sql_management_client import MySQLManagementClient
from .version import VERSION

__all__ = ['MySQLManagementClient']

__version__ = VERSION

90 changes: 90 additions & 0 deletions src/rdbms_vnet/azext_rdbms/mysql/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------

from .proxy_resource import ProxyResource
from .tracked_resource import TrackedResource
from .storage_profile import StorageProfile
from .server_properties_for_create import ServerPropertiesForCreate
from .server_properties_for_default_create import ServerPropertiesForDefaultCreate
from .server_properties_for_restore import ServerPropertiesForRestore
from .server_properties_for_geo_restore import ServerPropertiesForGeoRestore
from .sku import Sku
from .server import Server
from .server_for_create import ServerForCreate
from .server_update_parameters import ServerUpdateParameters
from .firewall_rule import FirewallRule
from .virtual_network_rule import VirtualNetworkRule
from .database import Database
from .configuration import Configuration
from .operation_display import OperationDisplay
from .operation import Operation
from .operation_list_result import OperationListResult
from .log_file import LogFile
from .performance_tier_service_level_objectives import PerformanceTierServiceLevelObjectives
from .performance_tier_properties import PerformanceTierProperties
from .name_availability_request import NameAvailabilityRequest
from .name_availability import NameAvailability
from .server_paged import ServerPaged
from .firewall_rule_paged import FirewallRulePaged
from .virtual_network_rule_paged import VirtualNetworkRulePaged
from .database_paged import DatabasePaged
from .configuration_paged import ConfigurationPaged
from .log_file_paged import LogFilePaged
from .performance_tier_properties_paged import PerformanceTierPropertiesPaged
from .my_sql_management_client_enums import (
ServerVersion,
SslEnforcementEnum,
ServerState,
GeoRedundantBackup,
SkuTier,
VirtualNetworkRuleState,
OperationOrigin,
)

__all__ = [
'ProxyResource',
'TrackedResource',
'StorageProfile',
'ServerPropertiesForCreate',
'ServerPropertiesForDefaultCreate',
'ServerPropertiesForRestore',
'ServerPropertiesForGeoRestore',
'Sku',
'Server',
'ServerForCreate',
'ServerUpdateParameters',
'FirewallRule',
'VirtualNetworkRule',
'Database',
'Configuration',
'OperationDisplay',
'Operation',
'OperationListResult',
'LogFile',
'PerformanceTierServiceLevelObjectives',
'PerformanceTierProperties',
'NameAvailabilityRequest',
'NameAvailability',
'ServerPaged',
'FirewallRulePaged',
'VirtualNetworkRulePaged',
'DatabasePaged',
'ConfigurationPaged',
'LogFilePaged',
'PerformanceTierPropertiesPaged',
'ServerVersion',
'SslEnforcementEnum',
'ServerState',
'GeoRedundantBackup',
'SkuTier',
'VirtualNetworkRuleState',
'OperationOrigin',
]
Loading