google-cloud-core 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29d08e42cbb895f42a47e1eedcd28c7b15dd77fa
4
- data.tar.gz: f5808aba10c3cf10fd76786e774b878f7ee9925f
3
+ metadata.gz: c54259c5d0a2e13f518a15f3a1a4b485cb4ac16b
4
+ data.tar.gz: 86286197809cef4e199315bfd94852aca25b402e
5
5
  SHA512:
6
- metadata.gz: 9e6c3fcc7b93a69cf29254ccef9c5c7b404743cdbc6e05ea1d08ace3a2c79c884c97e9d536e9e99d5259ca441d5e2d72c1df2af1af7df8557ea6cda3ca96b2c2
7
- data.tar.gz: ef9af1e66e014e1f92922e6c869798a27294cb11f0b7d2402780603a80a3a1a327ad883c4ec9f4ab63ced13af05740cf105f466c75eb6b5e7ca395e1c4ebc6cf
6
+ metadata.gz: 0e14f448eb90a2365ade333f56e5c6b85859de19ee13664b6f1f10cb7c914de0900ddc5add35551ed1164780429686962f86f7766e9edad626515ceec1f6fa15
7
+ data.tar.gz: 8b62a8873b893455fa9936ff35c77fc7d49c17df451d1e35e8240f0d690673fae1c1b6d7540693a524af510d1d7b653132226fa15f7f70d34b26eee63c150d7a
@@ -0,0 +1,145 @@
1
+ # Copyright 2015 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "faraday"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Core
21
+ ##
22
+ # @private
23
+ # Represents the Google Cloud Platform environments.
24
+ module Environment
25
+ CHECK_URI = "http://169.254.169.254"
26
+ PROJECT_URI = "#{CHECK_URI}/computeMetadata/v1/project"
27
+ INSTANCE_METADATA_URI = "#{CHECK_URI}/computeMetadata/v1/instance"
28
+
29
+ ##
30
+ # Check if running from a gce vm, which actually hosts GCE, GKE, and
31
+ # GAE environments.
32
+ def self.gce_vm? connection: nil
33
+ @metadata ||= {}
34
+ return @metadata[:gce_vm] if @metadata.key? :gce_vm
35
+
36
+ conn = connection || Faraday.default_connection
37
+ resp = conn.get CHECK_URI do |req|
38
+ req.options.timeout = 0.1
39
+ end
40
+ @metadata[:gce_vm] = if resp.status != 200 ||
41
+ !resp.headers.key?("Metadata-Flavor")
42
+ false
43
+ else
44
+ resp.headers["Metadata-Flavor"] == "Google"
45
+ end
46
+ rescue Faraday::TimeoutError, Faraday::ConnectionFailed
47
+ @metadata ||= {}
48
+ @metadata[:gce_vm] = false
49
+ end
50
+
51
+ ##
52
+ # Check if running from Google Compute Engine, and not using GAE or GKE
53
+ #
54
+ # @return [Boolean] True if running from GCE and not GAE or GKE
55
+ def self.gce?
56
+ gce_vm? && !gae? && !gke?
57
+ end
58
+
59
+ ##
60
+ # Check if running from Google Container Engine by querying for
61
+ # GKE cluster name and VM instance_zone
62
+ #
63
+ # @return [Boolean] True if self.gke_cluster_name() and
64
+ # self.instance_zone() both return true values
65
+ def self.gke?
66
+ gke_cluster_name && instance_zone
67
+ end
68
+
69
+ ##
70
+ # Check if running from Google App Engine by checking existance of
71
+ # GAE_VM, GAE_MODULE_NAME, GAE_MODULE_VERSION environment variables.
72
+ #
73
+ # @return [Boolean] True if all three GAE environment variables are
74
+ # defined
75
+ def self.gae?
76
+ ENV["GAE_VM"] && gae_module_id && gae_module_version
77
+ end
78
+
79
+ def self.project_id
80
+ uri = "#{PROJECT_URI}/project-id"
81
+ get_metadata_attribute uri, :project_id
82
+ end
83
+
84
+ ##
85
+ # Retrieve GAE module name
86
+ def self.gae_module_id
87
+ ENV["GAE_MODULE_NAME"]
88
+ end
89
+
90
+ ##
91
+ # Retrieve GAE module version
92
+ def self.gae_module_version
93
+ ENV["GAE_MODULE_VERSION"]
94
+ end
95
+
96
+ ##
97
+ # Retrieve GKE cluster name
98
+ def self.gke_cluster_name
99
+ uri = "#{INSTANCE_METADATA_URI}/attributes/cluster-name"
100
+ get_metadata_attribute uri, :cluster_name
101
+ end
102
+
103
+ ##
104
+ # Retrieve GKE namespace id
105
+ def self.gke_namespace_id
106
+ ENV["GKE_NAMESPACE_ID"]
107
+ end
108
+
109
+ ##
110
+ # Retrieve GCE VM zone
111
+ def self.instance_zone
112
+ uri = "#{INSTANCE_METADATA_URI}/zone"
113
+ full_zone = get_metadata_attribute uri, :zone
114
+ full_zone.nil? ? nil : full_zone.split(%r{/}).last
115
+ end
116
+
117
+ ##
118
+ # Retrieve GCE VM instance_id
119
+ def self.instance_id
120
+ uri = "#{INSTANCE_METADATA_URI}/id"
121
+ get_metadata_attribute uri, :instance_id
122
+ end
123
+
124
+ ##
125
+ # Helper method to send HTTP request to GCP metadata service and
126
+ # retrieve environment information
127
+ def self.get_metadata_attribute uri, attr_name, connection: nil
128
+ @metadata ||= {}
129
+ return @metadata[attr_name] if @metadata.key? attr_name
130
+
131
+ conn = connection || Faraday.default_connection
132
+ conn.headers = { "Metadata-Flavor" => "Google" }
133
+ resp = conn.get uri do |req|
134
+ req.options.timeout = 0.1
135
+ end
136
+
137
+ @metadata[attr_name] = resp.status == 200 ? resp.body : nil
138
+ rescue Faraday::TimeoutError, Faraday::ConnectionFailed
139
+ @metadata ||= {}
140
+ @metadata[attr_name] = nil
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Core
19
- VERSION = "0.20.1"
19
+ VERSION = "0.21.0"
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.1
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-08-27 00:00:00.000000000 Z
12
+ date: 2016-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -123,6 +123,20 @@ dependencies:
123
123
  - - "~>"
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0.9'
126
+ - !ruby/object:Gem::Dependency
127
+ name: yard-doctest
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: 0.1.6
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 0.1.6
126
140
  description: google-cloud-core is the internal shared library for google-cloud-ruby.
