Exemple #1
0
 def __init__(self,
              credentials=None,
              endpoint=None,
              protocol=None,
              region=None,
              connection_timeout_in_mills=None,
              send_buf_size=None,
              recv_buf_size=None,
              retry_policy=None,
              security_token=None,
              cname_enabled=False,
              backup_endpoint=None):
     self.credentials = credentials
     self.endpoint = compat.convert_to_bytes(
         endpoint) if endpoint is not None else endpoint
     self.protocol = protocol
     self.region = region
     self.connection_timeout_in_mills = connection_timeout_in_mills
     self.send_buf_size = send_buf_size
     self.recv_buf_size = recv_buf_size
     if retry_policy is None:
         self.retry_policy = BackOffRetryPolicy()
     else:
         self.retry_policy = retry_policy
     self.security_token = security_token
     self.cname_enabled = cname_enabled
     self.backup_endpoint = compat.convert_to_bytes(
         backup_endpoint
     ) if backup_endpoint is not None else backup_endpoint
Exemple #2
0
    def test_list_bucket(self):
        """test list_bucket function normally"""

        if self.bos.does_bucket_exist("aaaaaaxzr1"):
            self.bos.delete_bucket("aaaaaaxzr1")
        if self.bos.does_bucket_exist("aaaaaaxzr2"):
            self.bos.delete_bucket("aaaaaaxzr2")

        time1 = utils.get_canonical_time()
        self.bos.create_bucket("aaaaaaxzr1")

        time2 = utils.get_canonical_time()
        self.bos.create_bucket("aaaaaaxzr2")

        response = self.bos.list_buckets()
        self.check_headers(response)

        self.assertEqual(response.owner.id, bos_test_config.OWNER_ID)
        self.assertEqual(response.owner.display_name,
                         bos_test_config.DISPLAY_NAME)
        for bucket in response.buckets:
            if bucket.name == "aaaaaaxzr1":
                self.assertEqual(
                    compat.convert_to_bytes(bucket.creation_date)[0:19],
                    compat.convert_to_bytes(time1)[0:19])
            elif bucket.name == "aaaaaaxzr2":
                self.assertEqual(
                    compat.convert_to_bytes(bucket.creation_date)[0:19],
                    compat.convert_to_bytes(time2)[0:19])
        self.bos.delete_bucket("aaaaaaxzr1")
        self.bos.delete_bucket("aaaaaaxzr2")
Exemple #3
0
 def _convert_config(self, config=None):
     if config is not None:
         if config.endpoint is not None:
             config.endpoint = compat.convert_to_bytes(config.endpoint)
         if config.credentials is not None:
             config.credentials.access_key_id = \
                 compat.convert_to_bytes(config.credentials.access_key_id)
             config.credentials.secret_access_key = \
                 compat.convert_to_bytes(config.credentials.secret_access_key)
     return config
Exemple #4
0
    def _wrapper(credentials, http_method, path, headers, params):
        credentials.access_key_id = compat.convert_to_bytes(
            credentials.access_key_id)
        credentials.secret_access_key = compat.convert_to_bytes(
            credentials.secret_access_key)

        return bce_v1_signer.sign(credentials,
                                  compat.convert_to_bytes(http_method),
                                  compat.convert_to_bytes(path),
                                  headers,
                                  params,
                                  headers_to_sign=headers_to_sign)
Exemple #5
0
    def test_list_object_with_max_keys(self):
        """test list_objects function with max_keys"""
        for i in range(0, 9):
            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)

            all_list = list()
            tmp_list = list()

            for item in response.contents:
                all_list.append(item.key)

            response = self.bos.list_objects(self.BUCKET, max_keys=4)
            for item in response.contents:
                tmp_list.append(item.key)

            response = self.bos.list_objects(self.BUCKET,
                                             max_keys=5,
                                             marker=tmp_list[-1])
            for item in response.contents:
                tmp_list.append(item.key)

            self.assertListEqual(all_list, tmp_list)
