{"action":"created","comment":{"url":"https://api.github.com/repos/CCI-MOC/haas/pulls/comments/62146754","id":62146754,"diff_hunk":"@@ -0,0 +1,801 @@\n+# Copyright 2013-2015 Massachusetts Open Cloud Contributors\n+#\n+# Licensed under the Apache License, Version 2.0 (the \"License\");\n+# you may not use this file except in compliance with the\n+# License.  You may obtain a copy of the License at\n+#\n+#     http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing,\n+# software distributed under the License is distributed on an \"AS\n+# IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either\n+# express or implied.  See the License for the specific language\n+# governing permissions and limitations under the License.\n+\n+\"\"\"This module implements the HaaS client library.\"\"\"\n+from haas import config\n+from haas.config import cfg\n+from error_clientlib import *\n+\n+from haas.rest import APIError, ServerError\n+    # Do we need ServerError ? It is not supposed to know \n+    # what happens on the server side\n+import requests\n+import os\n+import urllib\n+import json\n+\n+\n+#Return Codes:\n+\"\"\" Following is how return codes should be interpreted:\n+    -1  :       Operation Failed\n+    1   :       Operation was successful\n+    2   :       Duplication error. Entity already exists\n+    3   :\n+\n+\"\"\"\n+\n+\n+\n+class Haas:\n+    \"\"\"Client class for making Haas API calls.\n+    Note that this client library is not yet complete and will be added to as\n+    needed.\n+\n+    Example:\n+        h = Haas(endpoint=\"http://127.0.0.1:5000\")\n+        h.node_connect_network(\"node-2000\", \"eth1\", \"production-network\",\n+        \"vlan/native\")\n+\n+    Errors are thrown when receiving HTTP status_codes from the HaaS server\n+    that are not in the [200,300) range\n+    \"\"\"\n+\n+    def __init__(self, endpoint=None):\n+        \"\"\"Initiatlize an instance.\n+\n+        If endpoint is None, use the endpoint specification from:\n+            1) The HAAS_ENDPOINT env variable or\n+            2) Take the endpoint from [client] endpoint\n+\n+        Exceptions:\n+            LookupError - no endpoint could be found\n+        \"\"\"\n+        config.load(filename='haas.cfg')\n+        if endpoint != None:\n+            self.endpoint = endpoint\n+        else:\n+            self.endpoint = os.environ.get('HAAS_ENDPOINT')\n+            if self.endpoint is None:\n+                try:\n+                    self.endpoint = cfg.get('client', 'endpoint')\n+                except LookupError:\n+                    print(\"no endpoint found\")\n+\n+    def object_url(self, *args):\n+        \"\"\"Append the arguments to the endpoint URL\"\"\"\n+        url = self.endpoint\n+        for arg in args:\n+            url += '/' + urllib.quote(arg,'')\n+        return url\n+\n+    def user_create(self, username, password):\n+        \"\"\"Create a user <username> with password <password>.\"\"\"\n+        url = self.object_url('user', username)\n+        data = {'password': password}\n+        r = requests.put(url, data=json.dumps(data))\n+\n+        if (200 <= r.status_code < 300):\n+            return                    #Operation successful\n+        elif (r.status_code == 409):\n+            raise DuplicateName(\"FAILURE: Name already exists\" )    #Username already exists\n+        elif(500 <= r.status_code):\n+            raise ServersideError(\"FAILURE: Server failed to process the request \")\n+        else:\n+            raise UnknownError(\"FAILURE: Contact the HaaS Admin. Unknown error occured !!! \")      #Operation failed\n+\n+    def network_create(self, network, creator, access, net_id):\n+        \"\"\"Create a link-layer <network>. See docs/networks.md for details.\"\"\"\n+        url = self.object_url('network', network)\n+        data = {'creator': creator, 'access': access, 'net_id': net_id}\n+        r = requests.put(url, data=json.dumps(data))\n+\n+        if (200 <= r.status_code < 300):","path":"haas/client.py","position":103,"original_position":103,"commit_id":"3df4e8b37d8572d760ae4892dd0139042e7b663c","original_commit_id":"3df4e8b37d8572d760ae4892dd0139042e7b663c","user":{"login":"henn","id":1813729,"avatar_url":"https://avatars.githubusercontent.com/u/1813729?v=3","gravatar_id":"","url":"https://api.github.com/users/henn","html_url":"https://github.com/henn","followers_url":"https://api.github.com/users/henn/followers","following_url":"https://api.github.com/users/henn/following{/other_user}","gists_url":"https://api.github.com/users/henn/gists{/gist_id}","starred_url":"https://api.github.com/users/henn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/henn/subscriptions","organizations_url":"https://api.github.com/users/henn/orgs","repos_url":"https://api.github.com/users/henn/repos","events_url":"https://api.github.com/users/henn/events{/privacy}","received_events_url":"https://api.github.com/users/henn/received_events","type":"User","site_admin":false},"body":"Same comment here as in the CLI: it might make sense to check the status code in the dispatcher function once for all of the client calls, then throw exceptions as necessary from there.","created_at":"2016-05-05T04:03:17Z","updated_at":"2016-05-05T04:03:18Z","html_url":"https://github.com/CCI-MOC/haas/pull/498#discussion_r62146754","pull_request_url":"https://api.github.com/repos/CCI-MOC/haas/pulls/498","_links":{"self":{"href":"https://api.github.com/repos/CCI-MOC/haas/pulls/comments/62146754"},"html":{"href":"https://github.com/CCI-MOC/haas/pull/498#discussion_r62146754"},"pull_request":{"href":"https://api.github.com/repos/CCI-MOC/haas/pulls/498"}}},"pull_request":{"url":"https://api.github.com/repos/CCI-MOC/haas/pulls/498","id":46716278,"html_url":"https://github.com/CCI-MOC/haas/pull/498","diff_url":"https://github.com/CCI-MOC/haas/pull/498.diff","patch_url":"https://github.com/CCI-MOC/haas/pull/498.patch","issue_url":"https://api.github.com/repos/CCI-MOC/haas/issues/498","number":498,"state":"open","locked":false,"title":"Introduce python client library for HaaS","user":{"login":"SahilTikale","id":2398718,"avatar_url":"https://avatars.githubusercontent.com/u/2398718?v=3","gravatar_id":"","url":"https://api.github.com/users/SahilTikale","html_url":"https://github.com/SahilTikale","followers_url":"https://api.github.com/users/SahilTikale/followers","following_url":"https://api.github.com/users/SahilTikale/following{/other_user}","gists_url":"https://api.github.com/users/SahilTikale/gists{/gist_id}","starred_url":"https://api.github.com/users/SahilTikale/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SahilTikale/subscriptions","organizations_url":"https://api.github.com/users/SahilTikale/orgs","repos_url":"https://api.github.com/users/SahilTikale/repos","events_url":"https://api.github.com/users/SahilTikale/events{/privacy}","received_events_url":"https://api.github.com/users/SahilTikale/received_events","type":"User","site_admin":false},"body":"Hi,\r\nThis is my first version of the client library for HaaS. Attempt is to make a unified library that can become the basis client programs both cli and gui based. \r\n\r\nI have kept the exceptions and interfaces to the minimum will add and improve based on feedback.\r\nAdded meaningful messages for cli calls and formated the output sent by HaaS to the client. \r\n\r\nOnce this base code is approved I will add functionality for the api calls that were re-factored during the summer. ","created_at":"2015-10-04T00:22:21Z","updated_at":"2016-05-05T04:03:18Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b481477306317bef831edca21b7c544c8caa9dc2","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/CCI-MOC/haas/pulls/498/commits","review_comments_url":"https://api.github.com/repos/CCI-MOC/haas/pulls/498/comments","review_comment_url":"https://api.github.com/repos/CCI-MOC/haas/pulls/comments{/number}","comments_url":"https://api.github.com/repos/CCI-MOC/haas/issues/498/comments","statuses_url":"https://api.github.com/repos/CCI-MOC/haas/statuses/3df4e8b37d8572d760ae4892dd0139042e7b663c","head":{"label":"SahilTikale:sahilClientLib","ref":"sahilClientLib","sha":"3df4e8b37d8572d760ae4892dd0139042e7b663c","user":{"login":"SahilTikale","id":2398718,"avatar_url":"https://avatars.githubusercontent.com/u/2398718?v=3","gravatar_id":"","url":"https://api.github.com/users/SahilTikale","html_url":"https://github.com/SahilTikale","followers_url":"https://api.github.com/users/SahilTikale/followers","following_url":"https://api.github.com/users/SahilTikale/following{/other_user}","gists_url":"https://api.github.com/users/SahilTikale/gists{/gist_id}","starred_url":"https://api.github.com/users/SahilTikale/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SahilTikale/subscriptions","organizations_url":"https://api.github.com/users/SahilTikale/orgs","repos_url":"https://api.github.com/users/SahilTikale/repos","events_url":"https://api.github.com/users/SahilTikale/events{/privacy}","received_events_url":"https://api.github.com/users/SahilTikale/received_events","type":"User","site_admin":false},"repo":{"id":35552937,"name":"haas","full_name":"SahilTikale/haas","owner":{"login":"SahilTikale","id":2398718,"avatar_url":"https://avatars.githubusercontent.com/u/2398718?v=3","gravatar_id":"","url":"https://api.github.com/users/SahilTikale","html_url":"https://github.com/SahilTikale","followers_url":"https://api.github.com/users/SahilTikale/followers","following_url":"https://api.github.com/users/SahilTikale/following{/other_user}","gists_url":"https://api.github.com/users/SahilTikale/gists{/gist_id}","starred_url":"https://api.github.com/users/SahilTikale/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SahilTikale/subscriptions","organizations_url":"https://api.github.com/users/SahilTikale/orgs","repos_url":"https://api.github.com/users/SahilTikale/repos","events_url":"https://api.github.com/users/SahilTikale/events{/privacy}","received_events_url":"https://api.github.com/users/SahilTikale/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/SahilTikale/haas","description":"","fork":true,"url":"https://api.github.com/repos/SahilTikale/haas","forks_url":"https://api.github.com/repos/SahilTikale/haas/forks","keys_url":"https://api.github.com/repos/SahilTikale/haas/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SahilTikale/haas/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SahilTikale/haas/teams","hooks_url":"https://api.github.com/repos/SahilTikale/haas/hooks","issue_events_url":"https://api.github.com/repos/SahilTikale/haas/issues/events{/number}","events_url":"https://api.github.com/repos/SahilTikale/haas/events","assignees_url":"https://api.github.com/repos/SahilTikale/haas/assignees{/user}","branches_url":"https://api.github.com/repos/SahilTikale/haas/branches{/branch}","tags_url":"https://api.github.com/repos/SahilTikale/haas/tags","blobs_url":"https://api.github.com/repos/SahilTikale/haas/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SahilTikale/haas/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SahilTikale/haas/git/refs{/sha}","trees_url":"https://api.github.com/repos/SahilTikale/haas/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SahilTikale/haas/statuses/{sha}","languages_url":"https://api.github.com/repos/SahilTikale/haas/languages","stargazers_url":"https://api.github.com/repos/SahilTikale/haas/stargazers","contributors_url":"https://api.github.com/repos/SahilTikale/haas/contributors","subscribers_url":"https://api.github.com/repos/SahilTikale/haas/subscribers","subscription_url":"https://api.github.com/repos/SahilTikale/haas/subscription","commits_url":"https://api.github.com/repos/SahilTikale/haas/commits{/sha}","git_commits_url":"https://api.github.com/repos/SahilTikale/haas/git/commits{/sha}","comments_url":"https://api.github.com/repos/SahilTikale/haas/comments{/number}","issue_comment_url":"https://api.github.com/repos/SahilTikale/haas/issues/comments{/number}","contents_url":"https://api.github.com/repos/SahilTikale/haas/contents/{+path}","compare_url":"https://api.github.com/repos/SahilTikale/haas/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SahilTikale/haas/merges","archive_url":"https://api.github.com/repos/SahilTikale/haas/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SahilTikale/haas/downloads","issues_url":"https://api.github.com/repos/SahilTikale/haas/issues{/number}","pulls_url":"https://api.github.com/repos/SahilTikale/haas/pulls{/number}","milestones_url":"https://api.github.com/repos/SahilTikale/haas/milestones{/number}","notifications_url":"https://api.github.com/repos/SahilTikale/haas/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SahilTikale/haas/labels{/name}","releases_url":"https://api.github.com/repos/SahilTikale/haas/releases{/id}","deployments_url":"https://api.github.com/repos/SahilTikale/haas/deployments","created_at":"2015-05-13T14:11:46Z","updated_at":"2016-02-01T04:21:37Z","pushed_at":"2016-04-19T14:50:07Z","git_url":"git://github.com/SahilTikale/haas.git","ssh_url":"git@github.com:SahilTikale/haas.git","clone_url":"https://github.com/SahilTikale/haas.git","svn_url":"https://github.com/SahilTikale/haas","homepage":null,"size":2448,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"CCI-MOC:master","ref":"master","sha":"f9e15cb596d6e008aca0ea681be4957b2af98edf","user":{"login":"CCI-MOC","id":3578683,"avatar_url":"https://avatars.githubusercontent.com/u/3578683?v=3","gravatar_id":"","url":"https://api.github.com/users/CCI-MOC","html_url":"https://github.com/CCI-MOC","followers_url":"https://api.github.com/users/CCI-MOC/followers","following_url":"https://api.github.com/users/CCI-MOC/following{/other_user}","gists_url":"https://api.github.com/users/CCI-MOC/gists{/gist_id}","starred_url":"https://api.github.com/users/CCI-MOC/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CCI-MOC/subscriptions","organizations_url":"https://api.github.com/users/CCI-MOC/orgs","repos_url":"https://api.github.com/users/CCI-MOC/repos","events_url":"https://api.github.com/users/CCI-MOC/events{/privacy}","received_events_url":"https://api.github.com/users/CCI-MOC/received_events","type":"Organization","site_admin":false},"repo":{"id":16487339,"name":"haas","full_name":"CCI-MOC/haas","owner":{"login":"CCI-MOC","id":3578683,"avatar_url":"https://avatars.githubusercontent.com/u/3578683?v=3","gravatar_id":"","url":"https://api.github.com/users/CCI-MOC","html_url":"https://github.com/CCI-MOC","followers_url":"https://api.github.com/users/CCI-MOC/followers","following_url":"https://api.github.com/users/CCI-MOC/following{/other_user}","gists_url":"https://api.github.com/users/CCI-MOC/gists{/gist_id}","starred_url":"https://api.github.com/users/CCI-MOC/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CCI-MOC/subscriptions","organizations_url":"https://api.github.com/users/CCI-MOC/orgs","repos_url":"https://api.github.com/users/CCI-MOC/repos","events_url":"https://api.github.com/users/CCI-MOC/events{/privacy}","received_events_url":"https://api.github.com/users/CCI-MOC/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CCI-MOC/haas","description":"Hardware as a Service","fork":false,"url":"https://api.github.com/repos/CCI-MOC/haas","forks_url":"https://api.github.com/repos/CCI-MOC/haas/forks","keys_url":"https://api.github.com/repos/CCI-MOC/haas/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CCI-MOC/haas/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CCI-MOC/haas/teams","hooks_url":"https://api.github.com/repos/CCI-MOC/haas/hooks","issue_events_url":"https://api.github.com/repos/CCI-MOC/haas/issues/events{/number}","events_url":"https://api.github.com/repos/CCI-MOC/haas/events","assignees_url":"https://api.github.com/repos/CCI-MOC/haas/assignees{/user}","branches_url":"https://api.github.com/repos/CCI-MOC/haas/branches{/branch}","tags_url":"https://api.github.com/repos/CCI-MOC/haas/tags","blobs_url":"https://api.github.com/repos/CCI-MOC/haas/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CCI-MOC/haas/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CCI-MOC/haas/git/refs{/sha}","trees_url":"https://api.github.com/repos/CCI-MOC/haas/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CCI-MOC/haas/statuses/{sha}","languages_url":"https://api.github.com/repos/CCI-MOC/haas/languages","stargazers_url":"https://api.github.com/repos/CCI-MOC/haas/stargazers","contributors_url":"https://api.github.com/repos/CCI-MOC/haas/contributors","subscribers_url":"https://api.github.com/repos/CCI-MOC/haas/subscribers","subscription_url":"https://api.github.com/repos/CCI-MOC/haas/subscription","commits_url":"https://api.github.com/repos/CCI-MOC/haas/commits{/sha}","git_commits_url":"https://api.github.com/repos/CCI-MOC/haas/git/commits{/sha}","comments_url":"https://api.github.com/repos/CCI-MOC/haas/comments{/number}","issue_comment_url":"https://api.github.com/repos/CCI-MOC/haas/issues/comments{/number}","contents_url":"https://api.github.com/repos/CCI-MOC/haas/contents/{+path}","compare_url":"https://api.github.com/repos/CCI-MOC/haas/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CCI-MOC/haas/merges","archive_url":"https://api.github.com/repos/CCI-MOC/haas/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CCI-MOC/haas/downloads","issues_url":"https://api.github.com/repos/CCI-MOC/haas/issues{/number}","pulls_url":"https://api.github.com/repos/CCI-MOC/haas/pulls{/number}","milestones_url":"https://api.github.com/repos/CCI-MOC/haas/milestones{/number}","notifications_url":"https://api.github.com/repos/CCI-MOC/haas/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CCI-MOC/haas/labels{/name}","releases_url":"https://api.github.com/repos/CCI-MOC/haas/releases{/id}","deployments_url":"https://api.github.com/repos/CCI-MOC/haas/deployments","created_at":"2014-02-03T17:48:01Z","updated_at":"2016-04-07T15:30:55Z","pushed_at":"2016-05-05T03:08:51Z","git_url":"git://github.com/CCI-MOC/haas.git","ssh_url":"git@github.com:CCI-MOC/haas.git","clone_url":"https://github.com/CCI-MOC/haas.git","svn_url":"https://github.com/CCI-MOC/haas","homepage":"","size":2387,"stargazers_count":8,"watchers_count":8,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":28,"mirror_url":null,"open_issues_count":131,"forks":28,"open_issues":131,"watchers":8,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/CCI-MOC/haas/pulls/498"},"html":{"href":"https://github.com/CCI-MOC/haas/pull/498"},"issue":{"href":"https://api.github.com/repos/CCI-MOC/haas/issues/498"},"comments":{"href":"https://api.github.com/repos/CCI-MOC/haas/issues/498/comments"},"review_comments":{"href":"https://api.github.com/repos/CCI-MOC/haas/pulls/498/comments"},"review_comment":{"href":"https://api.github.com/repos/CCI-MOC/haas/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/CCI-MOC/haas/pulls/498/commits"},"statuses":{"href":"https://api.github.com/repos/CCI-MOC/haas/statuses/3df4e8b37d8572d760ae4892dd0139042e7b663c"}}}}