def create_channel(app_id, channel_id, access_key_id, access_key_secret, region_id, endpoint): try: client = AcsClient(access_key_id, access_key_secret, region_id) request = CreateChannelRequest.CreateChannelRequest() request.set_AppId(app_id) request.set_ChannelId(channel_id) # Use HTTP, x3 times faster than HTTPS. rtc_request.set_default_protocol_type(rtc_protocol_type.HTTP) response = client.do_action_with_exception(request) obj = json.loads(response) auth = ChannelAuth() auth.app_id = app_id auth.channel_id = channel_id auth.nonce = obj['Nonce'] auth.timestamp = obj['Timestamp'] auth.channel_key = obj['ChannelKey'] auth.request_id = obj['RequestId'] auth.recovered = False return auth except Exception as ex: return recover_for_error(ex, app_id, channel_id)
def test_default_protocol_type(self): self.assertEqual(get_default_protocol_type(), "http") set_default_protocol_type("https") self.assertEqual(get_default_protocol_type(), "https") with self.assertRaises(ClientException) as ex: set_default_protocol_type("WSS") self.assertEqual(ex.exception.error_code, "SDK.InvalidParams") self.assertEqual(ex.exception.message, "Invalid 'protocol_type', should be 'http' or 'https'") set_default_protocol_type("http") r = RpcRequest("product", "version", "action_name") self.assertEqual(r.get_protocol_type(), 'http') r = RpcRequest("product", "version", "action_name", protocol='https') self.assertEqual(r.get_protocol_type(), 'https') r = RpcRequest("product", "version", "action_name", protocol='http') self.assertEqual(r.get_protocol_type(), 'http')
as_completed as _as_completed) from contextlib import contextmanager as _contextmanager import json as _json from uuid import uuid1 as _uuid from aliyunsdkcore.client import AcsClient as _AcsClient import aliyunsdkcore.acs_exception.exceptions as _acs_exceptions import aliyunsdkcore.request as _acs_request from apyfal.host._csp import CSPHost as _CSPHost import apyfal.exceptions as _exc import apyfal._utilities as _utl from apyfal._utilities import get_logger as _get_logger # Set HTTPS by default for requests, require "pyopenssl" package _acs_request.set_default_protocol_type("https") #: Alibaba Cloud API version (Key is subdomain name, value is API version) API_VERSION = {'ecs': '2014-05-26', 'ram': '2015-05-01', 'sts': '2015-04-01'} #: Alibaba Cloud API main domain MAIN_DOMAIN = 'aliyuncs.com' class AlibabaCSP(_CSPHost): """Alibaba Cloud CSP Class Args: host_type (str): Cloud service provider name. (Default to "Alibaba").
def __init__(self, account_id=None, role=None, region_id="eu-central-1"): """ Constructor of alicloudConnect. We will try to connect with env first, config credentials and finally with instance role. User will use the AcsClient to deal with aliyun api. :param account_id: :param role: """ # caller will need to pass accessKey, secretKey # if stsToken = None / or pass stsToken if it is not none. self.__client = None self.access_key_id = None self.access_key_secret = None self.region_id = region_id self.sts_token = None aliyun_request.set_default_protocol_type(protocol_type.HTTPS) # each api has it's own version ! self.version_2014_05_26 = "2014-05-26" self.version_2015_05_01 = "2015-05-01" self.version_2015_04_01 = "2015-04-01" # endpoint definiton by service self.ecs_domain = "ecs.{}.aliyuncs.com".format(self.region_id) self.sts_domain = "sts.aliyuncs.com" self.ram_domain = "ram.aliyuncs.com" access_key_id = '' access_key_secret = '' connect_with_credential = False connect_with_role = False config_file = path.expanduser('~') + "/.aliyun/config.json" logging.info('Connection to Alicloud.') if 'ALICLOUD_ACCESS_KEY' in environ: logging.info( 'Trying to connect with credentials from environment.') access_key_id = environ['ALICLOUD_ACCESS_KEY'] if 'ALICLOUD_SECRET_KEY' in environ: access_key_secret = environ['ALICLOUD_SECRET_KEY'] connect_with_credential = True self.access_key_id = access_key_id self.access_key_secret = access_key_secret else: logging.info( 'Both access ALICLOUD_ACCESS_KEY and secret ALICLOUD_SECRET_KEY are mandatory.' ) elif path.exists(config_file): logging.info('Trying to connect with config credentials.') config = json.load(open(config_file)) access_key_id = config['profiles'][1]['access_key_id'] access_key_secret = config['profiles'][1]['access_key_secret'] region_id = config['profiles'][1]['region_id'] connect_with_credential = True self.access_key_id = access_key_id self.access_key_secret = access_key_secret elif account_id is not None and role is not None: logging.info('Trying to connect with instance role.') response = requests.get( 'http://100.100.100.200/latest/meta-data/ram/security-credentials/%s' % role, timeout=5).json() sts_access_key = response['AccessKeyId'] sts_secret_key = response['AccessKeySecret'] sts_token_session = response['SecurityToken'] self.access_key_id = sts_access_key self.access_key_secret = sts_secret_key connect_with_role = True else: logging.error('No valid connection type provided.') try: if connect_with_credential: self.__client = AcsClient(access_key_id, access_key_secret, region_id) elif connect_with_role: sts_token_credential = StsTokenCredential( sts_access_key, sts_secret_key, sts_token_session) self.sts_token = sts_token_credential self.__client = AcsClient(region_id=self.region_id, credential=sts_token_credential) except Exception as e: logging.error(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0])