Mr Cheng bio photo

Mr Cheng

I needed a way to “reboot” an Azure Cloud Service programmatically in Ruby. I wanted the Ruby equivalent of: Cloud Services > click the cloud service you want > Instances > Production > Reboot

I declared victory after monkey patching CloudServiceManagement::CloudServiceManagementService.

The rest of this post are notes about what happened before victory:

hoop #0 - pointing postman at azure-sdk-for-ruby

Via postman I tried to build a POST request but realized I need to auth. But then it has to be some MSFT-specific way? I didn’t get it working and didn’t bother. It wasn’t a wasted effort - I learned postman has integration into your mac so it will prompt you for things… interesting.

hoop #1 - incomplete azure-sdk-for-ruby

The first choice was the azure-sdk-for-ruby. After a bit of probing around, I concluded the latest version doesn’t allow for it. I concluded that after maybe 30 minutes of poking around it’s github. Apprently the is yard output – which explains it’s weirdness (that made things harder to find in my case).

It took a bit a time because I wasn’t familiar with the model/schema, but now I am and in retrospect it all makes sense:

  • A resource is some formerly physical thing – compute, network and storage. These each are available as gems azure_mgmt_compute, azure_mgmt_network, azure_mgmt_storage.
  • A service is a layer on top of the resources. And in this case I care about: service_management/azure/lib/azure/cloud_service_management

hoop #2 - examining Azure REST API

cert … I suppose because it somehow knows wants a cert. Cool beans.

though the underlying REST API does.

You just need to issue a POST to:

… and logically you also need to ensure you are authenticated.

hoop #3 - sift through internet noise about the EVIL monkey patch

Turns out Ruby lets you “monkey patch” or add your own method into an existing class. Now the problem is: how do I refer to the correct class?

In this case, that class is:

Azure::CloudServiceManagement::CloudServiceManagementService – or “monkey patch” it:

class Azure::CloudServiceManagement::CloudServiceManagementService

  def reboot_cloud_service(cloud_service_name, role_instance_name)
    request_path= "/services/hostedservices/#{cloud_service_name}/deploymentslots/production/roleinstances/#{role_instance_name}?comp=reboot"
    request = client.management_request(:post, request_path) "Rebooting cloud service #{cloud_service_name}. \n"

  def get_deployment(cloud_service_name)

    deployment_slot = 'production'
    request_path= "/services/hostedservices/#{cloud_service_name}/deploymentslots/#{deployment_slot}"
    request = client.management_request(:get, request_path) "Getting deployment #{cloud_service_name}. \n"