Find URI by Regionaccess = resp["access"]svc_cat = access["serviceCatalog"]compute_svc = svc_cat["compute"]eps = compute_svc["endpoints"]reg_ep = [ep for ep in eps if ep["region"] == region]base_uri = reg_ep["public_url"]list_uri = "%s/servers/detail" % base_uri
Create the Requestimport urllib2req = urllib2.Request(list_uri)
Add the Auth Headersreq.add_header("X-Auth-Token", token)req.add_header("X-Auth-Project-Id", tenant_id)
Add the HTTP Headersreq.add_header("Accept", "application/json")req.add_header("Content-Type", "application/json")req.add_header("User-Agent", "myapp/1.1")
Make the Requestraw_resp = urllib2.urlopen(req)resp = json.load(raw_resp)print resp
So for each request:• Retrieve authentication credentials from cache • Re-auth when no token, or when token expires• Determine endpoint for selected region from service catalog• Form URI from endpoint and API spec• Add auth header to request• Add "User-agent: xxx" header• Add "Content-Type: application/json" header• Add "Accept: application/json" header• Transmit request• Handle all errors• Retry if possible
...and then•Parse results•Headers•JSON body•Extract list of resource data•Return entity (or list of entities) with appropriate info•Sometimes just a value; e.g.: cloud_dns.domain.export() => string
Wouldnt it be betterto work with Python objects?
The Resource Class•Represents an individual cloud entities.•E.g.: a server; a load balancer•Has attributes that reflect all the informationabout that entity.•Has methods for interacting with that entity.
The Client Class•Handles all I/O•Calls from your app to pyrax•Calls from pyrax to the API server
The Manager Class•Configured with base URI for service•Configured with Resource type for service•Translates method calls to full URIs•Interprets responses •Creates Resource instances as necessary
Architecture list()Your Client JSON CloudApp objects GET URI API URI objects list() JSON method=GET determine Resource Manager method & URI parse JSON
Interacting with Resources•Many calls to the client require a resourcereference; e.g.: cloud_blockstorage.create_snapshot(volume)•Resources have a reference to their manager.•If you have an instance of that resource, you cancall it directly on the resource; e.g.: volume.create_snapshot()