Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ tags
/pkg
/doc
Gemfile.lock
nbproject/*
*.gem
.idea/*
.project


.DS_Store
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ This project provides a Ruby package that makes it easy to access and manage Win
* Virtual Machines
* create linux based VMs and ssh with cert and key option enabled for ssh and WINRM (both http & https)enabled for windows based VMs
* list, shut down, delete, find virtual machine deployments. While shutting down your VMs the provisioning state would be deallocated and this VM will not be included in the billing cycle.
* Create VM for a specific virtual network
* Cloud Services
* create, list, delete cloud services
* Storage Accounts
Expand Down Expand Up @@ -400,23 +401,25 @@ params = {
:vm_user => 'azureuser',
:image => '5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-63APR20130415',
:password => 'Password',
:location => 'West US'
:location => 'West US',
:affinity_group_name => 'affinity1'
}
options = {
:storage_account_name => 'storage_suse',
:winrm_transport => ['https','http'], #Currently http is supported. To enable https, set the transport protocol to https, simply rdp to the VM once VM is in ready state, export the certificate ( CN name would be the deployment name) from the certstore of the VM and install to your local machine and communicate WinRM via https.
:cloud_service_name => 'cloud_service_name',
:deployment_name =>'mydeployment',
:deployment_name =>'vm_name',
:tcp_endpoints => '80,3389:3390',
:private_key_file => 'c:/private_key.key', #required for ssh or winrm(https) certificate.
:certificate_file => 'c:/certificate.pem', #required for ssh or winrm(https) certificate.
:ssh_port => 2222,
:vm_size => 'Small' #valid choices are (ExtraSmall, Small, Medium, Large, ExtraLarge, A6, A7)
:vm_size => 'Small', #valid choices are (ExtraSmall, Small, Medium, Large, ExtraLarge, A6, A7)
:virtual_network_name => 'xplattestvnet'
}
virtual_machine_service.create_virtual_machine(params, options)

#Get a list of available virtual machine images
virtual_machine_image_service = Azure::VirtualMachineImageService.new
virtual_machine_image_service = Azure::VirtualMachineImageManagementService.new
virtual_machine_image_service.list_virtual_machine_images

#Get a list of available regional data center locations
Expand Down Expand Up @@ -499,7 +502,7 @@ require 'azure'

#Create a virtual network service object

vnet = Azure::VirtualNetworkManagement::VirtualNetworkManagementService.new
vnet = Azure::VirtualNetworkManagementService.new

#API to get a list of virtual networks created for a subscription.

Expand All @@ -515,14 +518,14 @@ dns_servers = [{:name => 'dns-1', :ip_address=>'1.2.3.4'}, {:name => 'dns-2',

options = {:subnet => subnets, :dns => dns_servers}

vnet.set_network_configuration ('virtual-network-name', 'affinity-group-name', address_space, options)
vnet.set_network_configuration('virtual-network-name', 'affinity-group-name', address_space, options)

#API to configure virtual network from xml file that can be exported from management portal and customized to add or delete vnet

vnetxml = './customnetwork.xml'

vnet.set_network_configuration (vnetxml)

vnet.set_network_configuration(vnetxml)
```
# Need Help?
Be sure to check out the Windows Azure [Developer Forums on Stack Overflow and MSDN](http://go.microsoft.com/fwlink/?LinkId=234489) if you have trouble with the provided code.
# Contribute Code or Provide Feedback
Expand Down
48 changes: 23 additions & 25 deletions lib/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,30 @@
#--------------------------------------------------------------------------
module Azure; end

require "azure/core"
require "azure/blob/blob_service"
require "azure/queue/queue_service"
require "azure/service_bus/service_bus_service"
require "azure/service_bus/empty_rule_action"
require "azure/service_bus/sql_rule_action"
require "azure/service_bus/sql_filter"
require "azure/service_bus/true_filter"
require "azure/service_bus/correlation_filter"
require "azure/table/table_service"
require "azure/table/batch"
require "azure/table/query"
require 'azure/core'
require 'azure/blob/blob_service'
require 'azure/queue/queue_service'
require 'azure/service_bus/service_bus_service'
require 'azure/service_bus/empty_rule_action'
require 'azure/service_bus/sql_rule_action'
require 'azure/service_bus/sql_filter'
require 'azure/service_bus/true_filter'
require 'azure/service_bus/correlation_filter'
require 'azure/table/table_service'
require 'azure/table/batch'
require 'azure/table/query'
require 'azure/core/utility'
require 'azure/base_management/management_http_request'
require 'azure/base_management/base_management_service'
require 'azure/virtual_machine_image_management/virtual_machine_image_management_service'
require 'azure/virtual_machine_management/virtual_machine_management_service'
require 'azure/storage_management/storage_management_service'
require 'azure/cloud_service_management/cloud_service_management_service'
require 'azure/base_management/location'
require 'azure/sql_database_management/sql_database_management_service'
require 'azure/virtual_network_management/virtual_network_management_service'

require "azure/core/utility"
require "azure/base_management/management_http_request"
require "azure/base_management/base_management_service"
require "azure/virtual_machine_image_management/virtual_machine_image_management_service"
require "azure/virtual_machine_management/virtual_machine_management_service"
require "azure/storage_management/storage_management_service"
require "azure/cloud_service_management/cloud_service_management_service"
require "azure/base_management/location"
require "azure/sql_database_management/sql_database_management_service"
require "azure/virtual_network_management/virtual_network_management_service"

# add some aliases for convenience
# add some aliases for convenience
Azure::BlobService = Azure::Blob::BlobService
Azure::QueueService = Azure::Queue::QueueService
Azure::TableService = Azure::Table::TableService
Expand All @@ -50,4 +49,3 @@ module Azure; end
Azure::VirtualMachineManagementService = Azure::VirtualMachineManagement::VirtualMachineManagementService
Azure::SqlDatabaseManagementService = Azure::SqlDatabaseManagement::SqlDatabaseManagementService
Azure::VirtualNetworkManagementService = Azure::VirtualNetworkManagement::VirtualNetworkManagementService

2 changes: 1 addition & 1 deletion lib/azure/base_management/affinity_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#--------------------------------------------------------------------------
module Azure
module BaseManagement
# Represents an AffinityGroup
class AffinityGroup

def initialize
yield self if block_given?
end
Expand Down
108 changes: 71 additions & 37 deletions lib/azure/base_management/base_management_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
require 'nokogiri'
require 'base64'
require 'openssl'
require "uri"
require "rexml/document"
require "azure/base_management/serialization"
require "azure/base_management/location"
require "azure/base_management/affinity_group"
require 'uri'
require 'rexml/document'
require 'azure/base_management/serialization'
require 'azure/base_management/location'
require 'azure/base_management/affinity_group'

include Azure::BaseManagement
include Azure::Core::Utility
Expand All @@ -29,22 +29,23 @@
module Azure
module BaseManagement
class BaseManagementService

def initialize
validate_configuration
cert_file = File.read (Azure.config.management_certificate)
cert_file = File.read(Azure.config.management_certificate)
begin
if Azure.config.management_certificate =~ /(pem)$/
certificate_key = OpenSSL::X509::Certificate.new(cert_file)
private_key = OpenSSL::PKey::RSA.new(cert_file)
else
#Parse pfx content
# Parse pfx content
cert_content = OpenSSL::PKCS12.new(Base64.decode64(cert_file))
certificate_key = OpenSSL::X509::Certificate.new(cert_content.certificate.to_pem)
certificate_key = OpenSSL::X509::Certificate.new(
cert_content.certificate.to_pem
)
private_key = OpenSSL::PKey::RSA.new(cert_content.key.to_pem)
end
rescue Exception => e
raise RuntimeError, "Management certificate not valid. Error: #{e.message}"
raise "Management certificate not valid. Error: #{e.message}"
end

Azure.configure do |config|
Expand All @@ -54,17 +55,28 @@ def initialize
end

def validate_configuration
raise RuntimeError, "Subscription ID not valid." if Azure.config.subscription_id.nil? or Azure.config.subscription_id.empty?
raise RuntimeError, "Management endpoint not valid." if Azure.config.management_endpoint.nil? or Azure.config.management_endpoint.empty?
raise RuntimeError, "Could not read from file '#{Azure.config.management_certificate}'." unless (test 'r', Azure.config.management_certificate)
raise RuntimeError, "Management certificate expects a .pem or .pfx file." unless(Azure.config.management_certificate =~ /(pem|pfx)$/)
subs_id = Azure.config.subscription_id
error_message = 'Subscription ID not valid.'
raise error_message if subs_id.nil? || subs_id.empty?

m_ep = Azure.config.management_endpoint
error_message = 'Management endpoint not valid.'
raise error_message if m_ep.nil? || m_ep.empty?

m_cert = Azure.config.management_certificate
error_message = "Could not read from file '#{m_cert}'."
raise error_message unless test('r', m_cert)

m_cert = Azure.config.management_certificate
error_message = 'Management certificate expects a .pem or .pfx file.'
raise error_message unless m_cert =~ /(pem|pfx)$/
end

# Public: Gets a list of regional data center locations from the server
#
# Returns an array of Azure::BaseManagement::Location objects
def list_locations
request = ManagementHttpRequest.new(:get, "/locations")
request = ManagementHttpRequest.new(:get, '/locations')
response = request.call
Serialization.locations_from_xml(response)
end
Expand All @@ -76,7 +88,7 @@ def list_locations
#
# Returns an array of Azure::BaseManagement::AffinityGroup objects
def list_affinity_groups
request_path = "/affinitygroups"
request_path = '/affinitygroups'
request = ManagementHttpRequest.new(:get, request_path, nil)
response = request.call
Serialization.affinity_groups_from_xml(response)
Expand All @@ -87,26 +99,37 @@ def list_affinity_groups
# ==== Attributes
#
# * +name+ - String. Affinity Group name.
# * +location+ - String. The location where the affinity group will be created.
# * +label+ - String. Name for the affinity specified as a base-64 encoded string.
# * +location+ - String. The location where the affinity group will
# be created.
# * +label+ - String. Name for the affinity specified as a
# base-64 encoded string.
#
# ==== Options
#
# Accepted key/value pairs are:
# * +:description+ - String. A description for the affinity group. (optional)
#
# * +:description+ - String. A description for the affinity group.
# (optional)
#
# See http://msdn.microsoft.com/en-us/library/windowsazure/gg715317.aspx
#
# Returns: None
def create_affinity_group(name, location, label, options={})
if name.nil? or name.strip.empty?
raise "Affinity Group name cannot be empty"
elsif list_affinity_groups.collect(&:name).include?(name)
raise Azure::Error::Error.new("ConflictError", 409, "An affinity group #{name} already exists in the current subscription.")
def create_affinity_group(name, location, label, options = {})
if name.nil? || name.strip.empty?
raise 'Affinity Group name cannot be empty'
elsif list_affinity_groups.map(&:name).include?(name)
raise Azure::Error::Error.new(
'ConflictError',
409,
"An affinity group #{name}"\
" already exists in the current subscription."
)
else
validate_location(location)
body = Serialization.affinity_group_to_xml(name, location, label, options)
request_path = "/affinitygroups"
body = Serialization.affinity_group_to_xml(name,
location,
label,
options)
request_path = '/affinitygroups'
request = ManagementHttpRequest.new(:post, request_path, body)
request.call
Loggerx.info "Affinity Group #{name} is created."
Expand All @@ -119,18 +142,20 @@ def create_affinity_group(name, location, label, options={})
# ==== Attributes
#
# * +name+ - String. Affinity Group name.
# * +label+ - String. Name for the affinity specified as a base-64 encoded string.
# * +label+ - String. Name for the affinity specified as a
# base-64 encoded string.
#
# ==== Options
#
# Accepted key/value pairs are:
# * +:description+ - String. A description for the affinity group. (optional)
# * +:description+ - String. A description for the affinity group.
# (optional)
#
# See http://msdn.microsoft.com/en-us/library/windowsazure/gg715316.aspx
#
# Returns: None
def update_affinity_group(name, label, options={})
raise "Label name cannot be empty" if label.nil? or label.empty?
def update_affinity_group(name, label, options = {})
raise 'Label name cannot be empty' if label.nil? || label.empty?
if affinity_group(name)
body = Serialization.resource_to_xml(label, options)
request_path = "/affinitygroups/#{name}"
Expand Down Expand Up @@ -178,23 +203,32 @@ def get_affinity_group(name)
end

private

def affinity_group(affinity_group_name)
if affinity_group_name.nil? or affinity_group_name.empty? or !list_affinity_groups.map{|x| x.name.downcase}.include?(affinity_group_name.downcase)
error = Azure::Error::Error.new("AffinityGroupNotFound", 404, "The affinity group does not exist.")
if affinity_group_name.nil? ||\
affinity_group_name.empty? ||\
!list_affinity_groups.map { |x| x.name.downcase }.include?(
affinity_group_name.downcase
)
error = Azure::Error::Error.new('AffinityGroupNotFound',
404,
'The affinity group does not exist.')
raise error
else
true
end
end

def validate_location(location_name)
locations = Azure::BaseManagementService.new.list_locations.collect(&:name)
if !locations.map(&:downcase).include?(location_name.downcase)
error = "Value '#{location_name}' specified for parameter 'location' is invalid. Allowed values are #{locations.join(',')}"
base_mgmt_service = Azure::BaseManagementService.new
locations = base_mgmt_service.list_locations.map(&:name)
unless locations.map(&:downcase).include?(location_name.downcase)
error = "Value '#{location_name}' specified for parameter"\
" 'location' is invalid."\
" Allowed values are #{locations.join(',')}"
raise error
end
end

end
end
end
Loading