lib/azure/armrest/template_deployment_service.rb in azure-armrest-0.0.3 vs lib/azure/armrest/template_deployment_service.rb in azure-armrest-0.0.4

- old
+ new

@@ -1,97 +1,44 @@ module Azure::Armrest -# Base class for managing templates and deployments -class TemplateDeploymentService < ArmrestService + # Base class for managing templates and deployments + class TemplateDeploymentService < ResourceGroupBasedService - def initialize(_armrest_configuration, _options = {}) - super + def initialize(_armrest_configuration, options = {}) + super + @provider = options[:provider] || 'Microsoft.Resources' + # Has to be hard coded for now + set_service_api_version({'api_version' => '2014-04-01-preview'}, '') + @service_name = 'deployments' + end - # has to be hard coded for now - set_service_api_version({'api_version' => '2014-04-01-preview'}, '') - end + # Get names of all deployments in a resource group + def list_names(resource_group = armrest_configuration.resource_group) + list(resource_group).map(&:name) + end - # Get names of all deployments in a resource group - def list_names(resource_group = armrest_configuration.resource_group) - list(resource_group).map {|e| e['name']} - end + # Get all deployments for the current subscription + def list_all + list_in_all_groups + end - # Get all deployments in a resource group - # If the resource group is nil, then return deployments in all groups - def list(resource_group = armrest_configuration.resource_group) - if resource_group - url = build_deployment_url(resource_group) - JSON.parse(rest_get(url))['value'] - else - threads = [] - array = [] - mutex = Mutex.new + # Get all operations of a deployment in a resource group + def list_deployment_operations(deploy_name, resource_group = armrest_configuration.resource_group) + raise ArgumentError, "must specify resource group" unless resource_group + raise ArgumentError, "must specify name of the resource" unless deploy_name - resource_groups.each do |rg| - threads << Thread.new(rg['name']) do |group| - url = build_deployment_url(group) - results = JSON.parse(rest_get(url))['value'] - if results && !results.empty? - mutex.synchronize { - results.each { |hash| hash['resourceGroup'] = group } - array << results - } - end - end - end - - threads.each(&:join) - array.flatten + url = build_url(resource_group, deploy_name, 'operations') + response = rest_get(url) + JSON.parse(response)['value'].map{ |hash| TemplateDeploymentOperation.new(hash) } end - end - # Get the deployment in a resource group - def get(deploy_name, resource_group = armrest_configuration.resource_group) - url = build_deployment_url(resource_group, deploy_name) - JSON.parse(rest_get(url)) - end + # Get the operation of a deployment in a resource group + def get_deployment_operation(op_id, deploy_name, resource_group = armrest_configuration.resource_group) + raise ArgumentError, "must specify resource group" unless resource_group + raise ArgumentError, "must specify name of the resource" unless deploy_name + raise ArgumentError, "must specify operation id" unless op_id - # Get all operations of a deployment in a resource group - def list_deployment_operations(deploy_name, resource_group = armrest_configuration.resource_group) - url = build_deployment_url(resource_group, deploy_name, 'operations') - JSON.parse(rest_get(url))['value'] + url = build_url(resource_group, deploy_name, 'operations', op_id) + response = rest_get(url) + TemplateDeploymentOperation.new(response) + end end - - # Get the operation of a deployment in a resource group - def get_deployment_operation(deploy_name, op_id, resource_group = armrest_configuration.resource_group) - url = build_deployment_url(resource_group, deploy_name, 'operations', op_id) - JSON.parse(rest_get(url)) - end - - # Create a template deployment - # The template and parameters should be provided through the options hash - def create(deploy_name, options, resource_group = armrest_configuration.resource_group) - url = build_deployment_url(resource_group, deploy_name) - body = options.has_key?('properties') ? options.to_json : {'properties' => options}.to_json - JSON.parse(rest_put(url, body)) - end - - # Delete a deployment - def delete(deploy_name, resource_group = armrest_configuration.resource_group) - url = build_deployment_url(resource_group, deploy_name) - rest_delete(url) - end - - private - - - # Builds a URL based on subscription_id an resource_group and any other - # arguments provided, and appends it with the api_version. - # - def build_deployment_url(resource_group, *args) - url = File.join( - Azure::Armrest::COMMON_URI, - armrest_configuration.subscription_id, - 'resourceGroups', - resource_group, - 'deployments', - ) - - url = File.join(url, *args) unless args.empty? - url << "?api-version=#{@api_version}" - end -end end