azure-armrest 0.4.1 → 0.4.2

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
  SHA1:
3
- metadata.gz: 0a888f86dfd49210ca1ce4232968d2449f8ee1a2
4
- data.tar.gz: 8a19fb58fe560e115b0153e843f0eaaf8329c61d
3
+ metadata.gz: 172b9e9bbaf79e6ce3b794516c9d03871b1bbefd
4
+ data.tar.gz: 22f969732daa71dd7f072485ac5f491cd7649ff7
5
5
  SHA512:
6
- metadata.gz: cc6c79041f6c46dcad609fd72e2e5c719c2b84cea7e0fc2cf10fd3da4bc789f92bfcdcddf69748fbfe1e9e837fc001ab607c1d6bc38d2ce0752639284f7acbb7
7
- data.tar.gz: 55fd98f1b9af93b7e84b6eaef6b98d5814a31531518ceb00c5c617e2242c547d74ab40c3f4d073bab98d2feb0ca5e09cf1292f45a982bf49532f60817426cc90
6
+ metadata.gz: 389e975c413a315045b02edc52920f623df4df1fa576d9bc788b8653d342e2cbcab7008ff3a14828248df436f759df3181458c963c0a39b61c19c7ab4a3f7f26
7
+ data.tar.gz: a888720f50eefa4ab1ce91b23ae19beecd82cc2e6f670133af0d86d44d4f7f9daa00c7567a6b06458518d683c6551afa5b2ee696e507e88a8da60a53011082df
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ = 0.4.2 - 19-Dec-2016
2
+ * Added the delete_by_id method, so you can now delete by ID string.
3
+ * Added the TemplateDeploymentService#delete_associated_resources method.
4
+
1
5
  = 0.4.1 - 16-Dec-2016
2
6
  * The Configuration#environment accessor was added. As of this release you
3
7
  can specify 'usgov' as an option to the constructor, and the appropriate
@@ -90,7 +90,7 @@ module Azure
90
90
  filter.empty? ? results : results.select { |obj| filter.all? { |k, v| obj.public_send(k) == v } }
91
91
  end
92
92
 
93
- # This method returns a model object based on an ID string for a Service.
93
+ # This method returns a model object based on an ID string for a resource.
94
94
  #
95
95
  # Example:
96
96
  #
@@ -98,21 +98,14 @@ module Azure
98
98
  #
99
99
  # vm = vms.get('your_vm', 'your_group')
100
100
  # nic_id = vm.properties.network_profile.network_interfaces[0].id
101
- # nic = vm.get_associated_resource(nic_id)
101
+ # nic = vm.get_by_id(nic_id)
102
102
  #
103
- def get_associated_resource(id_string)
103
+ def get_by_id(id_string)
104
104
  info = parse_id_string(id_string)
105
-
106
- if info['subservice_name']
107
- full_service_name = info['service_name'] + '/' + info['subservice_name']
108
- api_version = configuration.provider_default_api_version(info['provider'], full_service_name)
109
- api_version ||= configuration.provider_default_api_version(info['provider'], info['service_name'])
110
- else
111
- api_version = configuration.provider_default_api_version(info['provider'], info['service_name'])
112
- end
113
-
114
- api_version ||= configuration.api_version
115
- service_name = info['subservice_name'] || info['service_name']
105
+ api_version = api_version_lookup(info['provider'],
106
+ info['service_name'],
107
+ info['subservice_name'])
108
+ service_name = info['subservice_name'] || info['service_name'] || 'resourceGroups'
116
109
 
117
110
  url = File.join(configuration.resource_url, id_string) + "?api-version=#{api_version}"
118
111
 
@@ -123,7 +116,17 @@ module Azure
123
116
  model_class.new(rest_get(url))
124
117
  end
125
118
 
126
- alias get_by_id get_associated_resource
119
+ alias get_associated_resource get_by_id
120
+
121
+ def delete_by_id(id_string)
122
+ info = parse_id_string(id_string)
123
+ api_version = api_version_lookup(info['provider'],
124
+ info['service_name'],
125
+ info['subservice_name'])
126
+ url = File.join(configuration.resource_url, id_string) + "?api-version=#{api_version}"
127
+
128
+ delete_by_url(url, id_string)
129
+ end
127
130
 
