gcp_data 0.1.1 → 0.2.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
  SHA256:
3
- metadata.gz: 520c99905f33a6488c28287cff6c17066f2872ca0dd6a1e77d3aae4e22fc772e
4
- data.tar.gz: 1b5db4f3582ee307b18185a3c9fc1bbaf5e4fc08995b47c475040939cbb1baa1
3
+ metadata.gz: 398e39c8a777684e03619a5941d86c1f2b683093919d5c8d3bf14a411078b375
4
+ data.tar.gz: f1ffef56aa9e2281c0415f161b30542dd21c1fdfa73356f5a21e4bf1a4f230bc
5
5
  SHA512:
6
- metadata.gz: b5c972fe8d61620b514ae343c06965fde8f3ae210a3c0e9afd6fd55ed99169163904fd9d251302af4e1c95ae2767401a1feb896bfd06ef70c3c53fc819bdfec6
7
- data.tar.gz: f98fa34a9db7daf5cd2213fe818938f8a21816c0375afe67663e54f11d015a89b7ac889c38eb91a879390d07c88d6edb1d350aed286f95419e881d45cfcb8813
6
+ metadata.gz: 84e03b633b383e974a172816f417d7aa3b748d579ae0c9f03ee258b0f2828e9ff42da2b167afb4c076f7fcc97bd626c5cf5845096ddac277274073ee60872ccf
7
+ data.tar.gz: 21fa79d030a23a613707f842144e21bbbeba9d3b97cb855ff5062a95af3f655670bf4161a76e78fe54cf6ecf7597f40d544d89d27ed6eccbc9f6ff9808556425
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /Gemfile.lock
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
@@ -3,8 +3,11 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## [0.2.0]
7
+ - #1 additional precedence lookups
8
+
6
9
  ## [0.1.1]
7
- - remote puts hello debug
10
+ - remove puts hello debug
8
11
 
9
12
  ## [0.1.0]
10
13
  - Initial release
data/README.md CHANGED
@@ -18,19 +18,61 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install gcp_data
20
20
 
21
- Currently, requires the `gcloud` command to be installed.
21
+ This library can rely on the `gcloud` command, so it should be installed.
22
22
 
23
23
  ## Usage
24
24
 
25
- ```
25
+ ```ruby
26
26
  GcpData.project
27
27
  GcpData.region
