Beispiel #1
0
 def resource_parameter(self, dest, arg_group=None, required=True):
     """ Helper method to add the extra parameters needed to support specifying name or ID for target resources. """
     from azure.cli.command_modules.monitor.validators import get_target_resource_validator
     self.argument(dest,
                   options_list=['--{}'.format(dest)],
                   arg_group=arg_group,
                   required=required,
                   validator=get_target_resource_validator(
                       dest,
                       required,
                       preserve_resource_group_parameter=True),
                   help="Name or ID of the target resource.")
     self.extra('namespace',
                options_list=['--{}-namespace'.format(dest)],
                arg_group=arg_group,
                help="Target resource provider namespace.")
     self.extra('parent',
                options_list=['--{}-parent'.format(dest)],
                arg_group=arg_group,
                help="Target resource parent path, if applicable.")
     self.extra(
         'resource_type',
         options_list=['--{}-type'.format(dest)],
         arg_group=arg_group,
         help="Target resource type. Can also accept namespace/type format "
         "(Ex: 'Microsoft.Compute/virtualMachines)')")
Beispiel #2
0
 def resource_parameter_context(self,
                                dest,
                                arg_group=None,
                                required=True,
                                skip_validator=False):
     from azure.cli.command_modules.monitor.validators import get_target_resource_validator
     self.argument(dest,
                   options_list='--resource',
                   arg_group=arg_group,
                   required=required,
                   validator=get_target_resource_validator(dest, required)
                   if not skip_validator else None,
                   help="Name or ID of the target resource.")
     self.extra('namespace',
                options_list='--resource-namespace',
                arg_group=arg_group,
                help="Target resource provider namespace.")
     self.extra('parent',
                options_list='--resource-parent',
                arg_group=arg_group,
                help="Target resource parent path, if applicable.")
     self.extra(
         'resource_type',
         options_list='--resource-type',
         arg_group=arg_group,
         help="Target resource type. Can also accept namespace/type format "
         "(Ex: 'Microsoft.Compute/virtualMachines)')")
     self.extra('resource_group_name',
                options_list=['--resource-group', '-g'],
                arg_group=arg_group)
Beispiel #3
0
def register_resource_parameter(command, dest, arg_group=None, required=True):
    """ Helper method to add the extra parameters needed to support specifying name or ID
        for target resources. """
    register_cli_argument(command,
                          dest,
                          options_list=['--{}'.format(dest)],
                          arg_group=arg_group,
                          required=required,
                          validator=get_target_resource_validator(
                              dest, required),
                          help="Name or ID of the target resource.")
    register_extra_cli_argument(command,
                                'namespace',
                                options_list=['--{}-namespace'.format(dest)],
                                arg_group=arg_group,
                                help="Target resource provider namespace.")
    register_extra_cli_argument(
        command,
        'parent',
        options_list=['--{}-parent'.format(dest)],
        arg_group=arg_group,
        help="Target resource parent path, if applicable.")
    register_extra_cli_argument(
        command,
        'resource_type',
        options_list=['--{}-type'.format(dest)],
        arg_group=arg_group,
        help=
        "Target resource type. Can also accept namespace/type format (Ex: 'Microsoft.Compute/virtualMachines)')"
    )