128
131
  # Get information about a single resource +name+ within resource group
129
132
  # +rgroup+, or the resource group that was set in the configuration.
@@ -157,17 +160,8 @@ module Azure
157
160
 
158
161
  url = build_url(rgroup, name)
159
162
  url = yield(url) || url if block_given?
160
- response = rest_delete(url)
161
-
162
- if response.code == 204
163
- msg = "#{self.class} resource #{rgroup}/#{name} not found"
164
- raise Azure::Armrest::ResourceNotFoundException.new(response.code, msg, response)
165
- end
166
163
 
167
- headers = Azure::Armrest::ResponseHeaders.new(response.headers)
168
- headers.response_code = response.code
169
-
170
- headers
164
+ delete_by_url(url, "#{rgroup}/#{name}")
171
165
  end
172
166
 
173
167
  private
@@ -175,15 +169,11 @@ module Azure
175
169
  # Parse the provider and service name out of an ID string.
176
170
  def parse_id_string(id_string)
177
171
  regex = %r{
178
- subscriptions/
179
- (?<subscription_id>[^\/]+)?/
180
- resourceGroups/
181
- (?<resource_group>[^\/]+)?/
182
- providers/
183
- (?<provider>[^\/]+)?/
184
- (?<service_name>[^\/]+)?/
185
- (?<resource_name>[^\/]+)
186
- (/(?<subservice_name>[^\/]+)?/(?<subservice_resource_name>[^\/]+))*
172
+ subscriptions/(?<subscription_id>[^\/]+)?
173
+ (/resourceGroups/(?<resource_group>[^\/]+)?)?
174
+ (/providers/(?<provider>[^\/]+)?)?
175
+ (/(?<service_name>[^\/]+)?/(?<resource_name>[^\/]+))?
176
+ (/(?<subservice_name>[^\/]+)?/(?<subservice_resource_name>[^\/]+))?
187
177
  \z
188
178
  }x
189
179
 
@@ -191,6 +181,30 @@ module Azure
191
181
  Hash[match.names.zip(match.captures)]
192
182
  end
193
183
 
184
+ def api_version_lookup(provider_name, service_name, subservice_name)
185
+ provider_name ||= 'Microsoft.Resources'
186
+ service_name ||= 'resourceGroups'
187
+ if subservice_name
188
+ full_service_name = "#{service_name}/#{subservice_name}"
189
+ api_version = configuration.provider_default_api_version(provider_name, full_service_name)
190
+ end
191
+ api_version ||= configuration.provider_default_api_version(provider_name, service_name)
192
+ api_version ||= configuration.api_version
193
+ end
194
+
195
+ def delete_by_url(url, resource_name = '')
196
+ response = rest_delete(url)
197
+
198
+ if response.code == 204
199
+ msg = "resource #{resource_name} not found"
200
+ raise Azure::Armrest::ResourceNotFoundException.new(response.code, msg, response)
201
+ end
202
+
203
+ Azure::Armrest::ResponseHeaders.new(response.headers).tap do |headers|
204
+ headers.response_code = response.code
205
+ end
206
+ end
207
+
194
208
  def validate_resource_group(name)
195
209
  raise ArgumentError, "must specify resource group" unless name
196
210
  end
@@ -203,13 +217,18 @@ module Azure
203
217
  # arguments provided, and appends it with the api_version.
204
218
  #
205
219
  def build_url(resource_group = nil, *args)
206
- url = base_url
207
- url = File.join(url, 'resourceGroups', resource_group) if resource_group
208
- url = File.join(url, 'providers', @provider, @service_name)
209
- url = File.join(url, *args) unless args.empty?
220
+ url = File.join(configuration.resource_url, build_id_string(resource_group, *args))
210
221
  url << "?api-version=#{@api_version}"
211
222
  end
212
223
 
