{"id":443,"date":"2016-02-12T10:21:05","date_gmt":"2016-02-12T10:21:05","guid":{"rendered":"http:\/\/www.haxed.me.uk\/?p=443"},"modified":"2016-02-12T10:42:06","modified_gmt":"2016-02-12T10:42:06","slug":"list-cloud-networks-using-rackspace-api","status":"publish","type":"post","link":"https:\/\/haxed.me.uk\/index.php\/2016\/02\/12\/list-cloud-networks-using-rackspace-api\/","title":{"rendered":"List Cloud Networks using Rackspace API"},"content":{"rendered":"<p>In the previous chapter we learnt how to add networks using the API. It&#8217;s really simple, its basically a network and label placeholder. But what about viewing the networks we have after we&#8217;ve made some? This is pretty simple to confirm.<\/p>\n<p>I have simplified the code a bit to make it easier to read.<\/p>\n<pre>\r\n#!\/bin\/sh\r\n\r\nUSERNAME='mycloudusername'\r\nAPIKEY='mycloudapikey'\r\nACCOUNT_NUMBER=10010101\r\nAPI_ENDPOINT=\"https:\/\/lon.networks.api.rackspacecloud.com\/v2.0\/$ACCOUNT_NUMBER\"\r\n\r\nTOKEN=`curl https:\/\/identity.api.rackspacecloud.com\/v2.0\/tokens -X POST -d '{ \"auth\":{\"RAX-KSKEY:apiKeyCredentials\": { \"username\":\"'$USERNAME'\", \"apiKey\": \"'$APIKEY'\" }} }' -H \"Content-type: application\/json\" |  python -mjson.tool | grep -A5 token | grep id | cut -d '\"' -f4`\r\n\r\ncurl -i -X GET https:\/\/lon.networks.api.rackspacecloud.com\/v2.0\/networks -H \"X-Auth-Token: $TOKEN\" \r\n<\/pre>\n<p>Output<\/p>\n<pre>\r\n# .\/list-networks.sh\r\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\n100  5143  100  5028  100   115   4472    102  0:00:01  0:00:01 --:--:--  4477\r\nHTTP\/1.1 200 OK\r\nDate: Fri, 12 Feb 2016 10:13:49 GMT\r\nVia: 1.1 Repose (Repose\/6.2.0.2)\r\nDate: Fri, 12 Feb 2016 10:13:49 GMT\r\nContent-Type: application\/json; charset=UTF-8\r\nContent-Length: 336\r\nServer: Jetty(9.2.z-SNAPSHOT)\r\n\r\n{\"networks\": [{\"status\": \"ACTIVE\", \"subnets\": [], \"name\": \"Isolatednet\", \"admin_state_up\": true, \"tenant_id\": \"10010101\", \"shared\": false, \"id\": \"ae36972f-5cba-4327-8bff-15d8b05dc3ee\"}], \"networks_links\": [{\"href\": \"http:\/\/localhost:9696\/v2.0\/networks?marker=ae36972f-5cba-4327-8bff-15d8b05dc3ee&page_reverse=True\", \"rel\": \"previous\"}]}\r\n<\/pre>\n<p>Pretty cool, but the format kind of sucks, I forgot to use python |-mjson.tool or jq to format the json output. Lets do that now by adding the line to the end of the curl -i line.<\/p>\n<p>Now the output is nice:<\/p>\n<pre>\r\n{\r\n    \"networks\": [\r\n        {\r\n            \"admin_state_up\": true,\r\n            \"id\": \"ae36972f-5cba-4327-8bff-15d8b05dc3ee\",\r\n            \"name\": \"Isolatednet\",\r\n            \"shared\": false,\r\n            \"status\": \"ACTIVE\",\r\n            \"subnets\": [],\r\n            \"tenant_id\": \"10010101\"\r\n        }\r\n    ],\r\n    \"networks_links\": [\r\n        {\r\n            \"href\": \"http:\/\/localhost:9696\/v2.0\/networks?marker=ae36972f-5cba-4327-8bff-15d8b05dc3ee&page_reverse=True\",\r\n            \"rel\": \"previous\"\r\n        }\r\n    ]\r\n}\r\n<\/pre>\n<p>The complete code will look like;<\/p>\n<pre>\r\n#!\/bin\/sh\r\n\r\nUSERNAME='mycloudusername'\r\nAPIKEY='mycloudapikey'\r\nACCOUNT_NUMBER=10010101\r\nAPI_ENDPOINT=\"https:\/\/lon.networks.api.rackspacecloud.com\/v2.0\/$ACCOUNT_NUMBER\"\r\n\r\nTOKEN=`curl https:\/\/identity.api.rackspacecloud.com\/v2.0\/tokens -X POST -d '{ \"auth\":{\"RAX-KSKEY:apiKeyCredentials\": { \"username\":\"'$USERNAME'\", \"apiKey\": \"'$APIKEY'\" }} }' -H \"Content-type: application\/json\" |  python -mjson.tool | grep -A5 token | grep id | cut -d '\"' -f4`\r\n\r\n# with header no formatting\r\n#curl -i -X GET https:\/\/lon.networks.api.rackspacecloud.com\/v2.0\/networks -H \"X-Auth-Token: $TOKEN\"\r\n# without header with formatting\r\ncurl -X GET https:\/\/lon.networks.api.rackspacecloud.com\/v2.0\/networks -H \"X-Auth-Token: $TOKEN\" | python -mjson.tool\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the previous chapter we learnt how to add networks using the API. It&#8217;s really simple, its basically a network and label placeholder. But what about viewing the networks we have after we&#8217;ve made some? This is pretty simple to &hellip; <a href=\"https:\/\/haxed.me.uk\/index.php\/2016\/02\/12\/list-cloud-networks-using-rackspace-api\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,19,7,4,12],"tags":[],"class_list":["post-443","post","type-post","status-publish","format-standard","hentry","category-api","category-bash","category-management-tools","category-networking","category-openstack-api"],"_links":{"self":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":2,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"predecessor-version":[{"id":445,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/443\/revisions\/445"}],"wp:attachment":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}