def create_ali_ecs_PrePaid(ImageId, InstanceType, SecurityGroupIds, VSwitchId, InstanceName, Description, HostName, ZoneId, regionId, service_name, PeriodUnit, *args, **kwargs): print(args) print(kwargs) client = AcsClient(accessKeyId, accessSecret, regionId) request = RunInstancesRequest() request.set_accept_format('json') password = '******' request.set_ImageId(ImageId) request.set_InstanceType(InstanceType) request.set_SecurityGroupIdss(SecurityGroupIds) request.set_VSwitchId(VSwitchId) request.set_InstanceName(InstanceName) request.set_Description(Description) request.set_HostName(HostName) request.set_Password(password) request.set_ZoneId(ZoneId) request.set_Amount(1) request.set_SecurityEnhancementStrategy("Active") request.set_Tags([ { "Key": "group", "Value": service_name } ]) for kwarg in kwargs: # 判断数据盘指定与否 if 'datadisk' in kwarg: DataDisk = kwargs['datadisk'] request.set_DataDisks([ { "Size": DataDisk, "Category": "cloud_ssd" } ]) request.set_Period(1) request.set_PeriodUnit(PeriodUnit) request.set_AutoRenew(True) request.set_AutoRenewPeriod(1) request.set_InstanceChargeType("PrePaid") response = client.do_action_with_exception(request) res = json.loads(str(response, encoding='utf-8')) return res
def run_instances(self): """ 调用创建实例的API,得到实例ID后继续查询实例状态 :return:instance_ids 需要检查的实例ID """ request = RunInstancesRequest() request.set_DryRun(self.dry_run) request.set_InstanceChargeType(self.instance_charge_type) request.set_AutoRenew(self.auto_renew) request.set_Period(self.period) request.set_PeriodUnit(self.period_unit) request.set_ZoneId(self.zone_id) request.set_InternetChargeType(self.internet_charge_type) request.set_ImageId(self.image_id) request.set_SecurityGroupId(self.security_group_id) request.set_InstanceName(self.instance_name) request.set_Password(self.password) request.set_Amount(self.amount) request.set_InternetMaxBandwidthOut(self.internet_max_bandwidth_out) request.set_HostName(self.host_name) request.set_IoOptimized(self.io_optimized) request.set_SecurityEnhancementStrategy( self.security_enhancement_strategy) request.set_InstanceType(self.instance_type) request.set_SystemDiskSize(self.system_disk_size) request.set_SystemDiskCategory(self.system_disk_category) request.set_DataDisks(self.data_disks) body = self.client.do_action_with_exception(request) data = json.loads(body) instance_ids = data['InstanceIdSets']['InstanceIdSet'] print('Success. Instance creation succeed. InstanceIds: {}'.format( ', '.join(instance_ids))) return instance_ids