bbc_client.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides a client class for BBC.
  14. """
  15. import copy
  16. import json
  17. import logging
  18. import random
  19. import string
  20. import uuid
  21. from baidubce import bce_base_client
  22. from baidubce.auth import bce_v1_signer
  23. from baidubce.http import bce_http_client
  24. from baidubce.http import handler
  25. from baidubce.http import http_methods
  26. from baidubce.utils import aes128_encrypt_16char_key
  27. from baidubce.utils import required
  28. from baidubce.services.bbc import bbc_model
  29. from baidubce import compat
  30. _logger = logging.getLogger(__name__)
  31. default_billing_to_purchase_created = bbc_model.Billing('Postpaid')
  32. class BbcClient(bce_base_client.BceBaseClient):
  33. """
  34. Bbc client sdk
  35. """
  36. prefix_v2 = b'/v2'
  37. def __init__(self, config=None):
  38. bce_base_client.BceBaseClient.__init__(self, config)
  39. def _merge_config(self, config=None):
  40. if config is None:
  41. return self.config
  42. else:
  43. new_config = copy.copy(self.config)
  44. new_config.merge_non_none_values(config)
  45. return new_config
  46. def _send_request(self, http_method, path,
  47. body=None, headers=None, params=None,
  48. config=None, body_parser=None, api_version=b'/v1'):
  49. config = self._merge_config(config)
  50. if body_parser is None:
  51. body_parser = handler.parse_json
  52. return bce_http_client.send_request(
  53. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  54. http_method, api_version + path, body, headers, params)
  55. @required(flavor_id = (bytes, str), image_id = (bytes, str), raid_id = (bytes, str))
  56. def create_instance(self, flavor_id, image_id, raid_id, root_disk_size_in_gb=20, purchase_count=1,
  57. zone_name=None, subnet_id=None, billing=None, name=None, admin_pass=None,
  58. auto_renew_time_unit=None, auto_renew_time=0, deploy_set_id=None, enable_ht=False,
  59. security_group_id=None, client_token=None, config=None, tags=None):
  60. """
  61. Create a bbc instance with the specified options.
  62. You must fill the field of clientToken, which is especially for keeping idempotent.
  63. This is an asynchronous interface.
  64. :param flavor_id:
  65. The id of flavor, list all available flavors in BbcClient.list_flavors.
  66. :type flavor_id: string
  67. :param image_id:
  68. The id of image, list all available images in BbcClient.list_images.
  69. :type image_id: string
  70. :param raid_id:
  71. The id of raid, list all available raids in BbcClient.get_flavor_raid.
  72. :type raid_id: string
  73. :param root_disk_size_in_gb:
  74. System disk size of the physical machine to be created
  75. :type root_disk_size_in_gb: int
  76. :param purchase_count:
  77. The number of instances to buy, the default value is 1.
  78. :type purchase_count: int
  79. :param zone_name:
  80. The optional parameter to specify the available zone for the instance.
  81. See more detail through list_zones method
  82. :type zone_name: string
  83. :param subnet_id:
  84. The optional parameter to specify the id of subnet from vpc, optional param
  85. default value is default subnet from default vpc
  86. :type subnet_id: string
  87. :param billing:
  88. Billing information.
  89. :type billing: bbc_model.Billing
  90. :param name:
  91. The optional parameter to desc the instance that will be created.
  92. :type name: string
  93. :param enable_ht:
  94. The optional parameter to enable instance hyperthread.
  95. :type name: bool
  96. :param admin_pass:
  97. The optional parameter to specify the password for the instance.
  98. If specify the adminPass,the adminPass must be a 8-16 characters String
  99. which must contains letters, numbers and symbols.
  100. The symbols only contains "!@#$%^*()".
  101. The adminPass will be encrypted in AES-128 algorithm
  102. with the substring of the former 16 characters of user SecretKey.
  103. If not specify the adminPass, it will be specified by an random string.
  104. See more detail on
  105. https://cloud.baidu.com/doc/BBC/s/3jwvxu9iz#%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86%E4%BC%A0%E8%BE%93%E8%A7%84%E8%8C%83
  106. :type admin_pass: string
  107. :param auto_renew_time_unit
  108. The parameter to specify the unit of the auto renew time.
  109. The auto renew time unit can be "month" or "year".
  110. The default value is "month".
  111. :type auto_renew_time_unit: string
  112. :param auto_renew_time
  113. The parameter to specify the auto renew time, the default value is 0.
  114. :type auto_renew_time: string
  115. :deploy_set_id:
  116. The id of the deploy set
  117. :type deploy_set_id: string
  118. :param security_group_id:
  119. The optional parameter to specify the securityGroupId of the instance
  120. vpcId of the securityGroupId must be the same as the vpcId of subnetId
  121. :type security_group_id: string
  122. :param client_token:
  123. An ASCII string whose length is less than 64.
  124. The request will be idempotent if client token is provided.
  125. If the clientToken is not specified by the user,
  126. a random String generated by default algorithm will be used.
  127. See more detail at
  128. https://bce.baidu.com/doc/BCC/API.html#.E5.B9.82.E7.AD.89.E6.80.A7
  129. :type client_token: string
  130. :param tags:
  131. List of tags to be bind
  132. :type tags: list
  133. :return:
  134. :rtype baidubce.bce_response.BceResponse
  135. """
  136. path = b'/instance'
  137. params = {}
  138. if client_token is None:
  139. params['clientToken'] = generate_client_token()
  140. else:
  141. params['clientToken'] = client_token
  142. if root_disk_size_in_gb == 0:
  143. root_disk_size_in_gb = 20
  144. if purchase_count < 1:
  145. purchase_count = 1
  146. if billing is None:
  147. billing = default_billing_to_purchase_created
  148. body = {
  149. 'flavorId': flavor_id,
  150. 'imageId': image_id,
  151. 'raidId': raid_id,
  152. 'rootDiskSizeInGb': root_disk_size_in_gb,
  153. 'purchaseCount': purchase_count,
  154. 'billing': billing.__dict__
  155. }
  156. if zone_name is not None:
  157. body['zoneName'] = zone_name
  158. if subnet_id is not None:
  159. body['subnetId'] = subnet_id
  160. if security_group_id is not None:
  161. body['securityGroupId'] = security_group_id
  162. if name is not None:
  163. body['name'] = name
  164. if deploy_set_id is not None:
  165. body['deploySetId'] = deploy_set_id
  166. if enable_ht:
  167. body['enableHt'] = True
  168. if admin_pass is not None:
  169. secret_access_key = self.config.credentials.secret_access_key
  170. cipher_admin_pass = aes128_encrypt_16char_key(admin_pass, secret_access_key)
  171. body['adminPass'] = cipher_admin_pass
  172. if auto_renew_time_unit is None:
  173. body['autoRenewTimeUnit'] = "month"
  174. else:
  175. body['autoRenewTimeUnit'] = auto_renew_time_unit
  176. if auto_renew_time != 0:
  177. body['autoRenewTime'] = auto_renew_time
  178. if tags is not None:
  179. body['tags'] = tags
  180. return self._send_request(http_methods.POST, path, json.dumps(body),
  181. params=params, config=config)
  182. def list_instances(self, marker=None, max_keys=1000, internal_ip=None, config=None):
  183. """
  184. Return a list of instances owned by the authenticated user.
  185. :param marker:
  186. The optional parameter marker specified in the original request to specify
  187. where in the results to begin listing.
  188. Together with the marker, specify the list result which listing should begin.
  189. If the marker is not specified, the list result will listing from the first one.
  190. :type marker: string
  191. :param max_keys:
  192. The optional parameter to specify the max number of list result to return.
  193. The default value is 1000.
  194. :type max_keys: int
  195. :param internal_ip:
  196. The identified internal ip of instance.
  197. :type internal_ip: string
  198. :return:
  199. :rtype baidubce.bce_response.BceResponse
  200. """
  201. path = b'/instance'
  202. params = {}
  203. if marker is not None:
  204. params['marker'] = marker
  205. if max_keys is not None:
  206. params['maxKeys'] = max_keys
  207. if internal_ip is not None:
  208. params['internalIp'] = internal_ip
  209. return self._send_request(http_methods.GET, path, params=params, config=config)
  210. @required(instance_id=(bytes, str))
  211. def get_instance(self, instance_id, contains_failed=False, config=None):
  212. """
  213. Get the detailed information of specified instance.
  214. :param instance_id:
  215. The id of instance.
  216. :type instance_id: string
  217. :param contains_failed:
  218. The optional parameters to get the failed message.If true, it means get the failed message.
  219. :type contains_failed: boolean
  220. :return:
  221. :rtype baidubce.bce_response.BceResponse
  222. """
  223. instance_id = compat.convert_to_bytes(instance_id)
  224. path = b'/instance/%s' % instance_id
  225. params = {}
  226. if contains_failed:
  227. params['containsFailed'] = contains_failed
  228. return self._send_request(http_methods.GET, path, params=params, config=config)
  229. @required(instance_id=(bytes, str))
  230. def start_instance(self, instance_id, config=None):
  231. """
  232. Starting the instance owned by the user.
  233. You can start the instance only when the instance is Stopped,
  234. otherwise, it's will get 409 errorCode.
  235. This is an asynchronous interface.
  236. :param instance_id: id of instance proposed to start
  237. :type instance_id: string
  238. :return:
  239. :rtype baidubce.bce_response.BceResponse
  240. """
  241. instance_id = compat.convert_to_bytes(instance_id)
  242. path = b'/instance/%s' % instance_id
  243. params = {
  244. 'start': None
  245. }
  246. return self._send_request(http_methods.PUT, path, params=params, config=config)
  247. @required(instance_id=(bytes, str))
  248. def stop_instance(self, instance_id, force_stop=False, config=None):
  249. """
  250. Stopping the instance owned by the user.
  251. You can stop the instance only when the instance is Running,
  252. otherwise, it's will get 409 errorCode.
  253. This is an asynchronous interface.
  254. :param instance_id:
  255. The id of instance.
  256. :type instance_id: string
  257. :param force_stop:
  258. The optional parameter to stop the instance forcibly.If true,
  259. it will stop the instance just like power off immediately
  260. and it may result in losing important data which have not been written to disk.
  261. :type force_stop: boolean
  262. :return:
  263. :rtype baidubce.bce_response.BceResponse
  264. """
  265. instance_id = compat.convert_to_bytes(instance_id)
  266. path = b'/instance/%s' % instance_id
  267. body = {
  268. 'forceStop': force_stop
  269. }
  270. params = {
  271. 'stop': None
  272. }
  273. return self._send_request(http_methods.PUT, path, json.dumps(body),
  274. params=params, config=config)
  275. @required(instance_id=(bytes, str))
  276. def reboot_instance(self, instance_id, force_stop=False, config=None):
  277. """
  278. Rebooting the instance owned by the user.
  279. You can reboot the instance only when the instance is Running,
  280. otherwise, it's will get 409 errorCode.
  281. This is an asynchronous interface.
  282. :param instance_id:
  283. The id of instance.
  284. :type instance_id: string
  285. :param force_stop:
  286. The optional parameter to stop the instance forcibly.If true,
  287. it will stop the instance just like power off immediately
  288. and it may result in losing important data which have not been written to disk.
  289. :type force_stop: boolean
  290. :return:
  291. :rtype baidubce.bce_response.BceResponse
  292. """
  293. instance_id = compat.convert_to_bytes(instance_id)
  294. path = b'/instance/%s' % instance_id
  295. body = {
  296. 'forceStop': force_stop
  297. }
  298. params = {
  299. 'reboot': None
  300. }
  301. return self._send_request(http_methods.PUT, path, json.dumps(body),
  302. params=params, config=config)
  303. @required(instance_id=str)
  304. def batch_add_ip(self, instance_id, private_ips=None, secondary_private_ip_address_count=None, config=None):
  305. """
  306. batch_add_ip
  307. :param instance_id:
  308. :param private_ips:
  309. :param secondary_private_ip_address_count:
  310. :param config:
  311. :return:
  312. :rtype baidubce.bce_response.BceResponse
  313. """
  314. path = b'/instance/batchAddIp'
  315. body = {
  316. 'instanceId': instance_id,
  317. }
  318. if private_ips is not None:
  319. body['privateIps'] = private_ips
  320. if secondary_private_ip_address_count is not None:
  321. body['secondaryPrivateIpAddressCount'] = secondary_private_ip_address_count
  322. params = {
  323. }
  324. return self._send_request(http_methods.PUT, path, json.dumps(body),
  325. params=params, config=config)
  326. @required(instance_id=str, private_ips=list)
  327. def batch_delete_ip(self, instance_id, private_ips, config=None):
  328. """
  329. :param instance_id:
  330. :param private_ips:
  331. :param config:
  332. :return:
  333. """
  334. path = b'/instance/batchDelIp'
  335. body = {
  336. 'instanceId': instance_id,
  337. 'privateIps': private_ips,
  338. }
  339. params = {
  340. }
  341. return self._send_request(http_methods.PUT, path, json.dumps(body),
  342. params=params, config=config)
  343. @required(instance_id=(bytes, str),
  344. renew_time_unit=(bytes, str),
  345. renew_time=int)
  346. def create_auto_renew_rules(self, instance_id, renew_time_unit, renew_time, config=None):
  347. """
  348. Creating auto renew rules for the bbc.
  349. It only works for the prepaid bbc.
  350. :param instance_id:
  351. The id of instance.
  352. :type instance_id: string
  353. :param renew_time_unit
  354. The parameter to specify the unit of the renew time.
  355. The renew time unit can be "month" or "year".
  356. :type renew_time_unit: string
  357. :param renew_time
  358. The parameter to specify the renew time.
  359. :type renew_time: int
  360. """
  361. instance_id = compat.convert_to_bytes(instance_id)
  362. path = b'/instance/batchCreateAutoRenewRules'
  363. body = {
  364. 'instanceId': instance_id,
  365. 'renewTimeUnit': renew_time_unit,
  366. 'renewTime': renew_time
  367. }
  368. params = {
  369. }
  370. return self._send_request(http_methods.POST, path, json.dumps(body),
  371. params=params, config=config)
  372. @required(instance_id=(bytes, str))
  373. def delete_auto_renew_rules(self, instance_id, config=None):
  374. """
  375. Deleting auto renew rules for the bbc.
  376. It only works for the prepaid bbc.
  377. :param instance_id:
  378. The id of instance.
  379. :type instance_id: string
  380. """
  381. instance_id = compat.convert_to_bytes(instance_id)
  382. path = b'/instance/batchDeleteAutoRenewRules'
  383. body = {
  384. 'instanceId': instance_id
  385. }
  386. params = {
  387. }
  388. return self._send_request(http_methods.POST, path, json.dumps(body),
  389. params=params, config=config)
  390. def describe_regions(self, region, config=None):
  391. """
  392. List all region's endpoint information with the specific parameters.
  393. Use global endpoint bbc.baidubce.com to get BBC's endpoint.
  394. :param region:
  395. The id of region.
  396. :type region: string
  397. :return:
  398. :rtype baidubce.bce_response.BceResponse
  399. """
  400. path = b'/region/describeRegions'
  401. body = {
  402. 'region': region
  403. }
  404. params = {}
  405. return self._send_request(http_methods.POST, path, json.dumps(body),
  406. params=params, config=config, api_version=self.prefix_v2)
  407. @required(instance_id=(bytes, str),
  408. name=(bytes, str))
  409. def modify_instance_name(self, instance_id, name, config=None):
  410. """
  411. Modifying the name of the instance.
  412. You can modify the instance name only when the instance is Running or Stopped ,
  413. otherwise, it's will get 409 errorCode.
  414. :param instance_id:
  415. The id of instance.
  416. :type instance_id: string
  417. :param name:
  418. The new value for instance's name.
  419. :type name: string
  420. :return:
  421. :rtype baidubce.bce_response.BceResponse
  422. """
  423. instance_id = compat.convert_to_bytes(instance_id)
  424. path = b'/instance/%s' % instance_id
  425. body = {
  426. 'name': name
  427. }
  428. params = {
  429. 'rename': None
  430. }
  431. return self._send_request(http_methods.PUT, path, json.dumps(body),
  432. params=params, config=config)
  433. def modify_instance_desc(self, instance_id, desc, config=None):
  434. """
  435. Modifying the description of the instance.
  436. You can modify the description only when the instance is Running or Stopped ,
  437. otherwise, it's will get 409 errorCode.
  438. :param instance_id:
  439. The id of instance.
  440. :type instance_id: string
  441. :param desc:
  442. The new description of the instance.
  443. :type name: string
  444. :return:
  445. :rtype baidubce.bce_response.BceResponse
  446. """
  447. instance_id = compat.convert_to_bytes(instance_id)
  448. path = b'/instance/%s' % instance_id
  449. body = {
  450. 'desc': desc
  451. }
  452. params = {
  453. 'updateDesc': None
  454. }
  455. return self._send_request(http_methods.PUT, path, json.dumps(body),
  456. params=params, config=config)
  457. @required(instance_id=(bytes, str),
  458. image_id=(bytes, str),
  459. admin_pass=(bytes, str))
  460. def rebuild_instance(self, instance_id, image_id, admin_pass,
  461. is_preserve_data=True, raid_id=None, sys_root_size=20, config=None):
  462. """
  463. Rebuilding the instance owned by the user.
  464. After rebuilding the instance,
  465. all of snapshots created from original instance system disk will be deleted,
  466. all of customized images will be saved for using in the future.
  467. This is an asynchronous interface
  468. :param instance_id:
  469. The id of instance.
  470. :type instance_id: string
  471. :param image_id:
  472. The id of the image which is used to rebuild the instance.
  473. :type image_id: string
  474. :param admin_pass:
  475. The admin password to login the instance.
  476. The admin password will be encrypted in AES-128 algorithm
  477. with the substring of the former 16 characters of user SecretKey.
  478. See more detail on
  479. https://cloud.baidu.com/doc/BBC/s/3jwvxu9iz#%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86%E4%BC%A0%E8%BE%93%E8%A7%84%E8%8C%83
  480. :type admin_pass: string
  481. :param is_preserve_data:
  482. Whether or not to retain data, the default is true. The raid_id and
  483. sys_root_size fields do not take effect when the value is true
  484. :type is_preserve_data: bool
  485. :param raid_id:
  486. The id of raid. See more details on
  487. https://cloud.baidu.com/doc/BBC/s/Bjwvxu9ul#%E6%9F%A5%E8%AF%A2raid
  488. :type raid_id: string
  489. :param sys_root_size:
  490. System root partition size, default is 20G, value range is 20-100
  491. :type sys_root_size: int
  492. :return:
  493. :rtype baidubce.bce_response.BceResponse
  494. """
  495. secret_access_key = self.config.credentials.secret_access_key
  496. cipher_admin_pass = aes128_encrypt_16char_key(admin_pass, secret_access_key)
  497. instance_id = compat.convert_to_bytes(instance_id)
  498. path = b'/instance/%s' % instance_id
  499. body = {
  500. 'imageId': image_id,
  501. 'adminPass': cipher_admin_pass,
  502. 'isPreserveData': is_preserve_data,
  503. 'raidId': raid_id,
  504. 'sysRootSize': sys_root_size
  505. }
  506. params = {
  507. 'rebuild': None
  508. }
  509. return self._send_request(http_methods.PUT, path, json.dumps(body),
  510. params=params, config=config)
  511. @required(instance_id=(bytes, str))
  512. def release_instance(self, instance_id, config=None):
  513. """
  514. Releasing the instance owned by the user.
  515. Only the Postpaid instance or Prepaid which is expired can be released.
  516. After releasing the instance,
  517. all of the data will be deleted.
  518. all of snapshots created from original instance system disk will be deleted,
  519. all of customized images created from original instance system disk will be reserved.
  520. :param instance_id:
  521. The id of instance.
  522. :type instance_id: string
  523. :return:
  524. :rtype baidubce.bce_response.BceResponse
  525. """
  526. instance_id = compat.convert_to_bytes(instance_id)
  527. api_version = b'/v2'
  528. path = b'/instance/%s' % instance_id
  529. return self._send_request(http_methods.DELETE, path, config=config, api_version=api_version)
  530. @required(instance_id=(bytes, str),
  531. admin_pass=(bytes, str))
  532. def modify_instance_password(self, instance_id, admin_pass, config=None):
  533. """
  534. Modifying the password of the instance.
  535. You can change the instance password only when the instance is Running or Stopped ,
  536. otherwise, it's will get 409 errorCode.
  537. This is an asynchronous interface.
  538. :param instance_id:
  539. The id of instance.
  540. :type instance_id: string
  541. :param admin_pass:
  542. The new password to update.
  543. The adminPass will be encrypted in AES-128 algorithm
  544. with the substring of the former 16 characters of user SecretKey.
  545. :type admin_pass: string
  546. :return:
  547. :rtype baidubce.bce_response.BceResponse
  548. """
  549. secret_access_key = self.config.credentials.secret_access_key
  550. cipher_admin_pass = aes128_encrypt_16char_key(admin_pass, secret_access_key)
  551. instance_id = compat.convert_to_bytes(instance_id)
  552. path = b'/instance/%s' % instance_id
  553. body = {
  554. 'adminPass': cipher_admin_pass
  555. }
  556. params = {
  557. 'changePass': None
  558. }
  559. return self._send_request(http_methods.PUT, path, json.dumps(body),
  560. params=params, config=config)
  561. @required(bbc_ids = list)
  562. def get_vpc_subnet(self, bbc_ids, config=None):
  563. """
  564. Query VPC / Subnet information by BBC instance id
  565. :param bbc_ids:
  566. List of BBC instance IDs that need to query VPC / Subnet information
  567. :type bbc_ids: list
  568. :return:
  569. :rtype baidubce.bce_response.BceResponse
  570. """
  571. path = b'/vpcSubnet'
  572. body = {
  573. 'bbcIds': bbc_ids
  574. }
  575. return self._send_request(http_methods.POST, path, body=json.dumps(body), config = config)
  576. @required(instance_id=(bytes, str), change_tags = list)
  577. def unbind_tags(self, instance_id, change_tags, config=None):
  578. """
  579. Unbind the tags of existing instances
  580. :param instance_id:
  581. The id of instance.
  582. :type instance_id: string
  583. :param change_tags:
  584. List of tags to be unbind
  585. :type change_tags: list
  586. :return:
  587. :rtype baidubce.bce_response.BceResponse
  588. """
  589. instance_id = compat.convert_to_bytes(instance_id)
  590. path = b'/instance/%s/tag' % instance_id
  591. params = {
  592. 'unbind': None
  593. }
  594. body = {
  595. 'changeTags': change_tags
  596. }
  597. return self._send_request(http_methods.PUT, path, body=json.dumps(body),
  598. params=params, config=config)
  599. @required(instance_id=(bytes, str), change_tags = list)
  600. def bind_tags(self, instance_id, change_tags, config=None):
  601. """
  602. bind the tags of existing instances
  603. :param instance_id:
  604. The id of instance.
  605. :type instance_id: string
  606. :param change_tags:
  607. List of tags to be bind
  608. :type change_tags: list
  609. :return:
  610. :rtype baidubce.bce_response.BceResponse
  611. """
  612. instance_id = compat.convert_to_bytes(instance_id)
  613. path = b'/instance/%s/tag' % instance_id
  614. params = {
  615. 'bind': None
  616. }
  617. body = {
  618. 'changeTags': change_tags
  619. }
  620. return self._send_request(http_methods.PUT, path, body=json.dumps(body),
  621. params=params, config=config)
  622. @required(reserved_instance_ids=list,
  623. tags=list)
  624. def bind_reserved_instance_to_tags(self, reserved_instance_ids, tags, config=None):
  625. """
  626. :param reserved_instance_ids:
  627. :param tags:
  628. :param config:
  629. :return:
  630. """
  631. path = b'/bbc/reserved/tag'
  632. tag_list = [tag.__dict__ for tag in tags]
  633. body = {
  634. 'changeTags': tag_list,
  635. 'reservedInstanceIds': reserved_instance_ids
  636. }
  637. params = {
  638. 'bind': None
  639. }
  640. return self._send_request(http_methods.PUT, path, json.dumps(body),
  641. params=params, config=config, api_version=self.prefix_v2)
  642. @required(reserved_instance_ids=list,
  643. tags=list)
  644. def unbind_reserved_instance_from_tags(self, reserved_instance_ids, tags, config=None):
  645. """
  646. :param reserved_instance_ids:
  647. :param tags:
  648. :param config:
  649. :return:
  650. """
  651. path = b'/bbc/reserved/tag'
  652. tag_list = [tag.__dict__ for tag in tags]
  653. body = {
  654. 'changeTags': tag_list,
  655. 'reservedInstanceIds': reserved_instance_ids
  656. }
  657. params = {
  658. 'unbind': None
  659. }
  660. return self._send_request(http_methods.PUT, path, json.dumps(body),
  661. params=params, config=config, api_version=self.prefix_v2)
  662. def list_flavors(self, config=None):
  663. """
  664. :return:
  665. :rtype baidubce.bce_response.BceResponse
  666. """
  667. path = b'/flavor'
  668. return self._send_request(http_methods.GET, path, config = config)
  669. @required(flavor_id = (bytes, str))
  670. def get_flavor(self, flavor_id, config=None):
  671. """
  672. :param flavor_id:
  673. The id of flavor.
  674. :type flavor_id: string
  675. :return:
  676. :rtype baidubce.bce_response.BceResponse
  677. """
  678. flavor_id = compat.convert_to_bytes(flavor_id)
  679. path = b'/flavor/%s' % flavor_id
  680. return self._send_request(http_methods.GET, path, config=config)
  681. @required(flavor_id=(bytes, str))
  682. def get_flavor_raid(self, flavor_id, config=None):
  683. """
  684. :param flavor_id:
  685. The id of flavor.
  686. :type flavor_id: string
  687. :return:
  688. :rtype baidubce.bce_response.BceResponse
  689. """
  690. flavor_id = compat.convert_to_bytes(flavor_id)
  691. path = b'/flavorRaid/%s' % flavor_id
  692. return self._send_request(http_methods.GET, path, config=config)
  693. @required(image_name=(bytes, str),
  694. instance_id=(bytes, str))
  695. def create_image_from_instance_id(self,
  696. image_name,
  697. instance_id,
  698. client_token=None,
  699. config=None):
  700. """
  701. Creating a customized image which can be used for creating instance.
  702. You can create an image from an instance with this method.
  703. While creating an image from an instance, the instance must be Running or Stopped,
  704. otherwise, it's will get 409 errorCode.
  705. This is an asynchronous interface.
  706. :param image_name:
  707. The name for the image that will be created.
  708. The name length from 1 to 65,only contains letters,digital and underline.
  709. :type image_name: string
  710. :param instance_id:
  711. The optional parameter specify the id of the instance which will be used to create the new image.
  712. When instanceId and snapshotId are specified ,only instanceId will be used.
  713. :type instance_id: string
  714. :param client_token:
  715. An ASCII string whose length is less than 64.
  716. The request will be idempotent if client token is provided.
  717. If the clientToken is not specified by the user,
  718. a random String generated by default algorithm will be used.
  719. See more detail at
  720. https://bce.baidu.com/doc/BCC/API.html#.E5.B9.82.E7.AD.89.E6.80.A7
  721. :type client_token: string
  722. :return:
  723. :rtype baidubce.bce_response.BceResponse
  724. """
  725. path = b'/image'
  726. params = None
  727. if client_token is None:
  728. params = {
  729. 'clientToken': generate_client_token()
  730. }
  731. else:
  732. params = {
  733. 'clientToken': client_token
  734. }
  735. body = {
  736. 'imageName': image_name,
  737. 'instanceId': instance_id
  738. }
  739. return self._send_request(http_methods.POST, path, json.dumps(body),
  740. params=params, config=config)
  741. def list_images(self, image_type='All', marker=None, max_keys=1000, config=None):
  742. """
  743. Listing images owned by the authenticated user.
  744. :param image_type:
  745. The optional parameter to filter image to list.
  746. See more detail at
  747. https://bce.baidu.com/doc/BCC/API.html#ImageType"
  748. :type image_type: menu{'All', System', 'Custom', 'Integration'}
  749. :param marker:
  750. The optional parameter marker specified in the original request to specify
  751. where in the results to begin listing.
  752. Together with the marker, specifies the list result which listing should begin.
  753. If the marker is not specified, the list result will listing from the first one.
  754. :type marker: string
  755. :param max_keys:
  756. The optional parameter to specify the max number of list result to return.
  757. The default value is 1000.
  758. :type max_keys: int
  759. :return:
  760. :rtype baidubce.bce_response.BceResponse
  761. """
  762. path = b'/image'
  763. params = {
  764. 'imageType': image_type
  765. }
  766. if marker is not None:
  767. params['marker'] = marker
  768. if max_keys is not None:
  769. params['maxKeys'] = max_keys
  770. return self._send_request(http_methods.GET, path, params=params, config=config)
  771. @required(image_id=(bytes, str))
  772. def get_image(self, image_id, config=None):
  773. """
  774. Get the detail information of specified image.
  775. :param image_id:
  776. The id of image.
  777. :type image_id: string
  778. :return:
  779. :rtype baidubce.bce_response.BceResponse
  780. """
  781. image_id = compat.convert_to_bytes(image_id)
  782. path = b'/image/%s' % image_id
  783. return self._send_request(http_methods.GET, path, config=config)
  784. @required(image_id=(bytes, str))
  785. def delete_image(self, image_id, config=None):
  786. """
  787. Deleting the specified image.
  788. Only the customized image can be deleted,
  789. otherwise, it's will get 403 errorCode.
  790. :param image_id:
  791. The id of image.
  792. :type image_id: string
  793. :return:
  794. :rtype baidubce.bce_response.BceResponse
  795. """
  796. image_id = compat.convert_to_bytes(image_id)
  797. path = b'/image/%s' % image_id
  798. return self._send_request(http_methods.DELETE, path, config=config)
  799. def get_operation_log(self, marker=None, max_keys=100, start_time=None, end_time=None, config=None):
  800. """
  801. Querying information about physical machine operation logs
  802. :param marker:
  803. The optional parameter marker specified in the original request to specify
  804. where in the results to begin listing.
  805. Together with the marker, specifies the list result which listing should begin.
  806. If the marker is not specified, the list result will listing from the first one.
  807. :type marker: string
  808. :param max_keys:
  809. The optional parameter to specify the max number of list result to return.
  810. The default value is 1000.
  811. :type max_keys: int
  812. :param start_time:
  813. The start time of the physical machine operation (UTC time),
  814. the format is yyyy-MM-dd'T'HH: mm: ss'Z ', if it is empty, query the operation log of the day
  815. :type start_time: string
  816. :params end_time
  817. The end time of the physical machine operation (UTC time),
  818. the format is yyyy-MM-dd'T'HH: mm: ss'Z ', if it is empty, query the operation log of the day
  819. :type end_time: string
  820. :return:
  821. :rtype baidubce.bce_response.BceResponse
  822. """
  823. path = b'/operationLog'
  824. params = {}
  825. if marker is not None:
  826. params['marker'] = marker
  827. if max_keys is not None:
  828. params['maxKeys'] = max_keys
  829. if start_time is not None:
  830. params['startTime'] = start_time
  831. if end_time is not None:
  832. params['endTime'] = end_time
  833. return self._send_request(http_methods.GET, path, params=params, config=config)
  834. @required(concurrency=int, strategy=(bytes, str))
  835. def create_deploy_set(self, concurrency, strategy, name=None, desc=None, client_token=None, config=None):
  836. """
  837. Create a deploy set based on a specified policy and concurrency
  838. :param concurrency:
  839. Deployment set concurrency, range [1,5]
  840. :type concurrency: int
  841. :param strategy:
  842. Deployment set strategy, currently BBC strategy only supports: "tor_ha", "host_ha"
  843. :type strategy: string
  844. :param name:
  845. Deployment set name, supports uppercase and lowercase letters, numbers,
  846. Chinese, and -_ /. Special characters, must start with a letter, length 1-65
  847. :type name: string
  848. :param desc:
  849. Deployment set description
  850. :type desc: string
  851. :param client_token:
  852. An ASCII string whose length is less than 64.
  853. The request will be idempotent if client token is provided.
  854. If the clientToken is not specified by the user,
  855. a random String generated by default algorithm will be used.
  856. See more detail at
  857. https://bce.baidu.com/doc/BCC/API.html#.E5.B9.82.E7.AD.89.E6.80.A7
  858. :type client_token: string
  859. :return:
  860. :rtype baidubce.bce_response.BceResponse
  861. """
  862. path = b'/deployset'
  863. params = {}
  864. if client_token is None:
  865. params['clientToken'] = generate_client_token()
  866. else:
  867. params['clientToken'] = client_token
  868. body = {
  869. "strategy": strategy,
  870. "concurrency": concurrency
  871. }
  872. if name is not None:
  873. body["name"] = name
  874. if desc is not None:
  875. body['desc'] = desc
  876. return self._send_request(http_methods.POST, path, json.dumps(body),
  877. params=params, config=config)
  878. def list_deploy_sets(self, config=None):
  879. """
  880. List all deploy sets
  881. :return:
  882. :rtype baidubce.bce_response.BceResponse
  883. """
  884. path = b'/deployset'
  885. return self._send_request(http_methods.GET, path, config=config)
  886. @required(deploy_set_ids=(bytes, str))
  887. def get_deploy_set(self, deploy_set_id, config=None):
  888. """
  889. Get the specified deploy set
  890. :param deploy_set_id:
  891. The id of the deploy set
  892. :type deploy_set_id: String
  893. :return:
  894. :rtype baidubce.bce_response.BceResponse
  895. """
  896. deploy_set_id = compat.convert_to_bytes(deploy_set_id)
  897. path = b'/deployset/%s' % deploy_set_id
  898. return self._send_request(http_methods.GET, path, config=config)
  899. @required(deploy_set_ids=(bytes, str))
  900. def delete_deploy_set(self, deploy_set_id, config=None):
  901. """
  902. Delete the specified deploy sets
  903. :param deploy_set_ids:
  904. The ids of the deploy sets you want to delete
  905. :type deploy_set_ids: list
  906. :return:
  907. :rtype baidubce.bce_response.BceResponse
  908. """
  909. deploy_set_id = compat.convert_to_bytes(deploy_set_id)
  910. path = b'/deployset/%s' % deploy_set_id
  911. return self._send_request(http_methods.DELETE, path, config=config)
  912. def generate_client_token_by_uuid():
  913. """
  914. The default method to generate the random string for client_token
  915. if the optional parameter client_token is not specified by the user.
  916. :return:
  917. :rtype string
  918. """
  919. return str(uuid.uuid4())
  920. def generate_client_token_by_random():
  921. """
  922. The alternative method to generate the random string for client_token
  923. if the optional parameter client_token is not specified by the user.
  924. :return:
  925. :rtype string
  926. """
  927. client_token = ''.join(random.sample(string.ascii_letters + string.digits, 36))
  928. return client_token
  929. generate_client_token = generate_client_token_by_uuid