Skip to content

Instantly share code, notes, and snippets.

@blelump
Created April 27, 2015 14:41
Show Gist options
  • Save blelump/4ca30daaadb397188b9b to your computer and use it in GitHub Desktop.
Save blelump/4ca30daaadb397188b9b to your computer and use it in GitHub Desktop.
Sample Ruby code to create/update Azure autoscale availability set
require "azure"
require 'azure/base_management/management_http_request.rb'
require 'azure/core/http/http_request'
Azure.configure do |config|
config.storage_account_name = "storage_account_name"
config.storage_access_key = "storage_access_key"
config.management_certificate = "cert.pfx"
config.subscription_id = "subscription_id"
end
include Azure::Core::Http
# An extension of [ManagementHttpRequest][1] class to provide
# request authentication mechanism
class AutoScaleReq < Azure::BaseManagement::ManagementHttpRequest
def initialize(method, path, body = nil)
service = Azure::BaseManagement::BaseManagementService.new
super(method, path, body)
@headers['x-ms-version'] = '2013-10-01'
@headers['Content-Type'] = 'application/json'
end
def call
request = http_request_class.new(uri.request_uri, headers)
request.body = body if body
http = http_setup
HttpResponse.new(http.request(request))
end
end
body = {"Profiles"=>[
{"Capacity"=>{"Default"=>"1", "Maximum"=>"3", "Minimum"=>"1"},
"Name"=>"AutoscaleProfile",
"Rules"=>[
{
"MetricTrigger"=> {
"MetricName"=>"ApproximateMessageCount",
"MetricNamespace"=>"",
"MetricSource"=>"/Storage/<storage_name>/Queue/<queue_name>",
"Operator"=>"GreaterThanOrEqual",
"Statistic"=>"Average",
"Threshold"=>"20",
"TimeAggregation"=>"Average",
"TimeGrain"=>"PT5M",
"TimeWindow"=>"PT45M"
},
"ScaleAction"=>{"Cooldown"=>"PT5M",
"Direction"=>"Increase",
"Type"=>"ChangeCount",
"Value"=>"1"
}
},
{
"MetricTrigger"=>{
"MetricName"=>"ApproximateMessageCount",
"MetricNamespace"=>"",
"MetricSource"=>"/Storage/<storage_name>/Queue/<queue_name>",
"Operator"=>"LessThanOrEqual",
"Statistic"=>"Average",
"Threshold"=>"20",
"TimeAggregation"=>"Average",
"TimeGrain"=>"PT5M",
"TimeWindow"=>"PT45M"
}, "ScaleAction"=>{"Cooldown"=>"PT5M",
"Direction"=>"Decrease",
"Type"=>"ChangeCount",
"Value"=>"1"
}
}
],
:Name=>"AutoscaleProfile"
}
],
"Enabled"=>true
}
t = AutoScaleReq.new(:put,
"/services/monitoring/autoscalesettings?resourceId=/virtualmachines/<cloud_service_name>/availabilitySets/<availability_set_name>",
body.to_json)
res = t.call
p res.body
p res.status_code
[1]: https://github.com/Azure/azure-sdk-for-ruby/blob/master/lib/azure/base_management/management_http_request.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment