~jaypipes/glance/d3-proposed

« back to all changes in this revision

Viewing changes to glance/store/swift.py

  • Committer: Tarmac
  • Author(s): jaypipes at gmail
  • Date: 2011-07-28 18:43:52 UTC
  • mfrom: (163.3.6 bug817121)
  • Revision ID: tarmac-20110728184352-lj7y4jzr2297p4ia
Add more unit tests for URI parsing and get_backend_class() (which is going away in refactor-stores branch, but oh well..)

Now raising exception.BadStoreUri if glance.store.location.StoreLocation.parse_uri() is passed any URI that looks like this:

swift://user:pass@http://authurl.com/v1/container/obj

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
    the following:
45
45
 
46
46
        swift://user:pass@authurl.com/container/obj-id
 
47
        swift://account:user:pass@authurl.com/container/obj-id
47
48
        swift+http://user:pass@authurl.com/container/obj-id
48
49
        swift+https://user:pass@authurl.com/container/obj-id
49
50
 
50
 
    The swift+https:// URIs indicate there is an HTTPS authentication URL
 
51
    The swift+http:// URIs indicate there is an HTTP authentication URL.
 
52
    The default for Swift is an HTTPS authentication URL, so swift:// and
 
53
    swift+https:// are the same...
51
54
    """
52
55
 
53
56
    def process_specs(self):
64
67
        return ''
65
68
 
66
69
    def get_uri(self):
 
70
        authurl = self.authurl
 
71
        if authurl.startswith('http://'):
 
72
            authurl = authurl[7:]
 
73
        elif authurl.startswith('https://'):
 
74
            authurl = authurl[8:]
67
75
        return "%s://%s%s/%s/%s" % (
68
76
            self.scheme,
69
77
            self._get_credstring(),
70
 
            self.authurl,
 
78
            authurl,
71
79
            self.container,
72
80
            self.obj)
73
81
 
80
88
 
81
89
            swift://account:user:pass@authurl.com/container/obj
82
90
        """
 
91
        # Make sure that URIs that contain multiple schemes, such as:
 
92
        # swift://user:pass@http://authurl.com/v1/container/obj
 
93
        # are immediately rejected.
 
94
        if uri.count('://') != 1:
 
95
            reason = ("URI Cannot contain more than one occurrence of a "
 
96
                      "scheme. If you have specified a "
 
97
                      "URI like swift://user:pass@http://authurl.com/v1/"
 
98
                      "container/obj, you need to change it to use the "
 
99
                      "swift+http:// scheme, like so: "
 
100
                      "swift+http://user:pass@authurl.com/v1/container/obj")
 
101
            raise exception.BadStoreUri(uri, reason)
 
102
 
83
103
        pieces = urlparse.urlparse(uri)
84
104
        assert pieces.scheme in ('swift', 'swift+http', 'swift+https')
85
105
        self.scheme = pieces.scheme
122
142
        try:
123
143
            self.obj = path_parts.pop()
124
144
            self.container = path_parts.pop()
125
 
            self.authurl = netloc
126
 
            if len(path_parts) > 0:
127
 
                self.authurl = netloc + '/' + '/'.join(path_parts).strip('/')
 
145
            if not netloc.startswith('http'):
 
146
                # push hostname back into the remaining to build full authurl
 
147
                path_parts.insert(0, netloc)
 
148
                self.authurl = '/'.join(path_parts)
128
149
        except IndexError:
129
150
            reason = "Badly formed Swift URI"
130
151
            raise exception.BadStoreUri(uri, reason)
138
159
 
139
160
        HTTPS is assumed, unless 'swift+http' is specified.
140
161
        """
141
 
        if self.scheme.startswith('swift+http'):
 
162
        if self.scheme in ('swift+https', 'swift'):
 
163
            auth_scheme = 'https://'
 
164
        else:
142
165
            auth_scheme = 'http://'
143
 
        elif self.scheme.startswith('swift+https'):
144
 
            auth_scheme = 'https://'
145
 
        elif self.scheme.startswith('swift'):
146
 
            auth_scheme = 'https://'
147
 
        else:
148
 
            logger.warn("Unrecognized scheme '%s', defaulting auth url"
149
 
                        " to https", self.scheme)
150
 
            auth_scheme = 'https://'
151
166
 
152
167
        full_url = ''.join([auth_scheme, self.authurl])
153
168
        return full_url