def Run(self, args): """Displays configuration and metadata about a Cloud SQL instance. Information such as instance name, IP address, region, the CA certificate and configuration settings will be displayed. Args: args: argparse.Namespace, The arguments that this command was invoked with. Returns: A DatabaseInstancePresentation object representing the instance resource if fetching the instance was successful. Raises: HttpException: A http error response was received while executing api request. ResourceNotFoundError: The SQL instance was not found. """ client = api_util.SqlClient(api_util.API_VERSION_DEFAULT) sql_client = client.sql_client sql_messages = client.sql_messages validate.ValidateInstanceName(args.instance) instance_ref = client.resource_parser.Parse( args.instance, params={'project': properties.VALUES.core.project.GetOrFail}, collection='sql.instances') try: instance = sql_client.instances.Get( sql_messages.SqlInstancesGetRequest( project=instance_ref.project, instance=instance_ref.instance)) # TODO(b/122660263): Remove when V1 instances are no longer supported. if instance_api_util.IsInstanceV1(sql_messages, instance): instance_command_util.ShowV1DeprecationWarning() return instance_api_util.DatabaseInstancePresentation(instance) except apitools_exceptions.HttpError as error: if error.status_code == six.moves.http_client.FORBIDDEN: raise exceptions.ResourceNotFoundError( 'There was no instance found at {} or you are not authorized to ' 'access it.'.format(instance_ref.RelativeName())) raise calliope_exceptions.HttpException(error)
def testGetDatabaseInstancesWithBatchSize(self): self.mocked_client.instances.List.Expect( self.messages.SqlInstancesListRequest( maxResults=10, project=self.Project(), ), self.messages.InstancesListResponse( items=data.GetDatabaseInstancesListOfOne(), kind='sql#instancesList', nextPageToken='10', )) self.mocked_client.instances.List.Expect( self.messages.SqlInstancesListRequest( pageToken='10', project=self.Project(), maxResults=10, ), self.messages.InstancesListResponse( items=data.GetDatabaseInstancesListOfTwo(), kind='sql#instancesList', nextPageToken=None, )) instance_list = data.GetDatabaseInstancesListOfOne( ) + data.GetDatabaseInstancesListOfTwo() instance_list[ 0].state = self.messages.DatabaseInstance.StateValueValuesEnum.RUNNABLE instance_list[ 0].settings.activationPolicy = self.messages.Settings.ActivationPolicyValueValuesEnum.NEVER expected_instance_list = [ instances_util.DatabaseInstancePresentation(instance) for instance in instance_list ] self.assertEqual( expected_instance_list, list( instances_util._BaseInstances.GetDatabaseInstances( batch_size=10)))
def testInstancesList(self): instances_list = [ instances.DatabaseInstancePresentation(instance) for instance in data.GetDatabaseInstancesListOfTwo() + data.GetDatabaseInstancesListOfOne() ] mocked_database_instances = self.StartObjectPatch( instances._BaseInstances, 'GetDatabaseInstances') mocked_database_instances.return_value = instances_list self.Run('sql instances list') mocked_database_instances.assert_called_once() # pylint:disable=line-too-long self.AssertOutputContains("""\ NAME DATABASE_VERSION LOCATION TIER PRIMARY_ADDRESS PRIVATE_ADDRESS STATUS testinstance MYSQL_5_5 us-central D0 - - RUNNABLE backupless-instance1 MYSQL_5_5 us-central1-a D1 - - RUNNABLE backupless-instance2 MYSQL_5_5 us-central D1 - - STOPPED """, normalize_space=True)