def GenerateResourceSpec(self, resource_collection=None): """Creates a concept spec for the resource argument. Args: resource_collection: registry.APICollection, The collection that the resource arg must be for. This simply does some extra validation to ensure that resource arg is for the correct collection and api_version. If not specified, the resource arg will just be loaded based on the collection it specifies. Returns: concepts.ResourceSpec, The generated specification that can be added to a parser. """ if self.is_parent_resource and resource_collection: parent_collection, _, _ = resource_collection.full_name.rpartition( '.') resource_collection = registry.GetAPICollection( parent_collection, api_version=self._api_version) if resource_collection and not self.override_resource_collection: # Validate that the expected collection matches what was registered for # the resource argument specification. if resource_collection.full_name != self._full_collection_name: raise util.InvalidSchemaError( 'Collection names do not match for resource argument specification ' '[{}]. Expected [{}], found [{}]'.format( self.name, resource_collection.full_name, self._full_collection_name)) if (self._api_version and self._api_version != resource_collection.api_version): raise util.InvalidSchemaError( 'API versions do not match for resource argument specification ' '[{}]. Expected [{}], found [{}]'.format( self.name, resource_collection.api_version, self._api_version)) else: # No required collection, just load whatever the resource arg declared # for itself. resource_collection = registry.GetAPICollection( self._full_collection_name, api_version=self._api_version) attributes = concepts.ParseAttributesFromData( self._attribute_data, resource_collection.detailed_params) return concepts.ResourceSpec( resource_collection.full_name, resource_name=self.name, api_version=resource_collection.api_version, disable_auto_completers=self._disable_auto_completers, plural_name=self._plural_name, **{ attribute.parameter_name: attribute for attribute in attributes })
def FromYaml(cls, yaml_data, api_version=None): """Constructs an instance of ResourceSpec from yaml data. Args: yaml_data: dict, the parsed data from a resources.yaml file under command_lib/. api_version: string, overrides the default version in the resource registry if provided. Returns: A ResourceSpec object. """ if not yaml_data: return None collection = registry.GetAPICollection(yaml_data['collection'], api_version=api_version) attributes = ParseAttributesFromData(yaml_data.get('attributes'), collection.detailed_params) return cls( resource_collection=collection.full_name, resource_name=yaml_data['name'], api_version=collection.api_version, disable_auto_completers=yaml_data['disable_auto_completers'], plural_name=yaml_data.get('plural_name'), **{ attribute.parameter_name: attribute for attribute in attributes })
def MockCRUDMethods(self, *collections, **kwargs): """Mocks out the foo.projects.clusters to have CRUD methods.""" parent_collections = list(collections) if kwargs.get('mock_parents', True): for c in collections: full_collection_name, is_relative, api_version = ( self._ReadCollectionTuple(c)) while full_collection_name.count('.') > 1: full_collection_name = full_collection_name.rsplit('.', 1)[0] parent_collections.append( (full_collection_name, is_relative, api_version)) self.MockCollections(*parent_collections) methods = {} for c in collections: full_collection_name, is_relative, api_version = ( self._ReadCollectionTuple(c)) crud_methods = self._GetCRUDMethodsForCollection( full_collection_name, is_relative, api_version=api_version) get_method, list_method, create_method, patch_method = crud_methods collection = registry.GetAPICollection(full_collection_name, api_version) methods[full_collection_name] = methods.get( full_collection_name, []) + [ registry.APIMethod(self._CreateMockService(get_method), 'Get', collection, get_method), registry.APIMethod(self._CreateMockService(patch_method), 'Patch', collection, patch_method), registry.APIMethod(self._CreateMockService(create_method), 'Create', collection, create_method), registry.APIMethod(self._CreateMockService(list_method), 'List', collection, list_method) ] methods_mock = self.StartObjectPatch(registry, 'GetMethods') methods_mock.side_effect = lambda n, **kwargs: methods.get(n, [])
def testCompleterForAttribute_DifferentVersions(self): collections = [ ('example.projects.shelves.books', True), ('example.projects.shelves', True), ('example.projects', True), ('example.projects.shelves.books', True, 'v1beta1'), ('example.projects.shelves', True, 'v1beta1'), ('example.projects', True, 'v1beta1')] self.MockCRUDMethods(*collections) book_collection = registry.GetAPICollection( 'example.projects.shelves.books', 'v1beta1') resource_spec = util.GetBookResource(api_version='v1beta1') expected_completer = completers.ResourceArgumentCompleter( resource_spec, book_collection, registry.GetMethod('example.projects.shelves.books', 'list', api_version='v1beta1'), param='booksId', static_params={}) completer = completers.CompleterForAttribute(self.resource_spec, 'book')() self.assertEqual(expected_completer, completer) expected_completer = completers.ResourceArgumentCompleter( resource_spec, registry.GetAPICollection('example.projects.shelves', 'v1beta1'), registry.GetMethod('example.projects.shelves', 'list', api_version='v1beta1'), param='shelvesId', static_params={}) completer = completers.CompleterForAttribute(self.resource_spec, 'shelf')() self.assertEqual(expected_completer, completer) expected_completer = completers.ResourceArgumentCompleter( self.resource_spec, registry.GetAPICollection('example.projects', 'v1beta1'), registry.GetMethod('example.projects', 'list', api_version='v1beta1'), param='projectsId', static_params={}) completer = completers.CompleterForAttribute( self.resource_spec, 'project')() self.assertEqual(expected_completer, completer)
def testGetAPICollection(self): # Flat path resource. c = registry.GetAPICollection('compute.instances', 'v1') self.assertEqual(c.api_name, 'compute') self.assertEqual(c.api_version, 'v1') self.assertEqual(c.name, 'instances') self.assertEqual(c.full_name, 'compute.instances') self.assertEqual(c.base_url, 'https://compute.googleapis.com/compute/v1/') self.assertEqual( c.docs_url, 'https://developers.google.com/compute/docs/reference/latest/') self.assertEqual( c.detailed_path, 'projects/{project}/zones/{zone}/instances/{instance}') self.assertEqual(c.detailed_params, ['project', 'zone', 'instance']) self.assertEqual( c.path, 'projects/{project}/zones/{zone}/instances/{instance}') self.assertEqual(c.params, ['project', 'zone', 'instance']) # Atomic name resource. c = registry.GetAPICollection('pubsub.projects.topics', 'v1') self.assertEqual(c.api_name, 'pubsub') self.assertEqual(c.api_version, 'v1') self.assertEqual(c.name, 'projects.topics') self.assertEqual(c.full_name, 'pubsub.projects.topics') self.assertEqual(c.base_url, 'https://pubsub.googleapis.com/v1/') self.assertEqual(c.docs_url, 'https://cloud.google.com/pubsub/docs') self.assertEqual(c.detailed_path, 'projects/{projectsId}/topics/{topicsId}') self.assertEqual(c.detailed_params, ['projectsId', 'topicsId']) self.assertEqual(c.path, '{+topic}') self.assertEqual(c.params, ['topic']) with self.assertRaises(registry.UnknownAPIError): registry.GetAPICollections('junk') with self.assertRaises(registry.UnknownCollectionError): registry.GetAPICollection('compute.junk') with self.assertRaises(registry.UnknownAPIVersionError): registry.GetAPICollection('compute.instances', 'junk')
def GenerateResourceSpec(self, resource_collection=None): """Creates a concept spec for the resource argument. Args: resource_collection: registry.APICollection, The collection that the resource arg must be for. This simply does some extra validation to ensure that resource arg is for the correct collection and api_version. If not specified, the resource arg will just be loaded based on the collection it specifies. Returns: concepts.ResourceSpec, The generated specification that can be added to a parser. """ if resource_collection: # Validate that the expected collection matches what was registered for # the resource argument specification. if resource_collection.full_name != self._full_collection_name: raise util.InvalidSchemaError( 'Collection names do not match for resource argument specification ' '[{}]. Expected [{}], found [{}]'.format( self.name, resource_collection.full_name, self._full_collection_name)) if (self._api_version and self._api_version != resource_collection.api_version): raise util.InvalidSchemaError( 'API versions do not match for resource argument specification ' '[{}]. Expected [{}], found [{}]'.format( self.name, resource_collection.api_version, self._api_version)) else: # No required collection, just load whatever the resource arg declared # for itself. resource_collection = registry.GetAPICollection( self._full_collection_name, api_version=self._api_version) return self._GenerateResourceSpec(resource_collection.full_name, resource_collection.api_version, resource_collection.detailed_params)
def Run(self, args): return registry.GetAPICollection(args.collection, api_version=args.api_version)