Esempio n. 1
0
    def GET(self, req, *parts):
        """
        Handle GET Container (List Objects) request
        """

        env = self._fresh_env(req)
        env['PATH_INFO'] = concat(self.os_path,
                                  '/', '/'.join(parts))

        new_req = Request(env)
        res = new_req.get_response(self.app)
        if res.status_int == 200:
            flavor = json.loads(res.body).get('flavor')
            if flavor:
                body = {}
                body['resourceURI'] = '/'.join([self.uri_prefix,
                                                self.entity_uri])
                body['id'] = '/'.join([self.tenant_id, self.entity_uri,
                                       self.config_id])
                match_up(body, flavor, 'name', 'name')
                match_up(body, flavor, 'cpu', 'vcpus')
                body['memory'] = int(flavor.get('ram')) * 1000
                body['disks'] = [{'capacity': int(flavor.get('disk')) * 1000,
                                  'format': 'UNKNOWN'}]

            if self.res_content_type == 'application/xml':
                response_data = {self.entity_uri: body}
                remove_member(response_data, 'resourceURI')
            else:
                response_data = body

            new_content = make_response_data(response_data,
                                             self.res_content_type,
                                             self.metadata,
                                             self.uri_prefix)
            resp = Response()
            self._fixup_cimi_header(resp)
            resp.headers['Content-Type'] = self.res_content_type
            resp.status = 200
            resp.body = new_content
            return resp
        else:
            return res

        return res
Esempio n. 2
0
    def GET(self, req, *parts):
        """
        Handle GET Container (List Objects) request
        """

        env = self._fresh_env(req)
        env['PATH_INFO'] = concat(self.os_path, '/', '/'.join(parts))

        new_req = Request(env)
        res = new_req.get_response(self.app)
        if res.status_int == 200:
            flavor = json.loads(res.body).get('flavor')
            if flavor:
                body = {}
                body['resourceURI'] = '/'.join(
                    [self.uri_prefix, self.entity_uri])
                body['id'] = '/'.join(
                    [self.tenant_id, self.entity_uri, self.config_id])
                match_up(body, flavor, 'name', 'name')
                match_up(body, flavor, 'cpu', 'vcpus')
                body['memory'] = int(flavor.get('ram')) * 1000
                body['disks'] = [{
                    'capacity': int(flavor.get('disk')) * 1000,
                    'format': 'UNKNOWN'
                }]

            if self.res_content_type == 'application/xml':
                response_data = {self.entity_uri: body}
                remove_member(response_data, 'resourceURI')
            else:
                response_data = body

            new_content = make_response_data(response_data,
                                             self.res_content_type,
                                             self.metadata, self.uri_prefix)
            resp = Response()
            self._fixup_cimi_header(resp)
            resp.headers['Content-Type'] = self.res_content_type
            resp.status = 200
            resp.body = new_content
            return resp
        else:
            return res

        return res
Esempio n. 3
0
    def GET(self, req, *parts):
        """
        Handle GET machineVolumeCollection request
        """

        env = self._fresh_env(req)

        env['PATH_INFO'] = concat(self.os_path, '/',
                                  parts[0], '/os-volume_attachments')
        new_req = Request(env)
        res = new_req.get_response(self.app)

        if res.status_int == 200:
            content = json.loads(res.body)
            body = {}
            body['id'] = concat(self.tenant_id,
                                '/', self.entity_uri, '/', parts[0])
            body['resourceURI'] = concat(self.uri_prefix, '/',
                                            self.entity_uri)

            body['machineVolumes'] = []
            volumeAttachments = content.get('volumeAttachments', [])
            for data in volumeAttachments:
                entry = {}
                if self.res_content_type == 'application/json':
                    entry['resourceURI'] = concat(self.uri_prefix,
                                            '/MachineVolume')
                entry['id'] = concat(self.tenant_id, '/',
                                     'machineVolume/',
                                     data['serverId'], '/',
                                     data['id'])
                entry['initialLocation'] = data['device']
                entry['volume'] = {'href': concat(self.tenant_id,
                    '/Volume/', data['volumeId'])}

                operations = []
                operations.append(self._create_op('edit', entry['id']))
                operations.append(self._create_op('delete', entry['id']))
                entry['operations'] = operations

                body['machineVolumes'].append(entry)

            body['count'] = len(body['machineVolumes'])
            # deal with machinevolume operations
            operations = []
            operations.append(self._create_op('add', body['id']))
            body['operations'] = operations

            if self.res_content_type == 'application/xml':
                response_data = {'Collection': body}
                remove_member(response_data, 'resourceURI')
            else:
                response_data = body

            new_content = make_response_data(response_data,
                                             self.res_content_type,
                                             self.metadata,
                                             self.uri_prefix)
            resp = Response()
            self._fixup_cimi_header(resp)
            resp.headers['Content-Type'] = self.res_content_type
            resp.status = 200
            resp.body = new_content

            return resp
        else:
            return res