Beispiel #4
0
def register_resource_parameter_context(context, dest, arg_group=None, required=True):
    context.register(dest, options_list='--resource', arg_group=arg_group, required=required,
                     validator=get_target_resource_validator(dest, required), help="Name or ID of the target resource.")
    context.extra('namespace', options_list='--resource-namespace', arg_group=arg_group,
                  help="Target resource provider namespace.")
    context.extra('parent', options_list='--resource-parent', arg_group=arg_group,
                  help="Target resource parent path, if applicable.")
    context.extra('resource_type', options_list='--resource-type', arg_group=arg_group,
                  help="Target resource type. Can also accept namespace/type format "
                       "(Ex: 'Microsoft.Compute/virtualMachines)')")
    context.extra('resource_group_name', options_list=('--resource-group', '-g'), arg_group='Target Resource')
    def test_monitor_resource_id(self):
        from azure.cli.command_modules.monitor.validators import get_target_resource_validator
        from azure.cli.core.mock import DummyCli

        cmd = mock.MagicMock()
        cmd.cli_ctx = DummyCli()
        validator = get_target_resource_validator('name_or_id', True)

        id = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Compute/' \
             'virtualMachines/vm1'

        # must supply name or ID
        ns = self._build_namespace()
        with self.assertRaises(CLIError):
            validator(cmd, ns)

        # must only supply ID or name parameters
        ns = self._build_namespace(id, 'my-rg', 'blahblah', 'stuff')
        with self.assertRaises(CLIError):
            validator(cmd, ns)

        # error on invalid ID
        ns = self._build_namespace('bad-id')
        with self.assertRaises(CLIError):
            validator(cmd, ns)

        # allow Provider/Type syntax (same as resource commands)
        ns = self._build_namespace('vm1', 'my-rg', None, None,
                                   'Microsoft.Compute/virtualMachines')
        validator(cmd, ns)
        self.assertEqual(ns.name_or_id, id)

        # allow Provider and Type separate
        ns = self._build_namespace('vm1', 'my-rg', 'Microsoft.Compute', None,
                                   'virtualMachines')
        validator(cmd, ns)
        self.assertEqual(ns.name_or_id, id)

        # verify works with parent
        id = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Compute/' \
             'fakeType/type1/anotherFakeType/type2/virtualMachines/vm1'
        ns = self._build_namespace('vm1', 'my-rg', 'Microsoft.Compute',
                                   'fakeType/type1/anotherFakeType/type2',
                                   'virtualMachines')
        validator(cmd, ns)
        self.assertEqual(ns.name_or_id, id)

        # verify extra parameters are removed
        self.assertFalse(hasattr(ns, 'resource_name'))
        self.assertFalse(hasattr(ns, 'namespace'))
        self.assertFalse(hasattr(ns, 'parent'))
        self.assertFalse(hasattr(ns, 'resource_type'))
    def test_monitor_resource_id(self):
        from azure.cli.command_modules.monitor.validators import get_target_resource_validator
        from azure.cli.core.mock import DummyCli

        cmd = mock.MagicMock()
        cmd.cli_ctx = DummyCli()
        validator = get_target_resource_validator('name_or_id', True)

        id = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Compute/' \
             'virtualMachines/vm1'

        # must supply name or ID
        ns = self._build_namespace()
        with self.assertRaises(CLIError):
            validator(cmd, ns)

        # must only supply ID or name parameters
        ns = self._build_namespace(id, 'my-rg', 'blahblah', 'stuff')
        with self.assertRaises(CLIError):
            validator(cmd, ns)

        # error on invalid ID
        ns = self._build_namespace('bad-id')
        with self.assertRaises(CLIError):
            validator(cmd, ns)

        # allow Provider/Type syntax (same as resource commands)
        ns = self._build_namespace('vm1', 'my-rg', None, None, 'Microsoft.Compute/virtualMachines')
        validator(cmd, ns)
        self.assertEqual(ns.name_or_id, id)

        # allow Provider and Type separate
        ns = self._build_namespace('vm1', 'my-rg', 'Microsoft.Compute', None, 'virtualMachines')
        validator(cmd, ns)
        self.assertEqual(ns.name_or_id, id)

        # verify works with parent
        id = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Compute/' \
             'fakeType/type1/anotherFakeType/type2/virtualMachines/vm1'
        ns = self._build_namespace('vm1', 'my-rg', 'Microsoft.Compute', 'fakeType/type1/anotherFakeType/type2',
                                   'virtualMachines')
        validator(cmd, ns)
        self.assertEqual(ns.name_or_id, id)

        # verify extra parameters are removed
        self.assertFalse(hasattr(ns, 'resource_name'))
        self.assertFalse(hasattr(ns, 'namespace'))
        self.assertFalse(hasattr(ns, 'parent'))
        self.assertFalse(hasattr(ns, 'resource_type'))
Beispiel #7
0
 def resource_parameter(self, dest, arg_group=None, required=True, skip_validator=False, alias='resource',
                        preserve_resource_group_parameter=False):
     from azure.cli.command_modules.monitor.validators import get_target_resource_validator
     self.argument(dest, options_list='--{}'.format(alias), arg_group=arg_group, required=required,
                   validator=get_target_resource_validator(
                       dest, required, alias=alias,
                       preserve_resource_group_parameter=preserve_resource_group_parameter) if not skip_validator else None,
                   help="Name or ID of the target resource.")
     self.extra('namespace', options_list='--{}-namespace'.format(alias), arg_group=arg_group,
                help="Target resource provider namespace.")
     self.extra('parent', options_list='--{}-parent'.format(alias), arg_group=arg_group,
                help="Target resource parent path, if applicable.")
     self.extra('resource_type', options_list='--{}-type'.format(alias), arg_group=arg_group,
                help="Target resource type. Can also accept namespace/type format (Ex: 'Microsoft.Compute/virtualMachines')")
     self.extra('resource_group_name', options_list=['--resource-group', '-g'], arg_group=arg_group)
Beispiel #8
0
def register_resource_parameter(command, dest, arg_group=None, required=True):
    """ Helper method to add the extra parameters needed to support specifying name or ID
        for target resources. """
    register_cli_argument(command, dest, options_list=['--{}'.format(dest)], arg_group=arg_group, required=required, validator=get_target_resource_validator(dest, required), help="Name or ID of the target resource.")
    register_extra_cli_argument(command, 'namespace', options_list=['--{}-namespace'.format(dest)], arg_group=arg_group, help="Target resource provider namespace.")
    register_extra_cli_argument(command, 'parent', options_list=['--{}-parent'.format(dest)], arg_group=arg_group, help="Target resource parent path, if applicable.")
    register_extra_cli_argument(command, 'resource_type', options_list=['--{}-type'.format(dest)], arg_group=arg_group, help="Target resource type. Can also accept namespace/type format (Ex: 'Microsoft.Compute/virtualMachines)')")