azure_info 0.1.0 → 0.1.4

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: f54bada299a0f5e98838be7c05dab9de5d7c721f1a4934a1f08a2a94c9d15f2c
4
- data.tar.gz: 7e946eddcb904a1f352e50a8f1a7ca709263dd4b9343eb25fae4150d18602b6f
3
+ metadata.gz: e4ae9c9490f68fe1f75f04f8c4ad05adb4c4c3ddbe4723563e33739723c13f64
4
+ data.tar.gz: f463b07527a94b6851fefa659e568a9d4d59c1d0496dd136e62067a8bdf8d4f3
5
5
  SHA512:
6
- metadata.gz: 1e3f1049ebcae5a4ec412a1e65f9df3245cfcecd9704a3eaedf9eb75351f813ac3f8a2c9a672720ae24ff8d25cff94b80fd14e1017445c0aecd8b0ac5eead993
7
- data.tar.gz: 1d49d741021fdda2b73a70544dda901f4856e1a0a613edbcd77f7cfa64f3ec7cc177bcf9e554ef94f8c88aef2b54f7c8a50d669337296b13d77feb8e05e90f0d
6
+ metadata.gz: 5ea982a5564f29622a740324a6459ea316f4b174ef6723e4e1859ea6f8ed759909a930dda066d3680989972d2358a721e3d2ab57c28c62db6c554fce155c4b57
7
+ data.tar.gz: a82b55909878ea8bf6a00db2829762fb6c0632c0ef9d59b336cd8c8bc6ffa087526a52aacca18a883ba416b742be8ebb8ed6c09ba754fde4997127e33cde865f
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
+
6
+ ## [0.1.4] - 2021-12-27
7
+ - [#4](https://github.com/boltops-tools/azure_info/pull/4) improve error message when az cli not installed
8
+
9
+ ## [0.1.3] - 2021-12-27
10
+ - [#3](https://github.com/boltops-tools/azure_info/pull/3) improve error message when az cli not installed
11
+ - comment about what is used in the account and configure object
12
+
13
+ ## [0.1.2] - 2021-04-15
14
+ - [#2](https://github.com/boltops-tools/azure_info/pull/2) consider env vars also
15
+
16
+ ## [0.1.1]
17
+ - fix default location
18
+ - provide friendly error message when az not returning json
19
+
20
+ ## [0.1.0]
21
+ - Initial release.
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # AzureInfo
2
2
 
3
- Simple library to get current gcp data like subscription_id, tenant_id, group, and location.
3
+ [![Gem Version](https://badge.fury.io/rb/azure_info.svg)](https://badge.fury.io/rb/azure_info)
4
+
5
+ [![BoltOps Badge](https://img.boltops.com/boltops/badges/boltops-badge.png)](https://www.boltops.com)
6
+
7
+ Simple library to get current Azure info like subscription_id, tenant_id, group, and location.
4
8
 
5
9
  ## Installation
6
10
 
@@ -31,12 +35,33 @@ AzureInfo.tenant_id
31
35
 
32
36
  This tool calls out to the az CLI. So the az CLI is required.
33
37
 
34
- ## 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
35
61
 
36
62
  az login --username EMAIL_ADDRESS -t TENANT_ID
37
63
  az account set --subscription SUBSCRIPTION_ID
38
-
39
- az configure --defaults location=useast group=RESOURCE_GROUP
64
+ az configure --defaults location=eastus group=RESOURCE_GROUP
40
65
 
41
66
  ## Contributing
42
67
 
@@ -1,5 +1,5 @@
1
1
  module AzureInfo
2
- class Account < Base
2
+ class Account
3
3
  def get(name)
4
4
  az_account_show[name]
5
5
  end
@@ -8,8 +8,38 @@ module AzureInfo
8
8
  def az_account_show
9
9
  return @az_account_show if @az_account_show
10
10
  check_az_installed!
11
- out = `az account show --output json`.strip
11
+
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
+
12
18
  @az_account_show = JSON.load(out)
13
19
  end
20
+
21
+ def raise_status_error(command)
22
+ message =<<~EOL
23
+ ERROR: error running '#{command}'.
24
+ Maybe you havent ran `az login` or configured ~/.azure manually.
25
+ You can configure az login non-interactively with
26
+
27
+ az login --service-principal \
28
+ --username USERNAME \
29
+ --password PASSWORD \
30
+ --tenant TENANT
31
+
32
+ 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
+ EOL
34
+ raise Error.new(message)
35
+ end
36
+
37
+ def raise_empty_error(command)
38
+ message =<<~EOL
39
+ The '#{command}' return a blank string.
40
+ Something went wrong. Try running it manually and confirm it returns json.
41
+ EOL
42
+ raise Error.new(message)
43
+ end
14
44
  end
15
45
  end
@@ -3,20 +3,74 @@
3
3
  # az configure --defaults location=westus2 group=MyResourceGroup
4
4
  #
5
5
  module AzureInfo
6
- class Configure < Base
6
+ class Configure
7
7
  def get(name)
8
- item = az_configure_defaults.find do |i|
8
+ item = azure_configs.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 azure_configs
17
+ if az_installed?
18
+ return @azure_configs if @azure_configs
19
+ out = `az configure --list-defaults --output json`.strip
20
+ @azure_configs = JSON.load(out)
21
+ else
22
+ if env_vars_set?
23
+ []
24
+ else
25
+ error_message
26
+ end
27
+ end
28
+ end
29
+
30
+ def az_installed?
31
+ system("type az > /dev/null 2>&1")
32
+ end
33
+
34
+ def env_vars
35
+ %w[
36
+ ARM_CLIENT_ID
37
+ ARM_CLIENT_SECRET
38
+ ARM_LOCATION
39
+ ARM_SUBSCRIPTION_ID
40
+ ARM_TENANT_ID
41
+ ]
42
+ end
43
+
44
+ def env_vars_set?
45
+ env_vars.all? { |var| ENV[var] }
46
+ end
47
+
48
+ def error_message
49
+ all_vars = format_list(env_vars)
50
+ unset_vars = format_list(env_vars.reject { |var| ENV[var] })
51
+ message = <<~EOL
52
+ ERROR: az is not installed. Please install the az command and configure it.
53
+ AzureInfo uses it to detect azure resource group, location, subscription id, and tenant id.
54
+
55
+ You can also configure these environment variables instead of installing the az cli.
56
+
57
+ #{all_vars}
58
+
59
+ Currently, the unset vars are:
60
+
61
+ #{unset_vars}
62
+
63
+ EOL
64
+ if ENV['AZ_INFO_RAISE_ERROR']
65
+ raise Error.new(message)
66
+ else
67
+ puts message
68
+ exit 1
69
+ end
70
+ end
71
+
72
+ def format_list(vars)
73
+ vars.map { |var| " #{var}" }.join("\n")
20
74
  end
21
75
  end
22
76
  end
@@ -1,3 +1,3 @@
1
1
  module AzureInfo
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.4"
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") || "useast"
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.0
4
+ version: 0.1.4
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-03 00:00:00.000000000 Z
11
+ date: 2021-12-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - ".gitignore"
21
21
  - ".rspec"
22
+ - CHANGELOG.md
22
23
  - Gemfile
23
24
  - LICENSE.txt
24
25
  - README.md
@@ -28,7 +29,6 @@ files:
28
29
  - bin/setup
29
30
  - lib/azure_info.rb
30
31
  - lib/azure_info/account.rb
31
- - lib/azure_info/base.rb
32
32
  - lib/azure_info/configure.rb
33
33
  - lib/azure_info/version.rb
34
34
  homepage: https://github.com/boltops-tools/azure_info
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubygems_version: 3.1.2
54
+ rubygems_version: 3.2.32
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: 'Azure info: Simple library to get current subscription, resource group,
@@ -1,9 +0,0 @@
1
- module AzureInfo
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.")
7
- end
8
- end
9
- end