azure_info 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/azure_info/account.rb +19 -15
- data/lib/azure_info/base.rb +54 -0
- data/lib/azure_info/configure.rb +5 -51
- data/lib/azure_info/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 737b4ad017cbfce9e30a1d2eb1ca407c83b6c11197c676e9ec749b6d070ccea0
|
4
|
+
data.tar.gz: f698f4d4cd5a08eb51e16a64009289d3be5a49aa1712c3b480736ffc0c51bf83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44d63cfc5298e5ddb81c1811231baa1c6285acfc23ee4f6a923166bac07e8c91c82c00a0ffedf63c21e26cffb9f2a89c5a4fa4713db63f6bc5acb88c2cdaf08a
|
7
|
+
data.tar.gz: 73c4d6bac182e50c17582d0446555a947889f4a12826959674d8c0fcb2d5d63b68da1e0d76e46bca80c94a500e606e9e03f0faf8e1a9e3f1d66f859439b4f715
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,9 @@
|
|
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
|
+
|
6
9
|
## [0.1.4] - 2021-12-27
|
7
10
|
- [#4](https://github.com/boltops-tools/azure_info/pull/4) improve error message when az cli not installed
|
8
11
|
|
data/lib/azure_info/account.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module AzureInfo
|
2
|
-
class Account
|
2
|
+
class Account < Base
|
3
3
|
def get(name)
|
4
4
|
az_account_show[name]
|
5
5
|
end
|
@@ -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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
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
|
-
|
46
|
+
error_message(message)
|
43
47
|
end
|
44
48
|
end
|
45
49
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module AzureInfo
|
2
|
+
class Base
|
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")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/azure_info/configure.rb
CHANGED
@@ -3,9 +3,9 @@
|
|
3
3
|
# az configure --defaults location=westus2 group=MyResourceGroup
|
4
4
|
#
|
5
5
|
module AzureInfo
|
6
|
-
class Configure
|
6
|
+
class Configure < Base
|
7
7
|
def get(name)
|
8
|
-
item =
|
8
|
+
item = az_configure.find do |i|
|
9
9
|
i["name"] == name
|
10
10
|
end
|
11
11
|
item["value"] if item
|
@@ -13,11 +13,11 @@ module AzureInfo
|
|
13
13
|
|
14
14
|
private
|
15
15
|
# looks like az configure stores settings in ~/.azure/config
|
16
|
-
def
|
16
|
+
def az_configure
|
17
17
|
if az_installed?
|
18
|
-
return @
|
18
|
+
return @az_configure if @az_configure
|
19
19
|
out = `az configure --list-defaults --output json`.strip
|
20
|
-
@
|
20
|
+
@az_configure = JSON.load(out)
|
21
21
|
else
|
22
22
|
if env_vars_set?
|
23
23
|
[]
|
@@ -26,51 +26,5 @@ module AzureInfo
|
|
26
26
|
end
|
27
27
|
end
|
28
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")
|
74
|
-
end
|
75
29
|
end
|
76
30
|
end
|
data/lib/azure_info/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tung Nguyen
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- bin/setup
|
30
30
|
- lib/azure_info.rb
|
31
31
|
- lib/azure_info/account.rb
|
32
|
+
- lib/azure_info/base.rb
|
32
33
|
- lib/azure_info/configure.rb
|
33
34
|
- lib/azure_info/version.rb
|
34
35
|
homepage: https://github.com/boltops-tools/azure_info
|