def _MakeEntityFilter(namespaces, kinds):
    """Creates an entity filter for the given namespaces and kinds.

  Args:
    namespaces: a string list of the namespaces to include in the filter.
    kinds: a string list of the kinds to include in the filter.
  Returns:
    a GetMessages().EntityFilter (proto).
  """
    namespaces = namespaces or []
    namespaces = [_TransformNamespaceId(namespace) for namespace in namespaces]

    return util.GetMessages().GoogleDatastoreAdminV1EntityFilter(
        kinds=kinds or [], namespaceIds=namespaces)
def DeleteIndexes(project_id, indexes_to_delete_ids):
    """Sends the index deletion requests."""
    cnt = 0
    detail_message = None
    with progress_tracker.ProgressTracker(
            '.',
            autotick=False,
            detail_message_callback=lambda: detail_message,
    ) as pt:
        for index_id in indexes_to_delete_ids:
            GetIndexesService().Delete(
                util.GetMessages().DatastoreProjectsIndexesDeleteRequest(
                    projectId=project_id, indexId=index_id))
            cnt = cnt + 1
            detail_message = '{0:.0%}'.format(cnt / len(indexes_to_delete_ids))
            pt.Tick()
def BuildIndexProto(ancestor, kind, project_id, properties):
    """Builds and returns a GoogleDatastoreAdminV1Index."""
    messages = util.GetMessages()
    proto = messages.GoogleDatastoreAdminV1Index()
    proto.projectId = project_id
    proto.kind = kind
    proto.ancestor = ancestor
    proto.state = CREATING
    props = []
    for prop in properties:
        prop_proto = messages.GoogleDatastoreAdminV1IndexedProperty()
        prop_proto.name = prop.name
        if prop.direction == 'asc':
            prop_proto.direction = ASCENDING
        else:
            prop_proto.direction = DESCENDING
        props.append(prop_proto)
    proto.properties = props
    return proto
 def _IndexDefinitionToApiMessage(self, project_id, index_definition):
   """Converts an index definition to GoogleDatastoreAdminV1Index."""
   messages = util.GetMessages()
   proto = messages.GoogleDatastoreAdminV1Index()
   proto.projectId = project_id
   proto.state = index_api.CREATING
   proto.kind = index_definition.kind
   if index_definition.ancestor:
     proto.ancestor = index_api.ALL_ANCESTORS
   else:
     proto.ancestor = index_api.NO_ANCESTOR
   props = []
   if index_definition.properties is not None:
     for prop in index_definition.properties:
       prop_proto = messages.GoogleDatastoreAdminV1IndexedProperty()
       prop_proto.name = prop.name
       props.append(prop_proto)
       if prop.IsAscending():
         prop_proto.direction = index_api.ASCENDING
       else:
         prop_proto.direction = index_api.DESCENDING
   proto.properties = props
   return proto
def GetImportEntitiesRequest(project,
                             input_url,
                             kinds=None,
                             namespaces=None,
                             labels=None):
    """Returns a request for a Datastore Admin Import.

  Args:
    project: the project id to import, a string.
    input_url: the location of the GCS overall export file, a string.
    kinds: a string list of kinds to import.
    namespaces:  a string list of namespaces to import.
    labels: a string->string map of client labels.
  Returns:
    an ImportRequest message.
  """
    messages = util.GetMessages()
    request_class = messages.GoogleDatastoreAdminV1ImportEntitiesRequest

    entity_filter = _MakeEntityFilter(namespaces, kinds)

    labels_message = request_class.LabelsValue()
    labels_message.additionalProperties = []

    # We want label creation order to be deterministic.
    labels = labels or {}
    for key, value in sorted(labels.items()):
        labels_message.additionalProperties.append(
            request_class.LabelsValue.AdditionalProperty(key=key, value=value))

    import_request = request_class(labels=labels_message,
                                   entityFilter=entity_filter,
                                   inputUrl=input_url)

    return messages.DatastoreProjectsImportRequest(
        projectId=project,
        googleDatastoreAdminV1ImportEntitiesRequest=import_request)
def GetExportEntitiesRequest(project,
                             output_url_prefix,
                             kinds=None,
                             namespaces=None,
                             labels=None):
    """Returns a request for a Datastore Admin Export.

  Args:
    project: the project id to export, a string.
    output_url_prefix: the output GCS path prefix, a string.
    kinds: a string list of kinds to export.
    namespaces:  a string list of namespaces to export.
    labels: a string->string map of client labels.
  Returns:
    an ExportRequest message.
  """
    messages = util.GetMessages()
    request_class = messages.GoogleDatastoreAdminV1ExportEntitiesRequest

    labels_message = request_class.LabelsValue()
    labels_message.additionalProperties = []
    # We want label creation order to be deterministic.
    labels = labels or {}
    for key, value in sorted(labels.items()):
        labels_message.additionalProperties.append(
            request_class.LabelsValue.AdditionalProperty(key=key, value=value))

    entity_filter = _MakeEntityFilter(namespaces, kinds)
    export_request = request_class(labels=labels_message,
                                   entityFilter=entity_filter,
                                   outputUrlPrefix=output_url_prefix)

    request = messages.DatastoreProjectsExportRequest(
        projectId=project,
        googleDatastoreAdminV1ExportEntitiesRequest=export_request)
    return request
def ListIndexes(project_id):
    response = GetIndexesService().List(
        util.GetMessages().DatastoreProjectsIndexesListRequest(
            projectId=project_id))
    return {ApiMessageToIndexDefinition(index) for index in response.indexes}
from __future__ import unicode_literals

from googlecloudsdk.api_lib.datastore import util
from googlecloudsdk.core.console import progress_tracker
from googlecloudsdk.third_party.appengine.datastore import datastore_index


def GetIndexesService():
    """Returns the service for interacting with the Datastore Admin Service.

  This is used to manage the datastore indexes (create/delete).
  """
    return util.GetClient().projects_indexes


ASCENDING = (util.GetMessages().GoogleDatastoreAdminV1IndexedProperty.
             DirectionValueValuesEnum.ASCENDING)

DESCENDING = (util.GetMessages().GoogleDatastoreAdminV1IndexedProperty.
              DirectionValueValuesEnum.DESCENDING)

NO_ANCESTOR = (util.GetMessages().GoogleDatastoreAdminV1Index.
               AncestorValueValuesEnum.NONE)

ALL_ANCESTORS = (util.GetMessages().GoogleDatastoreAdminV1Index.
                 AncestorValueValuesEnum.ALL_ANCESTORS)

CREATING = (util.GetMessages().GoogleDatastoreAdminV1Index.
            StateValueValuesEnum.CREATING)


def ApiMessageToIndexDefinition(proto):