~rackspace-titan/glance/glance-clear-lp766295

« back to all changes in this revision

Viewing changes to glance/store/swift.py

  • Committer: Tarmac
  • Author(s): Eldar Nugaev
  • Date: 2011-04-19 16:32:41 UTC
  • mfrom: (113.1.1 trunk)
  • Revision ID: tarmac-20110419163241-00lnkrp38u8iohaw
Fixing tests.
Sorry for late response. 

Rick Harris wrote on 2011-04-12
Really nice job, Eldar.

> 46 +        image_swift = StringIO.StringIO("nevergonnamakeit")
> 47 +        options = SWIFT_OPTIONS.copy()
> 48 +        del options['swift_store_user']
> 49 +        self.assertRaises(BackendException,
> 50 +                          SwiftBackend.add,
> 51 +                          2, image_swift, options)

Might be better to DRY up the code some by adding a helper-method, like:

def assertOptionRequiredForSwift(self, key):
  image_swift = StringIO.StringIO("nevergonnamakeit")
  options = SWIFT_OPTIONS.copy()
  del options[key]
  self.assertRaises(BackendException,
    SwiftBackend.add,
    2, image_swift, options)

Marking as Needs Fixing, but if you don't like the idea, I'd be willing to set this as Approved (since the suggestion is so minor :-)

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
        return resp_body
77
77
 
78
78
    @classmethod
 
79
    def _option_get(cls, options, param):
 
80
        result = options.get(param)
 
81
        if not result:
 
82
            msg = ("Could not find %s in configuration options." % param)
 
83
            logger.error(msg)
 
84
            raise glance.store.BackendException(msg)
 
85
        return result
 
86
 
 
87
    @classmethod
79
88
    def add(cls, id, data, options):
80
89
        """
81
90
        Stores image data to Swift and returns a location that the image was
101
110
        from swift.common import client as swift_client
102
111
        container = options.get('swift_store_container',
103
112
                                DEFAULT_SWIFT_CONTAINER)
104
 
        auth_address = options.get('swift_store_auth_address')
105
 
        user = options.get('swift_store_user')
106
 
        key = options.get('swift_store_key')
107
113
 
108
114
        # TODO(jaypipes): This needs to be checked every time
109
115
        # because of the decision to make glance.store.Backend's
110
116
        # interface all @classmethods. This is inefficient. Backend
111
117
        # should be a stateful object with options parsed once in
112
118
        # a constructor.
113
 
        if not auth_address:
114
 
            msg = ("Could not find swift_store_auth_address in configuration "
115
 
                   "options.")
116
 
            logger.error(msg)
117
 
            raise glance.store.BackendException(msg)
118
 
        else:
119
 
            full_auth_address = auth_address
120
 
            if not full_auth_address.startswith('http'):
121
 
                full_auth_address = 'https://' + full_auth_address
122
 
 
123
 
        if not user:
124
 
            msg = ("Could not find swift_store_user in configuration "
125
 
                   "options.")
126
 
            logger.error(msg)
127
 
            raise glance.store.BackendException(msg)
128
 
 
129
 
        if not key:
130
 
            msg = ("Could not find swift_store_key in configuration "
131
 
                   "options.")
132
 
            logger.error(msg)
133
 
            raise glance.store.BackendException(msg)
 
119
        auth_address = cls._option_get(options, 'swift_store_auth_address')
 
120
        user = cls._option_get(options, 'swift_store_user')
 
121
        key = cls._option_get(options, 'swift_store_key')
 
122
 
 
123
        full_auth_address = auth_address
 
124
        if not full_auth_address.startswith('http'):
 
125
            full_auth_address = 'https://' + full_auth_address
134
126
 
135
127
        swift_conn = swift_client.Connection(
136
128
            authurl=full_auth_address, user=user, key=key, snet=False)