Exemple #6
0
 def test_get_normalized_char_list(self):
     """test _get_normalized_char_list"""
     chars = [
         '%00', '%01', '%02', '%03', '%04', '%05', '%06', '%07', '%08',
         '%09', '%0A', '%0B', '%0C', '%0D', '%0E', '%0F', '%10', '%11',
         '%12', '%13', '%14', '%15', '%16', '%17', '%18', '%19', '%1A',
         '%1B', '%1C', '%1D', '%1E', '%1F', '%20', '%21', '%22', '%23',
         '%24', '%25', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '-',
         '.', '%2F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
         '%3A', '%3B', '%3C', '%3D', '%3E', '%3F', '%40', 'A', 'B', 'C',
         'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '%5B', '%5C',
         '%5D', '%5E', '_', '%60', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
         'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
         'v', 'w', 'x', 'y', 'z', '%7B', '%7C', '%7D', '~', '%7F', '%80',
         '%81', '%82', '%83', '%84', '%85', '%86', '%87', '%88', '%89',
         '%8A', '%8B', '%8C', '%8D', '%8E', '%8F', '%90', '%91', '%92',
         '%93', '%94', '%95', '%96', '%97', '%98', '%99', '%9A', '%9B',
         '%9C', '%9D', '%9E', '%9F', '%A0', '%A1', '%A2', '%A3', '%A4',
         '%A5', '%A6', '%A7', '%A8', '%A9', '%AA', '%AB', '%AC', '%AD',
         '%AE', '%AF', '%B0', '%B1', '%B2', '%B3', '%B4', '%B5', '%B6',
         '%B7', '%B8', '%B9', '%BA', '%BB', '%BC', '%BD', '%BE', '%BF',
         '%C0', '%C1', '%C2', '%C3', '%C4', '%C5', '%C6', '%C7', '%C8',
         '%C9', '%CA', '%CB', '%CC', '%CD', '%CE', '%CF', '%D0', '%D1',
         '%D2', '%D3', '%D4', '%D5', '%D6', '%D7', '%D8', '%D9', '%DA',
         '%DB', '%DC', '%DD', '%DE', '%DF', '%E0', '%E1', '%E2', '%E3',
         '%E4', '%E5', '%E6', '%E7', '%E8', '%E9', '%EA', '%EB', '%EC',
         '%ED', '%EE', '%EF', '%F0', '%F1', '%F2', '%F3', '%F4', '%F5',
         '%F6', '%F7', '%F8', '%F9', '%FA', '%FB', '%FC', '%FD', '%FE',
         '%FF'
     ]
     self.assertListEqual([compat.convert_to_bytes(k) for k in chars],
                          utils._get_normalized_char_list())
Exemple #7
0
    def delete_subnet(self, subnet_id, client_token=None, config=None):
        """
        Delete the specified subnet owned by the user.
        :param subnet_id:
            The id of the subnet to be deleted.
        :type subnet_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 = b'/subnet/%s' % compat.convert_to_bytes(subnet_id)
        params = {}

        if client_token is None:
            params[b'clientToken'] = generate_client_token()
        else:
            params[b'clientToken'] = client_token

        return self._send_request(http_methods.DELETE,
                                  path,
                                  params=params,
                                  config=config)
Exemple #8
0
    def reboot_instance(self, instance_id, force_stop=False, config=None):
        """
        Rebooting the instance owned by the user.
        You can reboot the instance only when the instance is Running,
        otherwise, it's will get 409 errorCode.
        This is an asynchronous interface.

        :param instance_id:
            The id of instance.
        :type instance_id: string

        :param force_stop:
            The optional parameter to stop the instance forcibly.If true,
            it will stop the instance just like power off immediately
            and it may result in losing important data which have not been written to disk.
        :type force_stop: boolean

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        instance_id = compat.convert_to_bytes(instance_id)
        path = b'/instance/%s' % instance_id
        body = {'forceStop': force_stop}
        params = {'reboot': None}
        return self._send_request(http_methods.PUT,
                                  path,
                                  json.dumps(body),
                                  params=params,
                                  config=config)
