Example #1
0
def userSignUp(request):
    if request.method == 'POST':

        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            save = serializer.save()
            auth = Authentication()
            user, token = auth.authenticate(
                serializer.validated_data.get('mobile'),
                serializer.validated_data.get('password'))
            data = AuthUserSerializer(user).data
            result = {'token': token}
            result.update(data)
            wallet = WalletSerializer(data={'win_bal': '0', 'wal_bal': '10'})
            if wallet.is_valid():
                user_wallet = wallet.save()
                user_wallet.user = save
                user_wallet.save()
                user_txn = TransactionSerializer(
                    data={
                        'txn_status': 'credit',
                        'txn_amount': '10',
                        'txn_description': 'Signup bonus'
                    })
                if user_txn.is_valid():

                    user_txn1 = user_txn.save()
                    user_txn1.user = save
                    user_txn1.save()

            return Response(data=result, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
def userSignIn(request):
    if request.method == 'POST':
        serializer = UserLoginSerializer(data=request.data)
        if serializer.is_valid():
            auth = Authentication()
            user, token = auth.authenticate(**serializer.validated_data)
            data = AuthUserSerializer(user).data
            result = {'token': token}
            result.update(data)
            return Response(data=result, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #3
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    @undocumented: http_connect
    @undocumented: make_request
    @undocumented: _check_container_name
    """

    def __init__(self, username=None, api_key=None, timeout=10, **kwargs):
        """
        Accepts keyword arguments for Rackspace Cloud username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword.

        @type username: str
        @param username: a Rackspace Cloud username
        @type api_key: str
        @param api_key: a Rackspace Cloud API key
        """
        self.connection_args = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        self.user_agent = kwargs.get('useragent', consts.user_agent)
        self.timeout = timeout

        self.auth = 'auth' in kwargs and kwargs['auth'] or None

        if not self.auth:
            authurl = kwargs.get('authurl', consts.us_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username, api_key, authurl=authurl,
                            useragent=self.user_agent)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or \
                                                              THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                              HTTPConnection
        self.http_connect()

    def convert_iso_datetime(self, dt):
        """
        Convert iso8601 to datetime
        """
        isoFormat = "%Y-%m-%dT%H:%M:%S.000+0000"
        if type(dt) is datetime.datetime:
            return dt
        if dt.endswith("Z"):
            dt = dt.split('Z')[0]
            isoFormat = "%Y-%m-%dT%H:%M:%S"
        return datetime.datetime.strptime(dt, isoFormat)

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port, \
                                              timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        """
        query_args = ""
        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join(
                   [unicode_quote(i) for i in path]))
        if isinstance(parms, dict) and parms:
            query_args = \
                ['%s=%s' % (quote(x),
                            quote(str(y))) for (x, y) in parms.items()]
        elif isinstance(parms, list) and parms:
            query_args = \
                ["%s" % x for x in parms]
        path = '%s?%s' % (path, '&'.join(query_args))

        headers = {'Content-Length': str(len(data)),
                   'User-Agent': self.user_agent,
                   'X-Auth-Token': self.token}
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            if 'PYTHON_CLOUDDNS_DEBUG' in os.environ and \
                    os.environ['PYTHON_CLOUDDNS_DEBUG'].strip():
                import sys
                url = "https://%s%s\n" % \
                    (self.connection_args[0],
                     path)
                sys.stderr.write("METHOD: %s\n" % (str(method)))
                sys.stderr.write("URL: %s" % (url))
                sys.stderr.write("HEADERS: %s\n" % (str(headers)))
                sys.stderr.write("DATA: %s\n" % (str(data)))
                sys.stderr.write("curl -X '%s' -H 'X-Auth-Token: %s' %s %s" % \
                                     (method, self.token, url, str(data)))
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()
        return response

    def get_domains(self):
        return DomainResults(self, self.list_domains_info())

    def list_domains_info(self, filter_by_name=None):
        parms = {}
        if filter_by_name:
            parms = {'name': filter_by_name}
        response = self.make_request('GET', ['domains'], parms=parms)
        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        read_output = response.read()
        return json.loads(read_output)['domains']

    def get_domain(self, id=None, **dico):
        filter_by_name = ""
        if id:
            dico['id'] = id
        if 'name' in dico:
            dico['name'] = dico['name'].lower()
            filter_by_name = dico['name']

        domains = self.list_domains_info(filter_by_name=filter_by_name)
        for domain in domains:
            for k in dico:
                if k in domain and domain[k] == dico[k]:
                    return Domain(self, **domain)
        raise UnknownDomain("Not found")

    def get_zone(self, **dico):
        filter_by_name_split = dico['name'].split('.')
        ret = None
        for i in range(len(filter_by_name_split),1,-1):
            dico['name'] = ".".join(filter_by_name_split[-i:])
            try: ret = self.get_domain(**dico)
            except UnknownDomain: pass
        if not ret: raise UnknownDomain
        else: return ret

    def get_domain_details(self, id=None):
        """Get details on a particular domain"""
        parms = { 'showRecords': 'false', 'showSubdomains': 'false' }
        response = self.make_request('GET', ['domains', str(id)], parms=parms)

        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        read_output = response.read()
        domains = json.loads(read_output)

        return Domain(self, **domains)

    # Take a reponse parse it if there is asyncResponse and wait for
    # it (TODO: should offer to not)
    def wait_for_async_request(self, response):
        if (response.status < 200) or (response.status > 299):
            _output = response.read().strip()
            try:
                output = json.loads(_output)
            except ValueError:
                output = None
            api_reasons = ""
            if output and 'validationErrors' in output:
                for msg in output['validationErrors']['messages']:
                    api_reasons += " (%s)" % msg
            raise ResponseError(response.status, response.reason+api_reasons)
        output = json.loads(response.read())
        jobId = output['jobId']
        while True:
            response = self.make_request('GET', ['status', jobId],
                                         parms=['showDetails=True']) 
            if (response.status < 200) or (response.status > 299):
                response.read()
                raise ResponseError(response.status, response.reason)
            _output = response.read().strip()
            output = json.loads(_output)
            if output['status'] == 'COMPLETED':
                try:
                    return output['response']
                except KeyError:
                    return output
            if output['status'] == 'ERROR':
                if (output['error']['code'] == 409 and 
                    output['error']['details'] == 'Domain already exists'):
                    raise DomainAlreadyExists
                if (output['error']['code'] == 409 and 
                    output['error']['details'].find('belongs to another owner')):
                    raise NotDomainOwner
                raise ResponseError(output['error']['code'],
                                    output['error']['details'])
            time.sleep(1)
            continue

    def _domain(self, name, ttl, emailAddress, comment=""):
        if not ttl >= 300:
            raise Exception("Ttl is a minimun of 300 seconds")
        s = '<domain name="%s" ttl="%s" emailAddress="%s" comment="%s"></domain>'
        return s % (name, ttl, emailAddress, comment)

    def create_domain(self, name, ttl, emailAddress, comment=""):
        domain = [name, ttl, emailAddress, comment]
        return self.create_domains([domain])[0]

    def create_domains(self, domains):
        xml = '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0">'
        ret = []
        for dom in domains:
            ret.append(self._domain(*dom))
        xml += "\n".join(ret)
        xml += "</domains>"
        response = self.make_request('POST', ['domains'], data=xml)
        output = self.wait_for_async_request(response)

        ret = []
        for domain in output['domains']:
            ret.append(Domain(connection=self, **domain))
        return ret

    def delete_domain(self, domain_id):
        return self.delete_domains([domain_id])

    def delete_domains(self, domains_id):
        ret = ["id=%s" % (i) for i in domains_id]
        response = self.make_request('DELETE',
                                     ['domains'],
                                     parms=ret,
                                      )
        return self.wait_for_async_request(response)

    def import_domain(self, bind_zone):
        """
        Allows for a bind zone file to be imported in one operation.  The
        bind_zone parameter can be a string or a file object.
        """

        if type(bind_zone) is file:
            bind_zone = bind_zone.read()

        xml = '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0">'
        xml += '<domain contentType="BIND_9">'
        xml += '<contents>%s</contents>' % bind_zone
        xml += '</domain></domains>'

        response = self.make_request('POST', ['domains', 'import'], data=xml)
        output = self.wait_for_async_request(response)

        ret = []
        for domain in output['domains']:
            ret.append(Domain(self, **domain))
        return ret
Example #4
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    @undocumented: cdn_connect
    @undocumented: http_connect
    @undocumented: cdn_request
    @undocumented: make_request
    @undocumented: _check_container_name
    """
    def __init__(self, username=None, api_key=None, timeout=5, **kwargs):
        """
        Accepts keyword arguments for Mosso username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword. Setting the argument
        servicenet to True will make use of Rackspace servicenet network.

        @type username: str
        @param username: a Mosso username
        @type api_key: str
        @param api_key: a Mosso API key
        @type servicenet: bool
        @param servicenet: Use Rackspace servicenet to access Cloud Files.
        @type cdn_log_retention: bool
        @param cdn_log_retention: set logs retention for this cdn enabled
        container.
        """
        self.cdn_enabled = False
        self.cdn_args = None
        self.connection_args = None
        self.cdn_connection = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        self.servicenet = kwargs.get('servicenet', False)
        self.user_agent = kwargs.get('useragent', consts.user_agent)
        self.timeout = timeout

        # if the environement variable RACKSPACE_SERVICENET is set (to
        # anything) it will automatically set servicenet=True
        if not 'servicenet' in kwargs \
                and 'RACKSPACE_SERVICENET' in os.environ:
            self.servicenet = True

        self.auth = 'auth' in kwargs and kwargs['auth'] or None

        if not self.auth:
            authurl = kwargs.get('authurl', consts.us_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username,
                                           api_key,
                                           authurl=authurl,
                                           useragent=self.user_agent)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.cdn_url, self.token) = self.auth.authenticate()
        url = self._set_storage_url(url)
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or \
                                                              THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                              HTTPConnection
        self.http_connect()
        if self.cdn_url:
            self.cdn_connect()

    def _set_storage_url(self, url):
        if self.servicenet:
            return "https://snet-%s" % url.replace("https://", "")
        return url

    def cdn_connect(self):
        """
        Setup the http connection instance for the CDN service.
        """
        (host, port, cdn_uri, is_ssl) = parse_url(self.cdn_url)
        self.cdn_connection = self.conn_class(host, port, timeout=self.timeout)
        self.cdn_enabled = True

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port, \
                                              timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

    def cdn_request(self, method, path=[], data='', hdrs=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, performs an http request against the CDN service.
        """
        if not self.cdn_enabled:
            raise CDNNotEnabled()

        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path]))
        headers = {
            'Content-Length': str(len(data)),
            'User-Agent': self.user_agent,
            'X-Auth-Token': self.token
        }
        if isinstance(hdrs, dict):
            headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.cdn_connect()
            self.cdn_connection.request(method, path, data, headers)
            return self.cdn_connection.getresponse()

        try:
            self.cdn_connection.request(method, path, data, headers)
            response = self.cdn_connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()

        return response

    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        """
        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path]))

        if isinstance(parms, dict) and parms:
            query_args = \
                ['%s=%s' % (quote(x),
                            quote(str(y))) for (x, y) in parms.items()]
            path = '%s?%s' % (path, '&'.join(query_args))

        headers = {
            'Content-Length': str(len(data)),
            'User-Agent': self.user_agent,
            'X-Auth-Token': self.token
        }
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()

        return response

    def get_info(self):
        """
        Return tuple for number of containers and total bytes in the account

        >>> connection.get_info()
        (5, 2309749)

        @rtype: tuple
        @return: a tuple containing the number of containers and total bytes
                 used by the account
        """
        response = self.make_request('HEAD')
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-account-container-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-account-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return (count, size)

    def _check_container_name(self, container_name):
        if not container_name or \
                '/' in container_name or \
                len(container_name) > consts.container_name_limit:
            raise InvalidContainerName(container_name)

    def create_container(self, container_name, error_on_existing=False):
        """
        Given a container name, returns a L{Container} item, creating a new
        Container if one does not already exist.

        >>> connection.create_container('new_container')
        <cloudfiles.container.Container object at 0xb77d628c>

        @param container_name: name of the container to create
        @type container_name: str
        @param error_on_existing: raise ContainerExists if container already
        exists
        @type error_on_existing: bool
        @rtype: L{Container}
        @return: an object representing the newly created container
        """
        self._check_container_name(container_name)

        response = self.make_request('PUT', [container_name])
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        if error_on_existing and (response.status == 202):
            raise ContainerExists(container_name)
        return Container(self, container_name)

    def delete_container(self, container_name):
        """
        Given a container name, delete it.

        >>> connection.delete_container('old_container')

        @param container_name: name of the container to delete
        @type container_name: str
        """
        if isinstance(container_name, Container):
            container_name = container_name.name
        self._check_container_name(container_name)

        response = self.make_request('DELETE', [container_name])

        if (response.status == 409):
            raise ContainerNotEmpty(container_name)
        elif (response.status == 404):
            raise NoSuchContainer
        elif (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)

        if self.cdn_enabled:
            response = self.cdn_request('POST', [container_name],
                                        hdrs={'X-CDN-Enabled': 'False'})

    def get_all_containers(self, limit=None, marker=None, **parms):
        """
        Returns a Container item result set.

        >>> connection.get_all_containers()
        ContainerResults: 4 containers
        >>> print ', '.join([container.name for container in
                             connection.get_all_containers()])
        new_container, old_container, pictures, music

        @rtype: L{ContainerResults}
        @return: an iterable set of objects representing all containers on the
                 account
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        return ContainerResults(self, self.list_containers_info(**parms))

    def get_container(self, container_name):
        """
        Return a single Container item for the given Container.

        >>> connection.get_container('old_container')
        <cloudfiles.container.Container object at 0xb77d628c>
        >>> container = connection.get_container('old_container')
        >>> container.size_used
        23074

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the container
        """
        self._check_container_name(container_name)

        response = self.make_request('HEAD', [container_name])
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-container-object-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-container-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if response.status == 404:
            raise NoSuchContainer(container_name)
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name, count, size)

    def list_public_containers(self):
        """
        Returns a list of containers that have been published to the CDN.

        >>> connection.list_public_containers()
        ['container1', 'container2', 'container3']

        @rtype: list(str)
        @return: a list of all CDN-enabled container names as strings
        """
        response = self.cdn_request('GET', [''])
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def list_containers_info(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers, including object count and size.

        >>> connection.list_containers_info()
        [{u'count': 510, u'bytes': 2081717, u'name': u'new_container'},
         {u'count': 12, u'bytes': 23074, u'name': u'old_container'},
         {u'count': 0, u'bytes': 0, u'name': u'container1'},
         {u'count': 0, u'bytes': 0, u'name': u'container2'},
         {u'count': 0, u'bytes': 0, u'name': u'container3'},
         {u'count': 3, u'bytes': 2306, u'name': u'test'}]

        @rtype: list({"name":"...", "count":..., "bytes":...})
        @return: a list of all container info as dictionaries with the
                 keys "name", "count", and "bytes"
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        parms['format'] = 'json'
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return json_loads(response.read())

    def list_containers(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers.

        >>> connection.list_containers()
        ['new_container',
         'old_container',
         'container1',
         'container2',
         'container3',
         'test']

        @rtype: list(str)
        @return: a list of all containers names as strings
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def __getitem__(self, key):
        """
        Container objects can be grabbed from a connection using index
        syntax.

        >>> container = conn['old_container']
        >>> container.size_used
        23074

        @rtype: L{Container}
        @return: an object representing the container
        """
        return self.get_container(key)
Example #5
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory 
    for Container instances.

    @undocumented: cdn_connect
    @undocumented: http_connect
    @undocumented: cdn_request
    @undocumented: make_request
    @undocumented: _check_container_name
    """
    def __init__(self, username=None, api_key=None, **kwargs):
        """
        Accepts keyword arguments for Mosso username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword.
        
        @type username: str
        @param username: a Mosso username
        @type api_key: str
        @param api_key: a Mosso API key
        """
        self.cdn_enabled = False
        self.cdn_args = None
        self.connection_args = None
        self.cdn_connection = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        socket.setdefaulttimeout = int(kwargs.get('timeout', 5))
        self.auth = kwargs.has_key('auth') and kwargs['auth'] or None
        
        if not self.auth:
            authurl = kwargs.get('authurl', consts.default_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username, api_key, authurl)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")
        
        self._authenticate()
        
    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.cdn_url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)
        self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                      HTTPConnection
        self.http_connect()
        if self.cdn_url:
            self.cdn_connect()

    def cdn_connect(self):
        """
        Setup the http connection instance for the CDN service.
        """
        (host, port, cdn_uri, is_ssl) = parse_url(self.cdn_url)
        conn_class = is_ssl and HTTPSConnection or HTTPConnection
        self.cdn_connection = conn_class(host, port)
        self.cdn_enabled = True

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port)
        self.connection.set_debuglevel(self.debuglevel)

    def cdn_request(self, method, path=[], data='', hdrs=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, performs an http request against the CDN service.
        """
        if not self.cdn_enabled:
            raise CDNNotEnabled()

        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join([quote(i) for i in path]))
        headers = {'Content-Length': len(data), 'User-Agent': consts.user_agent, 
                   'X-Auth-Token': self.token}
        if isinstance(hdrs, dict):
            headers.update(hdrs)
        
        # Send the request
        self.cdn_connection.request(method, path, data, headers)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.cdn_connect()
            self.cdn_connection.request(method, path, data, headers)
            return self.cdn_connection.getresponse()

        try:
            response = self.cdn_connection.getresponse()
        except HTTPException:
            response = retry_request()

        if response.status == 401:
            self._authenticate()
            response = retry_request()

        return response


    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters, 
        performs an http request.
        """
        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join([quote(i) for i in path]))
        
        if isinstance(parms, dict) and parms:
            query_args = \
                ['%s=%s' % (quote(x),quote(str(y))) for (x,y) in parms.items()]
            path = '%s?%s' % (path, '&'.join(query_args))
            
        headers = {'Content-Length': len(data), 'User-Agent': consts.user_agent, 
                   'X-Auth-Token': self.token}
        isinstance(hdrs, dict) and headers.update(hdrs)
        
        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except HTTPException:
            response = retry_request()
            
        if response.status == 401:
            self._authenticate()
            response = retry_request()

        return response

    def get_info(self):
        """
        Return tuple for number of containers and total bytes in the account

        >>> connection.get_info()
        (5, 2309749)

        @rtype: tuple
        @return: a tuple containing the number of containers and total bytes
                 used by the account
        """
        response = self.make_request('HEAD')
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-account-container-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-account-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return (count, size)

    def _check_container_name(self, container_name):
        if not container_name or \
                '/' in container_name or \
                len(container_name) > consts.container_name_limit:
            raise InvalidContainerName(container_name)

    def create_container(self, container_name):
        """
        Given a container name, returns a L{Container} item, creating a new
        Container if one does not already exist.

        >>> connection.create_container('new_container')
        <cloudfiles.container.Container object at 0xb77d628c>

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the newly created container
        """
        self._check_container_name(container_name)
        
        response = self.make_request('PUT', [container_name])
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name)

    def delete_container(self, container_name):
        """
        Given a container name, delete it.

        >>> connection.delete_container('old_container')

        @param container_name: name of the container to delete
        @type container_name: str
        """
        if isinstance(container_name, Container):
            container_name = container_name.name
        self._check_container_name(container_name)
        
        response = self.make_request('DELETE', [container_name])
        buff = response.read()
        
        if (response.status == 409):
            raise ContainerNotEmpty(container_name)
        elif (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)

        if self.cdn_enabled:
            response = self.cdn_request('POST', [container_name],
                                hdrs={'X-CDN-Enabled': 'False'})

    def get_all_containers(self, limit=None, marker=None, **parms):
        """
        Returns a Container item result set.

        >>> connection.get_all_containers()
        ContainerResults: 4 containers
        >>> print ', '.join([container.name for container in
                             connection.get_all_containers()])
        new_container, old_container, pictures, music

        @rtype: L{ContainerResults}
        @return: an iterable set of objects representing all containers on the
                 account
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        return ContainerResults(self, self.list_containers_info(**parms))

    def get_container(self, container_name):
        """
        Return a single Container item for the given Container.

        >>> connection.get_container('old_container')
        <cloudfiles.container.Container object at 0xb77d628c>
        >>> container = connection.get_container('old_container')
        >>> container.size_used
        23074

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the container
        """
        self._check_container_name(container_name)
        
        response = self.make_request('HEAD', [container_name])
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-container-object-count':
                try:    
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-container-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if response.status == 404:
            raise NoSuchContainer(container_name)
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name, count, size)

    def list_public_containers(self):
        """
        Returns a list of containers that have been published to the CDN.

        >>> connection.list_public_containers()
        ['container1', 'container2', 'container3']

        @rtype: list(str)
        @return: a list of all CDN-enabled container names as strings
        """
        response = self.cdn_request('GET', [''])
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def list_containers_info(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers, including object count and size.

        >>> connection.list_containers_info()
        [{u'count': 510, u'bytes': 2081717, u'name': u'new_container'},
         {u'count': 12, u'bytes': 23074, u'name': u'old_container'},
         {u'count': 0, u'bytes': 0, u'name': u'container1'},
         {u'count': 0, u'bytes': 0, u'name': u'container2'},
         {u'count': 0, u'bytes': 0, u'name': u'container3'},
         {u'count': 3, u'bytes': 2306, u'name': u'test'}]

        @rtype: list({"name":"...", "count":..., "bytes":...})
        @return: a list of all container info as dictionaries with the
                 keys "name", "count", and "bytes"
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        parms['format'] = 'json'
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return json_loads(response.read())

    def list_containers(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers.

        >>> connection.list_containers()
        ['new_container',
         'old_container',
         'container1',
         'container2',
         'container3',
         'test']

        @rtype: list(str)
        @return: a list of all containers names as strings
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def __getitem__(self, key):
        """
        Container objects can be grabbed from a connection using index
        syntax.

        >>> container = conn['old_container']
        >>> container.size_used
        23074

        @rtype: L{Container}
        @return: an object representing the container
        """
        return self.get_container(key)
Example #6
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    create container ---> PUT
    delete container ---> DELETE
    rename container ---> POST  没实现
    set ACL          ---> POST  没实现
    get container    ---> GET
    get info         ---> HEAD
    list containers  ---> GET 解析json数据
    _authenticate ---> 认证
    http_connect  ---> 生成conn连接
    make_request  ---> 向服务端发送http请求
    """

    def __init__(self, username=None, api_key=None, timeout=5, **kwargs):
        """
        Accepts keyword arguments for chouti username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword. 

        @type username: str
        @param username: a chouti username, pattern is account:admin
        @type api_key: str
        @param api_key: a chouti password
        container.
        """
        self.connection_args = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        self.servicenet = kwargs.get('servicenet', False)
        self.user_agent = kwargs.get('useragent', consts.user_agent)
        self.timeout = timeout

        self.username = username
        self.api_key = api_key
        self._share_user_uri = kwargs.get('_share_user_uri', None)
        self._share_request = kwargs.get('_share_request', False)

        if kwargs.get('share_request', False):
            # 产生一个专为共享请求的请求连接方法
            self.make_request = self.make_share_requst

        self.auth = 'auth' in kwargs and kwargs['auth'] or None

        if not self.auth:
            authurl = kwargs.get('authurl', consts.chouti_authurl)
            if username and api_key and authurl:
                # 此处的auth为Authentication类的实例
                self.auth = Authentication(username, api_key, authurl=authurl,
                            useragent=self.user_agent)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        私有方法,开始认证
        """
        (url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or \
                                                              THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                              HTTPConnection
        self.http_connect()

    def authorization(self, url=None):
        """
        授权功能
        @type url: str
        @param url: 用户接收到的共享文件的链接. url需要解析,解析出container与object
        先认证授权,成功后返回连接对象,此对象所使用的make_request均为share_request
        """
        path = []
        self._share_user_uri, cont = url.rsplit('/', 1)
        # 临时开关,临时使用share_request
        self._share_request = True
        path.append(cont)
        resp = self.make_request('HEAD', path)
        if resp.status == 204:
            self._share_request = False
            return Connection(self.username, self.api_key, _share_request=True, _share_user_uri = self._share_user_uri)
        else:
            self._share_request = False
            return None

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        # roamin9 set the authorization conn's right uri
        if self._share_request:
            self.uri = self._share_user_uri
        self.connection = self.conn_class(host, port=port, \
                                              timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        
        @type method: str
        @param method: http method
        @type path: list
        @param path: the url's path, include [container_name], [obj_name]
        @type hdrs: dict
        @param hdrs: http headers
        @type parms: dict
        @param parms: query args
        """
        if self._share_request and self._share_user_uri:
            path = '/%s/%s' % \
                     (self._share_user_uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path]))
        else:
            path = '/%s/%s' % \
                     (self.uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path]))

        if isinstance(parms, dict) and parms:
                # 查询参数中的变量是固定的,为limits等字符,都为英文字符
                # 但查询参数中的值有可能是unicode值,
                # 因此,对于对于查询参数中的值需要进行unicode处理,使用unicode_quote()
                # 这应该算一个bug,可以提交给作者
            query_args = \
                ['%s=%s' % (unicode_quote(x),
                            unicode_quote(y)) for (x, y) in parms.items()]
            path = '%s?%s' % (path, '&'.join(query_args))

        headers = {
                   # 设置了Content-Length,这样上传或下载文件时需要优化一下
                   'Content-Length': str(len(data)),
                   'User-Agent': self.user_agent,
                   'X-Auth-Token': self.token
                   }
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()

        return response

    def get_info(self):
        """
        Return tuple for number of containers and total bytes in the account

        >>> connection.get_info()
        (5, 2309749)

        @rtype: tuple
        @return: a tuple containing the number of containers and total bytes
                 used by the account
        """
        response = self.make_request('HEAD')
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-account-container-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-account-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return (count, size)

    def _check_container_name(self, container_name):
                # container名称中不能包含'/'
        if not container_name or \
                '/' in container_name or \
                len(container_name) > consts.container_name_limit:
            raise InvalidContainerName(container_name)

    def create_container(self, container_name):
        """
        Given a container name, returns a L{Container} item, creating a new
        Container if one does not already exist.

        >>> connection.create_container('new_container')
        <cloudfiles.container.Container object at 0xb77d628c>

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the newly created container
        """
        self._check_container_name(container_name)

        response = self.make_request('PUT', [container_name])
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name)

    def delete_container(self, container_name):
        """
        Given a container name, delete it.

        >>> connection.delete_container('old_container')

        @param container_name: name of the container to delete
        @type container_name: str
        """
        # Container类的实例
        if isinstance(container_name, Container):
            container_name = container_name.name
        self._check_container_name(container_name)

        response = self.make_request('DELETE', [container_name])

        if (response.status == 409):
            raise ContainerNotEmpty(container_name)
        elif (response.status == 404):
            raise NoSuchContainer
        elif (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)

        # 一旦删除了一个container,需要标记CDN的状态为关闭
        #if self.cdn_enabled:
        #    response = self.cdn_request('POST', [container_name],
        #                        hdrs={'X-CDN-Enabled': 'False'})

    def get_all_containers(self, limit=None, marker=None, **parms):
        """
        Returns a Container item result set.

        >>> connection.get_all_containers()
        ContainerResults: 4 containers
        >>> print ', '.join([container.name for container in
                             connection.get_all_containers()])
        new_container, old_container, pictures, music

        @rtype: L{ContainerResults}
        @return: an iterable set of objects representing all containers on the
                 account
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        return ContainerResults(self, self.list_containers_info(**parms))

    def get_container(self, container_name):
        """
        Return a single Container item for the given Container.

        >>> connection.get_container('old_container')
        <cloudfiles.container.Container object at 0xb77d628c>
        >>> container = connection.get_container('old_container')
        >>> container.size_used
        23074

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the container
        """
        self._check_container_name(container_name)

        response = self.make_request('HEAD', [container_name])
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-container-object-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-container-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if response.status == 404:
            raise NoSuchContainer(container_name)
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name, count, size)

    #def list_public_containers(self):
    #    """
    #    Returns a list of containers that have been published to the CDN.

    #    >>> connection.list_public_containers()
    #    ['container1', 'container2', 'container3']

    #    @rtype: list(str)
    #    @return: a list of all CDN-enabled container names as strings
    #    """
    #    response = self.cdn_request('GET', [''])
    #    if (response.status < 200) or (response.status > 299):
    #        buff = response.read()
    #        raise ResponseError(response.status, response.reason)
    #    return response.read().splitlines()

    def list_containers_info(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers, including object count and size.

        >>> connection.list_containers_info()
        [{u'count': 510, u'bytes': 2081717, u'name': u'new_container'},
         {u'count': 12, u'bytes': 23074, u'name': u'old_container'},
         {u'count': 0, u'bytes': 0, u'name': u'container1'},
         {u'count': 0, u'bytes': 0, u'name': u'container2'},
         {u'count': 0, u'bytes': 0, u'name': u'container3'},
         {u'count': 3, u'bytes': 2306, u'name': u'test'}]

        @rtype: list({"name":"...", "count":..., "bytes":...})
        @return: a list of all container info as dictionaries with the
                 keys "name", "count", and "bytes"
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        parms['format'] = 'json'
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return json_loads(response.read())

    def list_containers(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers.

        >>> connection.list_containers()
        ['new_container',
         'old_container',
         'container1',
         'container2',
         'container3',
         'test']

        @rtype: list(str)
        @return: a list of all containers names as strings
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def __getitem__(self, key):
        """
        Container objects can be grabbed from a connection using index
        syntax.

        >>> container = conn['old_container']
        >>> container.size_used
        23074

        @rtype: L{Container}
        @return: an object representing the container
        """
        return self.get_container(key)
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    @undocumented: cdn_connect
    @undocumented: http_connect
    @undocumented: cdn_request
    @undocumented: make_request
    @undocumented: _check_container_name
    """

    def __init__(self, username=None, api_key=None, timeout=15, **kwargs):
        """
        Accepts keyword arguments for Mosso username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword. Setting the argument
        servicenet to True will make use of Rackspace servicenet network.

        @type username: str
        @param username: a Mosso username
        @type api_key: str
        @param api_key: a Mosso API key
        @type servicenet: bool
        @param servicenet: Use Rackspace servicenet to access Cloud Files.
        @type cdn_log_retention: bool
        @param cdn_log_retention: set logs retention for this cdn enabled
        container.
        """
        self.cdn_enabled = False
        self.cdn_args = None
        self.connection_args = None
        self.cdn_connection = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        self.servicenet = kwargs.get('servicenet', False)
        self.user_agent = kwargs.get('useragent', consts.user_agent)
        self.timeout = timeout

        # if the environement variable RACKSPACE_SERVICENET is set (to
        # anything) it will automatically set servicenet=True
        if not 'servicenet' in kwargs \
                and 'RACKSPACE_SERVICENET' in os.environ:
            self.servicenet = True

        self.auth = 'auth' in kwargs and kwargs['auth'] or None

        if not self.auth:
            authurl = kwargs.get('authurl', consts.us_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username, api_key, authurl=authurl,
                            useragent=self.user_agent, timeout=self.timeout)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")
        self._authenticate()
    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.cdn_url, self.token) = self.auth.authenticate()
        url = self._set_storage_url(url)
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or \
                                                              THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                              HTTPConnection
        self.http_connect()
        if self.cdn_url:
            self.cdn_connect()

    def _set_storage_url(self, url):
        if self.servicenet:
            return "https://snet-%s" % url.replace("https://", "")
        return url

    def cdn_connect(self):
        """
        Setup the http connection instance for the CDN service.
        """
        (host, port, cdn_uri, is_ssl) = parse_url(self.cdn_url)
        self.cdn_connection = self.conn_class(host, port, timeout=self.timeout)
        self.cdn_enabled = True

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port, \
                                              timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

    def cdn_request(self, method, path=[], data='', hdrs=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, performs an http request against the CDN service.
        """
        if not self.cdn_enabled:
            raise CDNNotEnabled()

        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path]))
        headers = {'Content-Length': str(len(data)),
                   'User-Agent': self.user_agent,
                   'X-Auth-Token': self.token}
        if isinstance(hdrs, dict):
            headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.cdn_connect()
            self.cdn_connection.request(method, path, data, headers)
            return self.cdn_connection.getresponse()

        try:
            self.cdn_connection.request(method, path, data, headers)
            response = self.cdn_connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()

        return response

    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        """
        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path]))

        if isinstance(parms, dict) and parms:
            path = '%s?%s' % (path, urlencode(parms))

        headers = {'Content-Length': str(len(data)),
                   'User-Agent': self.user_agent,
                   'X-Auth-Token': self.token}
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()

        return response

    def get_info(self):
        """
        Return tuple for number of containers, total bytes in the account and account metadata

        >>> connection.get_info()
        (5, 2309749)

        @rtype: tuple
        @return: a tuple containing the number of containers, total bytes
                 used by the account and a dictionary containing account metadata
        """
        response = self.make_request('HEAD')
        count = size = None
        metadata = {}
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-account-container-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-account-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
            if hdr[0].lower().startswith('x-account-meta-'):
                metadata[hdr[0].lower()[15:]] = hdr[1]
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return (count, size, metadata)

    def update_account_metadata(self, metadata):
        """
        Update account metadata
        >>> metadata = {'x-account-meta-foo' : 'bar'}
        >>> connection.update_account_metadata(metadata)

        @param metadata: Dictionary of metadata
        @type metdada: dict
        """
        response = self.make_request('POST', hdrs=metadata)
        response.read()
        if (response.status < 200) or (response.status > 299):
           raise ResponseError(response.status, response.reason)

    def _check_container_name(self, container_name):
        if not container_name or \
                '/' in container_name or \
                len(container_name) > consts.container_name_limit:
            raise InvalidContainerName(container_name)

    def create_container(self, container_name, error_on_existing=False):
        """
        Given a container name, returns a L{Container} item, creating a new
        Container if one does not already exist.

        >>> connection.create_container('new_container')
        <cloudfiles.container.Container object at 0xb77d628c>

        @param container_name: name of the container to create
        @type container_name: str
        @param error_on_existing: raise ContainerExists if container already
        exists
        @type error_on_existing: bool
        @rtype: L{Container}
        @return: an object representing the newly created container
        """
        self._check_container_name(container_name)

        response = self.make_request('PUT', [container_name])
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        if error_on_existing and (response.status == 202):
            raise ContainerExists(container_name)
        return Container(self, container_name)

    def delete_container(self, container_name):
        """
        Given a container name, delete it.

        >>> connection.delete_container('old_container')

        @param container_name: name of the container to delete
        @type container_name: str
        """
        if isinstance(container_name, Container):
            container_name = container_name.name
        self._check_container_name(container_name)

        response = self.make_request('DELETE', [container_name])
        response.read()

        if (response.status == 409):
            raise ContainerNotEmpty(container_name)
        elif (response.status == 404):
            raise NoSuchContainer
        elif (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)

        if self.cdn_enabled:
            response = self.cdn_request('POST', [container_name],
                                hdrs={'X-CDN-Enabled': 'False'})

    def get_all_containers(self, limit=None, marker=None, **parms):
        """
        Returns a Container item result set.

        >>> connection.get_all_containers()
        ContainerResults: 4 containers
        >>> print ', '.join([container.name for container in
                             connection.get_all_containers()])
        new_container, old_container, pictures, music

        @rtype: L{ContainerResults}
        @return: an iterable set of objects representing all containers on the
                 account
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        return ContainerResults(self, self.list_containers_info(**parms))

    def get_container(self, container_name):
        """
        Return a single Container item for the given Container.

        >>> connection.get_container('old_container')
        <cloudfiles.container.Container object at 0xb77d628c>
        >>> container = connection.get_container('old_container')
        >>> container.size_used
        23074

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the container
        """
        self._check_container_name(container_name)

        response = self.make_request('HEAD', [container_name])
        count = size = None
        metadata = {}
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-container-object-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-container-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
            if hdr[0].lower().startswith('x-container-meta-'):
                metadata[hdr[0].lower()[17:]] = hdr[1]
        buff = response.read()
        if response.status == 404:
            raise NoSuchContainer(container_name)
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name, count, size, metadata)

    def list_public_containers(self):
        """
        Returns a list of containers that have been published to the CDN.

        >>> connection.list_public_containers()
        ['container1', 'container2', 'container3']

        @rtype: list(str)
        @return: a list of all CDN-enabled container names as strings
        """
        response = self.cdn_request('GET', [''])
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def list_containers_info(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers, including object count and size.

        >>> connection.list_containers_info()
        [{u'count': 510, u'bytes': 2081717, u'name': u'new_container'},
         {u'count': 12, u'bytes': 23074, u'name': u'old_container'},
         {u'count': 0, u'bytes': 0, u'name': u'container1'},
         {u'count': 0, u'bytes': 0, u'name': u'container2'},
         {u'count': 0, u'bytes': 0, u'name': u'container3'},
         {u'count': 3, u'bytes': 2306, u'name': u'test'}]

        @rtype: list({"name":"...", "count":..., "bytes":...})
        @return: a list of all container info as dictionaries with the
                 keys "name", "count", and "bytes"
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        parms['format'] = 'json'
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return json_loads(response.read())

    def list_containers(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers.

        >>> connection.list_containers()
        ['new_container',
         'old_container',
         'container1',
         'container2',
         'container3',
         'test']

        @rtype: list(str)
        @return: a list of all containers names as strings
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def __getitem__(self, key):
        """
        Container objects can be grabbed from a connection using index
        syntax.

        >>> container = conn['old_container']
        >>> container.size_used
        23074

        @rtype: L{Container}
        @return: an object representing the container
        """
        return self.get_container(key)
Example #8
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    create container ---> PUT
    delete container ---> DELETE
    rename container ---> POST  没实现
    set ACL          ---> POST  没实现
    get container    ---> GET
    get info         ---> HEAD
    list containers  ---> GET 解析json数据
    _authenticate ---> 认证
    http_connect  ---> 生成conn连接
    make_request  ---> 向服务端发送http请求
    """
    def __init__(self, username=None, api_key=None, timeout=5, **kwargs):
        """
        Accepts keyword arguments for chouti username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword. 

        @type username: str
        @param username: a chouti username, pattern is account:admin
        @type api_key: str
        @param api_key: a chouti password
        container.
        """
        self.connection_args = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        self.servicenet = kwargs.get('servicenet', False)
        self.user_agent = kwargs.get('useragent', consts.user_agent)
        self.timeout = timeout

        self.username = username
        self.api_key = api_key
        self._share_user_uri = kwargs.get('_share_user_uri', None)
        self._share_request = kwargs.get('_share_request', False)

        if kwargs.get('share_request', False):
            # 产生一个专为共享请求的请求连接方法
            self.make_request = self.make_share_requst

        self.auth = 'auth' in kwargs and kwargs['auth'] or None

        if not self.auth:
            authurl = kwargs.get('authurl', consts.chouti_authurl)
            if username and api_key and authurl:
                # 此处的auth为Authentication类的实例
                self.auth = Authentication(username,
                                           api_key,
                                           authurl=authurl,
                                           useragent=self.user_agent)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        私有方法,开始认证
        """
        (url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or \
                                                              THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                              HTTPConnection
        self.http_connect()

    def authorization(self, url=None):
        """
        授权功能
        @type url: str
        @param url: 用户接收到的共享文件的链接. url需要解析,解析出container与object
        先认证授权,成功后返回连接对象,此对象所使用的make_request均为share_request
        """
        path = []
        self._share_user_uri, cont = url.rsplit('/', 1)
        # 临时开关,临时使用share_request
        self._share_request = True
        path.append(cont)
        resp = self.make_request('HEAD', path)
        if resp.status == 204:
            self._share_request = False
            return Connection(self.username,
                              self.api_key,
                              _share_request=True,
                              _share_user_uri=self._share_user_uri)
        else:
            self._share_request = False
            return None

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        # roamin9 set the authorization conn's right uri
        if self._share_request:
            self.uri = self._share_user_uri
        self.connection = self.conn_class(host, port=port, \
                                              timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        
        @type method: str
        @param method: http method
        @type path: list
        @param path: the url's path, include [container_name], [obj_name]
        @type hdrs: dict
        @param hdrs: http headers
        @type parms: dict
        @param parms: query args
        """
        if self._share_request and self._share_user_uri:
            path = '/%s/%s' % \
                     (self._share_user_uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path]))
        else:
            path = '/%s/%s' % \
                     (self.uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path]))

        if isinstance(parms, dict) and parms:
            # 查询参数中的变量是固定的,为limits等字符,都为英文字符
            # 但查询参数中的值有可能是unicode值,
            # 因此,对于对于查询参数中的值需要进行unicode处理,使用unicode_quote()
            # 这应该算一个bug,可以提交给作者
            query_args = \
                ['%s=%s' % (unicode_quote(x),
                            unicode_quote(y)) for (x, y) in parms.items()]
            path = '%s?%s' % (path, '&'.join(query_args))

        headers = {
            # 设置了Content-Length,这样上传或下载文件时需要优化一下
            'Content-Length': str(len(data)),
            'User-Agent': self.user_agent,
            'X-Auth-Token': self.token
        }
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()

        return response

    def get_info(self):
        """
        Return tuple for number of containers and total bytes in the account

        >>> connection.get_info()
        (5, 2309749)

        @rtype: tuple
        @return: a tuple containing the number of containers and total bytes
                 used by the account
        """
        response = self.make_request('HEAD')
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-account-container-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-account-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return (count, size)

    def _check_container_name(self, container_name):
        # container名称中不能包含'/'
        if not container_name or \
                '/' in container_name or \
                len(container_name) > consts.container_name_limit:
            raise InvalidContainerName(container_name)

    def create_container(self, container_name):
        """
        Given a container name, returns a L{Container} item, creating a new
        Container if one does not already exist.

        >>> connection.create_container('new_container')
        <cloudfiles.container.Container object at 0xb77d628c>

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the newly created container
        """
        self._check_container_name(container_name)

        response = self.make_request('PUT', [container_name])
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name)

    def delete_container(self, container_name):
        """
        Given a container name, delete it.

        >>> connection.delete_container('old_container')

        @param container_name: name of the container to delete
        @type container_name: str
        """
        # Container类的实例
        if isinstance(container_name, Container):
            container_name = container_name.name
        self._check_container_name(container_name)

        response = self.make_request('DELETE', [container_name])

        if (response.status == 409):
            raise ContainerNotEmpty(container_name)
        elif (response.status == 404):
            raise NoSuchContainer
        elif (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)

        # 一旦删除了一个container,需要标记CDN的状态为关闭
        #if self.cdn_enabled:
        #    response = self.cdn_request('POST', [container_name],
        #                        hdrs={'X-CDN-Enabled': 'False'})

    def get_all_containers(self, limit=None, marker=None, **parms):
        """
        Returns a Container item result set.

        >>> connection.get_all_containers()
        ContainerResults: 4 containers
        >>> print ', '.join([container.name for container in
                             connection.get_all_containers()])
        new_container, old_container, pictures, music

        @rtype: L{ContainerResults}
        @return: an iterable set of objects representing all containers on the
                 account
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        return ContainerResults(self, self.list_containers_info(**parms))

    def get_container(self, container_name):
        """
        Return a single Container item for the given Container.

        >>> connection.get_container('old_container')
        <cloudfiles.container.Container object at 0xb77d628c>
        >>> container = connection.get_container('old_container')
        >>> container.size_used
        23074

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the container
        """
        self._check_container_name(container_name)

        response = self.make_request('HEAD', [container_name])
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-container-object-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-container-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if response.status == 404:
            raise NoSuchContainer(container_name)
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name, count, size)

    #def list_public_containers(self):
    #    """
    #    Returns a list of containers that have been published to the CDN.

    #    >>> connection.list_public_containers()
    #    ['container1', 'container2', 'container3']

    #    @rtype: list(str)
    #    @return: a list of all CDN-enabled container names as strings
    #    """
    #    response = self.cdn_request('GET', [''])
    #    if (response.status < 200) or (response.status > 299):
    #        buff = response.read()
    #        raise ResponseError(response.status, response.reason)
    #    return response.read().splitlines()

    def list_containers_info(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers, including object count and size.

        >>> connection.list_containers_info()
        [{u'count': 510, u'bytes': 2081717, u'name': u'new_container'},
         {u'count': 12, u'bytes': 23074, u'name': u'old_container'},
         {u'count': 0, u'bytes': 0, u'name': u'container1'},
         {u'count': 0, u'bytes': 0, u'name': u'container2'},
         {u'count': 0, u'bytes': 0, u'name': u'container3'},
         {u'count': 3, u'bytes': 2306, u'name': u'test'}]

        @rtype: list({"name":"...", "count":..., "bytes":...})
        @return: a list of all container info as dictionaries with the
                 keys "name", "count", and "bytes"
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        parms['format'] = 'json'
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return json_loads(response.read())

    def list_containers(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers.

        >>> connection.list_containers()
        ['new_container',
         'old_container',
         'container1',
         'container2',
         'container3',
         'test']

        @rtype: list(str)
        @return: a list of all containers names as strings
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def __getitem__(self, key):
        """
        Container objects can be grabbed from a connection using index
        syntax.

        >>> container = conn['old_container']
        >>> container.size_used
        23074

        @rtype: L{Container}
        @return: an object representing the container
        """
        return self.get_container(key)
Example #9
0
def main():
    with open("config.json", "r") as config_file:
        config = json.load(config_file)

    MAPSERVER_URL = config["mapServerUrl"]
    GENERATE_TOKEN_URL = config["generateTokenUrl"]
    MINIMUM_ZOOM_LEVEL = config["minimumZoomLevel"]
    MAXIMUM_ZOOM_LEVEL = config["maximumZoomlevel"]

    USERNAME = os.getenv('TILESET_GENERATOR_USERNAME')
    PASSWORD = os.getenv('TILESET_GENERATOR_PASSWORD')

    TPK = config["tpk"]
    XYZ = config["xyz"]
    ZIP = config["zip"]

    tiles_folder = pathlib.Path('tiles')
    if tiles_folder.exists() and tiles_folder.is_dir():
        shutil.rmtree("tiles")
    os.mkdir("tiles")

    authentication = Authentication(GENERATE_TOKEN_URL)
    token = authentication.authenticate(USERNAME, PASSWORD)
    downloader = Downloader(token, MAPSERVER_URL)
    zoom_levels = f"{str(MINIMUM_ZOOM_LEVEL)}-{str(MAXIMUM_ZOOM_LEVEL)}"

    for shapefile in glob.glob("shapefiles/*.shp"):
        attempt = 1
        success = False
        print(f"\nWorking on shapefile: {shapefile}")
        while attempt < 3 and success == False:
            try:
                downloaded_tpk_location = downloader.download(
                    shapefile, zoom_levels)
                shapefile_tiles_folder = pathlib.Path(
                    downloaded_tpk_location).parents[1]

                print("Extracting tiles...")
                with tpkutils.TPK(downloaded_tpk_location) as tpk:
                    zoom_list = list(
                        range(MINIMUM_ZOOM_LEVEL, MAXIMUM_ZOOM_LEVEL + 1))
                    tpk.to_disk(f"{shapefile_tiles_folder}/xyz",
                                zoom=zoom_list, drop_empty=True)

                print("Zipping tiles...")
                shutil.make_archive(
                    f"{shapefile_tiles_folder}/zip/layer", 'zip', f"{shapefile_tiles_folder}/xyz")

                if TPK == False:
                    shutil.rmtree(f"{shapefile_tiles_folder}/tpk")
                    print("Deleting TPK file...")
                if XYZ == False:
                    shutil.rmtree(f"{shapefile_tiles_folder}/xyz")
                    print("Deleting XYZ files...")
                if ZIP == False:
                    shutil.rmtree(f"{shapefile_tiles_folder}/zip")
                    print("Deleting ZIP file...")
                print("Done!")
                success = True
            except exceptions.InvalidTokenException:
                print("Token expired, generating a new one...")
                token = authentication.authenticate(USERNAME, PASSWORD)
                downloader = Downloader(token, MAPSERVER_URL)
                continue
            except Exception as exception:
                print("Unexpected error: ", exception)
                attempt = attempt + 1
                continue
Example #10
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    @undocumented: http_connect
    @undocumented: make_request
    @undocumented: _check_container_name
    """
    def __init__(self, username=None, api_key=None, timeout=10, **kwargs):
        """
        Accepts keyword arguments for Rackspace Cloud username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword.

        @type username: str
        @param username: a Rackspace Cloud username
        @type api_key: str
        @param api_key: a Rackspace Cloud API key
        """
        self.connection_args = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        self.user_agent = kwargs.get('useragent', consts.user_agent)
        self.timeout = timeout
        self._total_domains = -1

        self.auth = 'auth' in kwargs and kwargs['auth'] or None

        if not self.auth:
            authurl = kwargs.get('authurl', consts.us_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username,
                                           api_key,
                                           authurl=authurl,
                                           useragent=self.user_agent)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    @property
    def total_domains(self):
        if self._total_domains == -1:
            self.list_domains_info(offset=0, limit=1)
        return self._total_domains

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or \
                                                              THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                              HTTPConnection
        self.http_connect()

    def convert_iso_datetime(self, dt):
        """
        Convert iso8601 to datetime
        """
        isoFormat = "%Y-%m-%dT%H:%M:%S.000+0000"
        if type(dt) is datetime.datetime:
            return dt
        if dt.endswith("Z"):
            dt = dt.split('Z')[0]
            isoFormat = "%Y-%m-%dT%H:%M:%S"
        return datetime.datetime.strptime(dt, isoFormat)

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port, \
                                              timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        """
        query_args = ""
        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join(
                   [unicode_quote(i) for i in path]))
        if isinstance(parms, dict) and parms:
            query_args = \
                ['%s=%s' % (quote(x),
                            quote(str(y))) for (x, y) in parms.items()]
        elif isinstance(parms, list) and parms:
            query_args = \
                ["%s" % x for x in parms]
        path = '%s?%s' % (path, '&'.join(query_args))

        headers = {
            'Content-Length': str(len(data)),
            'User-Agent': self.user_agent,
            'X-Auth-Token': self.token,
            'Content-Type': 'application/xml'
        }
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            if 'PYTHON_CLOUDDNS_DEBUG' in os.environ and \
                    os.environ['PYTHON_CLOUDDNS_DEBUG'].strip():
                import sys
                url = "https://%s%s\n" % \
                    (self.connection_args[0],
                     path)
                sys.stderr.write("METHOD: %s\n" % (str(method)))
                sys.stderr.write("URL: %s" % (url))
                sys.stderr.write("HEADERS: %s\n" % (str(headers)))
                sys.stderr.write("DATA: %s\n" % (str(data)))
                sys.stderr.write("curl -X '%s' -H 'X-Auth-Token: %s' %s %s" % \
                                     (method, self.token, url, str(data)))
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()
        return response

    def get_domains(self, name=None, offset=0, limit=None):
        return DomainResults(self, self.list_domains_info(name, offset, limit))

    def list_domains_info(self, name=None, offset=0, limit=None):
        if offset != 0:
            if limit is None:
                raise ValueError('limit must be specified when setting offset')
            elif offset % limit > 0:
                raise ValueError(
                    'offset (%d) must be a multiple of limit (%d)' %
                    (offset, limit))
        if limit is None:
            limit = int(ceil(self.total_domains / 100.0) * 100)
        domains = []
        step = min(limit, 100) if limit > 0 else 1
        for _offset in xrange(offset, offset + limit, step):
            resp = self._list_domains_info_raw(name, _offset, step)
            domains_info = json.loads(resp)
            if 'totalEntries' in domains_info:
                self._total_domains = domains_info['totalEntries']
            domains.extend(domains_info['domains'])
        return domains[:limit]

    def _list_domains_info_raw(self, name, offset, limit):
        parms = {'offset': offset, 'limit': limit}
        if name is not None:
            parms.update({'name': name})
        response = self.make_request('GET', ['domains'], parms=parms)
        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        return response.read()

    def get_domain(self, id=None, **dico):
        if id:
            dico['id'] = id
        if 'name' in dico:
            dico['name'] = dico['name'].lower()

        domains = self.list_domains_info(name=dico.get('name', None))
        for domain in domains:
            for k in dico:
                if k in domain and domain[k] == dico[k]:
                    return Domain(self, **domain)
        raise UnknownDomain("Not found")

    def get_domain_details(self, id=None):
        """Get details on a particular domain"""
        parms = {'showRecords': 'false', 'showSubdomains': 'false'}
        response = self.make_request('GET', ['domains', str(id)], parms=parms)

        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        read_output = response.read()
        domains = json.loads(read_output)

        return Domain(self, **domains)

    # Take a reponse parse it if there is asyncResponse and wait for
    # it (TODO: should offer to not)
    def wait_for_async_request(self, response):
        if (response.status < 200) or (response.status > 299):
            _output = response.read().strip()
            try:
                output = json.loads(_output)
            except ValueError:
                output = None
            api_reasons = ""
            if output and 'validationErrors' in output:
                for msg in output['validationErrors']['messages']:
                    api_reasons += " (%s)" % msg
            raise ResponseError(response.status, response.reason + api_reasons)
        output = json.loads(response.read())
        jobId = output['jobId']
        while True:
            response = self.make_request('GET', ['status', jobId],
                                         parms=['showDetails=True'])
            if (response.status < 200) or (response.status > 299):
                response.read()
                raise ResponseError(response.status, response.reason)
            _output = response.read().strip()
            output = json.loads(_output)
            if output['status'] == 'COMPLETED':
                try:
                    return output['response']
                except KeyError:
                    return output
            if output['status'] == 'ERROR':
                if (output['error']['code'] == 409 and
                        output['error']['details'] == 'Domain already exists'):
                    raise DomainAlreadyExists
                if (output['error']['code'] == 409 and output['error']
                    ['details'].find('belongs to another owner')):
                    raise NotDomainOwner
                raise ResponseError(output['error']['code'],
                                    output['error']['details'])
            time.sleep(1)
            continue

    def _domain(self, name, ttl, emailAddress, comment=""):
        if not ttl >= 300:
            raise Exception("Ttl is a minimun of 300 seconds")
        s = '<domain name="%s" ttl="%s" emailAddress="%s" comment="%s"></domain>'
        return s % (name, ttl, emailAddress, comment)

    def create_domain(self, name, ttl, emailAddress, comment=""):
        domain = [name, ttl, emailAddress, comment]
        return self.create_domains([domain])[0]

    def create_domains(self, domains):
        xml = '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0">'
        ret = []
        for dom in domains:
            ret.append(self._domain(*dom))
        xml += "\n".join(ret)
        xml += "</domains>"
        response = self.make_request('POST', ['domains'], data=xml)
        output = self.wait_for_async_request(response)

        ret = []
        for domain in output['domains']:
            ret.append(Domain(connection=self, **domain))
        return ret

    def delete_domain(self, domain_id):
        return self.delete_domains([domain_id])

    def delete_domains(self, domains_id):
        ret = ["id=%s" % (i) for i in domains_id]
        response = self.make_request(
            'DELETE',
            ['domains'],
            parms=ret,
        )
        return self.wait_for_async_request(response)

    def import_domain(self, bind_zone):
        """
        Allows for a bind zone file to be imported in one operation.  The
        bind_zone parameter can be a string or a file object.
        """

        if type(bind_zone) is file:
            bind_zone = bind_zone.read()

        xml = '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0">'
        xml += '<domain contentType="BIND_9">'
        xml += '<contents>%s</contents>' % bind_zone
        xml += '</domain></domains>'

        response = self.make_request('POST', ['domains', 'import'], data=xml)
        output = self.wait_for_async_request(response)

        ret = []
        for domain in output['domains']:
            ret.append(Domain(self, **domain))
        return ret
Example #11
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory 
    for Container instances.

    @undocumented: cdn_connect
    @undocumented: http_connect
    @undocumented: cdn_request
    @undocumented: make_request
    @undocumented: _check_container_name
    """
    def __init__(self, username=None, api_key=None, **kwargs):
        """
        Accepts keyword arguments for Mosso username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword.
        
        @type username: str
        @param username: a Mosso username
        @type api_key: str
        @param api_key: a Mosso API key
        """
        self.cdn_enabled = False
        self.cdn_args = None
        self.connection_args = None
        self.cdn_connection = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        socket.setdefaulttimeout = int(kwargs.get('timeout', 5))
        self.auth = kwargs.has_key('auth') and kwargs['auth'] or None

        self.request_id = kwargs.get('request_id', None)
        # If use_cdn is set to False,
        # never make cdn requests regardless of whether cdn
        # is turned on for the account.
        self.use_cdn = kwargs.get('use_cdn', True)

        if not self.auth:
            authurl = kwargs.get('authurl', consts.default_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username, api_key, authurl)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.cdn_url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)
        self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                      HTTPConnection
        self.http_connect()
        if self.use_cdn and self.cdn_url:
            self.cdn_connect()

    def cdn_connect(self):
        """
        Setup the http connection instance for the CDN service.
        """
        (host, port, cdn_uri, is_ssl) = parse_url(self.cdn_url)
        conn_class = is_ssl and HTTPSConnection or HTTPConnection
        self.cdn_connection = conn_class(host, port)
        self.cdn_enabled = True

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port)
        self.connection.set_debuglevel(self.debuglevel)

    def cdn_request(self, method, path=[], data='', hdrs=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, performs an http request against the CDN service.
        """
        if not self.cdn_enabled:
            raise CDNNotEnabled()

        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join([quote(i) for i in path]))
        headers = {
            'Content-Length': len(data),
            'User-Agent': consts.user_agent,
            'X-Auth-Token': self.token
        }
        if isinstance(hdrs, dict):
            headers.update(hdrs)

        self._log_request(self.cdn_connection, method, path, headers)
        # Send the request
        self.cdn_connection.request(method, path, data, headers)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.cdn_connect()
            self.cdn_connection.request(method, path, data, headers)
            return self.cdn_connection.getresponse()

        try:
            response = self.cdn_connection.getresponse()
            self._log_response(response)
        except HTTPException as e:
            self._log_http_exception(e)
            response = retry_request()
            self._log_response(response, log.error)

        if response.status == 401:
            self._authenticate()
            response = retry_request()
            self._log_response(response, log.error)

        return response

    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters, 
        performs an http request.
        """
        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join([quote(i) for i in path]))

        if isinstance(parms, dict) and parms:
            query_args = \
                ['%s=%s' % (quote(x),quote(str(y))) for (x,y) in parms.items()]
            path = '%s?%s' % (path, '&'.join(query_args))

        headers = {
            'Content-Length': len(data),
            'User-Agent': consts.user_agent,
            'X-Auth-Token': self.token
        }
        isinstance(hdrs, dict) and headers.update(hdrs)

        self._log_request(self.connection, method, path, headers)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
            self._log_response(response)
        except HTTPException as e:
            self._log_http_exception(e)
            response = retry_request()
            self._log_response(response, log.error)

        if response.status == 401:
            self._authenticate()
            response = retry_request()
            self._log_response(response, log.error)

        return response

    def get_info(self):
        """
        Return tuple for number of containers and total bytes in the account

        >>> connection.get_info()
        (5, 2309749)

        @rtype: tuple
        @return: a tuple containing the number of containers and total bytes
                 used by the account
        """
        response = self.make_request('HEAD')
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-account-container-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-account-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return (count, size)

    def _check_container_name(self, container_name):
        if not container_name or \
                '/' in container_name or \
                len(container_name) > consts.container_name_limit:
            raise InvalidContainerName(container_name)

    def create_container(self, container_name):
        """
        Given a container name, returns a L{Container} item, creating a new
        Container if one does not already exist.

        >>> connection.create_container('new_container')
        <cloudfiles.container.Container object at 0xb77d628c>

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the newly created container
        """
        self._check_container_name(container_name)

        response = self.make_request('PUT', [container_name])
        buff = response.read()
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name)

    def delete_container(self, container_name):
        """
        Given a container name, delete it.

        >>> connection.delete_container('old_container')

        @param container_name: name of the container to delete
        @type container_name: str
        """
        if isinstance(container_name, Container):
            container_name = container_name.name
        self._check_container_name(container_name)

        response = self.make_request('DELETE', [container_name])
        buff = response.read()

        if (response.status == 409):
            raise ContainerNotEmpty(container_name)
        elif (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)

        if self.cdn_enabled:
            response = self.cdn_request('POST', [container_name],
                                        hdrs={'X-CDN-Enabled': 'False'})

    def get_all_containers(self, limit=None, marker=None, **parms):
        """
        Returns a Container item result set.

        >>> connection.get_all_containers()
        ContainerResults: 4 containers
        >>> print ', '.join([container.name for container in
                             connection.get_all_containers()])
        new_container, old_container, pictures, music

        @rtype: L{ContainerResults}
        @return: an iterable set of objects representing all containers on the
                 account
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        return ContainerResults(self, self.list_containers_info(**parms))

    def get_container(self, container_name):
        """
        Return a single Container item for the given Container.

        >>> connection.get_container('old_container')
        <cloudfiles.container.Container object at 0xb77d628c>
        >>> container = connection.get_container('old_container')
        >>> container.size_used
        23074

        @param container_name: name of the container to create
        @type container_name: str
        @rtype: L{Container}
        @return: an object representing the container
        """
        self._check_container_name(container_name)

        response = self.make_request('HEAD', [container_name])
        count = size = None
        for hdr in response.getheaders():
            if hdr[0].lower() == 'x-container-object-count':
                try:
                    count = int(hdr[1])
                except ValueError:
                    count = 0
            if hdr[0].lower() == 'x-container-bytes-used':
                try:
                    size = int(hdr[1])
                except ValueError:
                    size = 0
        buff = response.read()
        if response.status == 404:
            raise NoSuchContainer(container_name)
        if (response.status < 200) or (response.status > 299):
            raise ResponseError(response.status, response.reason)
        return Container(self, container_name, count, size)

    def list_public_containers(self):
        """
        Returns a list of containers that have been published to the CDN.

        >>> connection.list_public_containers()
        ['container1', 'container2', 'container3']

        @rtype: list(str)
        @return: a list of all CDN-enabled container names as strings
        """
        response = self.cdn_request('GET', [''])
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def list_containers_info(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers, including object count and size.

        >>> connection.list_containers_info()
        [{u'count': 510, u'bytes': 2081717, u'name': u'new_container'},
         {u'count': 12, u'bytes': 23074, u'name': u'old_container'},
         {u'count': 0, u'bytes': 0, u'name': u'container1'},
         {u'count': 0, u'bytes': 0, u'name': u'container2'},
         {u'count': 0, u'bytes': 0, u'name': u'container3'},
         {u'count': 3, u'bytes': 2306, u'name': u'test'}]

        @rtype: list({"name":"...", "count":..., "bytes":...})
        @return: a list of all container info as dictionaries with the
                 keys "name", "count", and "bytes"
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        parms['format'] = 'json'
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return json_loads(response.read())

    def list_containers(self, limit=None, marker=None, **parms):
        """
        Returns a list of Containers.

        >>> connection.list_containers()
        ['new_container',
         'old_container',
         'container1',
         'container2',
         'container3',
         'test']

        @rtype: list(str)
        @return: a list of all containers names as strings
        @param limit: number of results to return, up to 10,000
        @type limit: int
        @param marker: return only results whose name is greater than "marker"
        @type marker: str
        """
        if limit:
            parms['limit'] = limit
        if marker:
            parms['marker'] = marker
        response = self.make_request('GET', [''], parms=parms)
        if (response.status < 200) or (response.status > 299):
            buff = response.read()
            raise ResponseError(response.status, response.reason)
        return response.read().splitlines()

    def __getitem__(self, key):
        """
        Container objects can be grabbed from a connection using index
        syntax.

        >>> container = conn['old_container']
        >>> container.size_used
        23074

        @rtype: L{Container}
        @return: an object representing the container
        """
        return self.get_container(key)

    # Request/Response Logging {{{
    def _log_response(self, response, logfn=log.debug):
        logfn('Request ID: %s, Response: status=%s; headers="%s"' %
              (self.request_id, str(response.status), "; ".join(
                  ["%s=%s" % (k, v) for (k, v) in response.getheaders()])))

    def _log_request(self, connection, method, path, headers, logfn=log.debug):
        host, port = connection.host, str(connection.port)
        logfn(
            'Request ID: %(request_id)s, %(method)s %(uri)s, headers="%(headers)s"'
            % dict(request_id=self.request_id,
                   method=method,
                   uri="%s:%s%s" % (host, port, path),
                   headers=headers))

    def _log_http_exception(self, e):
        log.error('Request ID: %s, HttpException: %s' %
                  (self.request_id, repr(e)))
Example #12
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    @undocumented: http_connect
    @undocumented: make_request
    @undocumented: _check_container_name
    """

    def __init__(self, username=None, api_key=None, timeout=10, **kwargs):
        """
        Accepts keyword arguments for Rackspace Cloud username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword.

        @type username: str
        @param username: a Rackspace Cloud username
        @type api_key: str
        @param api_key: a Rackspace Cloud API key
        """
        self.connection_args = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get("debuglevel", 0))
        self.user_agent = kwargs.get("useragent", consts.user_agent)
        self.timeout = timeout

        self.auth = "auth" in kwargs and kwargs["auth"] or None

        if not self.auth:
            authurl = kwargs.get("authurl", consts.us_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username, api_key, authurl=authurl, useragent=self.user_agent)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or HTTPConnection
        self.http_connect()

    def convert_iso_datetime(self, dt):
        """
        Convert iso8601 to datetime
        """
        isoFormat = "%Y-%m-%dT%H:%M:%S.000+0000"
        if type(dt) is datetime.datetime:
            return dt
        if dt.endswith("Z"):
            dt = dt.split("Z")[0]
            isoFormat = "%Y-%m-%dT%H:%M:%S"
        return datetime.datetime.strptime(dt, isoFormat)

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port, timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

    def make_request(self, method, path=[], data="", hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        """
        query_args = ""
        path = "/%s/%s" % (self.uri.rstrip("/"), "/".join([unicode_quote(i) for i in path]))
        if isinstance(parms, dict) and parms:
            query_args = ["%s=%s" % (quote(x), quote(str(y))) for (x, y) in parms.items()]
        elif isinstance(parms, list) and parms:
            query_args = ["%s" % x for x in parms]
        path = "%s?%s" % (path, "&".join(query_args))

        headers = {
            "Content-Length": str(len(data)),
            "User-Agent": self.user_agent,
            "X-Auth-Token": self.token,
            "Content-Type": "application/xml",
        }
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            """Re-connect and re-try a failed request once"""
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            if "PYTHON_CLOUDDNS_DEBUG" in os.environ and os.environ["PYTHON_CLOUDDNS_DEBUG"].strip():
                import sys

                url = "https://%s%s\n" % (self.connection_args[0], path)
                sys.stderr.write("METHOD: %s\n" % (str(method)))
                sys.stderr.write("URL: %s" % (url))
                sys.stderr.write("HEADERS: %s\n" % (str(headers)))
                sys.stderr.write("DATA: %s\n" % (str(data)))
                sys.stderr.write("curl -X '%s' -H 'X-Auth-Token: %s' %s %s" % (method, self.token, url, str(data)))
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers["X-Auth-Token"] = self.token
            response = retry_request()
        return response

    def get_domains(self):
        return DomainResults(self, self.list_domains_info())

    def list_domains_info(self, filter_by_name=None):
        parms = {}
        if filter_by_name:
            parms = {"name": filter_by_name}
        response = self.make_request("GET", ["domains"], parms=parms)
        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        read_output = response.read()
        return json.loads(read_output)["domains"]

    def get_domain(self, id=None, **dico):
        filter_by_name = ""
        if id:
            dico["id"] = id
        if "name" in dico:
            dico["name"] = dico["name"].lower()
            filter_by_name = dico["name"]

        domains = self.list_domains_info(filter_by_name=filter_by_name)
        for domain in domains:
            for k in dico:
                if k in domain and domain[k] == dico[k]:
                    return Domain(self, **domain)
        # TODO:
        raise Exception("Not found")

    # Take a reponse parse it if there is asyncResponse and wait for
    # it (TODO: should offer to not)
    def wait_for_async_request(self, response):
        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        output = json.loads(response.read())
        while True:
            if "callbackUrl" in output:
                jobId = output["jobId"]
                response = self.make_request("GET", ["status", jobId])
                if (response.status < 200) or (response.status > 299):
                    response.read()
                    raise ResponseError(response.status, response.reason)
                _output = response.read().strip()
                if not _output:
                    return True
                output = json.loads(_output)
                time.sleep(1)
                continue
            elif "DnsFault" in output:
                raise ResponseError(output["DnsFault"]["code"], output["DnsFault"]["message"])
            else:
                return output

    def _domain(self, name, ttl, emailAddress, comment):
        if not ttl >= 300:
            raise Exception("Ttl is a minimun of 300 seconds")
        s = '<domain name="%s" ttl="%s" emailAddress="%s" comment="%s"></domain>'
        return s % (name, ttl, emailAddress, comment)

    def create_domain(self, name, ttl, emailAddress, comment=""):
        domain = [name, ttl, emailAddress, comment]
        return self.create_domains([domain])[0]

    def create_domains(self, domains):
        xml = '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0">'
        ret = []
        for dom in domains:
            ret.append(self._domain(*dom))
        xml += "\n".join(ret)
        xml += "</domains>"
        response = self.make_request("POST", ["domains"], data=xml)
        output = self.wait_for_async_request(response)

        ret = []
        for domain in output["domains"]:
            ret.append(Domain(connection=self, **domain))
        return ret

    def delete_domain(self, domain_id):
        return self.delete_domains([domain_id])

    def delete_domains(self, domains_id):
        ret = ["id=%s" % (i) for i in domains_id]
        response = self.make_request("DELETE", ["domains"], parms=ret)
        return self.wait_for_async_request(response)
Example #13
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    @undocumented: http_connect
    @undocumented: make_request
    @undocumented: _check_container_name
    """
    def __init__(self, username=None, api_key=None, timeout=10, **kwargs):
        """
        Accepts keyword arguments for Rackspace Cloud username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword.

        @type username: str
        @param username: a Rackspace Cloud username
        @type api_key: str
        @param api_key: a Rackspace Cloud API key
        """
        self.connection_args = None
        self.connection = None
        self.token = None
        self.debuglevel = int(kwargs.get('debuglevel', 0))
        self.user_agent = kwargs.get('useragent', consts.user_agent)
        self.timeout = timeout

        self.auth = 'auth' in kwargs and kwargs['auth'] or None

        if not self.auth:
            authurl = kwargs.get('authurl', consts.us_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username,
                                           api_key,
                                           authurl=authurl,
                                           useragent=self.user_agent)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or \
                                                              THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or \
                                                              HTTPConnection
        self.http_connect()

    def convert_iso_datetime(self, dt):
        """
        Convert iso8601 to datetime
        """
        isoFormat = "%Y-%m-%dT%H:%M:%S.000+0000"
        if type(dt) is datetime.datetime:
            return dt
        if dt.endswith("Z"):
            dt = dt.split('Z')[0]
            isoFormat = "%Y-%m-%dT%H:%M:%S"
        return datetime.datetime.strptime(dt, isoFormat)

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port, \
                                              timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

    def make_request(self, method, path=[], data='', hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        """
        query_args = ""
        path = '/%s/%s' % \
                 (self.uri.rstrip('/'), '/'.join(
                   [unicode_quote(i) for i in path]))
        if isinstance(parms, dict) and parms:
            query_args = \
                ['%s=%s' % (quote(x),
                            quote(str(y))) for (x, y) in parms.items()]
        elif isinstance(parms, list) and parms:
            query_args = \
                ["%s" % x for x in parms]
        path = '%s?%s' % (path, '&'.join(query_args))

        headers = {
            'Content-Length': str(len(data)),
            'User-Agent': self.user_agent,
            'X-Auth-Token': self.token
        }
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            '''Re-connect and re-try a failed request once'''
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            if 'PYTHON_CLOUDDNS_DEBUG' in os.environ and \
                    os.environ['PYTHON_CLOUDDNS_DEBUG'].strip():
                import sys
                url = "https://%s%s\n" % \
                    (self.connection_args[0],
                     path)
                sys.stderr.write("METHOD: %s\n" % (str(method)))
                sys.stderr.write("URL: %s" % (url))
                sys.stderr.write("HEADERS: %s\n" % (str(headers)))
                sys.stderr.write("DATA: %s\n" % (str(data)))
                sys.stderr.write("curl -X '%s' -H 'X-Auth-Token: %s' %s %s" % \
                                     (method, self.token, url, str(data)))
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers['X-Auth-Token'] = self.token
            response = retry_request()
        return response

    def get_domains(self):
        return DomainResults(self, self.list_domains_info())

    def list_domains_info(self, filter_by_name=None):
        parms = {}
        if filter_by_name:
            parms = {'name': filter_by_name}
        response = self.make_request('GET', ['domains'], parms=parms)
        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        read_output = response.read()
        return json.loads(read_output)['domains']

    def get_domain(self, id=None, **dico):
        filter_by_name = ""
        if id:
            dico['id'] = id
        if 'name' in dico:
            dico['name'] = dico['name'].lower()
            filter_by_name = dico['name']

        domains = self.list_domains_info(filter_by_name=filter_by_name)
        for domain in domains:
            for k in dico:
                if k in domain and domain[k] == dico[k]:
                    return Domain(self, **domain)
        #TODO:
        raise Exception("Not found")

    # Take a reponse parse it if there is asyncResponse and wait for
    # it (TODO: should offer to not)
    def wait_for_async_request(self, response):
        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        output = json.loads(response.read())
        jobId = output['jobId']
        while True:
            response = self.make_request('GET', ['status', jobId],
                                         parms=['showDetails=True'])
            if (response.status < 200) or (response.status > 299):
                response.read()
                raise ResponseError(response.status, response.reason)
            _output = response.read().strip()
            output = json.loads(_output)
            if output['status'] == 'COMPLETED':
                try:
                    return output['response']
                except KeyError:
                    return output
            if output['status'] == 'ERROR':
                raise ResponseError(output['error']['code'],
                                    output['error']['details'])
            time.sleep(1)
            continue

    def _domain(self, name, ttl, emailAddress, comment=""):
        if not ttl >= 300:
            raise Exception("Ttl is a minimun of 300 seconds")
        s = '<domain name="%s" ttl="%s" emailAddress="%s" comment="%s"></domain>'
        return s % (name, ttl, emailAddress, comment)

    def create_domain(self, name, ttl, emailAddress, comment=""):
        domain = [name, ttl, emailAddress, comment]
        return self.create_domains([domain])[0]

    def create_domains(self, domains):
        xml = '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0">'
        ret = []
        for dom in domains:
            ret.append(self._domain(*dom))
        xml += "\n".join(ret)
        xml += "</domains>"
        response = self.make_request('POST', ['domains'], data=xml)
        output = self.wait_for_async_request(response)

        ret = []
        for domain in output['domains']:
            ret.append(Domain(connection=self, **domain))
        return ret

    def delete_domain(self, domain_id):
        return self.delete_domains([domain_id])

    def delete_domains(self, domains_id):
        ret = ["id=%s" % (i) for i in domains_id]
        response = self.make_request(
            'DELETE',
            ['domains'],
            parms=ret,
        )
        return self.wait_for_async_request(response)
Example #14
0
class Connection(object):
    """
    Manages the connection to the storage system and serves as a factory
    for Container instances.

    @undocumented: http_connect
    @undocumented: make_request
    @undocumented: _check_container_name
    """

    def __init__(self, username=None, api_key=None, timeout=10, **kwargs):
        """
        Accepts keyword arguments for Rackspace Cloud username and api key.
        Optionally, you can omit these keywords and supply an
        Authentication object using the auth keyword.

        @type username: str
        @param username: a Rackspace Cloud username
        @type api_key: str
        @param api_key: a Rackspace Cloud API key
        """
        self.connection_args = None
        self.connection = None
        self.defaultAccountId = None
        self.accountId = None
        self.token = None
        self.debuglevel = int(kwargs.get("debuglevel", 0))
        self.user_agent = kwargs.get("useragent", consts.user_agent)
        self.timeout = timeout

        self.auth = "auth" in kwargs and kwargs["auth"] or None

        if not self.auth:
            authurl = kwargs.get("authurl", consts.us_authurl)
            if username and api_key and authurl:
                self.auth = Authentication(username, api_key, authurl=authurl, useragent=self.user_agent)
            else:
                raise TypeError("Incorrect or invalid arguments supplied")

        self._authenticate()

    def _authenticate(self):
        """
        Authenticate and setup this instance with the values returned.
        """
        (url, self.token) = self.auth.authenticate()
        self.connection_args = parse_url(url)

        if version_info[0] <= 2 and version_info[1] < 6:
            self.conn_class = self.connection_args[3] and THTTPSConnection or THTTPConnection
        else:
            self.conn_class = self.connection_args[3] and HTTPSConnection or HTTPConnection
        self.http_connect()

    def convert_iso_datetime(self, dt):
        """
        Convert iso8601 to datetime
        """
        isoFormat = "%Y-%m-%dT%H:%M:%S.000+0000"
        if type(dt) is datetime.datetime:
            return dt
        if dt.endswith("Z"):
            dt = dt.split("Z")[0]
            isoFormat = "%Y-%m-%dT%H:%M:%S"
        return datetime.datetime.strptime(dt, isoFormat)

    def http_connect(self):
        """
        Setup the http connection instance.
        """
        (host, port, self.uri, is_ssl) = self.connection_args
        self.connection = self.conn_class(host, port=port, timeout=self.timeout)
        self.connection.set_debuglevel(self.debuglevel)

        self.set_account()

    def make_request(self, method, path=[], data="", hdrs=None, parms=None):
        """
        Given a method (i.e. GET, PUT, POST, etc), a path, data, header and
        metadata dicts, and an optional dictionary of query parameters,
        performs an http request.
        """
        query_args = ""
        path = "/%s/%s" % (self.uri.rstrip("/"), "/".join([unicode_quote(i) for i in path]))
        if isinstance(parms, dict) and parms:
            query_args = ["%s=%s" % (quote(x), quote(str(y))) for (x, y) in parms.items()]
        elif isinstance(parms, list) and parms:
            query_args = ["%s" % x for x in parms]
        path = "%s?%s" % (path, "&".join(query_args))

        headers = {"Content-Length": str(len(data)), "User-Agent": self.user_agent, "X-Auth-Token": self.token}
        isinstance(hdrs, dict) and headers.update(hdrs)

        def retry_request():
            """Re-connect and re-try a failed request once"""
            self.http_connect()
            self.connection.request(method, path, data, headers)
            return self.connection.getresponse()

        try:
            if "PYTHON_CLOUDDNS_DEBUG" in os.environ and os.environ["PYTHON_CLOUDDNS_DEBUG"].strip():
                import sys

                url = "https://%s%s\n" % (self.connection_args[0], path)
                sys.stderr.write("METHOD: %s\n" % (str(method)))
                sys.stderr.write("URL: %s" % (url))
                sys.stderr.write("HEADERS: %s\n" % (str(headers)))
                sys.stderr.write("DATA: %s\n" % (str(data)))
                sys.stderr.write("curl -X '%s' -H 'X-Auth-Token: %s' %s %s" % (method, self.token, url, str(data)))
            self.connection.request(method, path, data, headers)
            response = self.connection.getresponse()
        except (socket.error, IOError, HTTPException):
            response = retry_request()
        if response.status == 401:
            self._authenticate()
            headers["X-Auth-Token"] = self.token
            response = retry_request()
        return response

    def set_account(self, newAccountId=None):
        """Handles setting of accountId for accessing other clients"""

        # Split out the account ID off the DNS Management URL
        (baseUri, sep, oldAccountId) = self.uri.rstrip("/").rpartition("/")

        if not self.defaultAccountId:  # First call, set default
            self.defaultAccountId = oldAccountId

        if newAccountId:  # Set the new accountId
            self.uri = baseUri + "/" + newAccountId
            self.accountId = newAccountId
        else:  # set back to default
            # This could be a self call, but for now we will just set the two vars
            # self.set_account(self.defaultAccountId)
            self.uri = baseUri + "/" + self.defaultAccountId
            self.accountId = self.defaultAccountId

    def get_accountId(self):
        return self.accountId

    def get_domains(self):
        return DomainResults(self, self.list_domains_info())

    def list_domains_info(self, filter_by_name=None):
        parms = {}
        if filter_by_name:
            parms = {"name": filter_by_name}
        response = self.make_request("GET", ["domains"], parms=parms)
        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        read_output = response.read()
        return json.loads(read_output)["domains"]

    def get_domain(self, id=None, **dico):
        filter_by_name = ""
        if id:
            dico["id"] = id
        if "name" in dico:
            dico["name"] = dico["name"].lower()
            filter_by_name = dico["name"]

        domains = self.list_domains_info(filter_by_name=filter_by_name)
        for domain in domains:
            for k in dico:
                if k in domain and domain[k] == dico[k]:
                    return Domain(self, **domain)
        raise UnknownDomain("Not found")

    def get_domain_details(self, id=None):
        """Get details on a particular domain"""
        parms = {"showRecords": "false", "showSubdomains": "false"}
        response = self.make_request("GET", ["domains", str(id)], parms=parms)

        if (response.status < 200) or (response.status > 299):
            response.read()
            raise ResponseError(response.status, response.reason)
        read_output = response.read()
        domains = json.loads(read_output)

        return Domain(self, **domains)

    # Take a reponse parse it if there is asyncResponse and wait for
    # it (TODO: should offer to not)
    def wait_for_async_request(self, response):
        if (response.status < 200) or (response.status > 299):
            _output = response.read().strip()
            try:
                output = json.loads(_output)
            except ValueError:
                output = None
            api_reasons = ""
            if output and "validationErrors" in output:
                for msg in output["validationErrors"]["messages"]:
                    api_reasons += " (%s)" % msg
            raise ResponseError(response.status, response.reason + api_reasons)
        output = json.loads(response.read())
        jobId = output["jobId"]
        while True:
            response = self.make_request("GET", ["status", jobId], parms=["showDetails=True"])
            if (response.status < 200) or (response.status > 299):
                response.read()
                raise ResponseError(response.status, response.reason)
            _output = response.read().strip()
            output = json.loads(_output)
            if output["status"] == "COMPLETED":
                try:
                    return output["response"]
                except KeyError:
                    return output
            if output["status"] == "ERROR":
                if output["error"]["code"] == 409 and output["error"]["details"] == "Domain already exists":
                    raise DomainAlreadyExists
                if output["error"]["code"] == 409 and output["error"]["details"].find("belongs to another owner"):
                    raise NotDomainOwner
                raise ResponseError(output["error"]["code"], output["error"]["details"])
            time.sleep(1)
            continue

    def _domain(self, name, ttl, emailAddress, comment=""):
        if not ttl >= 300:
            raise Exception("Ttl is a minimun of 300 seconds")
        s = '<domain name="%s" ttl="%s" emailAddress="%s" comment="%s"></domain>'
        return s % (name, ttl, emailAddress, comment)

    def create_domain(self, name, ttl, emailAddress, comment=""):
        domain = [name, ttl, emailAddress, comment]
        return self.create_domains([domain])[0]

    def create_domains(self, domains):
        xml = '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0">'
        ret = []
        for dom in domains:
            ret.append(self._domain(*dom))
        xml += "\n".join(ret)
        xml += "</domains>"
        response = self.make_request("POST", ["domains"], data=xml)
        output = self.wait_for_async_request(response)

        ret = []
        for domain in output["domains"]:
            ret.append(Domain(connection=self, **domain))
        return ret

    def delete_domain(self, domain_id):
        return self.delete_domains([domain_id])

    def delete_domains(self, domains_id):
        ret = ["id=%s" % (i) for i in domains_id]
        response = self.make_request("DELETE", ["domains"], parms=ret)
        return self.wait_for_async_request(response)

    def import_domain(self, bind_zone):
        """
        Allows for a bind zone file to be imported in one operation.  The
        bind_zone parameter can be a string or a file object.
        """

        if type(bind_zone) is file:
            bind_zone = bind_zone.read()

        xml = '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0">'
        xml += '<domain contentType="BIND_9">'
        xml += "<contents>%s</contents>" % bind_zone
        xml += "</domain></domains>"

        response = self.make_request("POST", ["domains", "import"], data=xml)
        output = self.wait_for_async_request(response)

        ret = []
        for domain in output["domains"]:
            ret.append(Domain(self, **domain))
        return ret