azure-armrest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8d37ff1ea6844a2ba05ee94cb4cb8a660f7a4de0
4
+ data.tar.gz: 5c517080720615271223342ac26279df490b753e
5
+ SHA512:
6
+ metadata.gz: ddf50c2759ae7f41cb610cc7ac14aa1459b01b1ee0ef9761183cfb9ebab182f086a87828d3bc751ad8dbfa2123c0e9bdcf019e227d5db22ec9e5b6851056c93b
7
+ data.tar.gz: 98ef0ba9a5a445c3828407071ba4287910cb5b6d888590f384051b4318b0bc317818d9f3a2e9654be916a553de3f56420587af663b9fc1dd1c4b026d4cedb00c
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0"
4
+ - "2.1"
5
+ - "2.2"
6
+ - ruby-head
7
+ - jruby-head
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
11
+ - rvm: jruby-head
12
+ fast_finish: true
data/CHANGES ADDED
@@ -0,0 +1,2 @@
1
+ = 0.1.0 - ???
2
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in azure-armrest.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,78 @@
1
+ ## Description
2
+
3
+ A Ruby interface for Azure using the new REST API.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/azure-armrest.svg)](http://badge.fury.io/rb/azure-armrest)
6
+ [![Build Status](https://travis-ci.org/ManageIQ/azure-armrest.svg)](https://travis-ci.org/ManageIQ/azure-armrest)
7
+ [![Code Climate](https://codeclimate.com/github/ManageIQ/azure-armrest/badges/gpa.svg)](https://codeclimate.com/github/ManageIQ/azure-armrest)
8
+ [![Test Coverage](https://codeclimate.com/github/ManageIQ/azure-armrest/badges/coverage.svg)](https://codeclimate.com/github/ManageIQ/azure-armrest/coverage)
9
+ [![Dependency Status](https://gemnasium.com/ManageIQ/azure-armrest.svg)](https://gemnasium.com/ManageIQ/azure-armrest)
10
+
11
+ ## Synopsis
12
+
13
+ ```ruby
14
+ require 'azure/armrest'
15
+
16
+ # Set things on a global level. All other objects will then use the
17
+ # information you set here.
18
+
19
+ Azure::Armrest::ArmrestManager.configure(
20
+ :client_id => 'XXXXX',
21
+ :client_key => 'YYYYY',
22
+ :tenant_id => 'ZZZZZ',
23
+ :subscription_id => 'ABCDEFG'
24
+ )
25
+
26
+ # This will then use the configuration info set above.
27
+ vmm = Azure::Armrest::VirtualMachineManager.new
28
+
29
+ # Alternatively you can set the configuration information on a per-instance
30
+ # basis if you need different credentials for different classes.
31
+ vmm = Azure::Armrest::VirtualMachineManager.new(
32
+ :client_id => 'XXXXX',
33
+ :client_key => 'YYYYY',
34
+ :tenant_id => 'ZZZZZ',
35
+ :subscription_id => 'ABCDEFG'
36
+ )
37
+
38
+ # Call this before making method calls if using per-instance configuration.
39
+ # This is not necessary if you set it via ArmrestManager.configure.
40
+ vmm.get_token
41
+
42
+ # Create a virtual machine
43
+ vmm.create_virtual_machine(
44
+ :name => 'some_vm',
45
+ :location => 'West US',
46
+ :vm_size => 'Standard_A1',
47
+ :computer_name => 'whatever',
48
+ :admin_username => 'admin_user',
49
+ :admin_password => 'adminxxxxxx',
50
+ :os_disk => {:name => 'disk_name1', :os_type => 'Linux', :caching => 'read'},
51
+ :data_disks => {:name => 'data_disk1', :lun => 0, :caching => 'read'}
52
+ )
53
+ ```
54
+
55
+ ## Tokens and methods
56
+
57
+ You will not be able to make any method calls until you first call the
58
+ get_token method.
59
+
60
+ ## Subscriptions
61
+
62
+ If you do not provide a subscription ID to the constructor, then the first
63
+ subscription ID returned from a REST call will be used.
64
+
65
+ ## Notes
66
+
67
+ Currently only the client credentials strategy is supported. Support for other
68
+ strategies may be added over time.
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
73
+
74
+ ## Authors
75
+
76
+ * Daniel Berger
77
+ * Bronagh Sorota
78
+
@@ -0,0 +1,20 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ namespace :spec do
7
+ namespace :armrest do
8
+ desc 'Run tests for the Armrest module'
9
+ RSpec::Core::RakeTask.new(:module) do |t|
10
+ t.pattern = ['spec/armrest_module_spec.rb']
11
+ end
12
+
13
+ desc 'Run tests for the Armrest::ArmrestManager base class'
14
+ RSpec::Core::RakeTask.new(:manager) do |t|
15
+ t.pattern = ['spec/armrest_manager_spec.rb']
16
+ end
17
+ end
18
+ end
19
+
20
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'azure/armrest/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'azure-armrest'
8
+ spec.version = Azure::Armrest::VERSION
9
+ spec.authors = ['Daniel J. Berger', 'Bronagh Sorota', 'Greg Blomquist']
10
+ spec.email = ['dberger@redhat.com', 'bsorota@redhat.com', 'gblomqui@redhat.com']
11
+
12
+ spec.summary = 'An interface for ARM/JSON Azure REST API'
13
+ spec.description = <<-EOF
14
+ This is a Ruby interface for Azure using the newer REST API. This is
15
+ different than the current azure gem, which uses the older (XML) interface
16
+ behind the scenes.
17
+ EOF
18
+ spec.homepage = 'http://github.com/ManageIQ/azure-armrest'
19
+ spec.license = 'Apache 2.0'
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "codeclimate-test-reporter"
30
+
31
+ spec.add_dependency 'json'
32
+ spec.add_dependency 'rest-client'
33
+ end
@@ -0,0 +1 @@
1
+ require 'azure/armrest'
@@ -0,0 +1,32 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'thread'
4
+
5
+ # The Azure module serves as a namespace.
6
+ module Azure
7
+
8
+ # The Armrest module mostly serves as a namespace, but also contains any
9
+ # common constants shared by subclasses.
10
+ module Armrest
11
+ # The default Azure resource
12
+ RESOURCE = "https://management.azure.com/"
13
+
14
+ # The default authority resource
15
+ AUTHORITY = "https://login.windows.net/"
16
+
17
+ # A common URI for all subclasses
18
+ COMMON_URI = RESOURCE + "subscriptions/"
19
+ end
20
+
21
+ end
22
+
23
+ require 'azure/armrest/version'
24
+ require 'azure/armrest/armrest_manager'
25
+ require 'azure/armrest/storage_account_manager'
26
+ require 'azure/armrest/availability_set_manager'
27
+ require 'azure/armrest/virtual_machine_manager'
28
+ require 'azure/armrest/virtual_machine_image_manager'
29
+ require 'azure/armrest/virtual_machine_extension_manager'
30
+ require 'azure/armrest/virtual_network_manager'
31
+ require 'azure/armrest/subnet_manager'
32
+ require 'azure/armrest/event_manager'
@@ -0,0 +1,402 @@
1
+ module Azure
2
+ module Armrest
3
+ # Abstract base class for the other manager classes.
4
+ class ArmrestManager
5
+
6
+ # The subscription ID (billing unit) for your Azure services
7
+ attr_accessor :subscription_id
8
+
9
+ # The resource group within the subscription.
10
+ attr_accessor :resource_group
11
+
12
+ # The API version of the REST interface. The default is 2015-1-1.
13
+ attr_accessor :api_version
14
+
15
+ # Base url used for REST calls.
16
+ attr_accessor :base_url
17
+
18
+ # The bearer token set in the constructor.
19
+ attr_accessor :token
20
+
21
+ # The content-type used for http requests.
22
+ attr_reader :content_type
23
+
24
+ # The accept value used for http requests.
25
+ attr_reader :accept
26
+
27
+ # The oauth2 strategy used for gathering the authentication token.
28
+ # The default is 'client_credentials'.
29
+ attr_reader :grant_type
30
+
31
+ VALID_OPTIONS = %[
32
+ client_id
33
+ client_key
34
+ tenant_id
35
+ subscription_id
36
+ resource_group
37
+ api_version
38
+ grant_type
39
+ content_type
40
+ accept
41
+ token
42
+ ]
43
+
44
+ @@client_id = nil
45
+ @@client_key = nil
46
+ @@tenant_id = nil
47
+ @@subscription_id = nil
48
+ @@resource_group = nil
49
+ @@api_version = '2015-01-01'
50
+ @@grant_type = 'client_credentials'
51
+ @@content_type = 'application/json'
52
+ @@accept = 'application/json'
53
+ @@token = nil
54
+ @@providers = {} # Set in constructor
55
+
56
+ # Set configuration options globally. If set globally you do not need to
57
+ # pass configuration options to individual manager classes.
58
+ #
59
+ # Possible options are:
60
+ #
61
+ # - client_id
62
+ # - client_key
63
+ # - tenant_id
64
+ # - subscription_id
65
+ # - resource_group
66
+ # - api_version
67
+ # - grant_type
68
+ # - content_type
69
+ # - accept
70
+ # - token
71
+ #
72
+ # Of these, you should include a client_id, client_key and tenant_id.
73
+ # The resource_group can be specified here, but many methods allow you
74
+ # to specify a resource group if you prefer flexibility.
75
+ #
76
+ # If no subscription_id is provided then this method will attempt to find
77
+ # a list of associated subscriptions and use the first one it finds as
78
+ # the default. If no associated subscriptions are found, an ArgumentError
79
+ # is raised.
80
+ #
81
+ # The other options (grant_type, content_type, accept, token, and
82
+ # api_version) should generally NOT be set by you except in specific
83
+ # circumstances. Setting them explicitly will likely cause breakage.
84
+ # The api_version will typically be overridden on a per-provider/resource
85
+ # basis within subclasses anyway.
86
+ #
87
+ # You may need to associate your application with a subscription using
88
+ # the new portal or the New-AzureRoleAssignment powershell command.
89
+ #
90
+ def self.configure(options)
91
+ options.each do |k,v|
92
+ raise ArgumentError, "Invalid key: '#{k}'" unless VALID_OPTIONS.include?(k.to_s)
93
+ eval("@@#{k} = v")
94
+ end
95
+
96
+ token_url = Azure::Armrest::AUTHORITY + @@tenant_id + "/oauth2/token"
97
+
98
+ response = RestClient.post(
99
+ token_url,
100
+ :grant_type => @@grant_type,
101
+ :client_id => @@client_id,
102
+ :client_secret => @@client_key,
103
+ :resource => Azure::Armrest::RESOURCE
104
+ )
105
+
106
+ @@token = 'Bearer ' + JSON.parse(response)['access_token']
107
+
108
+ # Automatically set a subscription ID if one is not specified.
109
+ unless @@subscription_id
110
+ url = File.join(Azure::Armrest::RESOURCE, "subscriptions?api-version=#{@@api_version}")
111
+
112
+ response = RestClient.get(
113
+ url,
114
+ :content_type => @@content_type,
115
+ :authorization => @@token,
116
+ )
117
+
118
+ hash = JSON.parse(response.body)["value"].first
119
+
120
+ if hash.empty?
121
+ raise ArgumentError, "No associated subscription found"
122
+ else
123
+ @@subscription_id = hash.fetch("subscriptionId")
124
+ end
125
+ end
126
+ end
127
+
128
+ # Do not instantiate directly. This is an abstract base class from which
129
+ # all other manager classes should subclass, and call super within their
130
+ # own constructors.
131
+ #
132
+ # The possible options to the constructor are:
133
+ #
134
+ # * subscription_id - Your Azure subscription ID. If no subscription
135
+ # is specifified, then information for all subscriptions will be
136
+ # collected.
137
+ #
138
+ # * resource_group - The resource group within the subscription. If no
139
+ # resource group is specified, then information for all resource
140
+ # groups will be gathered.
141
+ #
142
+ # * client_id - Your Azure client ID. Mandatory.
143
+ #
144
+ # * client_key - The key (secret) for your client ID. Mandatory.
145
+ #
146
+ # * tenant_id - Your Azure tenant ID. Mandatory.
147
+ #
148
+ # * api_version - The REST API version to use for internal REST calls.
149
+ # The default is '2015-01-01'. In some cases this value is ignored
150
+ # in order to get the most recently supported api-version string.
151
+ #
152
+ def initialize(options = {})
153
+ # Mandatory params
154
+ @client_id = @@client_id || options.fetch(:client_id)
155
+ @client_key = @@client_key || options.fetch(:client_key)
156
+ @tenant_id = @@tenant_id || options.fetch(:tenant_id)
157
+
158
+ # Optional params
159
+ @subscription_id = @@subscription_id || options[:subscription_id]
160
+ @resource_group = @@resource_group || options[:resource_group]
161
+ @api_version = @@api_version || options[:api_version] || '2015-01-01'
162
+ @grant_type = @@grant_type || options[:grant_type] || 'client_credentials'
163
+
164
+ # The content-type used for all internal http requests
165
+ @content_type = @@content_type || 'application/json'
166
+ @accept = @@accept || 'application/json'
167
+
168
+ # Call the get_token method to set this.
169
+ @token = @@token || options[:token]
170
+
171
+ # Base URL used for REST calls. Modify within method calls as needed.
172
+ @base_url = Azure::Armrest::RESOURCE
173
+
174
+ set_providers_info
175
+ end
176
+
177
+ # Gets an authentication token, which is then used for all other methods.
178
+ # This will also set the subscription_id to the first subscription found
179
+ # if you did not set it in the constructor.
180
+ #
181
+ # If you did not call the the ArmrestManager.configure method then you
182
+ # must call this before calling any other methods.
183
+ #
184
+ def get_token
185
+ return self if @@token || @token
186
+
187
+ token_url = Azure::Armrest::AUTHORITY + @tenant_id + "/oauth2/token"
188
+
189
+ resp = RestClient.post(
190
+ token_url,
191
+ :grant_type => @grant_type,
192
+ :client_id => @client_id,
193
+ :client_secret => @client_key,
194
+ :resource => Azure::Armrest::RESOURCE
195
+ )
196
+
197
+ @token = 'Bearer ' + JSON.parse(resp)['access_token']
198
+ @@token = @token
199
+
200
+ unless @subscription_id
201
+ @subscription_id = subscriptions.first['subscriptionId']
202
+ end
203
+
204
+ set_providers_info
205
+
206
+ self
207
+ end
208
+
209
+ # Returns a list of the available resource providers.
210
+ #
211
+ def providers
212
+ url = url_with_api_version(@@api_version, @base_url, 'providers')
213
+ resp = rest_get(url)
214
+ JSON.parse(resp.body)["value"]
215
+ end
216
+
217
+ # Returns information about the specific provider +namespace+.
218
+ #
219
+ def provider_info(provider)
220
+ url = url_with_api_version(@@api_version, @base_url, 'providers', provider)
221
+ response = rest_get(url)
222
+ JSON.parse(response.body)
223
+ end
224
+
225
+ alias geo_locations provider_info
226
+
227
+ # Returns a list of all locations for all resource types of the given
228
+ # +provider+. If you do not specify a provider, then the locations for
229
+ # all providers will be returned.
230
+ #
231
+ # If you need individual details on a per-provider basis, use the
232
+ # provider_info method instead.
233
+ #--
234
+ #
235
+ def locations(provider = nil)
236
+ array = []
237
+
238
+ if provider
239
+ @@providers[provider].each do |key, data|
240
+ array << data['locations']
241
+ end
242
+ else
243
+ @@providers.each do |provider, resource_types|
244
+ @@providers[provider].each do |resource_type, data|
245
+ array << data['locations']
246
+ end
247
+ end
248
+ end
249
+
250
+ array.flatten.uniq
251
+ end
252
+
253
+ # Returns a list of subscriptions for the tenant.
254
+ #
255
+ def subscriptions
256
+ url = url_with_api_version(@@api_version, @base_url, 'subscriptions')
257
+ resp = rest_get(url)
258
+ JSON.parse(resp.body)["value"]
259
+ end
260
+
261
+ # Return information for the specified subscription ID, or the
262
+ # subscription ID that was provided in the constructor if none is
263
+ # specified.
264
+ #
265
+ def subscription_info(subscription_id = @subscription_id)
266
+ url = url_with_api_version(@@api_version, @base_url, 'subscriptions', subscription_id)
267
+ resp = rest_get(url)
268
+ JSON.parse(resp.body)
269
+ end
270
+
271
+ # Returns a list of resources for the current subscription. If a
272
+ # +resource_group+ is provided, only list resources for that
273
+ # resource group.
274
+ #
275
+ def resources(resource_group = nil)
276
+ if resource_group
277
+ url = url_with_api_version(
278
+ @@api_version, @base_url, 'subscriptions', subscription_id,
279
+ 'resourcegroups', resource_group, 'resources'
280
+ )
281
+ else
282
+ url = url_with_api_version(@base_url, 'subscriptions', subscription_id, 'resources')
283
+ end
284
+
285
+ response = rest_get(url)
286
+
287
+ JSON.parse(response.body)["value"]
288
+ end
289
+
290
+ # Returns a list of resource groups for the current subscription.
291
+ #
292
+ def resource_groups
293
+ url = url_with_api_version(
294
+ @@api_version, @base_url, 'subscriptions',
295
+ subscription_id, 'resourcegroups'
296
+ )
297
+ response = rest_get(url)
298
+ JSON.parse(response.body)["value"]
299
+ end
300
+
301
+ # Returns information on the specified +resource_group+ for the current
302
+ # subscription, or the resource group specified in the constructor if
303
+ # none is provided.
304
+ #
305
+ def resource_group_info(resource_group)
306
+ url = url_with_api_version(
307
+ @@api_version, @base_url, 'subscriptions',
308
+ subscription_id, 'resourcegroups', resource_group
309
+ )
310
+
311
+ resp = rest_get(url)
312
+ JSON.parse(resp.body)
313
+ end
314
+
315
+ # Returns a list of tags for the current subscription.
316
+ #
317
+ def tags
318
+ url = url_with_api_version(@@api_version, @base_url, 'subscriptions', subscription_id, 'tagNames')
319
+ resp = rest_get(url)
320
+ JSON.parse(resp.body)["value"]
321
+ end
322
+
323
+ # Returns a list of tenants that can be accessed.
324
+ #
325
+ def tenants
326
+ url = url_with_api_version(@@api_version, @base_url, 'tenants')
327
+ resp = rest_get(url)
328
+ JSON.parse(resp.body)
329
+ end
330
+
331
+ private
332
+
333
+ # REST verb methods
334
+
335
+ def rest_get(url)
336
+ RestClient.get(
337
+ url,
338
+ :accept => @accept,
339
+ :content_type => @content_type,
340
+ :authorization => @token,
341
+ )
342
+ end
343
+
344
+ def rest_put(url, body = '')
345
+ RestClient.put(
346
+ url,
347
+ body,
348
+ :accept => @accept,
349
+ :content_type => @content_type,
350
+ :authorization => @token,
351
+ )
352
+ end
353
+
354
+ def rest_post(url, body = '')
355
+ RestClient.post(
356
+ url,
357
+ body,
358
+ :accept => @accept,
359
+ :content_type => @content_type,
360
+ :authorization => @token,
361
+ )
362
+ end
363
+
364
+ def rest_delete(url)
365
+ RestClient.delete(
366
+ url,
367
+ :accept => @accept,
368
+ :content_type => @content_type,
369
+ :authorization => @token,
370
+ )
371
+ end
372
+
373
+ # Take an array of URI elements and join the together with the API version.
374
+ def url_with_api_version(api_version, *paths)
375
+ File.join(*paths) << "?api-version=#{api_version}"
376
+ end
377
+
378
+ # Build a one-time lookup table for each provider & resource. This
379
+ # lets subclasses set api-version strings properly for each method
380
+ # depending on whichever provider they're using.
381
+ #
382
+ # e.g. @@providers['Microsoft.Compute']['virtualMachines']['api_version']
383
+ #
384
+ # Note that for methods that don't depend on a resource type should use
385
+ # the @@api_version class variable instead or set it explicitly as needed.
386
+ #
387
+ def set_providers_info
388
+ if @@providers.empty? && @token
389
+ providers.each do |info|
390
+ @@providers[info['namespace']] = {}
391
+ info['resourceTypes'].each do |resource|
392
+ @@providers[info['namespace']][resource['resourceType']] = {
393
+ 'api_version' => resource['apiVersions'].first,
394
+ 'locations' => resource['locations'] - [''] # Ignore empty elements
395
+ }
396
+ end
397
+ end
398
+ end
399
+ end
400
+ end # ArmrestManager
401
+ end # Armrest
402
+ end # Azure