Exemplo 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:
            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
Exemplo n.º 2
0
    def GET(self, req, *parts):
        """
        Handle GET machine request
        """

        new_req = self._fresh_request(req)

        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)
            body['resourceURI'] = '/'.join([self.uri_prefix,
                                            self.entity_uri])

            env = self._fresh_env(req)
            env['PATH_INFO'] = '/%s/flavors/detail' % (self.tenant_id)
            new_req = Request(env)
            res = new_req.get_response(self.app)
            if res.status_int == 200:
                flavors = json.loads(res.body).get('flavors')
            else:
                flavors = []

            keyed_flavors = {}
            for flavor in flavors:
                keyed_flavors[flavor['id']] = flavor

            body['machines'] = []
            machines = content.get('servers', [])
            for machine in machines:
                entry = {}
                if self.res_content_type != 'application/xml':
                    entry['resourceURI'] = '/'.join([self.uri_prefix,
                                                 'Machine'])
                entry['id'] = concat(self.tenant_id, '/',
                                     'machine/',
                                     machine['id'])
                entry['name'] = machine['name']
                #entry['property'] = machine['metadata']
                entry['created'] = machine['created']
                entry['updated'] = machine['updated']
                entry['state'] = map_machine_state(machine['status'])
                flavor = keyed_flavors[machine['flavor']['id']]
                entry['cpu'] = flavor['vcpus']
                entry['memory'] = int(flavor['ram']) * 1000

                entry['volumes'] = {'href': '/'.join([self.tenant_id,
                    'MachineVolumeCollection', machine['id']])}
                entry['networkInterfaces'] = {'href': '/'.join([self.tenant_id,
                    'NetworkInterfacesCollection', machine['id']])}
                entry['disks'] = {'href': '/'.join([self.tenant_id,
                    'MachineDiskCollection', machine['id']])}

                body['machines'].append(entry)

            body['count'] = len(body['machines'])
            # deal with machine operations
            operations = []
            operations.append(self._create_op('add',
                                              '/'.join([self.tenant_id,
                                                       'machineCollection'])))
            body['operations'] = operations

            if self.res_content_type == 'application/xml':
                body['resourceURI'] = '/'.join([self.uri_prefix,
                                                'MachineCollection'])
                response_data = {'Collection': body}
            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