| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing,
- # software distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions
- # and limitations under the License.
- """
- This module provides a client class for User Service.
- """
- import copy
- import json
- import logging
- import uuid
- import sys
- from baidubce import bce_base_client
- from baidubce.auth import bce_v1_signer
- from baidubce.http import bce_http_client
- from baidubce.http import handler
- from baidubce.http import http_methods
- from baidubce import utils
- from baidubce.utils import required
- from baidubce import compat
- if sys.version < '3':
- reload(sys)
- sys.setdefaultencoding('utf-8')
- _logger = logging.getLogger(__name__)
- class UserServiceClient(bce_base_client.BceBaseClient):
- """
- BLB base sdk client
- """
- version = b'/v1'
- def __init__(self, config=None):
- """初始化BceClient对象。
- Args:
- config (dict,可选参数,默认值为None): BceClient的配置字典。
- """
- bce_base_client.BceBaseClient.__init__(self, config)
- def _merge_config(self, config=None):
- """
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- """
- if config is None:
- return self.config
- else:
- new_config = copy.copy(self.config)
- new_config.merge_non_none_values(config)
- return new_config
- def _send_request(self, http_method, path,
- body=None, headers=None, params=None,
- config=None, body_parser=None):
- """
- 发送请求方法
- Args:
- http_method (str): 请求的方法,可选值包括"GET", "POST", "PUT", "DELETE"。
- path (str): 请求路径。
- body (Any, optional): 请求体内容,可以是任意类型。默认值为None。
- headers (dict[bytes, bytes], optional): 请求头信息,以键值对形式存储。默认值为None。
- params (dict[str, str], optional): 请求参数,以键值对形式存储。默认值为None。
- config (dict, optional): 配置信息。
- body_parser (func, optional): 解析响应体的函数。如果为空则默认使用parse_json方法。
- Returns:
- Any: 返回请求返回的内容。
- """
- config = self._merge_config(config)
- if body_parser is None:
- body_parser = handler.parse_json
- if headers is None:
- headers = {b'Accept': b'*/*',
- b'Content-Type': b'application/json;charset=utf-8'}
- return bce_http_client.send_request(
- config, bce_v1_signer.sign, [handler.parse_error, body_parser],
- http_method, path, body, headers, params)
- @required(name=(bytes, str),
- serviceName=(bytes, str),
- instanceId=(bytes, str))
- def create_user_service(self, name, desc, serviceName,
- instanceId, client_token=None,
- authList=None, config=None):
- """
- Create user service for specified LoadBalancer
- :param name:
- name of user service
- :type name: string
- :param desc:
- description of user service
- :type desc: string
- :param serviceName:
- The name of service
- :type serviceName: string
- :param instanceId:
- The id of LoadBalancer
- :type instanceId: string
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param authList:
- List of Auth information
- :type service: list<Auth>
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, 'service')
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- body = {}
- if desc is not None:
- body['description'] = compat.convert_to_string(desc)
- body['name'] = name
- body['serviceName'] = serviceName
- body['instanceId'] = instanceId
- if authList is not None:
- body['authList'] = authList
- else:
- body['authList'] = [{"uid": "*", "auth": "deny"}]
- return self._send_request(http_methods.POST, path,
- body=json.dumps(body), params=params,
- config=config)
- @required(service=(bytes, str))
- def update_user_service(self, name=None, desc=None, service=None,
- client_token=None, config=None):
- """
- Update user service for specified LoadBalancer
- :param name:
- name of user service
- :type name: string
- :param desc:
- description of user service
- :type desc: string
- :param service:
- domain name of the service
- :type service: string
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, 'service', service)
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- params[b'modifyAttribute'] = None
- body = {}
- if desc is not None:
- body['description'] = compat.convert_to_string(desc)
- if name is not None:
- body["name"] = name
- return self._send_request(http_methods.PUT, path,
- body=json.dumps(body), params=params,
- config=config)
- @required(service=(bytes, str),
- instanceId=(bytes, str))
- def user_service_bind_instance(self, instanceId, service,
- client_token=None, config=None):
- """
- Bind a specified LoadBalancer to user service
- :param instanceId:
- The id of LoadBalancer
- :type instanceId: string
- :param service:
- domain name of the service
- :type service: string
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, 'service', service)
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- params[b'bind'] = None
- body = {}
- body['instanceId'] = instanceId
- return self._send_request(http_methods.PUT, path,
- body=json.dumps(body), params=params,
- config=config)
- @required(service=(bytes, str))
- def user_service_unbind_instance(self, service,
- client_token=None, config=None):
- """
- Unbind a specified LoadBalancer from user service
- :param service:
- domain name of the service
- :type service: string
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, 'service', service)
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- params[b'unbind'] = None
- body = {}
- return self._send_request(http_methods.PUT, path,
- body=json.dumps(body), params=params,
- config=config)
- @required(service=(bytes, str),
- authList=list)
- def user_service_add_auth(self, service,
- authList, client_token=None, config=None):
- """
- Add a new auth information to user service
- :param service:
- domain name of the service
- :type service: string
- :param authList:
- List of Auth information
- :type service: list<Auth>
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, 'service', service)
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- params[b'addAuth'] = None
- body = {}
- body['authList'] = authList
- return self._send_request(http_methods.PUT, path,
- body=json.dumps(body), params=params,
- config=config)
- @required(service=(bytes, str),
- authList=list)
- def user_service_edit_auth(self, service,
- authList, client_token=None, config=None):
- """
- Edit auth information to user service
- :param service:
- domain name of the service
- :type service: string
- :param authList:
- List of Auth information
- :type service: list<Auth>
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, 'service', service)
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- params[b'editAuth'] = None
- body = {}
- body['authList'] = authList
- return self._send_request(http_methods.PUT, path,
- body=json.dumps(body), params=params,
- config=config)
- @required(service=(bytes, str),
- uidList=list)
- def user_service_remove_auth(self, service,
- uidList, client_token=None, config=None):
- """
- Remove auth information from user service
- :param service:
- domain name of the service
- :type service: string
- :param uidList:
- List of uid
- :type service: list<String>
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, 'service', service)
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- params[b'removeAuth'] = None
- body = {}
- body['uidList'] = uidList
- return self._send_request(http_methods.PUT, path,
- body=json.dumps(body), params=params,
- config=config)
- def get_user_service_list(self, marker=None,
- maxKeys=None, client_token=None, config=None):
- """
- Get list of user service
- :param marker:
- Mark the start position of query
- :type service: string
- :param maxKeys:
- Max number of one page, default number is 1000
- :type service: integer
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, b'service')
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- if marker is not None:
- params[b'marker'] = marker
- if maxKeys is not None:
- params[b'maxKeys'] = maxKeys
- else:
- params[b'maxKeys'] = 1000
- body = {}
- return self._send_request(http_methods.GET, path,
- body=json.dumps(body), params=params,
- config=config)
- @required(service=(bytes, str))
- def get_user_service_detail(self, service,
- client_token=None, config=None):
- """
- Get user service detail information
- :param service:
- domain name of the service
- :type service: string
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, b'service', service)
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- body = {}
- return self._send_request(http_methods.GET, path,
- body=json.dumps(body), params=params,
- config=config)
- @required(service=(bytes, str))
- def delete_user_service(self, service,
- client_token=None, config=None):
- """
- Delete user service
- :param service:
- domain name of the service
- :type service: string
- :param client_token:
- If the clientToken is not specified by the user,
- a random String generated by default algorithm will be used.
- :type client_token: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = utils.append_uri(self.version, 'service', service)
- params = {}
- if client_token is None:
- params[b'clientToken'] = generate_client_token()
- else:
- params[b'clientToken'] = client_token
- body = {}
- return self._send_request(http_methods.DELETE, path,
- body=json.dumps(body), params=params,
- config=config)
- def generate_client_token_by_uuid():
- """
- The default method to generate the random string for client_token
- if the optional parameter client_token is not specified by the user.
- :return:
- :rtype string
- """
- return str(uuid.uuid4())
- generate_client_token = generate_client_token_by_uuid
|