Esempio n. 4
0
    def GET(self, req, *parts):
        """
        Handle GET Container (List Objects) request
        """

        env = copy.copy(req.environ)

        env['SCRIPT_NAME'] = self.os_version
        env['PATH_INFO'] = self.os_path
        # we will always use json format to get Nova information
        env['HTTP_ACCEPT'] = 'application/json'

        # need to remove this header, otherwise, it will always take the
        # original request accept content type
        if env.has_key('nova.best_content_type'):
            env.pop('nova.best_content_type')
        new_req = Request(env)

        res = new_req.get_response(self.app)
        if res.status_int == 200:
            content = json.loads(res.body)
            body = {}
            body['id'] = '/'.join([self.tenant_id, self.entity_uri])
            body['machineImages'] = []
            images = content.get('images', [])
            for image in images:
                entry = {}
                entry['resourceURI'] = '/'.join([self.uri_prefix,
                                                 'MachineImage'])
                entry['id'] = '/'.join([self.tenant_id,
                                     'MachineImage',
                                     image['id']])
                entry['type'] = 'IMAGE'
                entry['name'] = image['name']
                entry['created'] = image['created']
                entry['updated'] = image['updated']
                entry['state'] = map_image_state(image['status'])
                entry['imageLocation'] = entry['id']

                body['machineImages'].append(entry)

            body['count'] = len(body['machineImages'])
            if self.res_content_type == 'application/xml':
                remove_member(body, 'resourceURI')
                body['resourceURI'] = '/'.join([self.uri_prefix,
                                                self.entity_uri])
                response_data = {'Collection': body}
            else:
                body['resourceURI'] = '/'.join([self.uri_prefix,
                                                self.entity_uri])
                response_data = body

            new_content = make_response_data(response_data,
                                             self.res_content_type,
                                             self.metadata,
                                             self.uri_prefix)

            resp = Response()
            self._fixup_cimi_header(resp)
            resp.headers['Content-Type'] = self.res_content_type
            resp.status = 200
            resp.body = new_content
            return resp
        else:
            return res
Esempio n. 5
0
    def GET(self, req, *parts):
        """
        Handle GET Container (List Objects) request
        """

        env = self._fresh_env(req)
        env['PATH_INFO'] = concat(self.os_path,
                                  '/', '/'.join(parts))
        new_req = Request(env)
        res = new_req.get_response(self.app)

        if res.status_int == 200:
            data = json.loads(res.body).get('server')

            body = {}
            body['id'] = concat(self.tenant_id, '/Machine/',
                                parts[0])
            match_up(body, data, 'name', 'name')
            match_up(body, data, 'created', 'created')
            match_up(body, data, 'updated', 'updated')
            body['state'] = map_machine_state(data['status'])

            body['networkInterfaces'] = {'href': '/'.join([self.tenant_id,
                'NetworkInterfacesCollection', parts[0]])}

            body['volumes'] = {'href': '/'.join([self.tenant_id,
                'MachineVolumeCollection', parts[0]])}
            body['disks'] = {'href': '/'.join([self.tenant_id,
                'MachineDiskCollection', parts[0]])}


            # Send a request to get the details on flavor
            env = self._fresh_env(req)
            env['PATH_INFO'] = '/%s/flavors/%s' % (self.tenant_id,
                                                   data['flavor']['id'])
            new_req = Request(env)
            res = new_req.get_response(self.app)
            if res.status_int == 200:
                flavor = json.loads(res.body).get('flavor')
                match_up(body, flavor, 'cpu', 'vcpus')
                body['memory'] = int(flavor.get('ram')) * 1000

            # deal with machine operations
            operations = []
            action_url = '/'.join([self.tenant_id, 'Machine', parts[0]])

            action_name = '/'.join([self.uri_prefix, 'action/start'])
            operations.append(self._create_op(action_name, action_url))

            action_name = '/'.join([self.uri_prefix, 'action/stop'])
            operations.append(self._create_op(action_name, action_url))

            action_name = '/'.join([self.uri_prefix, 'action/restart'])
            operations.append(self._create_op(action_name, action_url))

            action_name = '/'.join([self.uri_prefix, 'action/pause'])
            operations.append(self._create_op(action_name, action_url))

            action_name = '/'.join([self.uri_prefix, 'action/suspend'])
            operations.append(self._create_op(action_name, action_url))

            action_name = 'delete'
            operations.append(self._create_op(action_name, action_url))

            body['operations'] = operations

            if self.res_content_type == 'application/xml':
                response_data = {'Machine': body}
                remove_member(response_data, 'resourceURI')
            else:
                body['resourceURI'] = '/'.join([self.uri_prefix,
                                               self.entity_uri])
                response_data = body

            new_content = make_response_data(response_data,
                                             self.res_content_type,
                                             self.metadata,
                                             self.uri_prefix)
            resp = Response()
            self._fixup_cimi_header(resp)
            resp.headers['Content-Type'] = self.res_content_type
            resp.status = 200
            resp.body = new_content
            return resp
        else:
            return res
