2
0

UpdateInstanceProjectId.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/python
  2. #coding:utf-8
  3. # -*- encoding:utf-8 -*-
  4. #使用方法
  5. '''
  6. 实例和项目绑定
  7. python UpdateInstanceProjectId.py filename projectId
  8. '''
  9. #filename 的文件格式示例
  10. '''
  11. eda6c7ec-4ecf-4f5e-ac0d-8e9f5b8530cf
  12. eda6c7ec-4ecf-4f5e-ac0d-8e9f5b8530c2
  13. eda6c7ec-4ecf-4f5e-ac0d-8e9f5b8530c3
  14. '''
  15. #示例文件
  16. '''
  17. UpdateInstanceProjectId
  18. '''
  19. from kscore.session import get_session
  20. import sys
  21. from kscore.exceptions import ClientError
  22. ks_access_key_id ='ak'
  23. ks_secret_access_key = 'sk'
  24. region = 'cn-shanghai-2'
  25. if __name__ == "__main__":
  26. if len(sys.argv) == 3:
  27. fileName = sys.argv[1]
  28. projectId = sys.argv[2]
  29. s = get_session()
  30. s.set_credentials(ks_access_key_id, ks_secret_access_key)
  31. iamClient = s.create_client("iam", region, use_ssl=True)
  32. try:
  33. f = open(fileName)
  34. except IOError:
  35. print 'File load Error'
  36. sys.exit(0)
  37. instanceId = f.readline()
  38. while instanceId:
  39. instanceId = instanceId.replace("\n", "")
  40. try:
  41. result = iamClient.update_instance_project_id(InstanceId=instanceId,ProjectId=projectId)
  42. print '实例ID:'+instanceId+'绑定项目:'+projectId+'的结果为'+str(result)
  43. except ClientError, e:
  44. print ' UpdateInstanceProjectId by instanceId '+instanceId+' error '+str(e)
  45. instanceId = f.readline()
  46. f.close()
  47. else:
  48. print "Parameter Error Must Support action and file"