28
28
  ```
29
29
 
30
- ## Contributing
30
+ ## Precedence
31
+
32
+ This library will return prjoect and region info using different sources with this precedence:
33
+
34
+ 1. Environment variables: GOOGLE_PROJECT, GOOGLE_REGION, GOOGLE_ZONE
35
+ 2. Google Credentials file: only project id available from the GOOGLE\_APPLICATION_CREDENTIALS file
36
+ 3. CLI: gcloud
37
+ 4. Defaults: region=us-central1 and zone==us-central1a
38
+
39
+ ### 1. Environment variables
40
+
41
+ The environment variables take the highest precedence: GOOGLE_PROJECT, GOOGLE_REGION, GOOGLE_ZONE
42
+
43
+ ### 2. Google credentials file
44
+
45
+ You can also authenticate to the Google API by setting a GOOGLE\_APPLICATION_CREDENTIALS env var that points to file a JSON file on your system. Example: `GOOGLE_APPLICATION_CREDENTIALS=~/.gcp/credentials.json`. More info at google docs: [Getting Started with Authentication](https://cloud.google.com/docs/authentication/getting-started).
46
+
47
+ This file contains a project_id key. So if you have set the GOOGLE\_APPLICATION_CREDENTIALS and not set the GOOGLE_PROJECT var, then this library will use the project_id from the GOOGLE\_APPLICATION_CREDENTIALS file.
31
48
 
32
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gcp_data.
49
+ ### 3. CLI: gcloud
33
50
 
34
- ## License
51
+ The gcloud cli can also be used to set and get google project and region info. Here's a cheatsheet of the commands:
52
+
53
+ gcloud config list
54
+ gcloud config set project project-123
55
+ gcloud config set compute/region us-central1
56
+ gcloud config set compute/zone us-central1-b
57
+
58
+ The commands saves to a file in ~/.config/gcloud. The file looks something like this:
59
+
60
+ ~/.config/gcloud/configurations/config_default
61
+
62
+ [core]
63
+ project = project-12345
64
+
65
+ [compute]
66
+ region = us-central1
67
+ zone = us-central1-a
68
+
69
+ ### 4. Defaults
70
+
71
+ The library will fallback to default values when it's unable to lookup the region and zone. The default values are:
72
+
73
+ region=us-central1
74
+ zone==us-central1a
75
+
76
+ ## Contributing
35
77
 
36
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tongueroo/gcp_data.
@@ -1,3 +1,4 @@
1
+ require "json"
1
2
  require "memoist"
2
3
 
3
4
  module GcpData
@@ -5,18 +6,34 @@ module GcpData
5
6
  extend Memoist
6
7
 
7
8
  def project
8
- gcloud_config("core/project")
9
+ ENV['GOOGLE_PROJECT'] || creds("project_id") || gcloud_config("core/project") || raise("Unable to look up google project_id")
9
10
  end
10
11
  memoize :project
11
12
 
12
13
  def region
13
- gcloud_config("compute/region")
14
+ ENV['GOOGLE_REGION'] || gcloud_config("compute/region") || 'us-central1'
14
15
  end
15
16
  memoize :region
16
17
 
18
+ def zone
19
+ ENV['GOOGLE_ZONE'] || gcloud_config("compute/zone") || 'us-central1a'
20
+ end
21
+ memoize :zone
22
+
23
+ def creds(name)
24
+ credentials[name] if credentials
25
+ end
26
+
27
+ def credentials
28
+ path = ENV['GOOGLE_APPLICATION_CREDENTIALS']
29
+ JSON.load(IO.read(path)) if path && File.exist?(path)
30
+ end
31
+ memoize :credentials
32
+
17
33
  def gcloud_config(key)
18
34
  check_gcloud_installed!
19
- `gcloud config get-value #{key}`.strip
35
+ val = `gcloud config get-value #{key}`.strip
36
+ val unless val == ''
20
37
  end
21
38
 
22
39
  def check_gcloud_installed!
@@ -1,3 +1,3 @@
1
1
  module GcpData
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcp_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-13 00:00:00.000000000 Z
11
+ date: 2020-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: memoist
@@ -36,7 +36,6 @@ files:
36
36
  - ".travis.yml"
37
37
  - CHANGELOG.md
38
38
  - Gemfile
39
- - Gemfile.lock
40
39
  - LICENSE.txt
41
40
  - README.md
42
41
  - Rakefile
@@ -1,36 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- gcp_data (0.1.0)
5
- memoist
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- diff-lcs (1.3)
11
- memoist (0.16.2)
12
- rake (12.3.3)
13
- rspec (3.9.0)
14
- rspec-core (~> 3.9.0)
15
- rspec-expectations (~> 3.9.0)
16
- rspec-mocks (~> 3.9.0)
17
- rspec-core (3.9.2)
18
- rspec-support (~> 3.9.3)
19
- rspec-expectations (3.9.2)
20
- diff-lcs (>= 1.2.0, < 2.0)
21
- rspec-support (~> 3.9.0)
22
- rspec-mocks (3.9.1)
23
- diff-lcs (>= 1.2.0, < 2.0)
24
- rspec-support (~> 3.9.0)
25
- rspec-support (3.9.3)
26
-
27
- PLATFORMS
28
- ruby
29
-
30
- DEPENDENCIES
31
- gcp_data!
32
- rake (~> 12.0)
33
- rspec (~> 3.0)
34
-
35
- BUNDLED WITH
36
- 2.1.4