Esempio n. 6
0
    def POST(self, req, *parts):
        """
        Handle POST machine request which will create a machine
        """

        try:
            request_data = get_request_data(req.body, self.req_content_type)
        except Exception as error:
            return get_err_response('MalformedBody')

        if request_data:
            data = request_data.get('body').get('MachineCreate')
            if not data:
                data = request_data.get('body')
            if data:
                new_body = {}
                match_up(new_body, data, 'name', 'name')

                if (data.get('machineTemplate') is None or
                    data.get('machineTemplate').get('machineImage') is None or
                    data.get('machineTemplate').get('machineConfig') is None):
                    return get_err_response('BadRequest')

                match_up(new_body, data, 'imageRef',
                         'machineTemplate/machineImage/href')

                match_up(new_body, data, 'flavorRef',
                         'machineTemplate/machineConfig/href')

                if (new_body.get('flavorRef') is None or
                    new_body.get('imageRef') is None):
                    return get_err_response('BadRequest')

                new_body['imageRef'] = new_body.get('imageRef').split('/')[-1]
                new_body['flavorRef'] = \
                    new_body.get('flavorRef').split('/')[-1]

                adminPass = data.get('credentials', {}).get('password')
                if adminPass:
                    new_body['adminPass'] = adminPass

                self.os_path = '/%s/servers' % (self.tenant_id)
                new_req = self._fresh_request(req)

                new_req.body = json.dumps({'server': new_body})
                resp = new_req.get_response(self.app)
                if resp.status_int == 201:
                    # resource created successfully, we redirect the request
                    # to query machine
                    resp_data = json.loads(resp.body)
                    id = resp_data.get('server').get('id')
                    env = self._fresh_env(req)
                    env['PATH_INFO'] = concat(self.request_prefix,
                                              '/', self.tenant_id,
                                              '/servers/', id)
                    env['REQUEST_METHOD'] = 'GET'
                    new_req = Request(env)
                    resp = new_req.get_response(self.app)
                    resp_data = {}
                    resp.headers['Location'] = \
                        '/'.join([self.request_prefix, self.tenant_id,
                                  'Machine', id])
                    resp.status = 201
                elif resp.status_int == 202:
                    resp_body_data = json.loads(resp.body).get('server')
                    id = resp_body_data.get('id')
                    resp_data = {}
                    resp_data['resourceURI'] = '/'.join([self.uri_prefix,
                                            'Machine'])
                    match_up(resp_data, data, 'name', 'name')
                    resp_data['id'] = concat(self.tenant_id, '/Machine/', id)
                    resp_data['credentials'] = {'userName': '******',
                        'password': resp_body_data.get('adminPass')}
                    if self.res_content_type == 'application/xml':
                        response_data = {'Machine': resp_data}
                        remove_member(response_data, 'resourceURI')
                    else:
                        response_data = resp_data

                    new_content = make_response_data(response_data,
                                             self.res_content_type,
                                             self.machine_metadata,
                                             self.uri_prefix)
                    resp = Response()
                    self._fixup_cimi_header(resp)
                    resp.headers['Content-Type'] = self.res_content_type
                    resp.headers['Location'] = \
                        '/'.join([self.request_prefix, self.tenant_id,
                                  'Machine', id])
                    resp.status = 202
                    resp.body = new_content
                return resp
            else:
                return get_err_response('BadRequest')
        else:
            return get_err_response('BadRequest')
Esempio n. 7
0
    def GET(self, req, *parts):
        """
        Handle GET Container (List Objects) request
        """

        env = copy.copy(req.environ)

        env['SCRIPT_NAME'] = self.os_version
        env['PATH_INFO'] = self.os_path
        # we will always use json format to get Nova information
        env['HTTP_ACCEPT'] = 'application/json'

        # need to remove this header, otherwise, it will always take the
        # original request accept content type
        if env.has_key('nova.best_content_type'):
            env.pop('nova.best_content_type')
        new_req = Request(env)

        res = new_req.get_response(self.app)
        if res.status_int == 200:
            content = json.loads(res.body)
            body = {}
            body['id'] = '/'.join([self.tenant_id, self.entity_uri])
            body['machineImages'] = []
            images = content.get('images', [])
            for image in images:
                entry = {}
                entry['resourceURI'] = '/'.join(
                    [self.uri_prefix, 'MachineImage'])
                entry['id'] = '/'.join(
                    [self.tenant_id, 'MachineImage', image['id']])
                entry['type'] = 'IMAGE'
                entry['name'] = image['name']
                entry['created'] = image['created']
                entry['updated'] = image['updated']
                entry['state'] = map_image_state(image['status'])
                entry['imageLocation'] = entry['id']

                body['machineImages'].append(entry)

            body['count'] = len(body['machineImages'])
            if self.res_content_type == 'application/xml':
                remove_member(body, 'resourceURI')
                body['resourceURI'] = '/'.join(
                    [self.uri_prefix, self.entity_uri])
                response_data = {'Collection': body}
            else:
                body['resourceURI'] = '/'.join(
                    [self.uri_prefix, self.entity_uri])
                response_data = body

            new_content = make_response_data(response_data,
                                             self.res_content_type,
                                             self.metadata, self.uri_prefix)

            resp = Response()
            self._fixup_cimi_header(resp)
            resp.headers['Content-Type'] = self.res_content_type
            resp.status = 200
            resp.body = new_content
            return resp
        else:
            return res