def ChoiceToEnum(choice, enum_type, item_type='choice', valid_choices=None): """Converts the typed choice into an apitools Enum value.""" if choice is None: return None name = ChoiceToEnumName(choice) valid_choices = ( valid_choices or [arg_utils.EnumNameToChoice(n) for n in enum_type.names()]) try: return enum_type.lookup_by_name(name) except KeyError: raise arg_parsers.ArgumentTypeError( 'Invalid {item}: {selection}. Valid choices are: [{values}].'.format( item=item_type, selection=arg_utils.EnumNameToChoice(name), values=', '.join(c for c in sorted(valid_choices))))
def GenerateRegionChoiceToEnum(): # Return a map of region choice strings (for arguments) to region enum values. msg = GetMessagesModuleAlpha() enums = msg.WorkerPool.RegionsValueListEntryValuesEnum return { arg_utils.EnumNameToChoice(region_val.name): region_val for region_val in enums if region_val != enums.REGION_UNSPECIFIED }
def GenerateRegionChoiceToEnum(): # Return a map of region choice strings (for arguments) to region enum values. msg = GetMessagesModuleAlpha() enums = msg.WorkerPool.RegionsValueListEntryValuesEnum d = { arg_utils.EnumNameToChoice(region_val.name): region_val for region_val in enums if region_val != enums.REGION_UNSPECIFIED } return collections.OrderedDict(sorted(d.items(), key=lambda t: t[0]))
def GetBootDisk(): type_enum = None if args.IsSpecified('boot_disk_type'): instance_message = messages.Instance type_enum = arg_utils.ChoiceEnumMapper( arg_name='boot-disk-type', message_enum=instance_message.BootDiskTypeValueValuesEnum, include_filter=lambda x: 'UNSPECIFIED' not in x ).GetEnumForChoice(arg_utils.EnumNameToChoice(args.boot_disk_type)) return type_enum
def GetDiskEncryption(): instance_message = util.GetMessages().Instance type_enum = None if args.IsSpecified('disk_encryption'): type_enum = arg_utils.ChoiceEnumMapper( arg_name='disk-encryption', message_enum=instance_message.BootDiskTypeValueValuesEnum, include_filter=lambda x: 'UNSPECIFIED' not in x).GetEnumForChoice( arg_utils.EnumNameToChoice(args.disk_encryption)) return type_enum
def CreateAcceleratorConfigMessage(): accelerator_config = util.GetMessages().AcceleratorConfig type_enum = None if args.IsSpecified('accelerator_type'): type_enum = arg_utils.ChoiceEnumMapper( arg_name='accelerator-type', message_enum=accelerator_config.TypeValueValuesEnum, include_filter=lambda x: 'UNSPECIFIED' not in x).GetEnumForChoice( arg_utils.EnumNameToChoice(args.accelerator_type)) return accelerator_config( type=type_enum, coreCount=args.accelerator_core_count)
def CreateSetAcceleratorRequest(args, messages): """Create and return Accelerator update request.""" instance = GetInstanceResource(args).RelativeName() set_acc_request = messages.SetInstanceAcceleratorRequest() accelerator_config = messages.SetInstanceAcceleratorRequest if args.IsSpecified('accelerator_core_count'): set_acc_request.coreCount = args.accelerator_core_count if args.IsSpecified('accelerator_type'): type_enum = arg_utils.ChoiceEnumMapper( arg_name='accelerator-type', message_enum=accelerator_config.TypeValueValuesEnum, include_filter=lambda x: 'UNSPECIFIED' not in x).GetEnumForChoice( arg_utils.EnumNameToChoice(args.accelerator_type)) set_acc_request.type = type_enum return messages.NotebooksProjectsLocationsInstancesSetAcceleratorRequest( name=instance, setInstanceAcceleratorRequest=set_acc_request)
def Create(self, resource, description=None, network_type=None): parent = resource.Parent().RelativeName() network_id = resource.Name() network = self.messages.VmwareEngineNetwork(description=description) type_enum = arg_utils.ChoiceEnumMapper( arg_name='type', message_enum=self.messages.VmwareEngineNetwork.TypeValueValuesEnum, include_filter=lambda x: 'TYPE_UNSPECIFIED' not in x ).GetEnumForChoice(arg_utils.EnumNameToChoice(network_type)) network.type = type_enum request = self.messages.VmwareengineProjectsLocationsVmwareEngineNetworksCreateRequest( parent=parent, vmwareEngineNetwork=network, vmwareEngineNetworkId=network_id, requestId=networks_util.GetUniqueId()) return self.service.Create(request)
def testFilter_ExcludeRegex(self): def MyFilter(value): choice_re = re.compile(r'UNSPECIFIED') if choice_re.match(value): return False return True mapper = arg_utils.ChoiceEnumMapper( '--test_arg', self.large_enum, help_str='Auxilio aliis.', include_filter=MyFilter) expected_choices = set([ arg_utils.EnumNameToChoice(x) for x in self._LARGE_TEST_ENUM_DICT if MyFilter(x) ]) self.assertEqual(set(mapper.choices), expected_choices) self.assertIsNone(mapper.custom_mappings) self._AssertAllMappings('test_arg', mapper)
def SetUp(self): self.parser = util.ArgumentParser() if six.PY2: my_enum_type = b'MY_ENUM' large_enum_type = b'LARGE_ENUM' else: my_enum_type = 'MY_ENUM' large_enum_type = 'LARGE_ENUM' self.test_enum = _messages.Enum.def_enum(self._TEST_ENUM_DICT, my_enum_type) self.large_enum = _messages.Enum.def_enum(self._LARGE_TEST_ENUM_DICT, large_enum_type) self.string_mapping = { x: arg_utils.EnumNameToChoice(x) for x in self._TEST_ENUM_DICT } self.tuple_mapping = { x: (x.split('_')[-1].lower(), 'Test Enum Help for {}'.format(x)) for x in self._TEST_ENUM_DICT }
def Create(self, resource, description=None, vmware_engine_network=None, peer_network=None, peer_network_type=None, peer_mtu=None, export_custom_routes=True, import_custom_routes=True, export_custom_routes_with_public_ip=True, import_custom_routes_with_public_ip=True, exchange_subnet_routes=True): parent = '/'.join(resource.RelativeName().split('/')[:-2]) peering_id = resource.Name() peering = self.messages.NetworkPeering(description=description) peer_network_type_enum = arg_utils.ChoiceEnumMapper( arg_name='peer-network-type', message_enum=self.messages.NetworkPeering. PeerNetworkTypeValueValuesEnum, include_filter=lambda x: 'UNSPECIFIED' not in x).GetEnumForChoice( arg_utils.EnumNameToChoice(peer_network_type)) peering.peerNetworkType = peer_network_type_enum peering.vmwareEngineNetwork = vmware_engine_network peering.peerNetwork = peer_network if peer_mtu: peering.peer_mtu = peer_mtu peering.exportCustomRoutes = export_custom_routes peering.importCustomRoutes = import_custom_routes peering.exportCustomRoutesWithPublicIp = export_custom_routes_with_public_ip peering.importCustomRoutesWithPublicIp = import_custom_routes_with_public_ip peering.exchangeSubnetRoutes = exchange_subnet_routes request = self.messages.VmwareengineProjectsLocationsGlobalNetworkPeeringsCreateRequest( parent=parent, networkPeering=peering, networkPeeringId=peering_id, requestId=uuid.uuid4().hex) return self.service.Create(request)
def testEnumNameToChoice(self, enum_name, choice): self.assertEqual(choice, arg_utils.EnumNameToChoice(enum_name))