kec.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # -*- encoding:utf-8 -*-
  2. from kscore.exceptions import ClientError
  3. from kscore.session import get_session
  4. from pprint import pprint
  5. if __name__ == "__main__":
  6. s = get_session()
  7. client = s.create_client("kec", "cn-beijing-6", use_ssl=False)
  8. # https://docs.ksyun.com/documents/816?type=3
  9. client.describe_instances()
  10. # https://docs.ksyun.com/documents/806?type=3
  11. client.run_instances(
  12. MaxCount=50, MinCount=20, ImageId="3f3bddcf-4982-4ab4-a63d-795e8d74e9d5",
  13. SubnetId="f1bd236b-7fd3-44d3-aef9-2d673a65466e", InstancePassword="Ksyun2017",
  14. SecurityGroupId="2f43a9e4-1a3c-448e-b661-efa6d04b82fc", DataDiskGb=50, ChargeType="Monthly",
  15. InstanceType="C1.1A", PurchaseTime=1, InstanceName="test", InstanceNameSuffix="1")
  16. # https://docs.ksyun.com/documents/809?type=3
  17. instances = ["2f43a9e4-1a3c-448e-b661-efa6d04b82fc", "2f43a9e4-1a3c-448e-b661-efa6d04b82fc"]
  18. instances = dict(("InstanceId.{}".format(index), instance) for index, instance in enumerate(instances, 1))
  19. client.terminate_instances(**instances)
  20. # 用户查询竞价实例历史价格列表
  21. # pprint("-------------------------------用户查询竞价实例历史价格列表---------------------------------")
  22. # _spot_price = client.describe_spot_price_history(InstanceType="E1.1A", AvailabilityZone="cn-shanghai-3a")
  23. # pprint(_spot_price)
  24. # 查看主机价格
  25. # pprint("-------------------------------查看主机价格---------------------------------")
  26. # _instance_price = client.describe_price(InstanceType="N3.1A", ImageId="IMG-5a7cb0e5-d297-4259-9944-38b3c053a7dc", ChargeType="Daily", SriovNetSupport=True)
  27. # pprint(_instance_price)
  28. # 创建实例启动模板
  29. # pprint("-------------------------------创建实例启动模板---------------------------------")
  30. # _instance_model = client.create_model(ImageId="IMG-5a7cb0e5-d297-4259-9944-38b3c053a7dc",
  31. # SubnetId="afa859fc-65ea-410d-99f2-d686cf9da1d9",
  32. # ChargeType="Daily", ModelName="wyn_test_model",
  33. # SecurityGroupId="18523073-c6c0-44ba-9858-1c0f92cb453d", PurchaseTime=0,
  34. # InstanceType="N3.1A")
  35. # pprint(_instance_model)
  36. # 删除实例启动模板
  37. # pprint("-------------------------------删除实例启动模板---------------------------------")
  38. # models = ["0565ffc5-55e3-44e9-b242-5ed3de725ee0"]
  39. # models = dict(("ModelId.{}".format(index), model) for index, model in enumerate(models, 1))
  40. # _del_model = client.terminate_models(**models)
  41. # pprint(_del_model)
  42. # 查看实例启动模板
  43. # pprint("-------------------------------查看实例启动模板---------------------------------")
  44. # _des_models = client.describe_models()
  45. # pprint(_des_models)
  46. # 云主机迁入容灾组
  47. # pprint("-------------------------------云主机迁入容灾组-------------------------------")
  48. # instances = ["5e78a001-e472-44f5-9aa4-c822a8121319"]
  49. # instances = dict(("InstanceId.{}".format(index), instance) for index, instance in enumerate(instances, 1))
  50. # add_vm_into_data_guard = client.add_vm_into_data_guard(DataGuardId="ec148c88-5958-49dc-97a2-76fc21c4552a", **instances)
  51. # pprint(add_vm_into_data_guard)
  52. # 修改容灾分组名称
  53. # pprint("-------------------------------修改容灾分组名称--------------------------------")
  54. # mod_data_guard_group_name = client.modify_data_guard_groups(DataGuardId="ec148c88-5958-49dc-97a2-76fc21c4552a", DataGuardName="wyn_test")
  55. # pprint(mod_data_guard_group_name)
  56. # 查询用户某区域的容灾分组容量
  57. # pprint("-------------------------------查询用户某区域的容灾分组容量--------------------------------")
  58. # des_data_guard_capacity = client.describe_data_guard_capacity()
  59. # pprint(des_data_guard_capacity)
  60. # 创建容灾分组
  61. # pprint("-------------------------------创建容灾分组---------------------------------")
  62. # cre_data_guard_group = client.create_data_guard_group(DataGuardName="my_dataguard_test")
  63. # pprint(cre_data_guard_group)
  64. # 删除容灾分组
  65. # pprint("-------------------------------删除容灾分组---------------------------------")
  66. # data_guards = ["ec148c88-5958-49dc-97a2-76fc21c4552a"]
  67. # data_guards = dict(("DataGuardId.{}".format(index), data_guard) for index, data_guard in enumerate(data_guards, 1))
  68. # del_data_guard_groups = client.delete_data_guard_groups(**data_guards)
  69. # pprint(del_data_guard_groups)
  70. # 查询容灾组信息
  71. # pprint("-------------------------------查询容灾组信息---------------------------------")
  72. # des_data_guard_group = client.describe_data_guard_group()
  73. # pprint(des_data_guard_group)
  74. # 云主机从容灾组中移除
  75. # pprint("-------------------------------云主机从容灾组中移除---------------------------------")
  76. # instances = ["5e78a001-e472-44f5-9aa4-c822a8121319"]
  77. # instances = dict(("InstanceId.{}".format(index), instance) for index, instance in enumerate(instances, 1))
  78. # remove_vm_from_data_guard = client.remove_vm_from_data_guard(DataGuardId="ec148c88-5958-49dc-97a2-76fc21c4552a", **instances)
  79. # pprint(remove_vm_from_data_guard)
  80. # #镜像导入
  81. # client.import_image(ImageName="", Architecture="", Platform="", ImageUrl="", ImageFormat="")
  82. # 镜像复制
  83. # pprint("-------------------------------镜像复制---------------------------------")
  84. # param = {
  85. # "ImageId.1": "432eb1f8-72f3-4320-a0f7-572b33b32431",
  86. # "DestinationRegion.1": "cn-shanghai-3"
  87. # }
  88. # copy_image = client.copy_image(**param)
  89. # pprint(copy_image)
  90. # 查看镜像分享信息
  91. # pprint("-------------------------------查看镜像分享信息---------------------------------")
  92. # describe_image_share_permission = client.describe_image_share_permission(ImageId="e5b7d077-021c-4fb2-9d0e-fa4700f99b58")
  93. # pprint(describe_image_share_permission)
  94. # 修改镜像分享信息
  95. # pprint("-------------------------------修改镜像分享信息---------------------------------")
  96. # accounts = ["73403544"]
  97. # accounts = dict(("AccountId.{}".format(index), account) for index, account in enumerate(accounts, 1))
  98. # modify_image_share_permission = client.modify_image_share_permission(ImageId="e5b7d077-021c-4fb2-9d0e-fa4700f99b58", Permission="share", **accounts)
  99. # pprint(modify_image_share_permission)
  100. # 镜像预热
  101. # pprint("-------------------------------镜像预热---------------------------------")
  102. # enable_image_caching = client.enable_image_caching(ImageId="432eb1f8-72f3-4320-a0f7-572b33b32431")
  103. # pprint(enable_image_caching)
  104. # 取消镜像预热
  105. # pprint("-------------------------------取消镜像预热---------------------------------")
  106. # disable_image_caching = client.disable_image_caching(ImageId="432eb1f8-72f3-4320-a0f7-572b33b32431")
  107. # pprint(disable_image_caching)
  108. # 创建本地盘快照
  109. # pprint("-------------------------------创建本地盘快照---------------------------------")
  110. # create_local_volume_snapshot = client.create_local_volume_snapshot(LocalVolumeId="58ce95e4-0e8f-44e1-8134-dca14697c103-a", LocalVolumeSnapshotName="wyn_test")
  111. # pprint(create_local_volume_snapshot)
  112. # 回滚快照
  113. # pprint("-------------------------------回滚快照---------------------------------")
  114. # rollback_local_volume = client.rollback_local_volume(LocalVolumeSnapshotId="6c08b3ff-34c2-4948-b81d-4c8cc9b7a084")
  115. # pprint(rollback_local_volume)
  116. # 删除快照
  117. # pprint("-------------------------------删除快照---------------------------------")
  118. # local_volume_snapshots = ["6c08b3ff-34c2-4948-b81d-4c8cc9b7a084"]
  119. # local_volume_snapshots = dict(("LocalVolumeSnapshotId.{}".format(index), local_volume_snapshot) for index, local_volume_snapshot in enumerate(local_volume_snapshots, 1))
  120. # delete_local_volume_snapshot = client.delete_local_volume_snapshot(**local_volume_snapshots)
  121. # pprint(delete_local_volume_snapshot)
  122. # 主机绑定密钥
  123. # pprint("-------------------------------主机绑定密钥---------------------------------")
  124. # param = {
  125. # "InstanceId.1": "5e78a001-e472-44f5-9aa4-c822a8121319",
  126. # "KeyId.1": "c079a41f-beb4-4b73-a68c-69f25b9c8819"
  127. # }
  128. # attach_key = client.attach_key(**param)
  129. # pprint(attach_key)
  130. # 主机解绑密钥
  131. # pprint("-------------------------------主机解绑密钥---------------------------------")
  132. # param = {
  133. # "InstanceId.1": "5e78a001-e472-44f5-9aa4-c822a8121319",
  134. # "KeyId.1": "c079a41f-beb4-4b73-a68c-69f25b9c8819"
  135. # }
  136. # detach_key = client.detach_key(**param)
  137. # pprint(detach_key)
  138. # 获取弹性伸缩配置
  139. # pprint("-------------------------------获取弹性伸缩配置---------------------------------")
  140. # list = client.describe_scaling_configuration()
  141. # print(list)
  142. # 创建弹性伸缩配置
  143. # pprint("-------------------------------创建弹性伸缩配置---------------------------------")
  144. # # API参数参见: https://docs.ksyun.com/documents/28246
  145. # res = client.create_scaling_configuration(**{
  146. # "ScalingConfigurationName": "test-scaling-configuration",
  147. # "ImageId": "IMG-5465174a-6d71-4770-b8e1-917a0dd92466",
  148. # "InstanceType": "N3.4B",
  149. # "ChargeType": "HourlyInstantSettlement",
  150. # "ProjectId": 0,
  151. # "KeyId.1": "71c17c37-c9cb-4faf-a86e-d76d35f3c4d9",
  152. # "DataDisk.1.Type": "SSD3.0",
  153. # "DataDisk.1.Size": 50,
  154. # "DataDisk.1.deleteWithInstance": True,
  155. # "SystemDisk.DiskType": "SSD3.0",
  156. # "SystemDisk.DiskSize": 40,
  157. # })
  158. # print(res)
  159. # 编辑弹性伸缩配置
  160. # pprint("-------------------------------编辑弹性伸缩配置---------------------------------")
  161. # # API参数参见: https://docs.ksyun.com/documents/28247
  162. # res = client.modify_scaling_configuration(**{
  163. # "ScalingConfigurationId": res["ScalingConfigurationId"],
  164. # "ScalingConfigurationName": "test-scaling-configuration123",
  165. # })
  166. # print(res)
  167. # 删除弹性伸缩配置
  168. # pprint("-------------------------------删除弹性伸缩配置---------------------------------")
  169. # res = client.delete_scaling_configuration(**{
  170. # "ScalingConfigurationId.1": res["ScalingConfigurationId"]
  171. # })
  172. # print(res)