224
+ def build_id_string(resource_group = nil, *args)
225
+ id_string = File.join('', 'subscriptions', configuration.subscription_id)
226
+ id_string = File.join(id_string, 'resourceGroups', resource_group) if resource_group
227
+ id_string = File.join(id_string, 'providers', @provider, @service_name)
228
+ id_string = File.join(id_string, *args) unless args.empty?
229
+ id_string
230
+ end
231
+
213
232
  # Aggregate resources from all resource groups.
214
233
  #
215
234
  # To be used in the cases where the API does not support list_all with
@@ -49,6 +49,54 @@ module Azure
49
49
  response = JSON.parse(rest_post(url))['template']
50
50
  DeploymentTemplate.new(response)
51
51
  end
52
+
53
+ # Delete a deployment and all associated resources that were generated by the
54
+ # deployment.
55
+ #
56
+ # This is a synchronous call. It waits until all deletions complete
57
+ def delete_associated_resources(deploy_name, resource_group = configuration.resource_group)
58
+ operations = list_deployment_operations(deploy_name, resource_group)
59
+ resource_ids = operations.collect do |op|
60
+ if op.properties.provisioning_operation =~ /^create$/i
61
+ op.properties.target_resource.id
62
+ end
63
+ end.compact
64
+ resource_ids << build_id_string(resource_group, deploy_name)
65
+
66
+ delete_resources(resource_ids, resource_ids.size)
67
+ end
68
+
69
+ private
70
+
71
+ def delete_resources(ids, retry_cnt)
72
+ if retry_cnt == 0
73
+ ids.each { |id| log("error", "Failed to delete #{id}") }
74
+ return
75
+ end
76
+
77
+ remaining_ids = ids.collect { |id| delete_resource(id) }.compact
78
+ delete_resources(remaining_ids, retry_cnt - 1) unless remaining_ids.empty?
79
+ end
80
+
81
+ def delete_resource(id_string)
82
+ log("Deleting #{id_string}")
83
+
84
+ wait(delete_by_id(id_string), 0)
85
+
86
+ log("Deleted #{id_string}")
87
+ nil
88
+ rescue Azure::Armrest::BadRequestException => err
89
+ log("debug", err.to_s)
90
+ log("Resource #{id_string} cannot be deleted because of BadRequestException. Will try again.")
91
+ id_string
92
+ rescue Azure::Armrest::PreconditionFailedException, Azure::Armrest::ConflictException => err
93
+ log("debug", err.to_s)
94
+ log("Resource #{id_string} cannot be deleted because it is used by others. Will try again.")
95
+ id_string
96
+ rescue Azure::Armrest::ResourceNotFoundException => err
97
+ log("debug", err.to_s)
98
+ nil
99
+ end
52
100
  end
53
101
  end
54
102
  end
@@ -1,5 +1,6 @@
1
1
  module Azure
2
2
  module Armrest
3
- VERSION = '0.4.1'.freeze
3
+ # The version of the azure-armrest library.
4
+ VERSION = '0.4.2'.freeze
4
5
  end
5
6
  end
@@ -189,19 +189,19 @@ module Azure
189
189
  end
190
190
 
191
191
  nics.each do |nic_id_string|
192
- nic = get_associated_resource(nic_id_string)
192
+ nic = get_by_id(nic_id_string)
193
193
  delete_and_wait(nis, nic.name, nic.resource_group, options)
194
194
 
195
195
  if options[:ip_addresses]
196
196
  nic.properties.ip_configurations.each do |ip|
197
- ip = get_associated_resource(ip.properties.public_ip_address.id)
197
+ ip = get_by_id(ip.properties.public_ip_address.id)
198
198
  delete_and_wait(ips, ip.name, ip.resource_group, options)
199
199
  end
200
200
  end
201
201
 
202
202
  if options[:network_security_groups]
203
203
  if nic.properties.respond_to?(:network_security_group)
204
- nsg = get_associated_resource(nic.properties.network_security_group.id)
204
+ nsg = get_by_id(nic.properties.network_security_group.id)
205
205
  delete_and_wait(nsgs, nsg.name, nsg.resource_group, options)
206
206
  end
207
207
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azure-armrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2016-12-16 00:00:00.000000000 Z
14
+ date: 2016-12-19 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: json