{"action":"created","comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/40572881","id":40572881,"diff_hunk":"@@ -0,0 +1,114 @@\n+/*\n+ * Licensed to Elasticsearch under one or more contributor\n+ * license agreements. See the NOTICE file distributed with\n+ * this work for additional information regarding copyright\n+ * ownership. Elasticsearch licenses this file to you under\n+ * the Apache License, Version 2.0 (the \"License\"); you may\n+ * not use this file except in compliance with the License.\n+ * 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\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied.  See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+\n+package org.elasticsearch.cloud.gce.network;\n+\n+import org.elasticsearch.cloud.gce.GceComputeService;\n+import org.elasticsearch.cloud.gce.GceComputeService.Fields;\n+import org.elasticsearch.common.Strings;\n+import org.elasticsearch.common.component.AbstractComponent;\n+import org.elasticsearch.common.network.NetworkService.CustomNameResolver;\n+import org.elasticsearch.common.settings.Settings;\n+\n+import java.io.IOException;\n+import java.net.InetAddress;\n+\n+/**\n+ * <p>Resolves certain GCE related 'meta' hostnames into an actual hostname\n+ * obtained from gce meta-data.</p>\n+ * Valid config values for {@link GceAddressResolverType}s are -\n+ * <ul>\n+ * <li>_gce_ - maps to privateIp</li>\n+ * <li>_gce:privateIp_</li>\n+ * <li>_gce:hostname_</li>\n+ * </ul>\n+ */\n+public class GceNameResolver extends AbstractComponent implements CustomNameResolver {\n+\n+    private final GceComputeService gceComputeService;\n+    private final Integer networkCard;\n+\n+    /**\n+     * enum that can be added to over time with more meta-data types\n+     */\n+    private enum GceAddressResolverType {\n+\n+        // some less verbose defaults\n+        PRIVATE_DNS(\"gce:hostname\", \"hostname\"),\n+        PRIVATE_IP(\"gce:privateIp\", \"network-interfaces/{{network}}/ip\"),\n+        GCE(\"gce\", PRIVATE_IP.gceName);\n+\n+        final String configName;\n+        final String gceName;\n+\n+        GceAddressResolverType(String configName, String gceName) {\n+            this.configName = configName;\n+            this.gceName = gceName;\n+        }\n+    }\n+\n+    /**\n+     * Construct a {@link CustomNameResolver}.\n+     */\n+    public GceNameResolver(Settings settings, GceComputeService gceComputeService) {\n+        super(settings);\n+        this.gceComputeService = gceComputeService;\n+        // We by default extract metadata from network card 0\n+        // network-interfaces/0/ip\n+        this.networkCard = settings.getAsInt(Fields.NETWORK_CARD, 0);\n+    }\n+\n+    /**\n+     * @param type the gce hostname type to discover.\n+     * @param networkCard\n+     * @return the appropriate host resolved from gce meta-data.\n+     * @see CustomNameResolver#resolveIfPossible(String)\n+     */\n+    private InetAddress[] resolve(GceAddressResolverType type, Integer networkCard) {\n+        try {\n+            // We replace network placeholder with networkCard value\n+            String gceMetadataPath = Strings.replace(type.gceName, \"{{network}}\", networkCard.toString());\n+            String metadataResult = gceComputeService.networkAddress(gceMetadataPath);\n+            if (metadataResult == null || metadataResult.length() == 0) {\n+                logger.error(\"no gce metadata returned for {}\", type.configName);\n+                return null;","path":"plugins/cloud-gce/src/main/java/org/elasticsearch/cloud/gce/network/GceNameResolver.java","position":null,"original_position":90,"commit_id":"956fb81afd5a210300c66e2dc9aa206701d2a8a7","original_commit_id":"32144b1d9728854afeb4d86eef06b46d6639b27e","user":{"login":"dadoonet","id":274222,"avatar_url":"https://avatars.githubusercontent.com/u/274222?v=3","gravatar_id":"","url":"https://api.github.com/users/dadoonet","html_url":"https://github.com/dadoonet","followers_url":"https://api.github.com/users/dadoonet/followers","following_url":"https://api.github.com/users/dadoonet/following{/other_user}","gists_url":"https://api.github.com/users/dadoonet/gists{/gist_id}","starred_url":"https://api.github.com/users/dadoonet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dadoonet/subscriptions","organizations_url":"https://api.github.com/users/dadoonet/orgs","repos_url":"https://api.github.com/users/dadoonet/repos","events_url":"https://api.github.com/users/dadoonet/events{/privacy}","received_events_url":"https://api.github.com/users/dadoonet/received_events","type":"User","site_admin":false},"body":"@bleskes Actually we can't throw an exception here because we will never be able to use for example `local`. \r\n\r\nOr we need to write differently that piece of code: https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/common/network/NetworkService.java#L169-L174","created_at":"2015-09-28T16:27:09Z","updated_at":"2015-09-28T16:27:09Z","html_url":"https://github.com/elastic/elasticsearch/pull/13612#discussion_r40572881","pull_request_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/13612","_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/40572881"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/13612#discussion_r40572881"},"pull_request":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/13612"}}},"pull_request":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/13612","id":45157445,"html_url":"https://github.com/elastic/elasticsearch/pull/13612","diff_url":"https://github.com/elastic/elasticsearch/pull/13612.diff","patch_url":"https://github.com/elastic/elasticsearch/pull/13612.patch","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/13612","number":13612,"state":"open","locked":false,"title":"Add `_gce_` network host setting","user":{"login":"dadoonet","id":274222,"avatar_url":"https://avatars.githubusercontent.com/u/274222?v=3","gravatar_id":"","url":"https://api.github.com/users/dadoonet","html_url":"https://github.com/dadoonet","followers_url":"https://api.github.com/users/dadoonet/followers","following_url":"https://api.github.com/users/dadoonet/following{/other_user}","gists_url":"https://api.github.com/users/dadoonet/gists{/gist_id}","starred_url":"https://api.github.com/users/dadoonet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dadoonet/subscriptions","organizations_url":"https://api.github.com/users/dadoonet/orgs","repos_url":"https://api.github.com/users/dadoonet/repos","events_url":"https://api.github.com/users/dadoonet/events{/privacy}","received_events_url":"https://api.github.com/users/dadoonet/received_events","type":"User","site_admin":false},"body":"When running in GCE platform, an instance has access to:\r\n\r\nhttp://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip\r\n\r\nWhich gives back the private IP address, for example `10.240.0.2`.\r\n\r\nhttp://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/externalIp\r\n\r\nGives back the public Ip address, for example `130.211.108.21`.\r\n\r\nAs we have for `ec2`, we can support new network host settings:\r\n\r\n* `_gce:privateIp_`: maps to `ip`\r\n* `_gce:publicIp_`: maps to `externalIp`\r\n* `_gce_` shorter version for `_gce:privateIp_`\r\n\r\nCloses #13605.\r\nCloses #13590.","created_at":"2015-09-16T10:59:41Z","updated_at":"2015-09-28T16:27:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":"969a54b94a70c645777eddda93028b3e70deddc5","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/13612/commits","review_comments_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/13612/comments","review_comment_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/13612/comments","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/956fb81afd5a210300c66e2dc9aa206701d2a8a7","head":{"label":"dadoonet:pr/13605-gce-add-network-host-setting","ref":"pr/13605-gce-add-network-host-setting","sha":"956fb81afd5a210300c66e2dc9aa206701d2a8a7","user":{"login":"dadoonet","id":274222,"avatar_url":"https://avatars.githubusercontent.com/u/274222?v=3","gravatar_id":"","url":"https://api.github.com/users/dadoonet","html_url":"https://github.com/dadoonet","followers_url":"https://api.github.com/users/dadoonet/followers","following_url":"https://api.github.com/users/dadoonet/following{/other_user}","gists_url":"https://api.github.com/users/dadoonet/gists{/gist_id}","starred_url":"https://api.github.com/users/dadoonet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dadoonet/subscriptions","organizations_url":"https://api.github.com/users/dadoonet/orgs","repos_url":"https://api.github.com/users/dadoonet/repos","events_url":"https://api.github.com/users/dadoonet/events{/privacy}","received_events_url":"https://api.github.com/users/dadoonet/received_events","type":"User","site_admin":false},"repo":{"id":2521491,"name":"elasticsearch","full_name":"dadoonet/elasticsearch","owner":{"login":"dadoonet","id":274222,"avatar_url":"https://avatars.githubusercontent.com/u/274222?v=3","gravatar_id":"","url":"https://api.github.com/users/dadoonet","html_url":"https://github.com/dadoonet","followers_url":"https://api.github.com/users/dadoonet/followers","following_url":"https://api.github.com/users/dadoonet/following{/other_user}","gists_url":"https://api.github.com/users/dadoonet/gists{/gist_id}","starred_url":"https://api.github.com/users/dadoonet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dadoonet/subscriptions","organizations_url":"https://api.github.com/users/dadoonet/orgs","repos_url":"https://api.github.com/users/dadoonet/repos","events_url":"https://api.github.com/users/dadoonet/events{/privacy}","received_events_url":"https://api.github.com/users/dadoonet/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dadoonet/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":true,"url":"https://api.github.com/repos/dadoonet/elasticsearch","forks_url":"https://api.github.com/repos/dadoonet/elasticsearch/forks","keys_url":"https://api.github.com/repos/dadoonet/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dadoonet/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dadoonet/elasticsearch/teams","hooks_url":"https://api.github.com/repos/dadoonet/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/dadoonet/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/dadoonet/elasticsearch/events","assignees_url":"https://api.github.com/repos/dadoonet/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/dadoonet/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/dadoonet/elasticsearch/tags","blobs_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dadoonet/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/dadoonet/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/dadoonet/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/dadoonet/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/dadoonet/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/dadoonet/elasticsearch/subscription","commits_url":"https://api.github.com/repos/dadoonet/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/dadoonet/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/dadoonet/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/dadoonet/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/dadoonet/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dadoonet/elasticsearch/merges","archive_url":"https://api.github.com/repos/dadoonet/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dadoonet/elasticsearch/downloads","issues_url":"https://api.github.com/repos/dadoonet/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/dadoonet/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/dadoonet/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/dadoonet/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dadoonet/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/dadoonet/elasticsearch/releases{/id}","created_at":"2011-10-05T19:24:28Z","updated_at":"2015-03-10T00:44:50Z","pushed_at":"2015-09-28T16:16:47Z","git_url":"git://github.com/dadoonet/elasticsearch.git","ssh_url":"git@github.com:dadoonet/elasticsearch.git","clone_url":"https://github.com/dadoonet/elasticsearch.git","svn_url":"https://github.com/dadoonet/elasticsearch","homepage":"http://elasticsearch.org","size":142752,"stargazers_count":2,"watchers_count":2,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":2,"default_branch":"master"}},"base":{"label":"elastic:master","ref":"master","sha":"e01a74d608bf144c710de6eb2087d9aaf1237d62","user":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"repo":{"id":507775,"name":"elasticsearch","full_name":"elastic/elasticsearch","owner":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elastic/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":false,"url":"https://api.github.com/repos/elastic/elasticsearch","forks_url":"https://api.github.com/repos/elastic/elasticsearch/forks","keys_url":"https://api.github.com/repos/elastic/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elastic/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elastic/elasticsearch/teams","hooks_url":"https://api.github.com/repos/elastic/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/elastic/elasticsearch/events","assignees_url":"https://api.github.com/repos/elastic/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/elastic/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/elastic/elasticsearch/tags","blobs_url":"https://api.github.com/repos/elastic/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elastic/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elastic/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/elastic/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/elastic/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/elastic/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/elastic/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/elastic/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/elastic/elasticsearch/subscription","commits_url":"https://api.github.com/repos/elastic/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/elastic/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/elastic/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/elastic/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elastic/elasticsearch/merges","archive_url":"https://api.github.com/repos/elastic/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elastic/elasticsearch/downloads","issues_url":"https://api.github.com/repos/elastic/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/elastic/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/elastic/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/elastic/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elastic/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/elastic/elasticsearch/releases{/id}","created_at":"2010-02-08T13:20:56Z","updated_at":"2015-09-28T15:51:00Z","pushed_at":"2015-09-28T16:16:51Z","git_url":"git://github.com/elastic/elasticsearch.git","ssh_url":"git@github.com:elastic/elasticsearch.git","clone_url":"https://github.com/elastic/elasticsearch.git","svn_url":"https://github.com/elastic/elasticsearch","homepage":"https://www.elastic.co/products/elasticsearch","size":438469,"stargazers_count":12887,"watchers_count":12887,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4203,"mirror_url":null,"open_issues_count":1250,"forks":4203,"open_issues":1250,"watchers":12887,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/13612"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/13612"},"issue":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/13612"},"comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/13612/comments"},"review_comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/13612/comments"},"review_comment":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/13612/commits"},"statuses":{"href":"https://api.github.com/repos/elastic/elasticsearch/statuses/956fb81afd5a210300c66e2dc9aa206701d2a8a7"}}}}