def _send_http_request(conn, http_method, uri, headers, body, send_buf_size): # putrequest() need that http_method and uri is Ascii on Py2 and unicode \ # on Py3 http_method = compat.convert_to_string(http_method) uri = compat.convert_to_string(uri) conn.putrequest(http_method, uri, skip_host=True, skip_accept_encoding=True) for k, v in iteritems(headers): k = utils.convert_to_standard_string(k) v = utils.convert_to_standard_string(v) conn.putheader(k, v) conn.endheaders() if body: if isinstance(body, (bytes, str)): conn.send(body) else: total = int(headers[http_headers.CONTENT_LENGTH]) sent = 0 while sent < total: size = total - sent if size > send_buf_size: size = send_buf_size buf = body.read(size) if not buf: raise BceClientError( 'Insufficient data, only %d bytes available while %s is %d' % (sent, http_headers.CONTENT_LENGTH, total)) conn.send(buf) sent += len(buf) return conn.getresponse()
def scale_cluster(self, cluster_id, instance_group_id, instance_count): """ Scale cluster :param cluster_id: cluster id :type cluster_id: string :param instance_group_id: instance group id :type instance_group_id: string :param instance_count: instance count :type instance_count: int :return: :rtype baidubce.bce_response.BceResponse """ path = "/cluster/%s/instanceGroup" % compat.convert_to_string( cluster_id) params = None body = { "instanceGroups": [{ "id": compat.convert_to_string(instance_group_id), "instanceCount": instance_count }] } return self._send_request(http_methods.PUT, path, params=params, body=json.dumps(body))
def test_list_objects(self): """test list_objects function normally""" for i in range(0, 10): self.bos.put_object_from_string( self.BUCKET, "test_object_%s" % compat.convert_to_bytes(random.random()), "This is a string.") response = self.bos.list_objects(self.BUCKET, prefix="", delimiter="") self.check_headers(response) self.assertEqual(response.is_truncated, 'false') self.assertEqual(response.max_keys, '1000') self.assertEqual(response.name, self.BUCKET) self.assertEqual(response.prefix, None) # TODO: test prefix and marker with Chineses for i in range(0, 5): key1 = "test_%s" % compat.convert_to_string(random.random()) key2 = "testfile_%s" % compat.convert_to_string(random.random()) self.bos.put_object_from_string(self.BUCKET, key1, "This is a string.") self.bos.put_object_from_string(self.BUCKET, key2, "This is a string.") prefix = 'test' marker = 'testfile' response = self.bos.list_objects(self.BUCKET, prefix=prefix) self.check_headers(response) self.assertEqual(len(response.contents), 20) self.assertEqual(response.prefix, prefix) response = self.bos.list_objects(self.BUCKET, marker=marker) self.check_headers(response) self.assertEqual(len(response.contents), 5) self.assertEqual(response.marker, marker)
def create_cluster(self, image_type, image_version, instance_groups, client_token=None, applications=None, auto_terminate=None, log_uri=None, name=None, steps=None, service_ha_enabled=None, safe_mode_enabled=None): """ Create cluster :param image_type: the type of virtual machine image :type image_type: string :param image_version: the version of virtual machine image :type image_version: string :param instance_groups: instance groups for cluster :type instance_groups: array :return: :rtype baidubce.bce_response.BceResponse """ path = '/cluster' params = None if client_token is not None: params = {'clientToken': client_token} body = { 'imageType': compat.convert_to_string(image_type), 'imageVersion': compat.convert_to_string(image_version), 'instanceGroups': instance_groups } if applications is not None: body['applications'] = applications if auto_terminate is not None: body['autoTerminate'] = auto_terminate if name is not None: body['name'] = name if log_uri is not None: body['logUri'] = log_uri if steps is not None: body['steps'] = steps if service_ha_enabled is not None: body['serviceHaEnabled'] = service_ha_enabled if safe_mode_enabled is not None: body['safeModeEnabled'] = safe_mode_enabled return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body))
def test_get_timestamp(self): """test_get_timestamp""" self.assertEqual("1970-01-01T00:00:01Z", utils.get_canonical_time(1)) if compat.PY3: self.assertRegex( compat.convert_to_string(utils.get_canonical_time()), "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z") else: self.assertRegexpMatches( compat.convert_to_string(utils.get_canonical_time()), "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z")
def create_vpc(self, name, cidr, description=None, client_token=None, config=None): """ The name of vpc to be created. :param name: The name of vpc to be created. :type name: string :param cidr: The CIDR of the vpc. :type cidr: string :param description: The description of the vpc. :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 :param config: :type config: baidubce.BceClientConfiguration :return: :rtype baidubce.bce_response.BceResponse """ path = b'/vpc' params = {} if client_token is None: params[b'clientToken'] = generate_client_token() else: params[b'clientToken'] = client_token body = { 'name': compat.convert_to_string(name), 'cidr': compat.convert_to_string(cidr) } if description is not None: body['description'] = compat.convert_to_string(description) return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params, config=config)
def update_subnet(self, subnet_id, name, description=None, client_token=None, config=None): """ Modify the special attribute to new value of the subnet owned by the user. :param subnet_id: The id of the specific subnet to be updated. :type subnet_id: string :param name: The name of the subnet :type name: string :param description: The option param to describe the subnet :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 :param config: :type config: baidubce.BceClientConfiguration :return: :rtype baidubce.bce_response.BceResponse """ path = b'/subnet/%s' % compat.convert_to_bytes(subnet_id) params = {b'modifyAttribute': None} body = {'name': compat.convert_to_string(name)} if client_token is None: params[b'clientToken'] = generate_client_token() else: params[b'clientToken'] = client_token if description is not None: body['description'] = compat.convert_to_string(description) return self._send_request(http_methods.PUT, path, json.dumps(body), params=params, config=config)
def set_metadata_from_headers(self, headers): """ :param headers: :return: """ for k, v in iteritems(headers): if k.startswith(compat.convert_to_string(http_headers.BCE_PREFIX)): k = 'bce_' + k[ len(compat.convert_to_string(http_headers.BCE_PREFIX)):] k = utils.pythonize_name(k.replace('-', '_')) if k.lower() == compat.convert_to_string( http_headers.ETAG.lower()): v = v.strip('"') setattr(self.metadata, k, v)
def list_steps(self, cluster_id, marker=None, max_keys=None): """ List step :param cluster_id: cluster id :type cluster_id: string :param marker: :type marker: string :param max_keys: max records returned. :type max_keys: int :return: :rtype baidubce.bce_response.BceResponse """ path = '/cluster/%s/step' % compat.convert_to_string(cluster_id) params = None if marker is not None or max_keys is not None: params = {} if marker is not None: params['marker'] = marker if max_keys is not None: params['maxKeys'] = max_keys return self._send_request(http_methods.GET, path, params=params)
def scale_cluster(self, cluster_id, instance_group_config, deleteClientIds=None): """ Scale cluster :param cluster_id: cluster id :type cluster_id: string :param instance_group_id: instance group id :type instance_group_id: string :param instance_count: instance count :type instance_count: int :return: :rtype baidubce.bce_response.BceResponse """ path = "/cluster/%s/instanceGroup" % compat.convert_to_string( cluster_id) params = None body = {"instanceGroups": instance_group_config} if deleteClientIds is not None: body['instances'] = deleteClientIds return self._send_request(http_methods.PUT, path, params=params, body=json.dumps(body))
def _parse_bos_object(http_response, response): """Sets response.body to http_response and response.user_metadata to a dict consists of all http headers starts with 'x-bce-meta-'. :param http_response: the http_response object returned by HTTPConnection.getresponse() :type http_response: httplib.HTTPResponse :param response: general response object which will be returned to the caller :type response: baidubce.BceResponse :return: always true :rtype bool """ user_metadata = {} headers_list = http_response.getheaders() if compat.PY3: temp_heads = [] for k, v in headers_list: k = k.lower() temp_heads.append((k, v)) headers_list = temp_heads prefix = compat.convert_to_string( http_headers.BCE_USER_METADATA_PREFIX) for k, v in headers_list: if k.startswith(prefix): k = k[len(prefix):] user_metadata[compat.convert_to_unicode(k)] = \ compat.convert_to_unicode(v) response.metadata.user_metadata = user_metadata response.data = http_response return True
def get_step(self, cluster_id, step_id): """ Get step :param cluster_id: cluster id :type cluster_id: string :param step_id: step id :type step_id: string :return: baidubce.bce_response.BceResponse :rtype baidubce.bce_response.BceResponse """ path = '/cluster/%s/step/%s' % (compat.convert_to_string(cluster_id), compat.convert_to_string(step_id)) return self._send_request(http_methods.GET, path)
def parse_error(http_response, response): """If the body is not empty, convert it to a python object and set as the value of response.body. http_response is always closed if no error occurs. :param http_response: the http_response object returned by HTTPConnection.getresponse() :type http_response: httplib.HTTPResponse :param response: general response object which will be returned to the caller :type response: baidubce.BceResponse :return: false if http status code is 2xx, raise an error otherwise :rtype bool :raise baidubce.exception.BceClientError: if http status code is NOT 2xx """ if http_response.status // 100 == http.client.OK // 100: return False if http_response.status // 100 == http.client.CONTINUE // 100: raise BceClientError(b'Can not handle 1xx http status code') bse = None body = http_response.read() if body: d = json.loads(compat.convert_to_string(body)) bse = BceServerError(d['message'], code=d['code'], request_id=d['requestId']) if bse is None: bse = BceServerError(http_response.reason, request_id=response.metadata.bce_request_id) bse.status_code = http_response.status raise bse
def dict_to_python_object_deep(d): """ :param d: :return: """ if isinstance(d, dict): attr = {} for k, v in iteritems(d): if not isinstance(k, compat.string_types): k = compat.convert_to_string(k) k = pythonize_name(k) if isinstance(v, dict) or isinstance(v, list): attr[k] = dict_to_python_object_deep(v) else: attr[k] = v return Expando(attr) elif isinstance(d, list): temp_list = [] for item in d: if isinstance(item, dict) or isinstance(item, list): temp_list.append(dict_to_python_object_deep(item)) else: temp_list.append(item) return temp_list else: return d
def test_parse_error(self): """test abort parse_error function in handler""" # test normal 2xx http_response = MockHttpResponse(status=208) self.assertFalse(handler.parse_error(http_response, None)) # test abnormal 1xx http_response = MockHttpResponse(status=108) err = None try: handler.parse_error(http_response, None) except BceClientError as e: err = e finally: self.assertIsNotNone(err) # test abnormal 3xx 4xx 5xx with json body json_content = {"message": "error", "code": 123, "requestId": 12345} http_response = MockHttpResponse(status=508, content=json.dumps(json_content)) err = None try: handler.parse_error(http_response, None) except BceServerError as e: err = e finally: self.assertIsNotNone(err) self.assertEqual(compat.convert_to_string(err), "error") self.assertEqual(err.code, 123) self.assertEqual(err.request_id, 12345) self.assertEqual(err.status_code, 508) # test abnormal 3xx 4xx 5xx without json body http_response = MockHttpResponse(status=508) response = BceResponse() response.metadata.bce_request_id = 12345 err = None try: handler.parse_error(http_response, response) except BceServerError as e: err = e finally: self.assertIsNotNone(err) self.assertEqual(compat.convert_to_string(err), "Mock") self.assertEqual(err.request_id, 12345) self.assertEqual(err.status_code, 508)
def list_instances(self, cluster_id, instance_group_id): """ List instances :param cluster_id: cluster id :type cluster_id: string :param instance_group_id: instance group id :type instance_group_id: string :return: :rtype baidubce.bce_response.BceResponse """ path = '/cluster/%s/instanceGroup/%s/instance' % ( compat.convert_to_string(cluster_id), compat.convert_to_string(instance_group_id)) return self._send_request(http_methods.GET, path)
def terminate_cluster(self, cluster_id): """ Terminate cluster :param cluster_id: cluster id :type cluster_id: string :return: :rtype baidubce.bce_response.BceResponse """ path = '/cluster/%s' % compat.convert_to_string(cluster_id) return self._send_request(http_methods.DELETE, path)
def dict_to_python_object(d): """ :param d: :return: """ attr = {} for k, v in iteritems(d): if not isinstance(k, compat.string_types): k = compat.convert_to_string(k) k = pythonize_name(k) attr[k] = v return Expando(attr)
def guess_content_type_by_file_name(file_name): """ Get file type by filename. :type file_name: string :param file_name: None ======================= :return: **Type Value** """ mime_map = dict() mime_map[b"js"] = b"application/javascript" mime_map[ b"xlsx"] = b"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" mime_map[ b"xltx"] = b"application/vnd.openxmlformats-officedocument.spreadsheetml.template" mime_map[ b"potx"] = b"application/vnd.openxmlformats-officedocument.presentationml.template" mime_map[ b"ppsx"] = b"application/vnd.openxmlformats-officedocument.presentationml.slideshow" mime_map[ b"pptx"] = b"application/vnd.openxmlformats-officedocument.presentationml.presentation" mime_map[ b"sldx"] = b"application/vnd.openxmlformats-officedocument.presentationml.slide" mime_map[ b"docx"] = b"application/vnd.openxmlformats-officedocument.wordprocessingml.document" mime_map[ b"dotx"] = b"application/vnd.openxmlformats-officedocument.wordprocessingml.template" mime_map[b"xlam"] = b"application/vnd.ms-excel.addin.macroEnabled.12" mime_map[ b"xlsb"] = b"application/vnd.ms-excel.sheet.binary.macroEnabled.12" try: name = os.path.basename(file_name.lower()) suffix = name.split(b'.')[-1] if suffix in iterkeys(mime_map): mime_type = mime_map[suffix] else: import mimetypes mimetypes.init() suffix = compat.convert_to_string(b"." + suffix) mime_type = mimetypes.types_map[suffix] mime_type = compat.convert_to_bytes(mime_type) except: mime_type = b'application/octet-stream' if not mime_type: mime_type = b'application/octet-stream' return mime_type
def parse_json(http_response, response): """If the body is not empty, convert it to a python object and set as the value of response.body. http_response is always closed if no error occurs. :param http_response: the http_response object returned by HTTPConnection.getresponse() :type http_response: httplib.HTTPResponse :param response: general response object which will be returned to the caller :type response: baidubce.BceResponse :return: always true :rtype bool """ body = http_response.read() if body: body = compat.convert_to_string(body) response.__dict__.update(json.loads(body, object_hook=utils.dict_to_python_object).__dict__) http_response.close() return True
def _get_connection(protocol, host, port, connection_timeout_in_millis): """ :param protocol :type protocol: baidubce.protocol.Protocol :param endpoint :type endpoint: str :param connection_timeout_in_millis :type connection_timeout_in_millis int """ host = compat.convert_to_string(host) if protocol.name == baidubce.protocol.HTTP.name: return http.client.HTTPConnection( host=host, port=port, timeout=connection_timeout_in_millis / 1000) elif protocol.name == baidubce.protocol.HTTPS.name: return http.client.HTTPSConnection( host=host, port=port, timeout=connection_timeout_in_millis / 1000) else: raise ValueError( 'Invalid protocol: %s, either HTTP or HTTPS is expected.' % protocol)
def add_steps(self, cluster_id, steps, client_token=None): """ Add steps :param cluster_id: cluster id :type cluster_id: string :param steps: steps to be added :type steps: Array :return: :rtype baidubce.bce_response.BceResponse """ path = '/cluster/%s/step' % compat.convert_to_string(cluster_id) params = None if client_token is not None: params = {'clientToken': client_token} body = json.dumps({'steps': steps}) return self._send_request(http_methods.POST, path, params=params, body=body)
def parse_host_port(endpoint, default_protocol): """ parse protocol, host, port from endpoint in config :type: string :param endpoint: endpoint in config :type: baidubce.protocol.HTTP or baidubce.protocol.HTTPS :param default_protocol: if there is no scheme in endpoint, we will use this protocol as default :return: tuple of protocol, host, port """ # netloc should begin with // according to RFC1808 if b"//" not in endpoint: endpoint = b"//" + endpoint try: # scheme in endpoint dominates input default_protocol parse_result = urlparse(endpoint, compat.convert_to_bytes(default_protocol.name)) except Exception as e: raise ValueError('Invalid endpoint:%s, error:%s' % (endpoint, compat.convert_to_string(e))) if parse_result.scheme == compat.convert_to_bytes( baidubce.protocol.HTTP.name): protocol = baidubce.protocol.HTTP port = baidubce.protocol.HTTP.default_port elif parse_result.scheme == compat.convert_to_bytes( baidubce.protocol.HTTPS.name): protocol = baidubce.protocol.HTTPS port = baidubce.protocol.HTTPS.default_port else: raise ValueError('Unsupported protocol %s' % parse_result.scheme) host = parse_result.hostname if parse_result.port is not None: port = parse_result.port return protocol, host, port
def get_md5_from_fp(fp, offset=0, length=-1, buf_size=8192): """ Get MD5 from file by fp. :type fp: FileIO :param fp: None :type offset: long :param offset: None :type length: long :param length: None ======================= :return: **file_size, MD(encode by base64)** """ origin_offset = fp.tell() if offset: fp.seek(offset) md5 = hashlib.md5() while True: bytes_to_read = buf_size if bytes_to_read > length > 0: bytes_to_read = length buf = fp.read(bytes_to_read) if not buf: break md5.update(buf) if length > 0: length -= len(buf) if length == 0: break fp.seek(origin_offset) #return base64.standard_b64encode(md5.digest()) return compat.convert_to_string(base64.standard_b64encode(md5.digest()))
def update_nat(self, nat_id, name, client_token=None, config=None): """ Update the name of a specified nat-gateway. :param nat_id: The id of specified nat-gateway. :type nat_id: string :param name: The new name of the nat-gateway :type name: 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 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, 'nat', nat_id) if client_token is None: client_token = generate_client_token() params = {b'clientToken': client_token} body = {'name': compat.convert_to_string(name)} return self._send_request(http_methods.PUT, path, body=json.dumps(body), params=params, config=config)
def create_nat(self, name, vpc_id, spec, billing=None, eips=None, client_token=None, config=None): """ Create a nat-gateway with the specified options. A nat gateway can bind only one public EIP, but can bind one or more EIPs in shared-bandwidth group :param client_token: An ASCII string whose length is less than 64. The request will be idempotent if client token is provided. :type client_token: string :param name: The name of nat-gateway that will be created. :type name: string :param vpc_id: The id of VPC. :type vpc_id: string :param spec: The size of nat-gateway that needs to be created. It includes the following three types: small: support 5 public-IP-bindings at most, medium: support 10 public-IP-bindings at most, large: support 15 public-IP-bindings at most. :type spec: string :param billing: Billing information. :type billing: nat_model.Billing :param eips: A public EIP or one/more EIPs in shared-bandwidth group, which will be bound with nat-gateway. :type eips: list<String> :param config: :type config: baidubce.BceClientConfiguration :return: :rtype baidubce.bce_response.BceResponse """ path = utils.append_uri(self.version, 'nat') if client_token is None: client_token = generate_client_token() params = {b'clientToken': client_token} if billing is None: billing = default_billing_to_purchase_created body = { 'name': compat.convert_to_string(name), 'vpcId': compat.convert_to_string(vpc_id), 'spec': compat.convert_to_string(spec), 'billing': billing.__dict__ } if eips is not None: body['eips'] = eips return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params, config=config)
def create_subnet(self, name, zone_name, cidr, vpc_id, subnet_type=None, description=None, client_token=None, config=None): """ Create a subnet with the specified options. :param name: The name of subnet that will be created. type name: string :param zone_name: The name of available zone which the subnet belong through listZones, we can get all available zone info at current region ee.g. "cn-gz-a" "cn-gz-b" type zone_name: string :param cidr: The CIDR of this subnet. type cidr: string :param vpc_id: The id of vpc which this subnet belongs. type vpc_id: string :param subnet_type: The option param to describe the type of subnet create type subnet_type: string :param description: The option param to describe the subnet 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 :param config: :type config: baidubce.BceClientConfiguration :return: :rtype baidubce.bce_response.BceResponse """ path = b'/subnet' params = {} if client_token is None: params[b'clientToken'] = generate_client_token() else: params[b'clientToken'] = client_token body = { 'name': compat.convert_to_string(name), 'zoneName': compat.convert_to_string(zone_name), 'cidr': compat.convert_to_string(cidr), 'vpcId': compat.convert_to_string(vpc_id) } if subnet_type is not None: body['subnetType'] = compat.convert_to_string(subnet_type) if description is not None: body['description'] = compat.convert_to_string(description) return self._send_request(http_methods.POST, path, body=json.dumps(body), params=params, config=config)
def send_request(config, sign_function, response_handler_functions, http_method, path, body, headers, params): """ Send request to BCE services. :param config :type config: baidubce.BceClientConfiguration :param sign_function: :param response_handler_functions: :type response_handler_functions: list :param request: :type request: baidubce.internal.InternalRequest :return: :rtype: baidubce.BceResponse """ _logger.debug(b'%s request start: %s %s, %s, %s', http_method, path, headers, params, body) headers = headers or {} user_agent = 'bce-sdk-python/%s/%s/%s' % (compat.convert_to_string( baidubce.SDK_VERSION), sys.version, sys.platform) user_agent = user_agent.replace('\n', '') user_agent = compat.convert_to_bytes(user_agent) headers[http_headers.USER_AGENT] = user_agent should_get_new_date = False if http_headers.BCE_DATE not in headers: should_get_new_date = True headers[http_headers.HOST] = config.endpoint if isinstance(body, str): body = body.encode(baidubce.DEFAULT_ENCODING) if not body: headers[http_headers.CONTENT_LENGTH] = 0 elif isinstance(body, bytes): headers[http_headers.CONTENT_LENGTH] = len(body) elif http_headers.CONTENT_LENGTH not in headers: raise ValueError(b'No %s is specified.' % http_headers.CONTENT_LENGTH) # store the offset of fp body offset = None if hasattr(body, "tell") and hasattr(body, "seek"): offset = body.tell() protocol, host, port = utils.parse_host_port(config.endpoint, config.protocol) headers[http_headers.HOST] = host if port != config.protocol.default_port: headers[http_headers.HOST] += b':' + compat.convert_to_bytes(port) headers[http_headers.AUTHORIZATION] = sign_function( config.credentials, http_method, path, headers, params) encoded_params = utils.get_canonical_querystring(params, False) if len(encoded_params) > 0: uri = path + b'?' + encoded_params else: uri = path check_headers(headers) retries_attempted = 0 errors = [] while True: conn = None try: # restore the offset of fp body when retrying if should_get_new_date is True: headers[http_headers.BCE_DATE] = utils.get_canonical_time() headers[http_headers.AUTHORIZATION] = sign_function( config.credentials, http_method, path, headers, params) if retries_attempted > 0 and offset is not None: body.seek(offset) conn = _get_connection(protocol, host, port, config.connection_timeout_in_mills) _logger.debug( 'request args:method=%s, uri=%s, headers=%s,patams=%s, body=%s', http_method, uri, headers, params, body) http_response = _send_http_request(conn, http_method, uri, headers, body, config.send_buf_size) headers_list = http_response.getheaders() # on py3 ,values of headers_list is decoded with ios-8859-1 from # utf-8 binary bytes # headers_list[*][0] is lowercase on py2 # headers_list[*][0] is raw value py3 if compat.PY3 and isinstance(headers_list, list): temp_heads = [] for k, v in headers_list: k = k.encode('latin-1').decode('utf-8') v = v.encode('latin-1').decode('utf-8') k = k.lower() temp_heads.append((k, v)) headers_list = temp_heads _logger.debug('request return: status=%d, headers=%s' % (http_response.status, headers_list)) response = BceResponse() response.set_metadata_from_headers(dict(headers_list)) for handler_function in response_handler_functions: if handler_function(http_response, response): break return response except Exception as e: if conn is not None: conn.close() # insert ">>>>" before all trace back lines and then save it errors.append('\n'.join( '>>>>' + line for line in traceback.format_exc().splitlines())) if config.retry_policy.should_retry(e, retries_attempted): delay_in_millis = config.retry_policy.get_delay_before_next_retry_in_millis( e, retries_attempted) time.sleep(delay_in_millis / 1000.0) else: raise BceHttpClientError( 'Unable to execute HTTP request. Retried %d times. ' 'All trace backs:\n%s' % (retries_attempted, '\n'.join(errors)), e) retries_attempted += 1
def create_cluster(self, image_type, image_version, instance_groups, client_token=None, applications=None, auto_terminate=None, log_uri=None, name=None, steps=None, service_ha_enabled=None, safe_mode_enabled=None, admin_pass=None, vpc_id=None, subnet_id=None, security_group=None, availability_zone=None, templateType=None): """ Create cluster :param image_type: the type of virtual machine image :type image_type: string :param image_version: the version of virtual machine image :type image_version: string :param instance_groups: instance groups for cluster :type instance_groups: array :return: :rtype baidubce.bce_response.BceResponse """ path = '/cluster' params = None if client_token is not None: params = {'clientToken': client_token} body = { 'imageType': compat.convert_to_string(image_type), 'imageVersion': compat.convert_to_string(image_version), 'instanceGroups': instance_groups } if applications is not None: body['applications'] = applications if auto_terminate is not None: body['autoTerminate'] = auto_terminate if name is not None: body['name'] = name if log_uri is not None: body['logUri'] = log_uri if steps is not None: body['steps'] = steps if service_ha_enabled is not None: body['serviceHaEnabled'] = service_ha_enabled if safe_mode_enabled is not None: body['safeModeEnabled'] = safe_mode_enabled if admin_pass is not None and self.config is not None: secret_access_key = self.config.credentials.secret_access_key body['adminPassword'] = aes128_encrypt_16char_key( admin_pass, secret_access_key) if vpc_id is not None: body['vpcId'] = vpc_id if subnet_id is not None: body['subnetId'] = subnet_id if security_group is not None: body['securityGroup'] = security_group if availability_zone is not None: body['availabilityZone'] = availability_zone if templateType is not None: body['templateType'] = templateType return self._send_request(http_methods.POST, path, params=params, body=json.dumps(body))
def update_acl(self, acl_rule_id, description=None, protocol=None, source_ip_address=None, destination_ip_address=None, source_port=None, destination_port=None, position=None, action=None, client_token=None, config=None): """ Modify the special attribute to new value of the acl owned by the user. :param acl_rule_id id of the acl to be modified :type acl_rule_id:string :param description: The option param to describe the acl rule. :type description: string :param protocol: The parameter specify which protocol will the acl rule work on :value: "all" or ""tcp" or "udp" or "icmp" :type protocol: string :param source_ip_address: source ip address which the rule applied to :type source_ip_address: string :param destination_ip_address: destination ip address which the rule applied to :type destination_ip_address: string :param source_port: port used by source ip address :value 1-65535 :type source_port: string :param destination_port: port used by destination ip address :value 1-65535 :type destination_port:string :param position: priority of the rule :value 1-5000,unique,The smaller the value, the higher the priority :type:position:Integer :param action: the rule is allowed or denied :value "allow" or "deny" :type action: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.prefix, 'acl', 'rule', acl_rule_id) params = {} if client_token is None: params[b'clientToken'] = generate_client_token() else: params[b'clientToken'] = client_token body = {} if description is not None: body['description'] = compat.convert_to_string(description) if protocol is not None: body['protocol'] = compat.convert_to_string(protocol) if source_ip_address is not None: body['sourceIpAddress'] = \ compat.convert_to_string(source_ip_address) if destination_ip_address is not None: body['destinationIpAddress'] = \ compat.convert_to_string(destination_ip_address) if source_port is not None: body['sourcePort'] = compat.convert_to_string(source_port) if destination_port is not None: body['destinationPort'] = \ compat.convert_to_string(destination_port) if position is not None: body['position'] = position if action is not None: body['action'] = compat.convert_to_string(action) return self._send_request(http_methods.PUT, path, json.dumps(body), params=params, config=config)