| OLD | NEW |
| (Empty) |
| 1 # Copyright 2011 Google Inc. All Rights Reserved. | |
| 2 # | |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 # you may not use this file except in compliance with the License. | |
| 5 # You may obtain a copy of the License at | |
| 6 # | |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 # | |
| 9 # Unless required by applicable law or agreed to in writing, software | |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 # See the License for the specific language governing permissions and | |
| 13 # limitations under the License. | |
| 14 | |
| 15 import boto | |
| 16 | |
| 17 from gslib.exception import ProjectIdException | |
| 18 from gslib.wildcard_iterator import WILDCARD_BUCKET_ITERATOR | |
| 19 | |
| 20 GOOG_PROJ_ID_HDR = 'x-goog-project-id' | |
| 21 | |
| 22 | |
| 23 class ProjectIdHandler(object): | |
| 24 """Google Project ID header handling.""" | |
| 25 | |
| 26 def __init__(self): | |
| 27 """Instantiates Project ID handler. Call after boto config file loaded.""" | |
| 28 config = boto.config | |
| 29 self.project_id = config.get_value('GSUtil', 'default_project_id') | |
| 30 | |
| 31 def SetProjectId(self, project_id): | |
| 32 """Overrides project ID value from config file default. | |
| 33 | |
| 34 Args: | |
| 35 project_id: Project ID to use. | |
| 36 """ | |
| 37 self.project_id = project_id | |
| 38 | |
| 39 def FillInProjectHeaderIfNeeded(self, command, uri, headers): | |
| 40 """Fills project ID header into headers if defined and applicable. | |
| 41 | |
| 42 Args: | |
| 43 command: The command being run. | |
| 44 uri: The URI against which this command is being run. | |
| 45 headers: Dictionary containing optional HTTP headers to pass to boto. | |
| 46 Must not be None. | |
| 47 """ | |
| 48 | |
| 49 # We only include the project ID header if it's a GS URI and a project_id | |
| 50 # was specified and | |
| 51 # (it's an 'mb', 'disablelogging, or 'enablelogging' command or | |
| 52 # a boto request in integration tests or | |
| 53 # (an 'ls' command that doesn't specify a bucket or wildcarded bucket)). | |
| 54 if (uri.scheme.lower() == 'gs' and self.project_id | |
| 55 and (command == 'mb' or command == 'disablelogging' | |
| 56 or command == 'enablelogging' | |
| 57 or command == 'test' | |
| 58 or (command == 'ls' and not uri.names_bucket()) | |
| 59 or (command == WILDCARD_BUCKET_ITERATOR))): | |
| 60 # Note: check for None (as opposed to "not headers") here because | |
| 61 # it's ok to pass empty headers. | |
| 62 if headers is None: | |
| 63 raise ProjectIdException( | |
| 64 'FillInProjectHeaderIfNeeded called with headers=None') | |
| 65 headers[GOOG_PROJ_ID_HDR] = self.project_id | |
| 66 elif headers.has_key(GOOG_PROJ_ID_HDR): | |
| 67 del headers[GOOG_PROJ_ID_HDR] | |
| OLD | NEW |