azure-armrest 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+ # Azure namespace
2
+ module Azure
3
+ # Armrest namespace
4
+ module Armrest
5
+ # Base class for managing availability sets.
6
+ class AvailabilitySetService < ArmrestService
7
+ # The provider used in requests when gathering ASM information.
8
+ attr_reader :provider
9
+
10
+ # Create and return a new AvailabilitySetService (ASM) instance.
11
+ #
12
+ def initialize(_armrest_configuration, options = {})
13
+ super
14
+
15
+ @provider = options[:provider] || 'Microsoft.Compute'
16
+
17
+ set_service_api_version(options, 'availabilitySets')
18
+ end
19
+
20
+ # Creates a new availability set with the given name. The optional +tags+
21
+ # argument should be a hash, if provided.
22
+ #
23
+ def create(name, location, tags = nil, resource_group = armrest_configuration.resource_group)
24
+ raise ArgumentError, "No resource group specified" if resource_group.nil?
25
+
26
+ url = build_url(resource_group, name)
27
+ body = {:name => name, :location => location, :tags => tags}.to_json
28
+ response = rest_put(url, body)
29
+ response.return!
30
+ end
31
+
32
+ alias update create
33
+
34
+ # Deletes the +name+ availability set.
35
+ #
36
+ def delete(name, resource_group = armrest_configuration.resource_group)
37
+ raise ArgumentError, "No resource group specified" if resource_group.nil?
38
+ url = build_url(resource_group, name)
39
+ response = rest_delete(url)
40
+ response.return!
41
+ end
42
+
43
+ # Retrieves the options of an availability set +name+.
44
+ #
45
+ def get(name, resource_group = armrest_configuration.resource_group)
46
+ raise ArgumentError, "No resource group specified" if resource_group.nil?
47
+ url = build_url(resource_group, name)
48
+ response = rest_get(url)
49
+ JSON.parse(response.body)
50
+ end
51
+
52
+ # List availability sets.
53
+ #
54
+ def list(resource_group = armrest_configuration.resource_group)
55
+ array = []
56
+
57
+ if resource_group
58
+ url = build_url(resource_group)
59
+ response = rest_get(url)
60
+ array << JSON.parse(response.body)['value']
61
+ else
62
+ threads = []
63
+ mutex = Mutex.new
64
+
65
+ resource_groups.each do |group|
66
+ url = build_url(group['name'])
67
+
68
+ threads << Thread.new(url) do |thread_url|
69
+ response = rest_get(thread_url)
70
+ result = JSON.parse(response)['value']
71
+ mutex.synchronize{ array << result if result }
72
+ end
73
+ end
74
+
75
+ threads.each(&:join)
76
+ end
77
+
78
+ array.flatten
79
+ end
80
+
81
+ # Builds a URL based on subscription_id an resource_group and any other
82
+ # arguments provided, and appends it with the api_version.
83
+ #
84
+ def build_url(resource_group, *args)
85
+ url = File.join(
86
+ Azure::Armrest::COMMON_URI,
87
+ armrest_configuration.subscription_id,
88
+ 'resourceGroups',
89
+ resource_group,
90
+ 'providers',
91
+ @provider,
92
+ 'availabilitySets',
93
+ )
94
+
95
+ url = File.join(url, *args) unless args.empty?
96
+ url << "?api-version=#{@api_version}"
97
+ end
98
+ end # AvailabilitySetService
99
+ end # Armrest
100
+ end # Azure
@@ -1,12 +1,12 @@
1
1
  module Azure
2
2
  module Armrest
3
- class EventManager < ArmrestManager
3
+ class EventService < ArmrestService
4
4
 
5
- def initialize(options = {})
5
+ def initialize(armrest_configuration, _options = {})
6
6
  super
7
7
 
8
8
  @base_url += "providers/microsoft.insights/eventtypes/management/values"
9
- @base_url += "?api-version=#{@api_version}"
9
+ @base_url += "?api-version=#{armrest_configuration.api_version}"
10
10
  end
11
11
 
12
12
  # check what data type the event channel is
