コード例 #1
0
class APIEndpoint(object):

    def __init__(self, rancher_base_url, access_key, secret_key,
                 use_account_api=False, project_id=None):
        super(APIEndpoint, self).__init__()

        self.rancher_base_url = rancher_base_url
        self.access_key = access_key
        self.secret_key = secret_key
        self.use_account_api = use_account_api

        self.endpoint = Hammock(rancher_base_url)

        # endpoint starts from base/v2-beta
        self.endpoint = self.endpoint('v2-beta')

        # if it is an Account API access, you should provide project_id
        if use_account_api:
            if not project_id:
                raise Exception('missing project_id')
            self.endpoint = self.endpoint.projects(project_id)

    @property
    def auth_param(self):
        return self.access_key, self.secret_key

    @property
    def request_contexts(self):
        return {
            'auth': self.auth_param
        }

    def stacks(self, stack_id):
        response = self.endpoint.stacks(stack_id).GET(**self.request_contexts)
        obj = response.json()
        return StackResource(self, obj)

    def services(self, service_id):
        response = self.endpoint.services(service_id).GET(**self.request_contexts)
        obj = response.json()
        return ServiceResource(self, obj)
コード例 #2
0
class APIEndpoint(object):
    def __init__(self,
                 rancher_base_url,
                 access_key,
                 secret_key,
                 use_account_api=False,
                 project_id=None):
        super(APIEndpoint, self).__init__()

        self.rancher_base_url = rancher_base_url
        self.access_key = access_key
        self.secret_key = secret_key
        self.use_account_api = use_account_api

        self.endpoint = Hammock(rancher_base_url)

        # endpoint starts from base/v2-beta
        self.endpoint = self.endpoint('v2-beta')

        # if it is an Account API access, you should provide project_id
        if use_account_api:
            if not project_id:
                raise Exception('missing project_id')
            self.endpoint = self.endpoint.projects(project_id)

    @property
    def auth_param(self):
        return self.access_key, self.secret_key

    @property
    def request_contexts(self):
        return {'auth': self.auth_param}

    def stacks(self, stack_id):
        """
        :param stack_id:
        :return:
        :rtype: StackResource
        """
        response = self.endpoint.stacks(stack_id).GET(**self.request_contexts)
        obj = response.json()
        return StackResource(self, obj)

    def services(self, service_id):
        """
        :param service_id:
        :return:
        :rtype: ServiceResource
        """
        response = self.endpoint.services(service_id).GET(
            **self.request_contexts)
        obj = response.json()
        return ServiceResource(self, obj)

    def containers(self, instance_id):
        """
        :param instance_id:
        :return:
        :rtype: ContainerResource
        """
        response = self.endpoint.containers(instance_id).GET(
            **self.request_contexts)
        obj = response.json()
        return ContainerResource(self, obj)

    def websocket(self, host_access, **kwargs):
        """
        :param host_access:
        :type host_access: HostAccess

        :return:
        """
        ws = BaseRancherWSApp(host_access=host_access)
        return ws