Esempio n. 1
0
    def load(self, response):
        """The load method parses the raw JSON response from the server.

        Most models are not returned in the main response body, but in a key
        such as 'Clusters', defined by the 'data_key' attribute on the class.
        Also, related objects are often returned and can be used to pre-cache
        related model objects without having to contact the server again.  This
        method handles all of those cases.

        Also, if a request has triggered a background operation, the request
        details are returned in a 'Requests' section. We need to store that
        request object so we can poll it until completion.
        """
        if 'Requests' in response and 'Requests' != self.data_key:
            from ambariclient.models import Request
            self.request = Request(self.cluster.requests,
                                   href=response.get('href'),
                                   data=response['Requests'])
        else:
            if 'href' in response:
                self._href = response.pop('href')
            if self.data_key and self.data_key in response:
                self._data.update(response.pop(self.data_key))
                # preload related object collections, if received
                for rel in [
                        x for x in self.relationships
                        if x in response and response[x]
                ]:
                    rel_class = self.relationships[rel]
                    collection = rel_class.collection_class(self.client,
                                                            rel_class,
                                                            parent=self)
                    self._relationship_cache[rel] = collection(response[rel])
            elif not self.data_key:
                self._data.update(response)
Esempio n. 2
0
    def load(self, response):
        """Parse the GET response for the collection.

        The response from a GET request against that url should look like:
            { 'items': [ item1, item2, ... ] }

        While each of the item objects is usually a subset of the information
        for each model, it generally includes the URL needed to load the full
        data in an 'href' key.  This information is used to lazy-load the
        details on the model, when needed.

        In some rare cases, a collection can have an asynchronous request
        triggered.  For those cases, we handle it here.
        """
        if 'Requests' in response:
            from ambariclient.models import Request
            self.request = Request(self.parent.cluster.requests,
                                   href=response.get('href'),
                                   data=response['Requests'])
        if 'items' in response:
            self._models = []
            for item in response['items']:
                model = self.model_class(self, href=item.get('href'))
                model.load(item)
                self._models.append(model)