azure_info 0.1.1 → 0.1.5

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: '08572df2ac88f55e30953102b474fdc5bf73335d969a07a1648eeea184292fce'
4
- data.tar.gz: 906aba07a7070e7723ff509d29dfd31326111d57b2b17f6eec959fe3212c384d
3
+ metadata.gz: 737b4ad017cbfce9e30a1d2eb1ca407c83b6c11197c676e9ec749b6d070ccea0
4
+ data.tar.gz: f698f4d4cd5a08eb51e16a64009289d3be5a49aa1712c3b480736ffc0c51bf83
5
5
  SHA512:
6
- metadata.gz: d3a57a19786c6b412838cd6426ce4d30f1ea2e7406947d991c5607b1356e2d9836711ac2f78f8b2fa0591d5eaa18535caaaffb0ff5199d530d1714fc6c1848b4
7
- data.tar.gz: b8cf86bf3740b9b08c964d36b9628f9cd23537fb6e67b689148e259b1e3d91534886e3ac2d5f9264ce97ca9e484fde3d7a9afb6c0b8d50a4c71aa6682622d0ff
6
+ metadata.gz: 44d63cfc5298e5ddb81c1811231baa1c6285acfc23ee4f6a923166bac07e8c91c82c00a0ffedf63c21e26cffb9f2a89c5a4fa4713db63f6bc5acb88c2cdaf08a
7
+ data.tar.gz: 73c4d6bac182e50c17582d0446555a947889f4a12826959674d8c0fcb2d5d63b68da1e0d76e46bca80c94a500e606e9e03f0faf8e1a9e3f1d66f859439b4f715
data/CHANGELOG.md CHANGED
@@ -3,6 +3,19 @@
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/), even before v1.0.
5
5
 
