{"action":"created","comment":{"url":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/comments/39158673","id":39158673,"diff_hunk":"@@ -0,0 +1,73 @@\n+#!/usr/bin/env python\n+import boto.ec2.elb\n+import boto.utils\n+import logging\n+import salt.log\n+\n+\n+# configure a logger in case we are running it directly from python\n+# (eg: command line or testing)\n+if not salt.log.is_logging_configured():\n+    salt.log.setup_console_logger()\n+log = logging.getLogger(__name__)\n+\n+\n+def get_elb_lbs():\n+    \"\"\"\n+    Returns a dictionary of load balancer names as keys\n+    each with their respective attributes\n+    \"\"\"\n+\n+    # attributes to extract from the load balancer boto objects\n+    # this could possibly be a named argument too\n+    extract_attrs = ['scheme', 'dns_name', 'vpc_id', 'name', 'security_groups']\n+\n+    try:\n+        instance_metadata = boto.utils.get_instance_metadata(timeout=5, num_retries=2)\n+    except Exception as e:\n+        log.exception(\"Error getting ELB names: {}\".format(e))\n+        return {'custom_grain_error': True}\n+\n+    # Setup the lbs grain\n+    lbs_grain = {'lbs': {}}\n+\n+    # Collect details about this instance\n+    current_ip = instance_metadata['local-ipv4']\n+    current_dns_name = instance_metadata['local-hostname']","path":"_grains/elb_lbs.py","position":36,"original_position":36,"commit_id":"a77366c12a3fb44e42bd8a4840fadb688b04c5dc","original_commit_id":"a77366c12a3fb44e42bd8a4840fadb688b04c5dc","user":{"login":"mattmb","id":2462243,"avatar_url":"https://avatars.githubusercontent.com/u/2462243?v=3","gravatar_id":"","url":"https://api.github.com/users/mattmb","html_url":"https://github.com/mattmb","followers_url":"https://api.github.com/users/mattmb/followers","following_url":"https://api.github.com/users/mattmb/following{/other_user}","gists_url":"https://api.github.com/users/mattmb/gists{/gist_id}","starred_url":"https://api.github.com/users/mattmb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattmb/subscriptions","organizations_url":"https://api.github.com/users/mattmb/orgs","repos_url":"https://api.github.com/users/mattmb/repos","events_url":"https://api.github.com/users/mattmb/events{/privacy}","received_events_url":"https://api.github.com/users/mattmb/received_events","type":"User","site_admin":false},"body":"Think you can ditch this line","created_at":"2015-09-10T13:23:54Z","updated_at":"2015-09-10T13:23:54Z","html_url":"https://github.com/ministryofjustice/aws-formula/pull/2#discussion_r39158673","pull_request_url":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/comments/39158673"},"html":{"href":"https://github.com/ministryofjustice/aws-formula/pull/2#discussion_r39158673"},"pull_request":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/2","id":44582493,"html_url":"https://github.com/ministryofjustice/aws-formula/pull/2","diff_url":"https://github.com/ministryofjustice/aws-formula/pull/2.diff","patch_url":"https://github.com/ministryofjustice/aws-formula/pull/2.patch","issue_url":"https://api.github.com/repos/ministryofjustice/aws-formula/issues/2","number":2,"state":"open","locked":false,"title":"add dynamic grain data for elb","user":{"login":"filipposc5","id":2116658,"avatar_url":"https://avatars.githubusercontent.com/u/2116658?v=3","gravatar_id":"","url":"https://api.github.com/users/filipposc5","html_url":"https://github.com/filipposc5","followers_url":"https://api.github.com/users/filipposc5/followers","following_url":"https://api.github.com/users/filipposc5/following{/other_user}","gists_url":"https://api.github.com/users/filipposc5/gists{/gist_id}","starred_url":"https://api.github.com/users/filipposc5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/filipposc5/subscriptions","organizations_url":"https://api.github.com/users/filipposc5/orgs","repos_url":"https://api.github.com/users/filipposc5/repos","events_url":"https://api.github.com/users/filipposc5/events{/privacy}","received_events_url":"https://api.github.com/users/filipposc5/received_events","type":"User","site_admin":false},"body":"# What does it do ? \r\nGets all load balancer names for the instance and a few of their attributes directly from boto ELB and stores them into a top level grain called \"lbs\". It will get all the load balancers and first filter by the instance's vpc_id and then by ensuring the instance_id is in the remaining load balancer's \"instances\" list. \r\n\r\n# Why? \r\nThe ELB name is needed because we need to know which ELBs to deregister during\r\nzero downtime upgrade and the naming convention changed with https://github.com/ministryofjustice/bootstrap-cfn/pull/143\r\nwhich currently breaks zero downtime upgrade. Previously we used to construct this as the name was predictable eg: foo-bar would become ELBfoobar, but currently this gets autonamed. There will be one or more pull requests in the salt repos (eg: moj-docker-deploy) that leverage this request in order to pass the correct name into the elb_reg.instance_deregistered \r\n\r\n# Actual error\r\nThe actual error which made us realise we need this was: \r\n```\r\n[INFO ] Executing state elb_reg.instance_deregistered for ELB-shiny-test-ext\r\n[WARNING ] BotoServerError: 403 Forbidden\r\n<ErrorResponse xmlns=\"http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/\">\r\n<Error>\r\n<Type>Sender</Type>\r\n<Code>AccessDenied</Code>\r\n<Message>User: arn:aws:sts::880656497252:assumed-role/shiny-project-dev-adc14f05-BaseHostRole-L9PN16XRU4XJ/i-6f8047c3 is not authorized to perform: elasticloadbalancing:DeregisterInstancesFromLoadBalancer\r\n...\r\n[Sep-7 10:40] Filippos Chalvatzoglou: on resource: arn:aws:elasticloadbalancing:eu-west-1:880656497252:loadbalancer/ELB-shiny-test-ext</Message>  </Error>  <RequestId>407e8a90-5544-11e5-b851-71222939b06b</RequestId>  </ErrorResponse>\r\n```\r\nAnd worth noting that although we get a 403 error, if it wasn't for security reasons we would get a 404 error because in reality the error is that ```ELB-shiny-test-ext``` didn't exist and the real (autogenerated) name was in fact ```shiny-pro-ELBshiny-NFGERR664KZ7-1985355```.\r\n","created_at":"2015-09-09T18:15:31Z","updated_at":"2015-09-10T13:23:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b3a0f7aa4b41968f2fcabec5d6c7121eacf87c4e","assignee":{"login":"filipposc5","id":2116658,"avatar_url":"https://avatars.githubusercontent.com/u/2116658?v=3","gravatar_id":"","url":"https://api.github.com/users/filipposc5","html_url":"https://github.com/filipposc5","followers_url":"https://api.github.com/users/filipposc5/followers","following_url":"https://api.github.com/users/filipposc5/following{/other_user}","gists_url":"https://api.github.com/users/filipposc5/gists{/gist_id}","starred_url":"https://api.github.com/users/filipposc5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/filipposc5/subscriptions","organizations_url":"https://api.github.com/users/filipposc5/orgs","repos_url":"https://api.github.com/users/filipposc5/repos","events_url":"https://api.github.com/users/filipposc5/events{/privacy}","received_events_url":"https://api.github.com/users/filipposc5/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/2/commits","review_comments_url":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/2/comments","review_comment_url":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ministryofjustice/aws-formula/issues/2/comments","statuses_url":"https://api.github.com/repos/ministryofjustice/aws-formula/statuses/a77366c12a3fb44e42bd8a4840fadb688b04c5dc","head":{"label":"ministryofjustice:add-load-balancers-in-grains","ref":"add-load-balancers-in-grains","sha":"a77366c12a3fb44e42bd8a4840fadb688b04c5dc","user":{"login":"ministryofjustice","id":2203574,"avatar_url":"https://avatars.githubusercontent.com/u/2203574?v=3","gravatar_id":"","url":"https://api.github.com/users/ministryofjustice","html_url":"https://github.com/ministryofjustice","followers_url":"https://api.github.com/users/ministryofjustice/followers","following_url":"https://api.github.com/users/ministryofjustice/following{/other_user}","gists_url":"https://api.github.com/users/ministryofjustice/gists{/gist_id}","starred_url":"https://api.github.com/users/ministryofjustice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ministryofjustice/subscriptions","organizations_url":"https://api.github.com/users/ministryofjustice/orgs","repos_url":"https://api.github.com/users/ministryofjustice/repos","events_url":"https://api.github.com/users/ministryofjustice/events{/privacy}","received_events_url":"https://api.github.com/users/ministryofjustice/received_events","type":"Organization","site_admin":false},"repo":{"id":41908290,"name":"aws-formula","full_name":"ministryofjustice/aws-formula","owner":{"login":"ministryofjustice","id":2203574,"avatar_url":"https://avatars.githubusercontent.com/u/2203574?v=3","gravatar_id":"","url":"https://api.github.com/users/ministryofjustice","html_url":"https://github.com/ministryofjustice","followers_url":"https://api.github.com/users/ministryofjustice/followers","following_url":"https://api.github.com/users/ministryofjustice/following{/other_user}","gists_url":"https://api.github.com/users/ministryofjustice/gists{/gist_id}","starred_url":"https://api.github.com/users/ministryofjustice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ministryofjustice/subscriptions","organizations_url":"https://api.github.com/users/ministryofjustice/orgs","repos_url":"https://api.github.com/users/ministryofjustice/repos","events_url":"https://api.github.com/users/ministryofjustice/events{/privacy}","received_events_url":"https://api.github.com/users/ministryofjustice/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ministryofjustice/aws-formula","description":"","fork":false,"url":"https://api.github.com/repos/ministryofjustice/aws-formula","forks_url":"https://api.github.com/repos/ministryofjustice/aws-formula/forks","keys_url":"https://api.github.com/repos/ministryofjustice/aws-formula/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ministryofjustice/aws-formula/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ministryofjustice/aws-formula/teams","hooks_url":"https://api.github.com/repos/ministryofjustice/aws-formula/hooks","issue_events_url":"https://api.github.com/repos/ministryofjustice/aws-formula/issues/events{/number}","events_url":"https://api.github.com/repos/ministryofjustice/aws-formula/events","assignees_url":"https://api.github.com/repos/ministryofjustice/aws-formula/assignees{/user}","branches_url":"https://api.github.com/repos/ministryofjustice/aws-formula/branches{/branch}","tags_url":"https://api.github.com/repos/ministryofjustice/aws-formula/tags","blobs_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/refs{/sha}","trees_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ministryofjustice/aws-formula/statuses/{sha}","languages_url":"https://api.github.com/repos/ministryofjustice/aws-formula/languages","stargazers_url":"https://api.github.com/repos/ministryofjustice/aws-formula/stargazers","contributors_url":"https://api.github.com/repos/ministryofjustice/aws-formula/contributors","subscribers_url":"https://api.github.com/repos/ministryofjustice/aws-formula/subscribers","subscription_url":"https://api.github.com/repos/ministryofjustice/aws-formula/subscription","commits_url":"https://api.github.com/repos/ministryofjustice/aws-formula/commits{/sha}","git_commits_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/commits{/sha}","comments_url":"https://api.github.com/repos/ministryofjustice/aws-formula/comments{/number}","issue_comment_url":"https://api.github.com/repos/ministryofjustice/aws-formula/issues/comments{/number}","contents_url":"https://api.github.com/repos/ministryofjustice/aws-formula/contents/{+path}","compare_url":"https://api.github.com/repos/ministryofjustice/aws-formula/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ministryofjustice/aws-formula/merges","archive_url":"https://api.github.com/repos/ministryofjustice/aws-formula/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ministryofjustice/aws-formula/downloads","issues_url":"https://api.github.com/repos/ministryofjustice/aws-formula/issues{/number}","pulls_url":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls{/number}","milestones_url":"https://api.github.com/repos/ministryofjustice/aws-formula/milestones{/number}","notifications_url":"https://api.github.com/repos/ministryofjustice/aws-formula/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ministryofjustice/aws-formula/labels{/name}","releases_url":"https://api.github.com/repos/ministryofjustice/aws-formula/releases{/id}","created_at":"2015-09-04T09:38:44Z","updated_at":"2015-09-04T12:57:21Z","pushed_at":"2015-09-10T10:50:56Z","git_url":"git://github.com/ministryofjustice/aws-formula.git","ssh_url":"git@github.com:ministryofjustice/aws-formula.git","clone_url":"https://github.com/ministryofjustice/aws-formula.git","svn_url":"https://github.com/ministryofjustice/aws-formula","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"ministryofjustice:master","ref":"master","sha":"edb36c13469e2310adead725ca6a31e3539f11f8","user":{"login":"ministryofjustice","id":2203574,"avatar_url":"https://avatars.githubusercontent.com/u/2203574?v=3","gravatar_id":"","url":"https://api.github.com/users/ministryofjustice","html_url":"https://github.com/ministryofjustice","followers_url":"https://api.github.com/users/ministryofjustice/followers","following_url":"https://api.github.com/users/ministryofjustice/following{/other_user}","gists_url":"https://api.github.com/users/ministryofjustice/gists{/gist_id}","starred_url":"https://api.github.com/users/ministryofjustice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ministryofjustice/subscriptions","organizations_url":"https://api.github.com/users/ministryofjustice/orgs","repos_url":"https://api.github.com/users/ministryofjustice/repos","events_url":"https://api.github.com/users/ministryofjustice/events{/privacy}","received_events_url":"https://api.github.com/users/ministryofjustice/received_events","type":"Organization","site_admin":false},"repo":{"id":41908290,"name":"aws-formula","full_name":"ministryofjustice/aws-formula","owner":{"login":"ministryofjustice","id":2203574,"avatar_url":"https://avatars.githubusercontent.com/u/2203574?v=3","gravatar_id":"","url":"https://api.github.com/users/ministryofjustice","html_url":"https://github.com/ministryofjustice","followers_url":"https://api.github.com/users/ministryofjustice/followers","following_url":"https://api.github.com/users/ministryofjustice/following{/other_user}","gists_url":"https://api.github.com/users/ministryofjustice/gists{/gist_id}","starred_url":"https://api.github.com/users/ministryofjustice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ministryofjustice/subscriptions","organizations_url":"https://api.github.com/users/ministryofjustice/orgs","repos_url":"https://api.github.com/users/ministryofjustice/repos","events_url":"https://api.github.com/users/ministryofjustice/events{/privacy}","received_events_url":"https://api.github.com/users/ministryofjustice/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ministryofjustice/aws-formula","description":"","fork":false,"url":"https://api.github.com/repos/ministryofjustice/aws-formula","forks_url":"https://api.github.com/repos/ministryofjustice/aws-formula/forks","keys_url":"https://api.github.com/repos/ministryofjustice/aws-formula/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ministryofjustice/aws-formula/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ministryofjustice/aws-formula/teams","hooks_url":"https://api.github.com/repos/ministryofjustice/aws-formula/hooks","issue_events_url":"https://api.github.com/repos/ministryofjustice/aws-formula/issues/events{/number}","events_url":"https://api.github.com/repos/ministryofjustice/aws-formula/events","assignees_url":"https://api.github.com/repos/ministryofjustice/aws-formula/assignees{/user}","branches_url":"https://api.github.com/repos/ministryofjustice/aws-formula/branches{/branch}","tags_url":"https://api.github.com/repos/ministryofjustice/aws-formula/tags","blobs_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/refs{/sha}","trees_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ministryofjustice/aws-formula/statuses/{sha}","languages_url":"https://api.github.com/repos/ministryofjustice/aws-formula/languages","stargazers_url":"https://api.github.com/repos/ministryofjustice/aws-formula/stargazers","contributors_url":"https://api.github.com/repos/ministryofjustice/aws-formula/contributors","subscribers_url":"https://api.github.com/repos/ministryofjustice/aws-formula/subscribers","subscription_url":"https://api.github.com/repos/ministryofjustice/aws-formula/subscription","commits_url":"https://api.github.com/repos/ministryofjustice/aws-formula/commits{/sha}","git_commits_url":"https://api.github.com/repos/ministryofjustice/aws-formula/git/commits{/sha}","comments_url":"https://api.github.com/repos/ministryofjustice/aws-formula/comments{/number}","issue_comment_url":"https://api.github.com/repos/ministryofjustice/aws-formula/issues/comments{/number}","contents_url":"https://api.github.com/repos/ministryofjustice/aws-formula/contents/{+path}","compare_url":"https://api.github.com/repos/ministryofjustice/aws-formula/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ministryofjustice/aws-formula/merges","archive_url":"https://api.github.com/repos/ministryofjustice/aws-formula/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ministryofjustice/aws-formula/downloads","issues_url":"https://api.github.com/repos/ministryofjustice/aws-formula/issues{/number}","pulls_url":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls{/number}","milestones_url":"https://api.github.com/repos/ministryofjustice/aws-formula/milestones{/number}","notifications_url":"https://api.github.com/repos/ministryofjustice/aws-formula/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ministryofjustice/aws-formula/labels{/name}","releases_url":"https://api.github.com/repos/ministryofjustice/aws-formula/releases{/id}","created_at":"2015-09-04T09:38:44Z","updated_at":"2015-09-04T12:57:21Z","pushed_at":"2015-09-10T10:50:56Z","git_url":"git://github.com/ministryofjustice/aws-formula.git","ssh_url":"git@github.com:ministryofjustice/aws-formula.git","clone_url":"https://github.com/ministryofjustice/aws-formula.git","svn_url":"https://github.com/ministryofjustice/aws-formula","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/2"},"html":{"href":"https://github.com/ministryofjustice/aws-formula/pull/2"},"issue":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/issues/2"},"comments":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/ministryofjustice/aws-formula/statuses/a77366c12a3fb44e42bd8a4840fadb688b04c5dc"}}}}