PageRenderTime 128ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/apiclient-rax.py

https://gitlab.com/kawsark/pyrestclient
Python | 178 lines | 91 code | 55 blank | 32 comment | 8 complexity | aded1dd5133b32c3f43ff3ab2bb4a3b3 MD5 | raw file
  1. #Rackspace Cloud REST API V2.0 Client in Python
  2. import requests
  3. import json, re
  4. #Specify a Rackspace cloud region you want to work with: IAD, DFW, ORD, HKG
  5. REGION="iad"
  6. #Specify a credential file with your API key. This information can be obtained from the Account page of Rackspace Cloud Control Panel
  7. #Note: Always protect credential files by storing them in a secure encrypted location.
  8. #It should be of the format below (without the '#'):
  9. # [rackspace_cloud]
  10. # username = my_username
  11. # api_key = 01234567890abcdef
  12. credentials_file="/path/to/credentials_file/pyrax.creds"
  13. #Uses Rackspace Cloud Identity API 2.0: https://developer.rackspace.com/docs/cloud-identity/v2/
  14. def getAuthToken():
  15. #Load credentials from credentials_file variable
  16. f = open(credentials_file, 'r')
  17. pattern = re.compile("\s*\[rackspace_cloud\]\s*username\s*=\s*(\S*)\s*api_key\s*=\s*(\S*)")
  18. match = pattern.search(f.read())
  19. if match:
  20. username = match.group(1) #Extract username
  21. api_key = match.group(2) #Extract API key
  22. #Authentication endpoint for API V2
  23. url = "https://identity.api.rackspacecloud.com/v2.0/tokens"
  24. #Define Authentication request parameters and authentication token endpoint
  25. data= {"auth": {"RAX-KSKEY:apiKeyCredentials":{"username": username, "apiKey": api_key}}}
  26. headers = {'Content-Type': 'application/json', 'Accept': 'application/json', }
  27. #Make authentication request and return token
  28. r = requests.post(url, json.dumps(data), headers=headers)
  29. if(r.status_code == 200):
  30. #Extract token from response:
  31. p1 = re.compile("token.*\"id\":\"(.*?)\",")
  32. m1 = p1.search(r.text)
  33. token = m1.group(1)
  34. #Extract tenantId from response:
  35. p2 = re.compile("\"tenantId\":\"(\d*?)\",")
  36. m2 = p2.search(r.text)
  37. tenantId = m2.group(1)
  38. print "token =", token, ", tenant =", tenantId
  39. return token, tenantId
  40. else:
  41. #Did not get a valid response
  42. print "Did not get a successful authentication response:", r.json()
  43. return None
  44. else:
  45. print "Could not extract username and api_Key from credentials file. Please adjust credentials_file variable to a valid pyrax credentials: https://github.com/rackspace/pyrax/blob/master/docs/getting_started.md#authenticating "
  46. return None
  47. #Using Rackspace Cloud Servers API v2.0 https://developer.rackspace.com/docs/cloud-servers/v2/
  48. def listCloudServers(token,tenantId):
  49. url = "https://"+REGION+".servers.api.rackspacecloud.com/v2/"+tenantId+"/servers"
  50. #Define Authentication request parameters and authentication token endpoint
  51. headers = {'Content-Type': 'application/json',
  52. 'Accept': 'application/json',
  53. 'X-Auth-Token': token}
  54. r = requests.get(url, headers=headers)
  55. print r.text
  56. #Using Cloud Networks - Neutron API v2.0 https://developer.rackspace.com/docs/cloud-networks/v2/
  57. def listCloudNetworks(token,tenantId):
  58. url = "https://"+REGION+".networks.api.rackspacecloud.com/v2.0/networks"
  59. #Define Authentication request parameters and authentication token endpoint
  60. headers = {'Content-Type': 'application/json',
  61. 'Accept': 'application/json',
  62. 'X-Auth-Token': token}
  63. r = requests.get(url, headers=headers)
  64. print r.text
  65. #Using Cloud Networks - Neutron API v2.0 https://developer.rackspace.com/docs/cloud-networks/v2/
  66. def listCloudSecurityGroups(token,tenantId):
  67. url = "https://"+REGION+".networks.api.rackspacecloud.com/v2.0/security-groups"
  68. #Define Authentication request parameters and authentication token endpoint
  69. headers = {'Content-Type': 'application/json',
  70. 'Accept': 'application/json',
  71. 'X-Auth-Token': token}
  72. r = requests.get(url, headers=headers)
  73. # data = { "security_group":{ "name":"new-webservers", "description":"security group for webservers"} }
  74. # r = requests.post(url, json.dumps(data), headers=headers)
  75. print r.text
  76. #Using Rackspace Cloud Backup API 1.0 https://developer.rackspace.com/docs/cloud-backup/v1/
  77. def listCloudBackups(token,tenantId):
  78. url = "https://"+REGION+".backup.api.rackspacecloud.com/v1.0/"+tenantId+"/backup-configuration"
  79. #Define Authentication request parameters and authentication token endpoint
  80. headers = {'Content-Type': 'application/json',
  81. 'Accept': 'application/json',
  82. 'X-Auth-Token': token}
  83. r = requests.get(url, headers=headers)
  84. print r.text
  85. #Using Rackspace Cloud Block Storage API 1.0 https://developer.rackspace.com/docs/cloud-block-storage/v1/
  86. def listCloudBlockStorage(token,tenantId):
  87. url = "https://"+REGION+".blockstorage.api.rackspacecloud.com/v1/"+tenantId+"/volumes"
  88. #Define Authentication request parameters and authentication token endpoint
  89. headers = {'Content-Type': 'application/json',
  90. 'Accept': 'application/json',
  91. 'X-Auth-Token': token}
  92. r = requests.get(url, headers=headers)
  93. print r.text
  94. #Using Rackspace Cloud DNS API 1.0 https://developer.rackspace.com/docs/cloud-dns/v1/
  95. def listCloudDNS(token,tenantId):
  96. url = "https://dns.api.rackspacecloud.com/v1.0/"+tenantId+"/domains"
  97. #Define Authentication request parameters and authentication token endpoint
  98. headers = {'Content-Type': 'application/json',
  99. 'Accept': 'application/json',
  100. 'X-Auth-Token': token}
  101. r = requests.get(url, headers=headers)
  102. print r.text
  103. def main():
  104. #Obtain authentication token
  105. token, tenantId = getAuthToken()
  106. #Make calls to view service catalog
  107. if token:
  108. print "Listing servers:"
  109. listCloudServers(token, tenantId)
  110. print "Listing networks"
  111. listCloudNetworks(token, tenantId)
  112. print "Listing Security groups:"
  113. listCloudSecurityGroups(token, tenantId)
  114. print "Listing backups:"
  115. listCloudBackups(token, tenantId)
  116. print "Listing Cloud Block Storage volumes:"
  117. listCloudBlockStorage(token, tenantId)
  118. print "Listing DNS:"
  119. listCloudDNS(token, tenantId)
  120. else:
  121. print "Could not obtain authentication token, please check API credentials"
  122. if __name__ == "__main__":
  123. main()