{"action":"created","comment":{"url":"https://api.github.com/repos/romana/core/pulls/comments/60304498","id":60304498,"diff_hunk":"@@ -0,0 +1,206 @@\n+// Copyright (c) 2016 Pani Networks\n+// All Rights Reserved.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n+// not use this file except in compliance with the License. You may obtain\n+// 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, software\n+// distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+// License for the specific language governing permissions and limitations\n+// under the License.\n+\n+package cmd\n+\n+import (\n+\t\"errors\"\n+\t\"fmt\"\n+\t\"io/ioutil\"\n+\t\"net/http\"\n+\t\"os\"\n+\n+\t\"github.com/romana/core/romana/util\"\n+\n+\tcli \"github.com/spf13/cobra\"\n+\tconfig \"github.com/spf13/viper\"\n+)\n+\n+// policyCmd represents the policy commands\n+var policyCmd = &cli.Command{\n+\tUse:   \"policy [add|remove|list]\",\n+\tShort: \"Add, Remove or List a policy.\",\n+\tLong: `Add, Remove or List a policy.\n+\n+For more information, please check http://romana.io\n+`,\n+}\n+\n+func init() {\n+\tpolicyCmd.AddCommand(policyAddCmd)\n+\tpolicyCmd.AddCommand(policyRemoveCmd)\n+\tpolicyCmd.AddCommand(policyListCmd)\n+}\n+\n+var policyAddCmd = &cli.Command{\n+\tUse:          \"add [tenantName][policyFile]\",\n+\tShort:        \"Add a new policy.\",\n+\tLong:         `Add a new policy.`,\n+\tRunE:         policyAdd,\n+\tSilenceUsage: true,\n+}\n+\n+var policyRemoveCmd = &cli.Command{\n+\tUse:          \"remove [tenantName][policyName]\",\n+\tShort:        \"Remove a specific policy.\",\n+\tLong:         `Remove a specific policy.`,\n+\tRunE:         policyRemove,\n+\tSilenceUsage: true,\n+}\n+\n+var policyListCmd = &cli.Command{\n+\tUse:          \"list [tenantName]\",\n+\tShort:        \"List policy for a specific tenant.\",\n+\tLong:         `List policy for a specific tenant.`,\n+\tRunE:         policyList,\n+\tSilenceUsage: true,\n+}\n+\n+// policyAdd adds kubernetes policy for a specific tenant\n+// using the policyFile provided.\n+func policyAdd(cmd *cli.Command, args []string) error {\n+\tif len(args) != 2 {\n+\t\treturn util.UsageError(cmd, \"TENANT and POLICY FILE name should be provided.\")\n+\t}\n+\n+\ttenantName := args[0]\n+\tpolicyFile := args[1]\n+\t// Tenant check once adaptor add supports for it.\n+\t/*\n+\t\tif !adaptor.TenantExists(tnt) {\n+\t\t\treturn errors.New(\"Tenant doesn't exists: \" + tnt)\n+\t\t}\n+\t*/\n+\n+\tf, err := os.Open(policyFile)\n+\tif err != nil {\n+\t\treturn errors.New(\"Couldn't open Policy file: \" + policyFile)\n+\t}\n+\tdefer f.Close()\n+\n+\tbaseURL := config.GetString(\"BaseURL\")\n+\tkubeURL := baseURL + fmt.Sprintf(\":8080/apis/romana.io/demo/v1\")\n+\tkubeURL = kubeURL + fmt.Sprintf(\"/namespaces/%s/networkpolicys\", tenantName)\n+\n+\treq, err := http.NewRequest(\"POST\", kubeURL, f)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\treq.Header.Set(\"Content-Type\", \"application/json\")\n+\n+\tresp, err := http.DefaultClient.Do(req)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tdefer resp.Body.Close()\n+\n+\tif resp.StatusCode == 201 {\n+\t\tif config.GetString(\"Format\") == \"json\" {\n+\t\t\tbody, err := ioutil.ReadAll(resp.Body)\n+\t\t\tif err != nil {\n+\t\t\t\treturn err\n+\t\t\t}\n+\t\t\tfmt.Printf(util.JSONIndent(string(body)))\n+\t\t} else {\n+\t\t\tfmt.Printf(\"Policy (%s) for Tenant (%s) successfully created.\\n\", policyFile, tenantName)\n+\t\t}\n+\t\treturn nil\n+\t} else {\n+\t\tif config.GetString(\"Format\") == \"json\" {\n+\t\t\tbody, err := ioutil.ReadAll(resp.Body)\n+\t\t\tif err != nil {\n+\t\t\t\treturn err\n+\t\t\t}\n+\t\t\tfmt.Printf(util.JSONIndent(string(body)))\n+\t\t\treturn nil\n+\t\t} else {\n+\t\t\treturn fmt.Errorf(\"Error creating Policy (%s) for Tenant (%s).\", policyFile, tenantName)\n+\t\t}\n+\t}\n+}\n+\n+// policyRemove removes kubernetes policy for a specific tenant\n+// using the policyName provided.\n+func policyRemove(cmd *cli.Command, args []string) error {\n+\tif len(args) < 2 {\n+\t\treturn util.UsageError(cmd, \"TENANT and POLICY name should be provided.\")\n+\t}\n+\n+\ttenantName := args[0]\n+\tpolicyNames := args[1:]\n+\t// Tenant check once adaptor add supports for it.\n+\t/*\n+\t\tif !adaptor.TenantExists(tnt) {\n+\t\t\treturn errors.New(\"Tenant doesn't exists: \" + tnt)\n+\t\t}\n+\t*/\n+\n+\tvar errs []error\n+\tbaseURL := config.GetString(\"BaseURL\")\n+\tkubeURL := baseURL + fmt.Sprintf(\":8080/apis/romana.io/demo/v1\")\n+\tfor _, policyName := range policyNames {\n+\t\tk := kubeURL + fmt.Sprintf(\"/namespaces/%s/networkpolicys/%s/\", tenantName, policyName)\n+\n+\t\treq, err := http.NewRequest(\"DELETE\", k, nil)\n+\t\tif err != nil {\n+\t\t\treturn err\n+\t\t}\n+\n+\t\tresp, err := http.DefaultClient.Do(req)\n+\t\tif err != nil {\n+\t\t\treturn err\n+\t\t}\n+\t\tdefer resp.Body.Close()\n+\n+\t\tif resp.StatusCode == 200 {\n+\t\t\tif config.GetString(\"Format\") == \"json\" {\n+\t\t\t\tbody, err := ioutil.ReadAll(resp.Body)\n+\t\t\t\tif err != nil {\n+\t\t\t\t\treturn err\n+\t\t\t\t}\n+\t\t\t\tfmt.Printf(util.JSONIndent(string(body)))\n+\t\t\t} else {\n+\t\t\t\tfmt.Printf(\"Policy (%s) for Tenant (%s) successfully deleted.\\n\", policyName, tenantName)\n+\t\t\t}\n+\t\t} else {\n+\t\t\tif config.GetString(\"Format\") == \"json\" {\n+\t\t\t\tbody, err := ioutil.ReadAll(resp.Body)\n+\t\t\t\tif err != nil {\n+\t\t\t\t\treturn err\n+\t\t\t\t}\n+\t\t\t\tfmt.Printf(util.JSONIndent(string(body)))","path":"romana/cmd/policy.go","position":173,"original_position":183,"commit_id":"642dc3737c92d66146062f02281e8f539caf8df7","original_commit_id":"642dc3737c92d66146062f02281e8f539caf8df7","user":{"login":"jbrendel","id":233741,"avatar_url":"https://avatars.githubusercontent.com/u/233741?v=3","gravatar_id":"","url":"https://api.github.com/users/jbrendel","html_url":"https://github.com/jbrendel","followers_url":"https://api.github.com/users/jbrendel/followers","following_url":"https://api.github.com/users/jbrendel/following{/other_user}","gists_url":"https://api.github.com/users/jbrendel/gists{/gist_id}","starred_url":"https://api.github.com/users/jbrendel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbrendel/subscriptions","organizations_url":"https://api.github.com/users/jbrendel/orgs","repos_url":"https://api.github.com/users/jbrendel/repos","events_url":"https://api.github.com/users/jbrendel/events{/privacy}","received_events_url":"https://api.github.com/users/jbrendel/received_events","type":"User","site_admin":false},"body":"Oh, I see! :-) Good!","created_at":"2016-04-19T20:25:31Z","updated_at":"2016-04-19T20:25:31Z","html_url":"https://github.com/romana/core/pull/70#discussion_r60304498","pull_request_url":"https://api.github.com/repos/romana/core/pulls/70","_links":{"self":{"href":"https://api.github.com/repos/romana/core/pulls/comments/60304498"},"html":{"href":"https://github.com/romana/core/pull/70#discussion_r60304498"},"pull_request":{"href":"https://api.github.com/repos/romana/core/pulls/70"}}},"pull_request":{"url":"https://api.github.com/repos/romana/core/pulls/70","id":66943403,"html_url":"https://github.com/romana/core/pull/70","diff_url":"https://github.com/romana/core/pull/70.diff","patch_url":"https://github.com/romana/core/pull/70.patch","issue_url":"https://api.github.com/repos/romana/core/issues/70","number":70,"state":"open","locked":false,"title":"Command line support for kubernetes policies.","user":{"login":"pritesh","id":9474171,"avatar_url":"https://avatars.githubusercontent.com/u/9474171?v=3","gravatar_id":"","url":"https://api.github.com/users/pritesh","html_url":"https://github.com/pritesh","followers_url":"https://api.github.com/users/pritesh/followers","following_url":"https://api.github.com/users/pritesh/following{/other_user}","gists_url":"https://api.github.com/users/pritesh/gists{/gist_id}","starred_url":"https://api.github.com/users/pritesh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pritesh/subscriptions","organizations_url":"https://api.github.com/users/pritesh/orgs","repos_url":"https://api.github.com/users/pritesh/repos","events_url":"https://api.github.com/users/pritesh/events{/privacy}","received_events_url":"https://api.github.com/users/pritesh/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-04-19T00:25:45Z","updated_at":"2016-04-19T20:25:31Z","closed_at":null,"merged_at":null,"merge_commit_sha":"15173e357a826660f24f1dbd91e33c1966557870","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/romana/core/pulls/70/commits","review_comments_url":"https://api.github.com/repos/romana/core/pulls/70/comments","review_comment_url":"https://api.github.com/repos/romana/core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/romana/core/issues/70/comments","statuses_url":"https://api.github.com/repos/romana/core/statuses/642dc3737c92d66146062f02281e8f539caf8df7","head":{"label":"romana:feature/cmd-kube-pol","ref":"feature/cmd-kube-pol","sha":"642dc3737c92d66146062f02281e8f539caf8df7","user":{"login":"romana","id":9849297,"avatar_url":"https://avatars.githubusercontent.com/u/9849297?v=3","gravatar_id":"","url":"https://api.github.com/users/romana","html_url":"https://github.com/romana","followers_url":"https://api.github.com/users/romana/followers","following_url":"https://api.github.com/users/romana/following{/other_user}","gists_url":"https://api.github.com/users/romana/gists{/gist_id}","starred_url":"https://api.github.com/users/romana/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/romana/subscriptions","organizations_url":"https://api.github.com/users/romana/orgs","repos_url":"https://api.github.com/users/romana/repos","events_url":"https://api.github.com/users/romana/events{/privacy}","received_events_url":"https://api.github.com/users/romana/received_events","type":"Organization","site_admin":false},"repo":{"id":43855674,"name":"core","full_name":"romana/core","owner":{"login":"romana","id":9849297,"avatar_url":"https://avatars.githubusercontent.com/u/9849297?v=3","gravatar_id":"","url":"https://api.github.com/users/romana","html_url":"https://github.com/romana","followers_url":"https://api.github.com/users/romana/followers","following_url":"https://api.github.com/users/romana/following{/other_user}","gists_url":"https://api.github.com/users/romana/gists{/gist_id}","starred_url":"https://api.github.com/users/romana/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/romana/subscriptions","organizations_url":"https://api.github.com/users/romana/orgs","repos_url":"https://api.github.com/users/romana/repos","events_url":"https://api.github.com/users/romana/events{/privacy}","received_events_url":"https://api.github.com/users/romana/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/romana/core","description":"Romana core components - Micro services written in Go.","fork":false,"url":"https://api.github.com/repos/romana/core","forks_url":"https://api.github.com/repos/romana/core/forks","keys_url":"https://api.github.com/repos/romana/core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/romana/core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/romana/core/teams","hooks_url":"https://api.github.com/repos/romana/core/hooks","issue_events_url":"https://api.github.com/repos/romana/core/issues/events{/number}","events_url":"https://api.github.com/repos/romana/core/events","assignees_url":"https://api.github.com/repos/romana/core/assignees{/user}","branches_url":"https://api.github.com/repos/romana/core/branches{/branch}","tags_url":"https://api.github.com/repos/romana/core/tags","blobs_url":"https://api.github.com/repos/romana/core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/romana/core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/romana/core/git/refs{/sha}","trees_url":"https://api.github.com/repos/romana/core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/romana/core/statuses/{sha}","languages_url":"https://api.github.com/repos/romana/core/languages","stargazers_url":"https://api.github.com/repos/romana/core/stargazers","contributors_url":"https://api.github.com/repos/romana/core/contributors","subscribers_url":"https://api.github.com/repos/romana/core/subscribers","subscription_url":"https://api.github.com/repos/romana/core/subscription","commits_url":"https://api.github.com/repos/romana/core/commits{/sha}","git_commits_url":"https://api.github.com/repos/romana/core/git/commits{/sha}","comments_url":"https://api.github.com/repos/romana/core/comments{/number}","issue_comment_url":"https://api.github.com/repos/romana/core/issues/comments{/number}","contents_url":"https://api.github.com/repos/romana/core/contents/{+path}","compare_url":"https://api.github.com/repos/romana/core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/romana/core/merges","archive_url":"https://api.github.com/repos/romana/core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/romana/core/downloads","issues_url":"https://api.github.com/repos/romana/core/issues{/number}","pulls_url":"https://api.github.com/repos/romana/core/pulls{/number}","milestones_url":"https://api.github.com/repos/romana/core/milestones{/number}","notifications_url":"https://api.github.com/repos/romana/core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/romana/core/labels{/name}","releases_url":"https://api.github.com/repos/romana/core/releases{/id}","deployments_url":"https://api.github.com/repos/romana/core/deployments","created_at":"2015-10-08T01:45:35Z","updated_at":"2016-04-13T22:10:15Z","pushed_at":"2016-04-19T06:46:24Z","git_url":"git://github.com/romana/core.git","ssh_url":"git@github.com:romana/core.git","clone_url":"https://github.com/romana/core.git","svn_url":"https://github.com/romana/core","homepage":"","size":511,"stargazers_count":10,"watchers_count":10,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":7,"forks":3,"open_issues":7,"watchers":10,"default_branch":"master"}},"base":{"label":"romana:master","ref":"master","sha":"3ffd3cd97d8c28f6987b0d79e0b42f3547bba00c","user":{"login":"romana","id":9849297,"avatar_url":"https://avatars.githubusercontent.com/u/9849297?v=3","gravatar_id":"","url":"https://api.github.com/users/romana","html_url":"https://github.com/romana","followers_url":"https://api.github.com/users/romana/followers","following_url":"https://api.github.com/users/romana/following{/other_user}","gists_url":"https://api.github.com/users/romana/gists{/gist_id}","starred_url":"https://api.github.com/users/romana/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/romana/subscriptions","organizations_url":"https://api.github.com/users/romana/orgs","repos_url":"https://api.github.com/users/romana/repos","events_url":"https://api.github.com/users/romana/events{/privacy}","received_events_url":"https://api.github.com/users/romana/received_events","type":"Organization","site_admin":false},"repo":{"id":43855674,"name":"core","full_name":"romana/core","owner":{"login":"romana","id":9849297,"avatar_url":"https://avatars.githubusercontent.com/u/9849297?v=3","gravatar_id":"","url":"https://api.github.com/users/romana","html_url":"https://github.com/romana","followers_url":"https://api.github.com/users/romana/followers","following_url":"https://api.github.com/users/romana/following{/other_user}","gists_url":"https://api.github.com/users/romana/gists{/gist_id}","starred_url":"https://api.github.com/users/romana/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/romana/subscriptions","organizations_url":"https://api.github.com/users/romana/orgs","repos_url":"https://api.github.com/users/romana/repos","events_url":"https://api.github.com/users/romana/events{/privacy}","received_events_url":"https://api.github.com/users/romana/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/romana/core","description":"Romana core components - Micro services written in Go.","fork":false,"url":"https://api.github.com/repos/romana/core","forks_url":"https://api.github.com/repos/romana/core/forks","keys_url":"https://api.github.com/repos/romana/core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/romana/core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/romana/core/teams","hooks_url":"https://api.github.com/repos/romana/core/hooks","issue_events_url":"https://api.github.com/repos/romana/core/issues/events{/number}","events_url":"https://api.github.com/repos/romana/core/events","assignees_url":"https://api.github.com/repos/romana/core/assignees{/user}","branches_url":"https://api.github.com/repos/romana/core/branches{/branch}","tags_url":"https://api.github.com/repos/romana/core/tags","blobs_url":"https://api.github.com/repos/romana/core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/romana/core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/romana/core/git/refs{/sha}","trees_url":"https://api.github.com/repos/romana/core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/romana/core/statuses/{sha}","languages_url":"https://api.github.com/repos/romana/core/languages","stargazers_url":"https://api.github.com/repos/romana/core/stargazers","contributors_url":"https://api.github.com/repos/romana/core/contributors","subscribers_url":"https://api.github.com/repos/romana/core/subscribers","subscription_url":"https://api.github.com/repos/romana/core/subscription","commits_url":"https://api.github.com/repos/romana/core/commits{/sha}","git_commits_url":"https://api.github.com/repos/romana/core/git/commits{/sha}","comments_url":"https://api.github.com/repos/romana/core/comments{/number}","issue_comment_url":"https://api.github.com/repos/romana/core/issues/comments{/number}","contents_url":"https://api.github.com/repos/romana/core/contents/{+path}","compare_url":"https://api.github.com/repos/romana/core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/romana/core/merges","archive_url":"https://api.github.com/repos/romana/core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/romana/core/downloads","issues_url":"https://api.github.com/repos/romana/core/issues{/number}","pulls_url":"https://api.github.com/repos/romana/core/pulls{/number}","milestones_url":"https://api.github.com/repos/romana/core/milestones{/number}","notifications_url":"https://api.github.com/repos/romana/core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/romana/core/labels{/name}","releases_url":"https://api.github.com/repos/romana/core/releases{/id}","deployments_url":"https://api.github.com/repos/romana/core/deployments","created_at":"2015-10-08T01:45:35Z","updated_at":"2016-04-13T22:10:15Z","pushed_at":"2016-04-19T06:46:24Z","git_url":"git://github.com/romana/core.git","ssh_url":"git@github.com:romana/core.git","clone_url":"https://github.com/romana/core.git","svn_url":"https://github.com/romana/core","homepage":"","size":511,"stargazers_count":10,"watchers_count":10,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":7,"forks":3,"open_issues":7,"watchers":10,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/romana/core/pulls/70"},"html":{"href":"https://github.com/romana/core/pull/70"},"issue":{"href":"https://api.github.com/repos/romana/core/issues/70"},"comments":{"href":"https://api.github.com/repos/romana/core/issues/70/comments"},"review_comments":{"href":"https://api.github.com/repos/romana/core/pulls/70/comments"},"review_comment":{"href":"https://api.github.com/repos/romana/core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/romana/core/pulls/70/commits"},"statuses":{"href":"https://api.github.com/repos/romana/core/statuses/642dc3737c92d66146062f02281e8f539caf8df7"}}}}