127
141
  email:
128
142
  - mike@blowmage.com
@@ -132,9 +146,7 @@ extensions: []
132
146
  extra_rdoc_files: []
133
147
  files:
134
148
  - lib/google/cloud.rb
135
- - lib/google/cloud/core/backoff.rb
136
- - lib/google/cloud/core/gce.rb
137
- - lib/google/cloud/core/grpc_backoff.rb
149
+ - lib/google/cloud/core/environment.rb
138
150
  - lib/google/cloud/core/grpc_utils.rb
139
151
  - lib/google/cloud/core/version.rb
140
152
  - lib/google/cloud/credentials.rb
@@ -1,89 +0,0 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Google
17
- module Cloud
18
- module Core
19
- ##
20
- # @private
21
- # Backoff allows users to control how Google API calls are retried.
22
- # If an API call fails the response will be checked to see if the
23
- # call can be retried. If the response matches the criteria, then it
24
- # will be retried with an incremental backoff. This means that an
25
- # increasing delay will be added between each retried call. The first
26
- # retry will be delayed one second, the second retry will be delayed
27
- # two seconds, and so on.
28
- class GrpcBackoff
29
- class << self
30
- ##
31
- # The number of times a retriable API call should be retried.
32
- #
33
- # The default value is `3`.
34
- attr_reader :retries
35
- def retries= new_retries
36
- @retries = new_retries
37
- end
38
-
39
- ##
40
- # The GRPC Status Codes that should be retried.
41
- #
42
- # The default values are `14`.
43
- attr_accessor :grpc_codes
44
-
45
- ##
46
- # The code to run when a backoff is handled.
47
- # This must be a Proc and must take the number of
48
- # retries as an argument.
49
- #
50
- # Note: This method is undocumented and may change.
51
- attr_accessor :backoff # :nodoc:
52
- end
53
- # Set the default values
54
- self.retries = 3
55
- self.grpc_codes = [14]
56
- self.backoff = ->(retries) { sleep retries.to_i }
57
-
58
- ##
59
- # @private
60
- # Creates a new GrpcBackoff object to catch common errors when calling
61
- # the Google API and handle the error by retrying the call.
62
- #
63
- # Google::Cloud::Core::GrpcBackoff.new(options).execute do
64
- # datastore.lookup lookup_req
65
- # end
66
- def initialize options = {}
67
- @retries = (options[:retries] || GrpcBackoff.retries).to_i
68
- @grpc_codes = (options[:grpc_codes] || GrpcBackoff.grpc_codes).to_a
69
- @backoff = options[:backoff] || GrpcBackoff.backoff
70
- end
71
-
72
- # @private
73
- def execute
74
- current_retries = 0
75
- loop do
76
- begin
77
- return yield
78
- rescue GRPC::BadStatus => e
79
- raise e unless @grpc_codes.include?(e.code) &&
80
- (current_retries < @retries)
81
- current_retries += 1
82
- @backoff.call current_retries
83
- end
84
- end
85
- end
86
- end
87
- end
88
- end
89
- end
@@ -1,60 +0,0 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "faraday"
17
-
18
- module Google
19
- module Cloud
20
- module Core
21
- ##
22
- # @private
23
- # Represents the Google Compute Engine environment.
24
- module GCE
25
- CHECK_URI = "http://169.254.169.254"
26
- PROJECT_URI = "#{CHECK_URI}/computeMetadata/v1/project/project-id"
27
-
28
- def self.gce? options = {}
29
- conn = options[:connection] || Faraday.default_connection
30
- resp = conn.get CHECK_URI do |req|
31
- req.options.timeout = 0.1
32
- end
33
- return false unless resp.status == 200
34
- return false unless resp.headers.key? "Metadata-Flavor"
35
- return resp.headers["Metadata-Flavor"] == "Google"
36
- rescue Faraday::TimeoutError, Faraday::ConnectionFailed
37
- return false
38
- end
39
-
40
- def self.project_id options = {}
41
- @gce ||= {}
42
- return @gce[:project_id] if @gce.key? :project_id
43
- conn = options[:connection] || Faraday.default_connection
44
- conn.headers = { "Metadata-Flavor" => "Google" }
45
- resp = conn.get PROJECT_URI do |req|
46
- req.options.timeout = 0.1
47
- end
48
- if resp.status == 200
49
- @gce[:project_id] = resp.body
50
- else
51
- @gce[:project_id] = nil
52
- end
53
- rescue Faraday::TimeoutError, Faraday::ConnectionFailed
54
- @gce ||= {}
55
- @gce[:project_id] = nil
56
- end
57
- end
58
- end
59
- end
60
- end
@@ -1,89 +0,0 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Google
17
- module Cloud
18
- module Core
19
- ##
20
- # @private
21
- # Backoff allows users to control how Google API calls are retried.
22
- # If an API call fails the response will be checked to see if the
23
- # call can be retried. If the response matches the criteria, then it
24
- # will be retried with an incremental backoff. This means that an
25
- # increasing delay will be added between each retried call. The first
26
- # retry will be delayed one second, the second retry will be delayed
27
- # two seconds, and so on.
28
- class GrpcBackoff
29
- class << self
30
- ##
31
- # The number of times a retriable API call should be retried.
32
- #
33
- # The default value is `3`.
34
- attr_reader :retries
35
- def retries= new_retries
36
- @retries = new_retries
37
- end
38
-
39
- ##
40
- # The GRPC Status Codes that should be retried.
41
- #
42
- # The default values are `14`.
43
- attr_accessor :grpc_codes
44
-
45
- ##
46
- # The code to run when a backoff is handled.
47
- # This must be a Proc and must take the number of
48
- # retries as an argument.
49
- #
50
- # Note: This method is undocumented and may change.
51
- attr_accessor :backoff # :nodoc:
52
- end
53
- # Set the default values
54
- self.retries = 3
55
- self.grpc_codes = [14]
56
- self.backoff = ->(retries) { sleep retries.to_i }
57
-
58
- ##
59
- # @private
60
- # Creates a new Backoff object to catch common errors when calling
61
- # the Google API and handle the error by retrying the call.
62
- #
63
- # Google::Cloud::Core::GrpcBackoff.new(options).execute do
64
- # datastore.lookup lookup_ref
65
- # end
66
- def initialize options = {}
67
- @retries = (options[:retries] || GrpcBackoff.retries).to_i
68
- @grpc_codes = (options[:grpc_codes] || GrpcBackoff.grpc_codes).to_a
69
- @backoff = options[:backoff] || GrpcBackoff.backoff
70
- end
71
-
72
- # @private
73
- def execute
74
- current_retries = 0
75
- loop do
76
- begin
77
- return yield
78
- rescue GRPC::BadStatus => e
79
- raise e unless @grpc_codes.include?(e.code) &&
80
- (current_retries < @retries)
81
- current_retries += 1
82
- @backoff.call current_retries
83
- end
84
- end
85
- end
86
- end
87
- end
88
- end
89
- end