6
+ ## [0.1.5] - 2021-12-27
7
+ - [#5](https://github.com/boltops-tools/azure_info/pull/5) improve error message when az cli not installed
8
+
9
+ ## [0.1.4] - 2021-12-27
10
+ - [#4](https://github.com/boltops-tools/azure_info/pull/4) improve error message when az cli not installed
11
+
12
+ ## [0.1.3] - 2021-12-27
13
+ - [#3](https://github.com/boltops-tools/azure_info/pull/3) improve error message when az cli not installed
14
+ - comment about what is used in the account and configure object
15
+
16
+ ## [0.1.2] - 2021-04-15
17
+ - [#2](https://github.com/boltops-tools/azure_info/pull/2) consider env vars also
18
+
6
19
  ## [0.1.1]
7
20
  - fix default location
8
21
  - provide friendly error message when az not returning json
data/README.md CHANGED
@@ -35,11 +35,32 @@ AzureInfo.tenant_id
35
35
 
36
36
  This tool calls out to the az CLI. So the az CLI is required.
37
37
 
38
- ## Setting the Defaults with az
38
+ ## Precedence
39
+
40
+ This library will return values using different sources with this precedence:
41
+
42
+ 1. Environment variables: ARM_GROUP, ARM_LOCATION, ARM_SUBSCRIPTION_ID, ARM_TENANT_ID
43
+ 2. az cli: Usually configured in the `~/.azure/config`. Use `az configure --list-defaults` to double check.
44
+ 3. Defaults: A default is set for `location=eastus`. The other values must be configured.
45
+
46
+ The config file used by the `az` command looks something like this:
47
+
48
+ ~/.azure/config
49
+
50
+ [cloud]
51
+ name = AzureCloud
52
+
53
+ [defaults]
54
+ location = eastus
55
+
56
+ This command is also useful:
57
+
58
+ az account show
59
+
60
+ ## Setting the Defaults with az cli
39
61
 
40
62
  az login --username EMAIL_ADDRESS -t TENANT_ID
41
63
  az account set --subscription SUBSCRIPTION_ID
42
-
43
64
  az configure --defaults location=eastus group=RESOURCE_GROUP
44
65
 
45
66
  ## Contributing
@@ -7,15 +7,22 @@ module AzureInfo
7
7
  # looks like az configure stores settings in ~/.azure/config
8
8
  def az_account_show
9
9
  return @az_account_show if @az_account_show
10
- check_az_installed!
11
10
 
12
- command = "az account show --output json"
13
- out = `#{command}`.strip
14
-
15
- raise_status_error(command) unless $?.success?
16
- raise_empty_error(command) if out.strip == ""
17
-
18
- @az_account_show = JSON.load(out)
11
+ if az_installed?
12
+ command = "az account show --output json"
13
+ out = `#{command}`.strip
14
+
15
+ raise_status_error(command) unless $?.success?
16
+ raise_empty_error(command) if out.strip == ""
17
+
18
+ @az_account_show = JSON.load(out)
19
+ else
20
+ if env_vars_set?
21
+ {}
22
+ else
23
+ error_message
24
+ end
25
+ end
19
26
  end
20
27
 
21
28
  def raise_status_error(command)
@@ -24,14 +31,11 @@ module AzureInfo
24
31
  Maybe you havent ran `az login` or configured ~/.azure manually.
25
32
  You can configure az login non-interactively with
26
33
 
27
- az login --service-principal \
28
- --username USERNAME \
29
- --password PASSWORD \
30
- --tenant TENANT
34
+ az login --service-principal --username USERNAME --password PASSWORD --tenant TENANT
31
35
 
32
36
  Per https://docs.microsoft.com/en-us/cli/azure/create-an-azure-service-principal-azure-cli?view=azure-cli-latest#sign-in-using-a-service-principal
33
37
  EOL
34
- raise Error.new(message)
38
+ error_message(message)
35
39
  end
36
40
 
37
41
  def raise_empty_error(command)
@@ -39,7 +43,7 @@ module AzureInfo
39
43
  The '#{command}' return a blank string.
40
44
  Something went wrong. Try running it manually and confirm it returns json.
41
45
  EOL
42
- raise Error.new(message)
46
+ error_message(message)
43
47
  end
44
48
  end
45
49
  end
@@ -1,9 +1,54 @@
1
1
  module AzureInfo
2
2
  class Base
3
- def check_az_installed!
4
- installed = system("type az > /dev/null 2>&1")
5
- return if installed
6
- raise Error.new("ERROR: az is not installed. Please install the az command.")
3
+ def az_installed?
4
+ system("type az > /dev/null 2>&1")
5
+ end
6
+
7
+ def env_vars
8
+ %w[
9
+ ARM_CLIENT_ID
10
+ ARM_CLIENT_SECRET
11
+ ARM_LOCATION
12
+ ARM_SUBSCRIPTION_ID
13
+ ARM_TENANT_ID
14
+ ARM_ACCOUNT
15
+ ]
16
+ end
17
+
18
+ def env_vars_set?
19
+ env_vars.all? { |var| ENV[var] }
20
+ end
21
+
22
+ def error_message(message=nil)
23
+ all_vars = format_list(env_vars)
24
+ unset_vars = format_list(env_vars.reject { |var| ENV[var] })
25
+ message ||= <<~EOL
26
+ ERROR: az is not installed. Please install the az command and configure it.
27
+ AzureInfo uses it to detect azure resource group, location, subscription id, and tenant id.
28
+
29
+ You can also configure these environment variables instead of installing the az cli.
30
+
31
+ #{all_vars}
32
+
33
+ Currently, the unset vars are:
34
+
35
+ #{unset_vars}
36
+
37
+ EOL
38
+ show_error(message)
39
+ end
40
+
41
+ def show_error(message)
42
+ if ENV['AZ_INFO_RAISE_ERROR']
43
+ raise Error.new(message)
44
+ else
45
+ puts message
46
+ exit 1
47
+ end
48
+ end
49
+
50
+ def format_list(vars)
51
+ vars.map { |var| " #{var}" }.join("\n")
7
52
  end
8
53
  end
9
54
  end
@@ -5,18 +5,26 @@
5
5
  module AzureInfo
6
6
  class Configure < Base
7
7
  def get(name)
8
- item = az_configure_defaults.find do |i|
8
+ item = az_configure.find do |i|
9
9
  i["name"] == name
10
10
  end
11
11
  item["value"] if item
12
12
  end
13
13
 
14
+ private
14
15
  # looks like az configure stores settings in ~/.azure/config
15
- def az_configure_defaults
16
- return @az_configure_defaults if @az_configure_defaults
17
- check_az_installed!
18
- out = `az configure --list-defaults --output json`.strip
19
- @az_configure_defaults = JSON.load(out)
16
+ def az_configure
17
+ if az_installed?
18
+ return @az_configure if @az_configure
19
+ out = `az configure --list-defaults --output json`.strip
20
+ @az_configure = JSON.load(out)
21
+ else
22
+ if env_vars_set?
23
+ []
24
+ else
25
+ error_message
26
+ end
27
+ end
20
28
  end
21
29
  end
22
30
  end
@@ -1,3 +1,3 @@
1
1
  module AzureInfo
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/azure_info.rb CHANGED
@@ -8,29 +8,31 @@ module AzureInfo
8
8
  class Error < StandardError; end
9
9
 
10
10
  def group
11
- configure.get("group")
11
+ ENV['ARM_GROUP'] || configure.get("group")
12
12
  end
13
13
  alias_method :group_id, :group
14
14
 
15
15
  def location
16
- configure.get("location") || "eastus"
16
+ ENV['ARM_LOCATION'] || configure.get("location") || "eastus"
17
17
  end
18
18
 
19
19
  def subscription_id
20
- account.get("id")
20
+ ENV['ARM_SUBSCRIPTION_ID'] || account.get("id")
21
21
  end
22
22
  alias_method :subscription, :subscription_id
23
23
 
24
24
  def tenant_id
25
- account.get("tenantId")
25
+ ENV['ARM_TENANT_ID'] || account.get("tenantId")
26
26
  end
27
27
  alias_method :tenant, :tenant_id
28
28
 
29
29
  private
30
+ # info: group (resource group), location
30
31
  def configure
31
32
  @configure ||= Configure.new
32
33
  end
33
34
 
35
+ # info: id (subscription id), name (subscription name), tenantId
34
36
  def account
35
37
  @account ||= Account.new
36
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azure_info
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.5
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-06-04 00:00:00.000000000 Z
11
+ date: 2021-12-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  requirements: []
55
- rubygems_version: 3.1.2
55
+ rubygems_version: 3.2.32
56
56
  signing_key:
57
57
  specification_version: 4
58
58
  summary: 'Azure info: Simple library to get current subscription, resource group,