gcp_data 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 398e39c8a777684e03619a5941d86c1f2b683093919d5c8d3bf14a411078b375
4
- data.tar.gz: f1ffef56aa9e2281c0415f161b30542dd21c1fdfa73356f5a21e4bf1a4f230bc
3
+ metadata.gz: 1e4fe524be4163e8b796361a35270f4e0230f1908b1380d58d17063ec5dbb01b
4
+ data.tar.gz: 0fb3fde5e05fb32529666d1b83eaa50fbef7db2c40ba15cfd28a9c89cbc4c2d0
5
5
  SHA512:
6
- metadata.gz: 84e03b633b383e974a172816f417d7aa3b748d579ae0c9f03ee258b0f2828e9ff42da2b167afb4c076f7fcc97bd626c5cf5845096ddac277274073ee60872ccf
7
- data.tar.gz: 21fa79d030a23a613707f842144e21bbbeba9d3b97cb855ff5062a95af3f655670bf4161a76e78fe54cf6ecf7597f40d544d89d27ed6eccbc9f6ff9808556425
6
+ metadata.gz: 2f82401965e69cf29d9b18e5b3956c305b4f4c033eb31a63680193917e9ee93b34620b3830c980d9cea51bd05a5d3441e23287e392279857bba4e895aba094b9
7
+ data.tar.gz: db5b4acea8d9dcda32b74fc4735a5f5919e383d69bf32f70abdd0cbb9b53fb459ff8a07b9c24af61abff441d54b77404c0aaf52b303dfa226d85fdf98f03bf69
data/CHANGELOG.md CHANGED
@@ -3,6 +3,10 @@
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.1] - 2021-12-27
7
+ - [#2](https://github.com/tongueroo/gcp_data/pull/2) friendly error message when gcloud cli not installed
8
+ - [#3](https://github.com/tongueroo/gcp_data/pull/3) autoset GOOGLE_PROJECT GOOGLE_REGION GOOGLE_ZONE from gcloud config o…
9
+
6
10
  ## [0.2.0]
7
11
  - #1 additional precedence lookups
8
12
 
data/README.md CHANGED
@@ -29,11 +29,11 @@ GcpData.region
29
29
 
30
30
  ## Precedence
31
31
 
32
- This library will return prjoect and region info using different sources with this precedence:
32
+ This library will return project and region info using different sources with this precedence:
33
33
 
34
34
  1. Environment variables: GOOGLE_PROJECT, GOOGLE_REGION, GOOGLE_ZONE
35
35
  2. Google Credentials file: only project id available from the GOOGLE\_APPLICATION_CREDENTIALS file
36
- 3. CLI: gcloud
36
+ 3. CLI: gcloud: project, region, and zone available
37
37
  4. Defaults: region=us-central1 and zone==us-central1a
38
38
 
39
39
  ### 1. Environment variables
@@ -68,11 +68,11 @@ The commands saves to a file in ~/.config/gcloud. The file looks something like
68
68
 
69
69
  ### 4. Defaults
70
70
 
71
- The library will fallback to default values when it's unable to lookup the region and zone. The default values are:
71
+ The library will fall back to default values when it's unable to lookup the region and zone. The default values are:
72
72
 
73
73
  region=us-central1
74
74
  zone==us-central1a
75
75
 
76
76
  ## Contributing
77
77
 
78
- Bug reports and pull requests are welcome on GitHub at https://github.com/tongueroo/gcp_data.
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/boltops-tools/gcp_data.
@@ -1,3 +1,3 @@
1
1
  module GcpData
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/gcp_data.rb CHANGED
@@ -6,17 +6,17 @@ module GcpData
6
6
  extend Memoist
7
7
 
8
8
  def project
9
- ENV['GOOGLE_PROJECT'] || creds("project_id") || gcloud_config("core/project") || raise("Unable to look up google project_id")
9
+ ENV['GOOGLE_PROJECT'] ||= gcloud_config("core/project") || creds("project_id") || raise("Unable to look up google project_id")
10
10
  end
11
11
  memoize :project
12
12
 
13
13
  def region
14
- ENV['GOOGLE_REGION'] || gcloud_config("compute/region") || 'us-central1'
14
+ ENV['GOOGLE_REGION'] ||= gcloud_config("compute/region") || 'us-central1'
15
15
  end
16
16
  memoize :region
17
17
 
18
18
  def zone
19
- ENV['GOOGLE_ZONE'] || gcloud_config("compute/zone") || 'us-central1a'
19
+ ENV['GOOGLE_ZONE'] ||= gcloud_config("compute/zone") || 'us-central1a'
20
20
  end
21
21
  memoize :zone
22
22
 
@@ -31,15 +31,67 @@ module GcpData
31
31
  memoize :credentials
32
32
 
33
33
  def gcloud_config(key)
34
- check_gcloud_installed!
35
- val = `gcloud config get-value #{key}`.strip
36
- val unless val == ''
34
+ if gcloud_installed?
35
+ # redirect stderr to stdout because (unset) is printed to stderr when a value is not set. IE:
36
+ #
37
+ # $ gcloud config get-value compute/region
38
+ # (unset)
39
+ #
40
+ command = "gcloud config get-value #{key} 2>&1"
41
+ puts "RUNNING: #{command}" if ENV['GCP_DATA_DEBUG']
42
+ val = `#{command}`.strip
43
+ val unless ['', '(unset)'].include?(val)
44
+ elsif !env_vars_set?
45
+ error_message
46
+ end
37
47
  end
38
48
 
39
- def check_gcloud_installed!
40
- installed = system("type gcloud > /dev/null 2>&1")
41
- return if installed
42
- raise Error.new("ERROR: gcloud is not installed. Please install the gcloud command.")
49
+ def gcloud_installed?
50
+ system("type gcloud > /dev/null 2>&1")
51
+ end
52
+
53
+ def env_vars
54
+ %w[
55
+ GOOGLE_APPLICATION_CREDENTIALS
56
+ GOOGLE_PROJECT
57
+ GOOGLE_REGION
58
+ ]
59
+ end
60
+
61
+ def env_vars_set?
62
+ env_vars.all? { |var| ENV[var] }
63
+ end
64
+
65
+ def error_message(message=nil)
66
+ all_vars = format_list(env_vars)
67
+ unset_vars = format_list(env_vars.reject { |var| ENV[var] })
68
+ message ||= <<~EOL
69
+ ERROR: gcloud is not installed. Please install the gcloud CLI and configure it.
70
+ GcpData uses it to detect google cloud project, region, zone.
71
+
72
+ You can also configure these environment variables instead of installing the google CLI.
73
+
74
+ #{all_vars}
75
+
76
+ Currently, the unset vars are:
77
+
78
+ #{unset_vars}
79
+
80
+ EOL
81
+ show_error(message)
82
+ end
83
+
84
+ def show_error(message)
85
+ if ENV['GCP_DATA_RAISE_ERROR']
86
+ raise Error.new(message)
87
+ else
88
+ puts message
89
+ exit 1
90
+ end
91
+ end
92
+
93
+ def format_list(vars)
94
+ vars.map { |var| " #{var}" }.join("\n")
43
95
  end
44
96
 
45
97
  extend self
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.2.0
4
+ version: 0.2.1
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-07-03 00:00:00.000000000 Z
11
+ date: 2021-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: memoist
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
66
  requirements: []
67
- rubygems_version: 3.1.2
67
+ rubygems_version: 3.2.32
68
68
  signing_key:
69
69
  specification_version: 4
70
70
  summary: Simple module to get current gcp data project and region