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 +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +29 -4
- data/lib/azure_info/account.rb +32 -2
- data/lib/azure_info/configure.rb +61 -7
- data/lib/azure_info/version.rb +1 -1
- data/lib/azure_info.rb +6 -4
- metadata +4 -4
- data/lib/azure_info/base.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4ae9c9490f68fe1f75f04f8c4ad05adb4c4c3ddbe4723563e33739723c13f64
|
4
|
+
data.tar.gz: f463b07527a94b6851fefa659e568a9d4d59c1d0496dd136e62067a8bdf8d4f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
3
|
+
[](https://badge.fury.io/rb/azure_info)
|
4
|
+
|
5
|
+
[](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
|
-
##
|
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
|
|
data/lib/azure_info/account.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module AzureInfo
|
2
|
-
class Account
|
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
|
-
|
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
|
data/lib/azure_info/configure.rb
CHANGED
@@ -3,20 +3,74 @@
|
|
3
3
|
# az configure --defaults location=westus2 group=MyResourceGroup
|
4
4
|
#
|
5
5
|
module AzureInfo
|
6
|
-
class Configure
|
6
|
+
class Configure
|
7
7
|
def get(name)
|
8
|
-
item =
|
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
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
data/lib/azure_info/version.rb
CHANGED
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") || "
|
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.
|
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:
|
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.
|
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,
|