Example #1
0
 def check_version(self):
     if self.version < base.OLDEST_SUPPORTED_VERSION:
         raise exceptions.ClientError(
             "Version %s unsupported, must be %s or higher" %
             (utils.version_str(self.version),
              utils.version_str(base.OLDEST_SUPPORTED_VERSION)))
     return
Example #2
0
 def check_version(self):
     if (self.model_class.min_version > OLDEST_SUPPORTED_VERSION
             and self.client.version < self.model_class.min_version):
         min_version = utils.version_str(self.model_class.min_version)
         curr_version = utils.version_str(self.client.version)
         raise exceptions.ClientError(
             message="Cannot access %s in version %s, it was added in "
             "version %s" % (self.url, curr_version, min_version))
Example #3
0
    def url(self):
        """Gets the url for the resource this model represents.

        It will just use the 'href' passed in to the constructor if that exists.
        Otherwise, it will generated it based on the collection's url and the
        model's identifier.
        """
        if self._href is not None:
            return self._href
        if self.identifier:
            return '/'.join([self.parent.url, self.identifier])
        raise exceptions.ClientError("Not able to determine object URL")
Example #4
0
    def inflate(self):
        """Load the resource from the server, if not already loaded."""
        if not self._is_inflated:
            if self._is_inflating:
                # catch infinite recursion when attempting to inflate
                # an object that doesn't have enough data to inflate
                msg = ("There is not enough data to inflate this object.  "
                       "Need either an href: {} or a {}: {}")
                msg = msg.format(self._href, self.primary_key,
                                 self._data.get(self.primary_key))
                raise exceptions.ClientError(msg)

            self._is_inflating = True
            self.load(self.client.get(self.url))
            self._is_inflated = True
            self._is_inflating = False
        return self
Example #5
0
    def cluster(self):
        """A helper method to find the parent cluster of an object.

        Many times you need to reference something else belonging to the same
        cluster, this method saves you the effort of figuring that out manually
        and should work for all cases where an object belongs to a cluster at
        some level.
        """
        from ambariclient.models import Cluster
        model = self
        while model is not None and not isinstance(model, Cluster):
            model = model.parent
        if model is None:
            if 'cluster_name' in self.fields:
                return self.client.clusters(self.cluster_name)
            raise exceptions.ClientError(
                "This model does not belong to a cluster: %s" % self.to_dict())
        return model