Exemple #9
0
    def create_auto_renew_rules(self,
                                instance_id,
                                renew_time_unit,
                                renew_time,
                                config=None):
        """
        Creating auto renew rules for the bbc.
        It only works for the prepaid bbc.

        :param instance_id:
            The id of instance.
        :type instance_id: string

        :param renew_time_unit
            The parameter to specify the unit of the renew time.
            The renew time unit can be "month" or "year".
        :type renew_time_unit: string

        :param renew_time
            The parameter to specify the renew time.
        :type renew_time: int
        """
        instance_id = compat.convert_to_bytes(instance_id)
        path = b'/instance/batchCreateAutoRenewRules'
        body = {
            'instanceId': instance_id,
            'renewTimeUnit': renew_time_unit,
            'renewTime': renew_time
        }
        params = {}
        return self._send_request(http_methods.POST,
                                  path,
                                  json.dumps(body),
                                  params=params,
                                  config=config)
Exemple #10
0
    def delete_route(self, route_rule_id, client_token=None, config=None):
        """
        Delete the  specific route rule.

        :param route_rule_id:
            The id of the specified route table.
        :type route_rule_id: string

        :param client_token:
            If the clientToken is not specified by the user, a random String
            generated by default algorithm will be used.
        :type route_table_id: string

        :param config:
        :type config: baidubce.BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        path = b'/route/rule/%s' % compat.convert_to_bytes(route_rule_id)
        params = {}
        if client_token is None:
            params[b'clientToken'] = generate_client_token()
        else:
            params[b'clientToken'] = client_token
        return self._send_request(http_methods.DELETE,
                                  path,
                                  params=params,
                                  config=config)
Exemple #11
0
    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)
Exemple #12
0
    def delete_vpc(self, vpc_id, client_token=None, config=None):
        """
        Delete the specified vpc owned by the user.All resource in the vpc must be deleted before the vpc itself
        can be deleted.

        :param vpc_id:
            The id of instance.
        :type vpc_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 = b'/vpc/%s' % compat.convert_to_bytes(vpc_id)
        params = {}

        if client_token is None:
            params[b'clientToken'] = generate_client_token()
        else:
            params[b'clientToken'] = client_token

        return self._send_request(http_methods.DELETE,
                                  path,
                                  params=params,
                                  config=config)
Exemple #13
0
    def unbind_tags(self, instance_id, change_tags, config=None):
        """
        Unbind the tags of existing instances

        :param instance_id:
            The id of instance.
        :type instance_id: string

        :param change_tags:
            List of tags to be unbind
        :type change_tags: list

        :return:
        :rtype baidubce.bce_response.BceResponse

        """
        instance_id = compat.convert_to_bytes(instance_id)
        path = b'/instance/%s/tag' % instance_id

        params = {'unbind': None}

        body = {'changeTags': change_tags}

        return self._send_request(http_methods.PUT,
                                  path,
                                  body=json.dumps(body),
                                  params=params,
                                  config=config)
