Ver Fonte

Relaxed naming restrictions for buckets and bucket objects

Nuwan Goonasekera há 8 anos atrás
pai
commit
9c7d18e372

+ 22 - 0
cloudbridge/cloud/base/resources.py

@@ -729,9 +729,20 @@ class BaseRegion(BaseCloudResource, Region):
 
 class BaseBucketObject(BaseCloudResource, BucketObject):
 
+    # Regular expression for valid bucket keys.
+    # They, must match the following criteria: http://docs.aws.amazon.com/"
+    # AmazonS3/latest/dev/UsingMetadata.html#object-key-guidelines
+    #
+    # Note: The following regex is based on: https://stackoverflow.com/question
+    # s/537772/what-is-the-most-correct-regular-expression-for-a-unix-file-path
+    CB_NAME_PATTERN = re.compile(r"[^\0]+")
+
     def __init__(self, provider):
         super(BaseBucketObject, self).__init__(provider)
 
+    def is_valid_resource_name(self, name):
+        return True if self.CB_NAME_PATTERN.match(name) else False
+
     def save_content(self, target_stream):
         """
         Download this object and write its
@@ -754,9 +765,20 @@ class BaseBucketObject(BaseCloudResource, BucketObject):
 
 class BaseBucket(BaseCloudResource, BasePageableObjectMixin, Bucket):
 
+    # Regular expression for valid bucket names.
+    # They, must match the following criteria: http://docs.aws.amazon.com/aws
+    # cloudtrail/latest/userguide/cloudtrail-s3-bucket-naming-requirements.html
+    #
+    # NOTE: The following regex is based on: https://stackoverflow.com/questio
+    # ns/2063213/regular-expression-for-validating-dns-label-host-name
+    CB_NAME_PATTERN = re.compile(r"^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{,63}(?<!-)$")
+
     def __init__(self, provider):
         super(BaseBucket, self).__init__(provider)
 
+    def is_valid_resource_name(self, name):
+        return True if self.CB_NAME_PATTERN.match(name) else False
+
     def __eq__(self, other):
         return (isinstance(other, Bucket) and
                 # pylint:disable=protected-access

+ 30 - 1
cloudbridge/cloud/interfaces/resources.py

@@ -67,11 +67,16 @@ class CloudResource(object):
         display value for the resource. Some resources may allow the resource
         name to be set.
 
-        The name property adheres to the following restrictions:
+        The name property adheres to the following restrictions for most
+        cloudbridge resources:
         * Names cannot be longer than 63 characters
         * May only contain lowercase letters, numeric characters, underscores,
           and dashes. International characters are allowed.
 
+        Some resources may relax/increase these restrictions (e.g. Buckets)
+        depending on their requirements. Consult the resource specific
+        documentation for exact restrictions.
+
         :rtype: ``str``
         :return: Name for this instance as returned by the cloud middleware.
         """
@@ -1801,6 +1806,18 @@ class BucketObject(CloudResource):
     """
     __metaclass__ = ABCMeta
 
+    @abstractproperty
+    def name(self):
+        """
+        The bucket object name adheres to a more relaxed naming requirement as
+        detailed here: http://docs.aws.amazon.com/AmazonS3/latest/dev/Using
+        Metadata.html#object-key-guidelines
+
+        :rtype: ``str``
+        :return: Name for this instance as returned by the cloud middleware.
+        """
+        pass
+
     @abstractproperty
     def size(self):
         """
@@ -1892,6 +1909,18 @@ class Bucket(PageableObjectMixin, CloudResource):
 
     __metaclass__ = ABCMeta
 
+    @abstractproperty
+    def name(self):
+        """
+        The bucket name adheres to a more relaxed naming requirement as
+        detailed here: http://docs.aws.amazon.com/awscloudtrail/latest/userguid
+        e/cloudtrail-s3-bucket-naming-requirements.html
+
+        :rtype: ``str``
+        :return: Name for this instance as returned by the cloud middleware.
+        """
+        pass
+
     @abstractmethod
     def get(self, name):
         """

+ 0 - 2
test/helpers/standard_interface_tests.py

@@ -115,8 +115,6 @@ def check_obj_name(test, obj):
     numeric characters, underscores, and dashes. International
     characters are allowed.
     """
-    test.assertTrue(obj.is_valid_resource_name(obj.name), "Object name %s is"
-                    " not a valid resource name" % obj.name)
 
     # if name has a setter, make sure invalid values cannot be set
     name_property = getattr(type(obj), 'name', None)