| OLD | NEW |
| (Empty) | |
| 1 # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. |
| 3 # All Rights Reserved |
| 4 # |
| 5 # Permission is hereby granted, free of charge, to any person obtaining a |
| 6 # copy of this software and associated documentation files (the |
| 7 # "Software"), to deal in the Software without restriction, including |
| 8 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 9 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 10 # persons to whom the Software is furnished to do so, subject to the fol- |
| 11 # lowing conditions: |
| 12 # |
| 13 # The above copyright notice and this permission notice shall be included |
| 14 # in all copies or substantial portions of the Software. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 # IN THE SOFTWARE. |
| 23 # |
| 24 |
| 25 import time |
| 26 from boto.compat import json |
| 27 |
| 28 |
| 29 class OptionStatus(dict): |
| 30 """ |
| 31 Presents a combination of status field (defined below) which are |
| 32 accessed as attributes and option values which are stored in the |
| 33 native Python dictionary. In this class, the option values are |
| 34 merged from a JSON object that is stored as the Option part of |
| 35 the object. |
| 36 |
| 37 :ivar domain_name: The name of the domain this option is associated with. |
| 38 :ivar create_date: A timestamp for when this option was created. |
| 39 :ivar state: The state of processing a change to an option. |
| 40 Possible values: |
| 41 |
| 42 * RequiresIndexDocuments: the option's latest value will not |
| 43 be visible in searches until IndexDocuments has been called |
| 44 and indexing is complete. |
| 45 * Processing: the option's latest value is not yet visible in |
| 46 all searches but is in the process of being activated. |
| 47 * Active: the option's latest value is completely visible. |
| 48 |
| 49 :ivar update_date: A timestamp for when this option was updated. |
| 50 :ivar update_version: A unique integer that indicates when this |
| 51 option was last updated. |
| 52 """ |
| 53 |
| 54 def __init__(self, domain, data=None, refresh_fn=None, save_fn=None): |
| 55 self.domain = domain |
| 56 self.refresh_fn = refresh_fn |
| 57 self.save_fn = save_fn |
| 58 self.refresh(data) |
| 59 |
| 60 def _update_status(self, status): |
| 61 self.creation_date = status['creation_date'] |
| 62 self.status = status['state'] |
| 63 self.update_date = status['update_date'] |
| 64 self.update_version = int(status['update_version']) |
| 65 |
| 66 def _update_options(self, options): |
| 67 if options: |
| 68 self.update(json.loads(options)) |
| 69 |
| 70 def refresh(self, data=None): |
| 71 """ |
| 72 Refresh the local state of the object. You can either pass |
| 73 new state data in as the parameter ``data`` or, if that parameter |
| 74 is omitted, the state data will be retrieved from CloudSearch. |
| 75 """ |
| 76 if not data: |
| 77 if self.refresh_fn: |
| 78 data = self.refresh_fn(self.domain.name) |
| 79 if data: |
| 80 self._update_status(data['status']) |
| 81 self._update_options(data['options']) |
| 82 |
| 83 def to_json(self): |
| 84 """ |
| 85 Return the JSON representation of the options as a string. |
| 86 """ |
| 87 return json.dumps(self) |
| 88 |
| 89 def startElement(self, name, attrs, connection): |
| 90 return None |
| 91 |
| 92 def endElement(self, name, value, connection): |
| 93 if name == 'CreationDate': |
| 94 self.created = value |
| 95 elif name == 'State': |
| 96 self.state = value |
| 97 elif name == 'UpdateDate': |
| 98 self.updated = value |
| 99 elif name == 'UpdateVersion': |
| 100 self.update_version = int(value) |
| 101 elif name == 'Options': |
| 102 self.update_from_json_doc(value) |
| 103 else: |
| 104 setattr(self, name, value) |
| 105 |
| 106 def save(self): |
| 107 """ |
| 108 Write the current state of the local object back to the |
| 109 CloudSearch service. |
| 110 """ |
| 111 if self.save_fn: |
| 112 data = self.save_fn(self.domain.name, self.to_json()) |
| 113 self.refresh(data) |
| 114 |
| 115 def wait_for_state(self, state): |
| 116 """ |
| 117 Performs polling of CloudSearch to wait for the ``state`` |
| 118 of this object to change to the provided state. |
| 119 """ |
| 120 while self.state != state: |
| 121 time.sleep(5) |
| 122 self.refresh() |
| 123 |
| 124 |
| 125 class IndexFieldStatus(OptionStatus): |
| 126 |
| 127 def _update_options(self, options): |
| 128 self.update(options) |
| 129 |
| 130 def save(self): |
| 131 pass |
| 132 |
| 133 |
| 134 class RankExpressionStatus(IndexFieldStatus): |
| 135 |
| 136 pass |
| 137 |
| 138 class ServicePoliciesStatus(OptionStatus): |
| 139 |
| 140 def new_statement(self, arn, ip): |
| 141 """ |
| 142 Returns a new policy statement that will allow |
| 143 access to the service described by ``arn`` by the |
| 144 ip specified in ``ip``. |
| 145 |
| 146 :type arn: string |
| 147 :param arn: The Amazon Resource Notation identifier for the |
| 148 service you wish to provide access to. This would be |
| 149 either the search service or the document service. |
| 150 |
| 151 :type ip: string |
| 152 :param ip: An IP address or CIDR block you wish to grant access |
| 153 to. |
| 154 """ |
| 155 return { |
| 156 "Effect":"Allow", |
| 157 "Action":"*", # Docs say use GET, but denies unless * |
| 158 "Resource": arn, |
| 159 "Condition": { |
| 160 "IpAddress": { |
| 161 "aws:SourceIp": [ip] |
| 162 } |
| 163 } |
| 164 } |
| 165 |
| 166 def _allow_ip(self, arn, ip): |
| 167 if 'Statement' not in self: |
| 168 s = self.new_statement(arn, ip) |
| 169 self['Statement'] = [s] |
| 170 self.save() |
| 171 else: |
| 172 add_statement = True |
| 173 for statement in self['Statement']: |
| 174 if statement['Resource'] == arn: |
| 175 for condition_name in statement['Condition']: |
| 176 if condition_name == 'IpAddress': |
| 177 add_statement = False |
| 178 condition = statement['Condition'][condition_name] |
| 179 if ip not in condition['aws:SourceIp']: |
| 180 condition['aws:SourceIp'].append(ip) |
| 181 |
| 182 if add_statement: |
| 183 s = self.new_statement(arn, ip) |
| 184 self['Statement'].append(s) |
| 185 self.save() |
| 186 |
| 187 def allow_search_ip(self, ip): |
| 188 """ |
| 189 Add the provided ip address or CIDR block to the list of |
| 190 allowable address for the search service. |
| 191 |
| 192 :type ip: string |
| 193 :param ip: An IP address or CIDR block you wish to grant access |
| 194 to. |
| 195 """ |
| 196 arn = self.domain.search_service_arn |
| 197 self._allow_ip(arn, ip) |
| 198 |
| 199 def allow_doc_ip(self, ip): |
| 200 """ |
| 201 Add the provided ip address or CIDR block to the list of |
| 202 allowable address for the document service. |
| 203 |
| 204 :type ip: string |
| 205 :param ip: An IP address or CIDR block you wish to grant access |
| 206 to. |
| 207 """ |
| 208 arn = self.domain.doc_service_arn |
| 209 self._allow_ip(arn, ip) |
| 210 |
| 211 def _disallow_ip(self, arn, ip): |
| 212 if 'Statement' not in self: |
| 213 return |
| 214 need_update = False |
| 215 for statement in self['Statement']: |
| 216 if statement['Resource'] == arn: |
| 217 for condition_name in statement['Condition']: |
| 218 if condition_name == 'IpAddress': |
| 219 condition = statement['Condition'][condition_name] |
| 220 if ip in condition['aws:SourceIp']: |
| 221 condition['aws:SourceIp'].remove(ip) |
| 222 need_update = True |
| 223 if need_update: |
| 224 self.save() |
| 225 |
| 226 def disallow_search_ip(self, ip): |
| 227 """ |
| 228 Remove the provided ip address or CIDR block from the list of |
| 229 allowable address for the search service. |
| 230 |
| 231 :type ip: string |
| 232 :param ip: An IP address or CIDR block you wish to grant access |
| 233 to. |
| 234 """ |
| 235 arn = self.domain.search_service_arn |
| 236 self._disallow_ip(arn, ip) |
| 237 |
| 238 def disallow_doc_ip(self, ip): |
| 239 """ |
| 240 Remove the provided ip address or CIDR block from the list of |
| 241 allowable address for the document service. |
| 242 |
| 243 :type ip: string |
| 244 :param ip: An IP address or CIDR block you wish to grant access |
| 245 to. |
| 246 """ |
| 247 arn = self.domain.doc_service_arn |
| 248 self._disallow_ip(arn, ip) |
| OLD | NEW |