@@ -0,0 +1,86 @@
1
+ module Azure
2
+ module Armrest
3
+ class ResourceGroupService < ArmrestService
4
+ # The provider used in http requests. The default is 'Microsoft.Resources'
5
+ attr_reader :provider
6
+
7
+ # Creates and returns a new ResourceGroupService object.
8
+ #
9
+ def initialize(_armrest_configuration, options = {})
10
+ super
11
+ @provider = options[:provider] || 'Microsoft.Resources'
12
+ set_service_api_version(options, 'resourceGroups')
13
+ end
14
+
15
+ # List all the resources for the current subscription. You can optionally
16
+ # pass :top or :filter options as well to restrict returned results.
17
+ #
18
+ # If you pass a :resource_group option, then only resources for that
19
+ # resource group are returned.
20
+ #
21
+ # Examples:
22
+ #
23
+ # rgs = ResourceGroupService.new
24
+ # rgs.list(:top => 2)
25
+ # rgs.list(:filter => "location eq 'centralus'")
26
+ #
27
+ def list(options = {})
28
+ url = build_url
29
+ url << "&$top=#{options[:top]}" if options[:top]
30
+ url << "&$filter=#{options[:filter]}" if options[:filter]
31
+
32
+ response = rest_get(URI.escape(url))
33
+
34
+ JSON.parse(response.body)["value"]
35
+ end
36
+
37
+ # Creates a new +group+ in +location+ for the current subscription.
38
+ # You may optionally apply +tags+.
39
+ #
40
+ def create(group, location, tags = nil)
41
+ body = {:location => location, :tags => tags}.to_json
42
+ url = build_url(group)
43
+
44
+ response = rest_put(url, body)
45
+
46
+ JSON.parse(response.body)
47
+ end
48
+
49
+ # Delete a resource group from the current subscription.
50
+ #
51
+ def delete(group)
52
+ url = build_url(group)
53
+ response = rest_delete(url)
54
+ response.return!
55
+ end
56
+
57
+ # Returns information for the given resource group.
58
+ #
59
+ def get(group)
60
+ url = build_url(group)
61
+ response = rest_get(url)
62
+ JSON.parse(response.body)
63
+ end
64
+
65
+ # Updates the tags for the given resource group.
66
+ #
67
+ def update(group, tags)
68
+ body = {:tags => tags}.to_json
69
+ url = build_url(group)
70
+ response = rest_patch(url, body)
71
+ response.return!
72
+ end
73
+
74
+ private
75
+
76
+ def build_url(group = nil, *args)
77
+ id = armrest_configuration.subscription_id
78
+ url = File.join(Azure::Armrest::COMMON_URI, id, 'resourcegroups')
79
+ url = File.join(url, group) if group
80
+ url = File.join(url, *args) unless args.empty?
81
+ url << "?api-version=#{@api_version}"
82
+ end
83
+
84
+ end # ResourceGroupService
85
+ end # Armrest
86
+ end # Azure
@@ -0,0 +1,111 @@
1
+ require 'cache_method'
2
+
3
+ module Azure
4
+ module Armrest
5
+ class ResourceProviderService < ArmrestService
6
+ # The provider used in http requests. The default is 'Microsoft.Resources'
7
+ attr_reader :provider
8
+
9
+ # The amount of time in seconds to cache certain methods. The default is 24 hours.
10
+ @cache_time = 24 * 60 * 60
11
+
12
+ class << self
13
+ # Get or set the cache time for all methods. The default is 24 hours.
14
+ attr_accessor :cache_time
15
+ end
16
+
17
+ # Creates and returns a new ResourceProviderService object.
18
+ #
19
+ # Note that many ResourceProviderService instance methods are cached. You
20
+ # can set the cache_time for certain methods in the constructor, but keep in
21
+ # mind that it is a global setting for the class. You can also set this
22
+ # at the class level if desired. The default cache time is 24 hours.
23
+ #
24
+ # You can also set the provider. The default is 'Microsoft.Resources'.
25
+ #
26
+ def initialize(_armrest_configuration, options = {})
27
+ super
28
+
29
+ @provider = options[:provider] || 'Microsoft.Resources'
30
+
31
+ if options[:cache_time]
32
+ @cache_time = options[:cache_time]
33
+ self.class.send(:cache_time=, @cache_time)
34
+ end
35
+
36
+ set_service_api_version(options, 'resourceGroups')
37
+ end
38
+
39
+ # List all the providers for the current subscription. The results of
40
+ # this method are cached.
41
+ #
42
+ def list
43
+ url = build_url
44
+ response = rest_get(url)
45
+ JSON.parse(response.body)["value"]
46
+ end
47
+
48
+ cache_method(:list, cache_time)
49
+
50
+ # Return information about a specific +namespace+ provider. The results
51
+ # of this method are cached.
52
+ #
53
+ def get(namespace)
54
+ url = build_url(namespace)
55
+ response = rest_get(url)
56
+ JSON.parse(response.body)
57
+ end
58
+
59
+ cache_method(:get, cache_time)
60
+
61
+ # Returns an array of geo-locations for the given +namespace+ provider.
62
+ # The results of this method are cached.
63
+ #
64
+ def list_geo_locations(namespace)
65
+ url = build_url(namespace)
66
+ response = rest_get(url)
67
+ JSON.parse(response.body)['resourceTypes'].first['locations']
68
+ end
69
+
70
+ cache_method(:list_geo_locations, cache_time)
71
+
72
+ # Returns an array of supported api-versions for the given +namespace+ provider.
73
+ # The results of this method are cached.
74
+ #
75
+ def list_api_versions(namespace)
76
+ url = build_url(namespace)
77
+ response = rest_get(url)
78
+ JSON.parse(response.body)['resourceTypes'].first['apiVersions']
79
+ end
80
+
81
+ cache_method(:list_api_versions, cache_time)
82
+
83
+ # Register the current subscription with the +namespace+ provider.
84
+ #
85
+ def register(namespace)
86
+ url = build_url(namespace, 'register')
87
+ response = rest_post(url)
88
+ response.return!
89
+ end
90
+
91
+ # Unregister the current subscription from the +namespace+ provider.
92
+ #
93
+ def unregister(namespace)
94
+ url = build_url(namespace, 'unregister')
95
+ response = rest_post(url)
96
+ response.return!
97
+ end
98
+
99
+ private
100
+
101
+ def build_url(namespace = nil, *args)
102
+ id = armrest_configuration.subscription_id
103
+ url = File.join(Azure::Armrest::COMMON_URI, id, 'providers')
104
+ url = File.join(url, namespace) if namespace
105
+ url = File.join(url, *args) unless args.empty?
106
+ url << "?api-version=#{@api_version}"
107
+ end
108
+
109
+ end # ResourceGroupService
110
+ end # Armrest
111
+ end # Azure
@@ -0,0 +1,86 @@
1
+ module Azure
2
+ module Armrest
3
+ class ResourceService < ArmrestService
4
+ # The provider used in http requests. The default is 'Microsoft.Resources'
5
+ attr_reader :provider
6
+
7
+ # Creates and returns a new ResourceService object.
8
+ #
9
+ def initialize(_armrest_configuration, options = {})
10
+ super
11
+ @provider = options[:provider] || 'Microsoft.Resources'
12
+ set_service_api_version(options, 'subscriptions')
13
+ end
14
+
15
+ # List all the resources for the current subscription. You can optionally
16
+ # pass :top or :filter options as well to restrict returned results.
17
+ #
18
+ # If you pass a :resource_group option, then only resources for that
19
+ # resource group are returned.
20
+ #
21
+ # Examples:
22
+ #
23
+ # rs = ResourceService.new
24
+ # rs.list(:top => 2)
25
+ # rs.list(:filter => "location eq 'centralus'")
26
+ #
27
+ def list(options = {})
28
+ subscription_id = armrest_configuration.subscription_id
29
+
30
+ if options[:resource_group]
31
+ url = File.join(
32
+ Azure::Armrest::COMMON_URI, subscription_id, 'resourcegroups',
33
+ options[:resource_group], 'resources'
34
+ )
35
+ else
36
+ url = File.join(Azure::Armrest::COMMON_URI, subscription_id, 'resources')
37
+ end
38
+
39
+ url << "?api-version=#{@api_version}"
40
+ url << "&$top=#{options[:top]}" if options[:top]
41
+ url << "&$filter=#{options[:filter]}" if options[:filter]
42
+
43
+ response = rest_get(URI.escape(url))
44
+
45
+ JSON.parse(response.body)["value"]
46
+ end
47
+
48
+ # Move the resources from +source_group+ under +source_subscription+,
49
+ # which may be a different subscription.
50
+ #
51
+ def move(source_group, source_subscription = armrest_configuration.subscription_id)
52
+ url = File.join(
53
+ Azure::Armrest::COMMON_URI, source_subscription,
54
+ 'resourcegroups', source_group, 'moveresources'
55
+ )
56
+
57
+ url << "?api-version=#{@api_version}"
58
+
59
+ response = rest_post(url)
60
+ response.return!
61
+ end
62
+
63
+ # Checks to see if the given 'resource_name' and 'resource_type' is allowed.
64
+ # This returns a JSON string that will indicate the status, including an error
65
+ # code and message on failure.
66
+ #
67
+ # If you want a simple boolean check, use the check_resource? method instead.
68
+ #
69
+ def check_resource(resource_name, resource_type)
70
+ body = JSON.dump(:Name => resource_name, :Type => resource_type)
71
+ url = File.join(Azure::Armrest::RESOURCE, 'providers', provider, 'checkresourcename')
72
+ url << "?api-version=#{@api_version}"
73
+
74
+ response = rest_post(url, body)
75
+ response.return!
76
+ end
77
+
78
+ # Similar to the check_resource method, but returns a boolean instead.
79
+ #
80
+ def check_resource?(resource_name, resource_type)
81
+ check_resource(resource_name, resource_type)['status'] == 'Allowed'
82
+ end
83
+
84
+ end # ResourceService
85
+ end # Armrest
86
+ end # Azure
@@ -0,0 +1,220 @@
1
+ module Azure
2
+ module Armrest
3
+ # Class for managing storage accounts.
4
+ class StorageAccountService < ArmrestService
5
+
6
+ # Valid account types for the create or update method.
7
+ VALID_ACCOUNT_TYPES = %w[
8
+ Standard_LRS
9
+ Standard_ZRS
10
+ Standard_GRS
11
+ Standard_RAGRS
12
+ ]
13
+
14
+ # Creates and returns a new StorageAccountService (SAS) instance.
15
+ #
16
+ def initialize(_armrest_configuration, options = {})
17
+ super
18
+ @provider = options[:provider] || 'Microsoft.Storage'
19
+ #set_service_api_version(options, 'storageAccounts')
20
+ @api_version = '2015-05-01-preview' # Must hard code for now
21
+ end
22
+
23
+ # Return information for the given storage account name for the
24
+ # provided +group+. If no group is specified, it will use the
25
+ # group set in the constructor.
26
+ #
27
+ # If the +include_keys+ option is set to true, then the keys for that
28
+ # storage account will be included in the output as well.
29
+ #
30
+ # Example:
31
+ #
32
+ # sas.get('portalvhds1234', 'Default-Storage-CentralUS')
33
+ #
34
+ def get(account_name, group = armrest_configuration.resource_group, include_keys = false)
35
+ raise ArgumentError, "must specify resource group" unless group
36
+
37
+ url = build_url(group, account_name)
38
+ results = JSON.parse(rest_get(url))
39
+ results['properties'].merge!(list_account_keys(account_name, group)) if include_keys
40
+
41
+ results
42
+ end
43
+
44
+ # Returns a list of available storage accounts for the given subscription
45
+ # for the provided +group+, or all resource groups if none is provided.
46
+ #
47
+ def list(group = nil)
48
+ if group
49
+ url = build_url(group)
50
+ JSON.parse(rest_get(url))['value']
51
+ else
52
+ array = []
53
+ threads = []
54
+ mutex = Mutex.new
55
+
56
+ resource_groups.each do |rg|
57
+ threads << Thread.new(rg['name']) do |group|
58
+ url = build_url(group)
59
+ response = rest_get(url)
60
+ results = JSON.parse(response)['value']
61
+ if results && !results.empty?
62
+ mutex.synchronize{
63
+ results.each{ |hash| hash['resourceGroup'] = group }
64
+ array << results
65
+ }
66
+ end
67
+ end
68
+ end
69
+
70
+ threads.each(&:join)
71
+
72
+ array.flatten
73
+ end
74
+ end
75
+
76
+ # List all storage accounts for the current subscription. This does not
77
+ # include storage account key information.
78
+ #
79
+ def list_all_for_subscription
80
+ sub_id = armrest_configuration.subscription_id
81
+ url = File.join(Azure::Armrest::COMMON_URI, sub_id, 'providers', @provider, 'storageAccounts')
82
+ url << "?api-version=#{@api_version}"
83
+ JSON.parse(rest_get(url))
84
+ end
85
+
86
+ alias list_all list_all_for_subscription
87
+
88
+ # Creates a new storage account, or updates an existing account with the
89
+ # specified parameters. The possible parameters are:
90
+ #
91
+ # - :name
92
+ # Required. The name of the storage account within the specified
93
+ # resource stack. Must be 3-24 alphanumeric lowercase characters.
94
+ #
95
+ # - :validating
96
+ # Optional. Set to 'nameAvailability' to indicate that the account
97
+ # name must be checked for global availability.
98
+ #
99
+ # - :type
100
+ # The type of storage account. The default is "Standard_GRS".
101
+ #
102
+ # - :location
103
+ # Required: One of the Azure geo regions, e.g. 'West US'.
104
+ #
105
+ # - :tags
106
+ # A hash of tags to describe the resource. You may have a maximum of
107
+ # 10 tags, and each key has a max size of 128 characters, and each
108
+ # value has a max size of 256 characters. These are optional.
109
+ #
110
+ # Example:
111
+ #
112
+ # sas = Azure::Armrest::StorageAccountService(config)
113
+ #
114
+ # sas.create(
115
+ # :name => "yourstorageaccount1",
116
+ # :location => "West US",
117
+ # :type => "Standard_ZRS",
118
+ # :tags => {:YourCompany => true}
119
+ # )
120
+ #
121
+ # For convenience you may also specify the :resource_group as an option.
122
+ #
123
+ def create(options = {}, rgroup = armrest_configuration.resource_group)
124
+ rgroup ||= options[:resource_group]
125
+ raise ArgumentError, "No resource group specified" if rgroup.nil?
126
+
127
+ # Mandatory options
128
+ name = options.fetch(:name)
129
+ location = options.fetch(:location)
130
+
131
+ # Optional
132
+ tags = options[:tags]
133
+ type = options[:type] || "Standard_GRS"
134
+
135
+ properties = {:accountType => type}
136
+
137
+ validate_account_type(type)
138
+ validate_account_name(name)
139
+
140
+ url = build_url(rgroup, name)
141
+ url << "&validating=" << options[:validating] if options[:validating]
142
+
143
+ body = {
144
+ :name => name,
145
+ :location => location,
146
+ :tags => tags,
147
+ :properties => properties
148
+ }.to_json
149
+
150
+ response = rest_put(url, body)
151
+ response.return!
152
+ end
153
+
154
+ alias update create
155
+
156
+ # Delete the given storage account name.
157
+ #
158
+ def delete(account_name, group = armrest_configuration.resource_group)
159
+ raise ArgumentError, "must specify resource group" unless group
160
+
161
+ url = build_url(group, account_name)
162
+ response = rest_delete(url)
163
+ response.return!
164
+ end
165
+
166
+ # Returns the primary and secondary access keys for the given
167
+ # storage account.
168
+ #
169
+ def list_account_keys(account_name, group = armrest_configuration.resource_group)
170
+ raise ArgumentError, "must specify resource group" unless group
171
+
172
+ url = build_url(group, account_name, 'listKeys')
173
+ response = rest_post(url)
174
+ JSON.parse(response)
175
+ end
176
+
177
+ # Regenerates the primary and secondary access keys for the given
178
+ # storage account.
179
+ #
180
+ def regenerate_storage_account_keys(account_name)
181
+ raise ArgumentError, "must specify resource group" unless group
182
+
183
+ url = build_url(group, account_name, 'regenerateKey')
184
+ response = rest_post(url)
185
+ response.return!
186
+ end
187
+
188
+ private
189
+
190
+ def validate_account_type(account_type)
191
+ unless VALID_ACCOUNT_TYPES.include?(account_type)
192
+ raise ArgumentError, "invalid account type '#{account_type}'"
193
+ end
194
+ end
195
+
196
+ def validate_account_name(name)
197
+ if name.size < 3 || name.size > 24 || name[/\W+/]
198
+ raise ArgumentError, "name must be 3-24 alpha-numeric characters only"
199
+ end
200
+ end
201
+
202
+ # Builds a URL based on subscription_id an resource_group and any other
203
+ # arguments provided, and appends it with the api-version.
204
+ def build_url(resource_group, *args)
205
+ url = File.join(
206
+ Azure::Armrest::COMMON_URI,
207
+ armrest_configuration.subscription_id,
208
+ 'resourceGroups',
209
+ resource_group,
210
+ 'providers',
211
+ @provider,
212
+ 'storageAccounts',
213
+ )
214
+
215
+ url = File.join(url, *args) unless args.empty?
216
+ url << "?api-version=#{@api_version}"
217
+ end
218
+ end
219
+ end
220
+ end