| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- # 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 ENDPOINT.
- """
- import copy
- import json
- import logging
- import uuid
- from baidubce.bce_base_client import BceBaseClient
- 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 compat
- _logger = logging.getLogger(__name__)
- class EndpointClient(BceBaseClient):
- """
- Endpoint base sdk client
- """
- prefix = b'/v1'
- path = b'/endpoint'
- def __init__(self, config=None):
- BceBaseClient.__init__(self, config)
- @staticmethod
- def _generate_default_client_token():
- """
- default client token by uuid1
- """
- return uuid.uuid1()
- def _merge_config(self, config):
- """
- :type config: baidubce.BceClientConfiguration
- :param config:
- :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):
- """
- :param http_method:
- :param path:
- :param body:
- :param headers:
- :param params:
- :type config: baidubce.BceClientConfiguration
- :param config:
- :param body_parser:
- :return: baidubce.BceResponse
- """
- 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, EndpointClient.prefix + path, body, headers,
- params)
- def list_services(self, config=None):
- """
- return all services
- """
- path = EndpointClient.path + b'/publicService'
- return self._send_request(http_methods.GET, path, config=config)
- def create_endpoint(self, vpc_id, subnet_id, name, service, billing, description=None, ip_address=None,
- client_token=None, config=None):
- """
- The method of endpoint to be created.
- :param vpc_id:
- vpc id
- :type vpc_id: str
- :param subnet_id:
- subnet id
- :type subnet_id: str
- :param name:
- name
- :type name: str
- :param service:
- service
- :type service: str
- :param billing:
- order_configuration
- :type billing:Billing
- :param ip_address:
- ip_address ipv4 address
- :type ip_address: str
- :param description:
- description .
- :type description: string
- :param client_token:
- An ASCII string whose length is less than 64.
- The request will be idempotent if clientToken is provided.
- If the clientToken is not specified by the user, a random String
- generated by default algorithm will be used.
- :type client_token: string
- """
- if client_token is None:
- client_token = self._generate_default_client_token()
- params = {
- b'clientToken': client_token
- }
- body = {
- "vpcId": vpc_id,
- "subnetId": subnet_id,
- "name": name,
- "service": service,
- "billing": {
- "paymentTiming": billing.payment_timing
- }
- }
- if description is not None:
- body['description'] = description
- if ip_address is not None:
- body['ipAddress'] = ip_address
- return self._send_request(http_methods.POST, EndpointClient.path, body=json.dumps(body), params=params,
- config=config)
- def delete_endpoint(self, endpoint_id, client_token=None, config=None):
- """
- release endpoint
- :param endpoint_id:
- The id of endpoint.
- :type endpoint_id: string
- :param client_token:
- An ASCII string whose length is less than 64.
- The request will be idempotent if clientToken is provided.
- 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 = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
- if client_token is None:
- client_token = self._generate_default_client_token()
- params = {
- b'clientToken': client_token
- }
- return self._send_request(http_methods.DELETE, path, params=params, config=config)
- def list_endpoints(self, vpc_id, name=None, ip_address=None, status=None, subnet_id=None, service=None, marker=None,
- max_Keys=None, config=None):
- """
- return all endpoint about vpc
- :param vpc_id:
- vpc id
- :type vpc_id:string
- :param name:
- name
- :type name:string
- :param ip_address:
- ip address
- :type ip_address:string
- :param status:
- status
- :type status:string
- :param subnet_id:
- subnet id
- :type subnet_id:string
- :param service:
- service
- :type service:string
- :param marker:
- The optional parameter marker specified in the original request to specify
- where in the results to begin listing.
- Together with the marker, specifies the list result which listing should begin.
- If the marker is not specified, the list result will listing from the first one.
- :type marker: string
- :param max_Keys:
- The optional parameter to specifies the max number of list result to return.
- The default value is 1000.
- :type max_Keys: int
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- params = {b'vpcId': vpc_id}
- if name is not None:
- params[b'name'] = name
- if ip_address is not None:
- params[b'ipAddress'] = ip_address
- if status is not None:
- params[b'status'] = status
- if subnet_id is not None:
- params[b'subnetId'] = subnet_id
- if service is not None:
- params[b'service'] = service
- if marker is not None:
- params[b'marker'] = marker
- if max_Keys is not None:
- params[b'maxKeys'] = max_Keys
- return self._send_request(http_methods.GET, EndpointClient.path, params=params, config=config)
- def get_endpoint(self, endpoint_id, config=None):
- """
- Get the detail information of endpoint.
- :param endpoint_id:
- The id of endpoint.
- :type endpoint_id: string
- :param config:
- :type config: baidubce.BceClientConfiguration
- :return:
- :rtype baidubce.bce_response.BceResponse
- """
- path = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
- return self._send_request(http_methods.GET, path, config=config)
- def update_endpoint(self, endpoint_id, name=None, description=None, client_token=None, config=None):
- """
- The method of endpoint to be update.
- :param endpoint_id: endpoint id
- :type endpoint_id: str
- :param name: endpoint name
- :type name: str
- :param description: the description of endpoint
- :type description: str
- :param client_token:
- An ASCII string whose length is less than 64.
- The request will be idempotent if clientToken is provided.
- 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 = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
- if client_token is None:
- client_token = self._generate_default_client_token()
- params = {
- b'clientToken': client_token
- }
- body = {}
- if description is not None:
- body['description'] = description
- if name is not None:
- body['name'] = name
- return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
- config=config)
- def update_endpoint_sg(self, endpoint_id, security_group_list, action=b'bindSg', client_token=None, config=None):
- """
- The method of endpoint's security group to be update.
- :param endpoint_id: endpoint id
- :type endpoint_id: str
- :param security_group_list: the id list of security group to be bind with.
- :type security_group_list: list
- :param name: action
- :type name: str
- :param client_token:
- An ASCII string whose length is less than 64.
- The request will be idempotent if clientToken is provided.
- 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 = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
- if client_token is None:
- client_token = self._generate_default_client_token()
- params = {
- action: '',
- b'clientToken': client_token
- }
- body = {
- "securityGroupIds": security_group_list
- }
- return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
- config=config)
- def update_endpoint_enterprise_sg(self, endpoint_id, enterprise_sg_list,
- action=b'bindEsg', client_token=None, config=None):
- """
- The method of endpoint's enterprise security group to be update.
- :param endpoint_id: endpoint id
- :type endpoint_id: str
- :param enterprise_sg_list: the id list of enterprise security group to be bind.
- :type enterprise_sg_list: list
- :param action: action
- :type action: str
- :param client_token:
- An ASCII string whose length is less than 64.
- The request will be idempotent if clientToken is provided.
- 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 = EndpointClient.path + b'/' + compat.convert_to_bytes(endpoint_id)
- if client_token is None:
- client_token = self._generate_default_client_token()
- params = {
- action: '',
- b'clientToken': client_token
- }
- body = {
- b'enterpriseSecurityGroupIds': enterprise_sg_list
- }
- return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params,
- config=config)
|