Exemple #14
0
    def modify_instance_password(self, instance_id, admin_pass, config=None):
        """
        Modifying the password of the instance.
        You can change the instance password only when the instance is Running or Stopped ,
        otherwise, it's will get 409 errorCode.
        This is an asynchronous interface.

        :param instance_id:
            The id of instance.
        :type instance_id: string

        :param admin_pass:
            The new password to update.
            The adminPass will be encrypted in AES-128 algorithm
            with the substring of the former 16 characters of user SecretKey.
        :type admin_pass: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        secret_access_key = self.config.credentials.secret_access_key
        cipher_admin_pass = aes128_encrypt_16char_key(admin_pass,
                                                      secret_access_key)
        instance_id = compat.convert_to_bytes(instance_id)
        path = b'/instance/%s' % instance_id
        body = {'adminPass': cipher_admin_pass}
        params = {'changePass': None}
        return self._send_request(http_methods.PUT,
                                  path,
                                  json.dumps(body),
                                  params=params,
                                  config=config)
Exemple #15
0
    def modify_instance_desc(self, instance_id, desc, config=None):
        """
        Modifying the description  of the instance.
        You can modify the description only when the instance is Running or Stopped ,
        otherwise, it's will get 409 errorCode.

        :param instance_id:
            The id of instance.
        :type instance_id: string

        :param desc:
            The new description of the instance.
        :type name: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        instance_id = compat.convert_to_bytes(instance_id)
        path = b'/instance/%s' % instance_id
        body = {'desc': desc}
        params = {'updateDesc': None}
        return self._send_request(http_methods.PUT,
                                  path,
                                  json.dumps(body),
                                  params=params,
                                  config=config)
Exemple #16
0
    def modify_instance_name(self, instance_id, name, config=None):
        """
        Modifying the name of the instance.
        You can modify the instance name only when the instance is Running or Stopped ,
        otherwise, it's will get 409 errorCode.

        :param instance_id:
            The id of instance.
        :type instance_id: string

        :param name:
            The new value for instance's name.
        :type name: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        instance_id = compat.convert_to_bytes(instance_id)
        path = b'/instance/%s' % instance_id
        body = {'name': name}
        params = {'rename': None}
        return self._send_request(http_methods.PUT,
                                  path,
                                  json.dumps(body),
                                  params=params,
                                  config=config)
Exemple #17
0
def sign(credentials,
         http_method,
         path,
         headers,
         params,
         timestamp=0,
         expiration_in_seconds=1800,
         headers_to_sign=None):
    """
    Create the authorization
    """

    _logger.debug('Sign params: %s %s %s %s %d %d %s' %
                  (http_method, path, headers, params, timestamp,
                   expiration_in_seconds, headers_to_sign))

    headers = headers or {}
    params = params or {}

    sign_key_info = b'bce-auth-v1/%s/%s/%d' % (
        credentials.access_key_id, utils.get_canonical_time(timestamp),
        expiration_in_seconds)
    sign_key = hmac.new(credentials.secret_access_key, sign_key_info,
                        hashlib.sha256).hexdigest()

    canonical_uri = path
    canonical_querystring = utils.get_canonical_querystring(params, True)

    canonical_headers = _get_canonical_headers(headers, headers_to_sign)

    string_to_sign = (b'\n').join(
        [http_method, canonical_uri, canonical_querystring, canonical_headers])
    sign_result = hmac.new(compat.convert_to_bytes(sign_key), string_to_sign,
                           hashlib.sha256).hexdigest()
    # convert to bytes
    sign_result = compat.convert_to_bytes(sign_result)

    if headers_to_sign:
        result = b'%s/%s/%s' % (sign_key_info,
                                (b';').join(headers_to_sign), sign_result)
    else:
        result = b'%s//%s' % (sign_key_info, sign_result)

    _logger.debug('sign_key=[%s] sign_string=[%d bytes][ %s ]' %
                  (sign_key, len(string_to_sign), string_to_sign))
    _logger.debug('result=%s' % result)
    return result
Exemple #18
0
 def get_logo_lib(self, lib, config=None):
     """
     :param lib: private logo lib
     :return: **Http Response**
     """
     return self._send_request(http_methods.GET,
                               b'/v1/logo/lib/%s' %
                               compat.convert_to_bytes(lib),
                               config=config)
Exemple #19
0
    def _send_request(self,
                      http_method,
                      path,
                      body=None,
                      headers=None,
                      params=None,
                      config=None,
                      body_parser=None):
        config = self._merge_config(config)
        if body_parser is None:
            body_parser = handler.parse_json

        config.endpoint = compat.convert_to_bytes(config.endpoint)
        return bce_http_client.send_request(
            config, sign_wrapper([b'host', b'x-bce-date']),
            [handler.parse_error, body_parser], http_method,
            compat.convert_to_bytes(BmrClient.prefix + path), body, headers,
            params)
Exemple #20
0
def check_headers(headers):
    """
    check value in headers, if \n in value, raise
    :param headers:
    :return:
    """
    for k, v in iteritems(headers):
        if isinstance(v, (bytes, str)) and \
        b'\n' in compat.convert_to_bytes(v):
            raise BceClientError(
                r'There should not be any "\n" in header[%s]:%s' % (k, v))
Exemple #21
0
 def del_face_brief(self, lib, brief, config=None):
     """
     :param lib: private face lib
     :param brief: private face brief
     :return: **Http Response**
     """
     params = {b'brief': brief}
     return self._send_request(http_methods.DELETE,
                               b'/v1/face/lib/%s' %
                               compat.convert_to_bytes(lib),
                               params=params,
                               config=config)
Exemple #22
0
 def get_logo_brief(self, lib, brief, config=None):
     """
     :param lib: private logo lib
     :param brief: private logo brief
     :return: **Http Response**
     """
     params = {b'brief': brief}
     return self._send_request(http_methods.GET,
                               b'/v1/logo/lib/%s' %
                               compat.convert_to_bytes(lib),
                               params=params,
                               config=config)
Exemple #23
0
 def del_logo_image(self, lib, image, config=None):
     """
     :param lib: private logo lib
     :param image: private logo image
     :return: **Http Response**
     """
     params = {b'image': image}
     return self._send_request(http_methods.DELETE,
                               b'/v1/logo/lib/%s' %
                               compat.convert_to_bytes(lib),
                               params=params,
                               config=config)
Exemple #24
0
    def get_flavor_raid(self, flavor_id, config=None):
        """
        :param flavor_id:
            The id of flavor.
        :type flavor_id: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        flavor_id = compat.convert_to_bytes(flavor_id)
        path = b'/flavorRaid/%s' % flavor_id
        return self._send_request(http_methods.GET, path, config=config)
