{"action":"created","comment":{"url":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/comments/81613954","pull_request_review_id":2576782,"id":81613954,"diff_hunk":"@@ -1,4 +1,199 @@\n import random\n+import logging\n+import auth_data\n+import os\n \n+auth_mgr = None\n+def connect_auth_db():\n+    global auth_mgr\n+    if not auth_mgr:\n+        db_path = os.path.join(auth_data.get_auth_db_path())\n+        auth_mgr = auth_data.AuthorizationDataManager(db_path)\n+        auth_mgr.connect()\n+       \n+def find_tenant_by_vm(vm_uuid):\n+    \"\"\"\n+        Find tenant which owns this VM by querying the auth DB\n+    \"\"\"\n+    global auth_mgr\n+    cur = auth_mgr.conn.execute(\n+                \"select * from vms where vm_id=?\",\n+                (vm_uuid,)\n+    )\n+    tenant_uuid = None\n+    tenant_name = None\n+    result = cur.fetchone()\n+    if result:\n+        tenant_uuid = result[2]\n+        cur = auth_mgr.conn.execute(\n+                \"select * from tenants where id=?\",\n+                (tenant_uuid,)\n+        )\n+        result = cur.fetchone()\n+        if result:\n+            tenant_name = result[1]\n+            logging.debug(\"Found tenant_uuid %s, tenant_name\", tenant_uuid, tenant_name)\n+\n+    return tenant_uuid, tenant_name\n+\n+def find_privileges_by_tenant_and_datastore(tenant_uuid, datastore):\n+    \"\"\"\n+        Return privileges for given (tenant_uuid, datastore) pair by\n+        querying the auth DB \n+    \"\"\"\n+    global auth_mgr\n+    privileges = []\n+    logging.debug(\"find_privileges tenant_uuid=%s datastore=%s\", tenant_uuid, datastore)\n+    cur = auth_mgr.conn.execute(\n+                \"select * from privileges where tenant_id=? and datastore=?\",\n+                (tenant_uuid,datastore)    \n+    )\n+    privileges = cur.fetchone()\n+    return privileges\n+\n+def has_mount_privilege(privileges):\n+    logging.debug(privileges)\n+    if not privileges:\n+        return False\n+    logging.debug(\"mount_volume=%d\", privileges['mount_volume'])\n+    return privileges['mount_volume']\n+\n+def has_create_privilege(privileges):\n+    logging.debug(privileges)\n+    if not privileges:\n+        return False\n+    logging.debug(\"create_volume=%d\", privileges['create_volume'])\n+    return privileges['create_volume']   \n+\n+def has_delete_privilege(privileges):\n+    logging.debug(privileges)\n+    if not privileges:\n+        return False\n+    logging.debug(\"delete_volume=%d\", privileges['delete_volume'])\n+    return privileges['delete_volume']\n+\n+\"\"\"\n+    For a given size string, return values in MB.\n+    Example:\n+    '100MB': return 100\n+    '100GB': return 100*1024\n+\"\"\"\n+def convert_to_MB(vol_size_str):\n+    unit = vol_size_str[-2:]\n+    value = int(vol_size_str[:-2])\n+    convertions = {'MB' : 1,\n+                   'GB' : 1024,\n+                   'TB' : 1024*1024,\n+                   'PB' : 1024*1024*1024,\n+    }\n+    \n+    if unit.upper() in convertions.keys():\n+        value = value*convertions[unit]\n+    else:\n+        logging.error(\"Invalid volume size\")\n+    return value  \n+\n+def get_vol_size(opts):\n+    if not opts:\n+        logging.error(\"Volume size does not specified\")\n+    return opts['size']    \n+\n+\"\"\"\n+    Check wheter the size of the volume to be created exceeds\n+    the max volume size specified in the privileges\n+\"\"\"\n+def check_max_volume_size(opts, privileges):\n+    vol_size_in_MB = convert_to_MB(get_vol_size(opts))\n+    max_vol_size_in_MB = privileges['max_volume_size']\n+    return vol_size_in_MB <= max_vol_size_in_MB\n+\n+\"\"\"\n+    Return total storage used by (tenant_uuid, datastore)\n+    by querying auth DB    \n+\"\"\"\n+def get_total_storage_used(tenant_uuid, datastore):\n+    total_storage_used = 0\n+    cur = auth_mgr.conn.execute(\n+                \"select * from volumes where tenant_id=? and datastore=?\",\n+                (tenant_uuid,datastore)    \n+    )\n+    volumes = cur.fetchall()\n+    for volume in volumes:\n+        # volume[3] is the volume_size filed in the volume table\n+        total_storage_used = total_storage_used+volume[3]","path":"esx_service/utils/auth.py","position":123,"original_position":123,"commit_id":"fd1af55bdbdc2bc647c38d65fdb1dc429b958a1d","original_commit_id":"fd1af55bdbdc2bc647c38d65fdb1dc429b958a1d","user":{"login":"andrewjstone","id":466937,"avatar_url":"https://avatars.githubusercontent.com/u/466937?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewjstone","html_url":"https://github.com/andrewjstone","followers_url":"https://api.github.com/users/andrewjstone/followers","following_url":"https://api.github.com/users/andrewjstone/following{/other_user}","gists_url":"https://api.github.com/users/andrewjstone/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewjstone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewjstone/subscriptions","organizations_url":"https://api.github.com/users/andrewjstone/orgs","repos_url":"https://api.github.com/users/andrewjstone/repos","events_url":"https://api.github.com/users/andrewjstone/events{/privacy}","received_events_url":"https://api.github.com/users/andrewjstone/received_events","type":"User","site_admin":false},"body":"Use name instead of index for access.","created_at":"2016-10-03T18:45:38Z","updated_at":"2016-10-03T18:59:51Z","html_url":"https://github.com/vmware/docker-volume-vsphere/pull/603#discussion_r81613954","pull_request_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/603","_links":{"self":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/comments/81613954"},"html":{"href":"https://github.com/vmware/docker-volume-vsphere/pull/603#discussion_r81613954"},"pull_request":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/603"}}},"pull_request":{"url":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/603","id":85831044,"html_url":"https://github.com/vmware/docker-volume-vsphere/pull/603","diff_url":"https://github.com/vmware/docker-volume-vsphere/pull/603.diff","patch_url":"https://github.com/vmware/docker-volume-vsphere/pull/603.patch","issue_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues/603","number":603,"state":"open","locked":false,"title":"Auth.liping","user":{"login":"lipingxue","id":4754483,"avatar_url":"https://avatars.githubusercontent.com/u/4754483?v=3","gravatar_id":"","url":"https://api.github.com/users/lipingxue","html_url":"https://github.com/lipingxue","followers_url":"https://api.github.com/users/lipingxue/followers","following_url":"https://api.github.com/users/lipingxue/following{/other_user}","gists_url":"https://api.github.com/users/lipingxue/gists{/gist_id}","starred_url":"https://api.github.com/users/lipingxue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lipingxue/subscriptions","organizations_url":"https://api.github.com/users/lipingxue/orgs","repos_url":"https://api.github.com/users/lipingxue/repos","events_url":"https://api.github.com/users/lipingxue/events{/privacy}","received_events_url":"https://api.github.com/users/lipingxue/received_events","type":"User","site_admin":false},"body":"Implementation of \"DockerVolumeTenant\" interface of docker-volume-auth-vmodl.\r\nSome of the code are commented out for testing purpose.\r\nPlease review those code too. ","created_at":"2016-09-19T16:59:38Z","updated_at":"2016-10-03T18:59:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"026aa5f9b0d8d41f2792da458ff3dd313b269aa8","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/603/commits","review_comments_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/603/comments","review_comment_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/comments{/number}","comments_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues/603/comments","statuses_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/statuses/fd1af55bdbdc2bc647c38d65fdb1dc429b958a1d","head":{"label":"vmware:auth.liping","ref":"auth.liping","sha":"fd1af55bdbdc2bc647c38d65fdb1dc429b958a1d","user":{"login":"vmware","id":473334,"avatar_url":"https://avatars.githubusercontent.com/u/473334?v=3","gravatar_id":"","url":"https://api.github.com/users/vmware","html_url":"https://github.com/vmware","followers_url":"https://api.github.com/users/vmware/followers","following_url":"https://api.github.com/users/vmware/following{/other_user}","gists_url":"https://api.github.com/users/vmware/gists{/gist_id}","starred_url":"https://api.github.com/users/vmware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vmware/subscriptions","organizations_url":"https://api.github.com/users/vmware/orgs","repos_url":"https://api.github.com/users/vmware/repos","events_url":"https://api.github.com/users/vmware/events{/privacy}","received_events_url":"https://api.github.com/users/vmware/received_events","type":"Organization","site_admin":false},"repo":{"id":49599693,"name":"docker-volume-vsphere","full_name":"vmware/docker-volume-vsphere","owner":{"login":"vmware","id":473334,"avatar_url":"https://avatars.githubusercontent.com/u/473334?v=3","gravatar_id":"","url":"https://api.github.com/users/vmware","html_url":"https://github.com/vmware","followers_url":"https://api.github.com/users/vmware/followers","following_url":"https://api.github.com/users/vmware/following{/other_user}","gists_url":"https://api.github.com/users/vmware/gists{/gist_id}","starred_url":"https://api.github.com/users/vmware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vmware/subscriptions","organizations_url":"https://api.github.com/users/vmware/orgs","repos_url":"https://api.github.com/users/vmware/repos","events_url":"https://api.github.com/users/vmware/events{/privacy}","received_events_url":"https://api.github.com/users/vmware/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/vmware/docker-volume-vsphere","description":"Docker Volume Driver for vSphere.","fork":false,"url":"https://api.github.com/repos/vmware/docker-volume-vsphere","forks_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/forks","keys_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/teams","hooks_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/hooks","issue_events_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues/events{/number}","events_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/events","assignees_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/assignees{/user}","branches_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/branches{/branch}","tags_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/tags","blobs_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/refs{/sha}","trees_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/statuses/{sha}","languages_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/languages","stargazers_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/stargazers","contributors_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/contributors","subscribers_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/subscribers","subscription_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/subscription","commits_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/commits{/sha}","git_commits_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/commits{/sha}","comments_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/comments{/number}","issue_comment_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues/comments{/number}","contents_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/contents/{+path}","compare_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/merges","archive_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/downloads","issues_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues{/number}","pulls_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls{/number}","milestones_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/milestones{/number}","notifications_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/labels{/name}","releases_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/releases{/id}","deployments_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/deployments","created_at":"2016-01-13T20:25:36Z","updated_at":"2016-10-03T16:49:32Z","pushed_at":"2016-10-01T00:22:42Z","git_url":"git://github.com/vmware/docker-volume-vsphere.git","ssh_url":"git@github.com:vmware/docker-volume-vsphere.git","clone_url":"https://github.com/vmware/docker-volume-vsphere.git","svn_url":"https://github.com/vmware/docker-volume-vsphere","homepage":"https://vmware.github.io/docker-volume-vsphere","size":7862,"stargazers_count":73,"watchers_count":73,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":12,"mirror_url":null,"open_issues_count":76,"forks":12,"open_issues":76,"watchers":73,"default_branch":"master"}},"base":{"label":"vmware:master","ref":"master","sha":"8d42baa5369672e8277989eb76c937e359007ac2","user":{"login":"vmware","id":473334,"avatar_url":"https://avatars.githubusercontent.com/u/473334?v=3","gravatar_id":"","url":"https://api.github.com/users/vmware","html_url":"https://github.com/vmware","followers_url":"https://api.github.com/users/vmware/followers","following_url":"https://api.github.com/users/vmware/following{/other_user}","gists_url":"https://api.github.com/users/vmware/gists{/gist_id}","starred_url":"https://api.github.com/users/vmware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vmware/subscriptions","organizations_url":"https://api.github.com/users/vmware/orgs","repos_url":"https://api.github.com/users/vmware/repos","events_url":"https://api.github.com/users/vmware/events{/privacy}","received_events_url":"https://api.github.com/users/vmware/received_events","type":"Organization","site_admin":false},"repo":{"id":49599693,"name":"docker-volume-vsphere","full_name":"vmware/docker-volume-vsphere","owner":{"login":"vmware","id":473334,"avatar_url":"https://avatars.githubusercontent.com/u/473334?v=3","gravatar_id":"","url":"https://api.github.com/users/vmware","html_url":"https://github.com/vmware","followers_url":"https://api.github.com/users/vmware/followers","following_url":"https://api.github.com/users/vmware/following{/other_user}","gists_url":"https://api.github.com/users/vmware/gists{/gist_id}","starred_url":"https://api.github.com/users/vmware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vmware/subscriptions","organizations_url":"https://api.github.com/users/vmware/orgs","repos_url":"https://api.github.com/users/vmware/repos","events_url":"https://api.github.com/users/vmware/events{/privacy}","received_events_url":"https://api.github.com/users/vmware/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/vmware/docker-volume-vsphere","description":"Docker Volume Driver for vSphere.","fork":false,"url":"https://api.github.com/repos/vmware/docker-volume-vsphere","forks_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/forks","keys_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/teams","hooks_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/hooks","issue_events_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues/events{/number}","events_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/events","assignees_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/assignees{/user}","branches_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/branches{/branch}","tags_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/tags","blobs_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/refs{/sha}","trees_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/statuses/{sha}","languages_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/languages","stargazers_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/stargazers","contributors_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/contributors","subscribers_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/subscribers","subscription_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/subscription","commits_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/commits{/sha}","git_commits_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/git/commits{/sha}","comments_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/comments{/number}","issue_comment_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues/comments{/number}","contents_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/contents/{+path}","compare_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/merges","archive_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/downloads","issues_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues{/number}","pulls_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls{/number}","milestones_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/milestones{/number}","notifications_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/labels{/name}","releases_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/releases{/id}","deployments_url":"https://api.github.com/repos/vmware/docker-volume-vsphere/deployments","created_at":"2016-01-13T20:25:36Z","updated_at":"2016-10-03T16:49:32Z","pushed_at":"2016-10-01T00:22:42Z","git_url":"git://github.com/vmware/docker-volume-vsphere.git","ssh_url":"git@github.com:vmware/docker-volume-vsphere.git","clone_url":"https://github.com/vmware/docker-volume-vsphere.git","svn_url":"https://github.com/vmware/docker-volume-vsphere","homepage":"https://vmware.github.io/docker-volume-vsphere","size":7862,"stargazers_count":73,"watchers_count":73,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":12,"mirror_url":null,"open_issues_count":76,"forks":12,"open_issues":76,"watchers":73,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/603"},"html":{"href":"https://github.com/vmware/docker-volume-vsphere/pull/603"},"issue":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues/603"},"comments":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/issues/603/comments"},"review_comments":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/603/comments"},"review_comment":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/pulls/603/commits"},"statuses":{"href":"https://api.github.com/repos/vmware/docker-volume-vsphere/statuses/fd1af55bdbdc2bc647c38d65fdb1dc429b958a1d"}}}}
{"actor":{"display_login":"andrewjstone"}}