lib/azure/armrest/template_deployment_service.rb in azure-armrest-0.4.1 vs lib/azure/armrest/template_deployment_service.rb in azure-armrest-0.4.2

- old
+ new

@@ -47,8 +47,56 @@ validate_resource(deploy_name) url = build_url(resource_group, deploy_name, 'exportTemplate') response = JSON.parse(rest_post(url))['template'] DeploymentTemplate.new(response) end + + # Delete a deployment and all associated resources that were generated by the + # deployment. + # + # This is a synchronous call. It waits until all deletions complete + def delete_associated_resources(deploy_name, resource_group = configuration.resource_group) + operations = list_deployment_operations(deploy_name, resource_group) + resource_ids = operations.collect do |op| + if op.properties.provisioning_operation =~ /^create$/i + op.properties.target_resource.id + end + end.compact + resource_ids << build_id_string(resource_group, deploy_name) + + delete_resources(resource_ids, resource_ids.size) + end + + private + + def delete_resources(ids, retry_cnt) + if retry_cnt == 0 + ids.each { |id| log("error", "Failed to delete #{id}") } + return + end + + remaining_ids = ids.collect { |id| delete_resource(id) }.compact + delete_resources(remaining_ids, retry_cnt - 1) unless remaining_ids.empty? + end + + def delete_resource(id_string) + log("Deleting #{id_string}") + + wait(delete_by_id(id_string), 0) + + log("Deleted #{id_string}") + nil + rescue Azure::Armrest::BadRequestException => err + log("debug", err.to_s) + log("Resource #{id_string} cannot be deleted because of BadRequestException. Will try again.") + id_string + rescue Azure::Armrest::PreconditionFailedException, Azure::Armrest::ConflictException => err + log("debug", err.to_s) + log("Resource #{id_string} cannot be deleted because it is used by others. Will try again.") + id_string + rescue Azure::Armrest::ResourceNotFoundException => err + log("debug", err.to_s) + nil + end end end end