Exemple #25
0
 def add_logo_image(self, lib, brief, image, config=None):
     """
     :param lib: private logo lib
     :param brief: private logo brief
     :param image: private logo image
     :return: **Http Response**
     """
     body = {'brief': brief, 'image': image}
     return self._send_request(http_methods.POST,
                               b'/v1/logo/lib/%s' %
                               compat.convert_to_bytes(lib),
                               body=json.dumps(body),
                               config=config)
Exemple #26
0
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
Exemple #27
0
    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)
Exemple #28
0
    def get_image(self, image_id, config=None):
        """
        Get the detail information of specified image.

        :param image_id:
            The id of image.
        :type image_id: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        image_id = compat.convert_to_bytes(image_id)
        path = b'/image/%s' % image_id
        return self._send_request(http_methods.GET, path, config=config)
Exemple #29
0
    def get_deploy_set(self, deploy_set_id, config=None):
        """
        Get the specified deploy set

        :param deploy_set_id:
            The id of the deploy set
        :type deploy_set_id: String

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        deploy_set_id = compat.convert_to_bytes(deploy_set_id)
        path = b'/deployset/%s' % deploy_set_id
        return self._send_request(http_methods.GET, path, config=config)
Exemple #30
0
    def delete_deploy_set(self, deploy_set_id, config=None):
        """
        Delete the specified deploy sets

        :param deploy_set_ids:
            The ids of the deploy sets you want to delete
        :type deploy_set_ids: list

        :return:
        :rtype baidubce.bce_response.BceResponse
        """
        deploy_set_id = compat.convert_to_bytes(deploy_set_id)
        path = b'/deployset/%s' % deploy_set_id
        return self._send_request(http_methods.DELETE, path, config=config)