|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import logging |
|
|
|
import boto3 |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
class ResourceMeta(object): |
|
""" |
|
An object containing metadata about a resource. |
|
""" |
|
def __init__(self, service_name, identifiers=None, client=None, |
|
data=None): |
|
|
|
self.service_name = service_name |
|
|
|
if identifiers is None: |
|
identifiers = [] |
|
|
|
self.identifiers = identifiers |
|
|
|
|
|
self.client = client |
|
|
|
self.data = data |
|
|
|
def __repr__(self): |
|
return 'ResourceMeta(\'{0}\', identifiers={1})'.format( |
|
self.service_name, self.identifiers) |
|
|
|
def __eq__(self, other): |
|
|
|
if other.__class__.__name__ != self.__class__.__name__: |
|
return False |
|
|
|
return self.__dict__ == other.__dict__ |
|
|
|
def copy(self): |
|
""" |
|
Create a copy of this metadata object. |
|
""" |
|
params = self.__dict__.copy() |
|
service_name = params.pop('service_name') |
|
return ResourceMeta(service_name, **params) |
|
|
|
|
|
class ServiceResource(object): |
|
""" |
|
A base class for resources. |
|
|
|
:type client: botocore.client |
|
:param client: A low-level Botocore client instance |
|
""" |
|
|
|
meta = None |
|
""" |
|
Stores metadata about this resource instance, such as the |
|
``service_name``, the low-level ``client`` and any cached ``data`` |
|
from when the instance was hydrated. For example:: |
|
|
|
# Get a low-level client from a resource instance |
|
client = resource.meta.client |
|
response = client.operation(Param='foo') |
|
|
|
# Print the resource instance's service short name |
|
print(resource.meta.service_name) |
|
|
|
See :py:class:`ResourceMeta` for more information. |
|
""" |
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
|
self.meta = self.meta.copy() |
|
|
|
|
|
if kwargs.get('client') is not None: |
|
self.meta.client = kwargs.get('client') |
|
else: |
|
self.meta.client = boto3.client(self.meta.service_name) |
|
|
|
|
|
|
|
for i, value in enumerate(args): |
|
setattr(self, self.meta.identifiers[i], value) |
|
|
|
|
|
|
|
for name, value in kwargs.items(): |
|
if name == 'client': |
|
continue |
|
|
|
if name not in self.meta.identifiers: |
|
raise ValueError('Unknown keyword argument: {0}'.format(name)) |
|
|
|
setattr(self, name, value) |
|
|
|
|
|
for identifier in self.meta.identifiers: |
|
if getattr(self, identifier) is None: |
|
raise ValueError( |
|
'Required parameter {0} not set'.format(identifier)) |
|
|
|
def __repr__(self): |
|
identifiers = [] |
|
for identifier in self.meta.identifiers: |
|
identifiers.append('{0}={1}'.format( |
|
identifier, repr(getattr(self, identifier)))) |
|
return "{0}({1})".format( |
|
self.__class__.__name__, |
|
', '.join(identifiers), |
|
) |
|
|
|
def __eq__(self, other): |
|
|
|
if other.__class__.__name__ != self.__class__.__name__: |
|
return False |
|
|
|
|
|
|
|
for identifier in self.meta.identifiers: |
|
if getattr(self, identifier) != getattr(other, identifier): |
|
return False |
|
|
|
return True |
|
|