1.9.30 sync.
[gae.git] / python / google / appengine / api / datastore_admin.py
blobd5184957a400dd2b7b75fac15d518c4ac5934ff1
1 #!/usr/bin/env python
3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 """The Python datastore admin API for managing indices and schemas.
23 """
32 from google.appengine.api import api_base_pb
33 from google.appengine.api import apiproxy_stub_map
34 from google.appengine.api import datastore
35 from google.appengine.api import datastore_errors
36 from google.appengine.api import datastore_types
37 from google.appengine.datastore import datastore_index
38 from google.appengine.datastore import datastore_pb
39 from google.appengine.runtime import apiproxy_errors
41 def GetIndices(_app=None):
42 """Fetches all composite indices in the datastore for this app.
44 Returns:
45 list of entity_pb.CompositeIndex
46 """
48 req = api_base_pb.StringProto()
49 req.set_value(datastore_types.ResolveAppId(_app))
50 resp = datastore_pb.CompositeIndices()
51 resp = _Call('GetIndices', req, resp)
52 return resp.index_list()
55 def CreateIndex(index):
56 """Creates a new composite index in the datastore for this app.
58 Args:
59 index: entity_pb.CompositeIndex
61 Returns:
62 int, the id allocated to the index
63 """
64 resp = api_base_pb.Integer64Proto()
65 resp = _Call('CreateIndex', index, resp)
66 return resp.value()
69 def UpdateIndex(index):
70 """Updates an index's status. The entire index definition must be present.
72 Args:
73 index: entity_pb.CompositeIndex
74 """
75 _Call('UpdateIndex', index, api_base_pb.VoidProto())
78 def DeleteIndex(index):
79 """Deletes an index. The entire index definition must be present.
81 Args:
82 index: entity_pb.CompositeIndex
83 """
84 _Call('DeleteIndex', index, api_base_pb.VoidProto())
87 def _Call(call, req, resp):
88 """Generic method for making a datastore API call.
90 Args:
91 call: string, the name of the RPC call
92 req: the request PB. if the app_id field is not set, it defaults to the
93 local app.
94 resp: the response PB
95 """
96 if hasattr(req, 'app_id'):
97 req.set_app_id(datastore_types.ResolveAppId(req.app_id()))
99 try:
100 result = apiproxy_stub_map.MakeSyncCall('datastore_v3', call, req, resp)
101 if result:
102 return result
103 return resp
104 except apiproxy_errors.ApplicationError, err:
105 raise datastore._ToDatastoreError(err)