example.rb
def run_example
  #
  # Create the Resource Manager Client with an Application (service principal) token provider
  #
  subscription_id = ENV['AZURE_SUBSCRIPTION_ID'] || '11111111-1111-1111-1111-111111111111' # your Azure Subscription Id

  MsRest.use_ssl_cert

  options = {
      tenant_id: ENV['AZURE_TENANT_ID'],
      client_id: ENV['AZURE_CLIENT_ID'],
      client_secret: ENV['AZURE_CLIENT_SECRET'],
      subscription_id: subscription_id,
  }

  resource_client = Resources::Client.new(options)
  network_client = Network::Client.new(options)
  storage_client = Storage::Client.new(options)
  compute_client = Compute::Client.new(options)
  authorization_client = Authorization::Client.new(options)

  #
  # Managing resource groups
  #
  resource_group_params = resource_client.model_classes.resource_group.new.tap do |rg|
    rg.location = LOCATION
  end

  # Create Resource group
  puts 'Create Resource Group'
  print_group resource_group = resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)

  # Create a user assigned identity
  if USER_ASSIGNED_IDENTITY
    puts 'Create User Assigned Identity'
    msi_client = Msi::Client.new(options)
    identity_create_params = MsiModels::Identity.new.tap do |identity|
      identity.location = LOCATION
    end
    print_item user_assigned_identity = msi_client.user_assigned_identities.create_or_update(GROUP_NAME, "myMsiIdentity", identity_create_params)
  end

  # Create storage account
  postfix = rand(1000)
  storage_account_name = "rubystor#{postfix}"
  puts "Creating a premium storage account with encryption off named #{storage_account_name} in resource group #{GROUP_NAME}"
  storage_create_params = StorageModels::StorageAccountCreateParameters.new.tap do |account|
    account.location = LOCATION
    account.sku = StorageModels::Sku.new.tap do |sku|
      sku.name = StorageModels::SkuName::PremiumLRS
      sku.tier = StorageModels::SkuTier::Premium
    end
    account.kind = StorageModels::Kind::Storage
    account.encryption = StorageModels::Encryption.new.tap do |encrypt|
      encrypt.services = StorageModels::EncryptionServices.new.tap do |services|
        services.blob = StorageModels::EncryptionService.new.tap do |service|
          service.enabled = false
        end
      end
    end
  end
  print_item storage_account = storage_client.storage_accounts.create(GROUP_NAME, storage_account_name, storage_create_params)


  puts 'Creating a virtual network for the VM'
  vnet_create_params = NetworkModels::VirtualNetwork.new.tap do |vnet|
    vnet.location = LOCATION
    vnet.address_space = NetworkModels::AddressSpace.new.tap do |addr_space|
      addr_space.address_prefixes = ['10.0.0.0/16']
    end
    vnet.dhcp_options = NetworkModels::DhcpOptions.new.tap do |dhcp|
      dhcp.dns_servers = ['8.8.8.8']
    end
    vnet.subnets = [
        NetworkModels::Subnet.new.tap do |subnet|
          subnet.name = 'rubySampleSubnet'
          subnet.address_prefix = '10.0.0.0/24'
        end
    ]
  end
  print_item vnet = network_client.virtual_networks.create_or_update(GROUP_NAME, 'sample-ruby-vnet', vnet_create_params)

  puts 'Creating a public IP address for the VM'
  public_ip_params = NetworkModels::PublicIPAddress.new.tap do |ip|
    ip.location = LOCATION
    ip.public_ipallocation_method = NetworkModels::IPAllocationMethod::Dynamic
    ip.dns_settings = NetworkModels::PublicIPAddressDnsSettings.new.tap do |dns|
      dns.domain_name_label = 'msi-vm-domain-name-label'
    end
  end
  print_item public_ip = network_client.public_ipaddresses.create_or_update(GROUP_NAME, 'sample-ruby-pubip', public_ip_params)

  puts 'Creating VM'
  vm = create_vm(compute_client, network_client, authorization_client, LOCATION, 'msi-vm', storage_account, vnet.subnets[0], public_ip, user_assigned_identity)

  # By default, the MSI accounts have no permissions
  # Next is the assignment of permissions to the account
  # example is Resource group access as Contributor, but
  # you can assign any permissions you'd want

  msi_accounts_to_assign = []
  if SYSTEM_ASSIGNED_IDENTITY
    msi_accounts_to_assign.push(vm.identity.principal_id)
  end
  if USER_ASSIGNED_IDENTITY
    msi_accounts_to_assign.push(user_assigned_identity.principal_id)
  end

  puts "Getting the Role ID of Contributor of a Resource group: #{GROUP_NAME}"
  role_name = 'Contributor'
  roles = authorization_client.role_definitions.list(resource_group.id, "roleName eq '#{role_name}'")
  print_item contributor_role = roles.first

  # Adding resource group scope to the MSI identities
  puts 'Creating the role assignment for the VM'
  msi_accounts_to_assign.each do |msi_identity|
    role_assignment_params = AuthorizationModels::RoleAssignmentCreateParameters.new.tap do |role_param|
      role_param.properties = AuthorizationModels::RoleAssignmentProperties.new.tap do |property|
        property.principal_id = msi_identity
        property.role_definition_id = contributor_role.id
      end
    end
    authorization_client.role_assignments.create(resource_group.id, SecureRandom.uuid, role_assignment_params)
  end


  puts 'Listing all of the resources within the group'
  resource_client.resources.list_by_resource_group(GROUP_NAME).each do |res|
    print_item res
  end
  puts ''

  export_template(resource_client)

  puts "Thank you for creating managed service identity Azure VM."
  puts "Use `netstat -tlnp` command verify that MSI service is running at 127.0.0.1:50342 address."
  puts "Connect to your new virtual machine via: 'ssh -p 22 #{vm.os_profile.admin_username}@#{public_ip.dns_settings.fqdn}'. Admin Password is: Pa$$w0rd92"

  puts 'Press any key to continue and delete the sample resources'
  gets

  # Delete Resource group and everything in it
  puts 'Delete Resource Group'
  resource_client.resource_groups.delete(GROUP_NAME)
  puts "\nDeleted: #{GROUP_NAME}"

end
# This sample shows how to create a Azure virtual machines with Managed Service Identity using the Azure Resource Manager APIs for Ruby.
#
# This script expects that the following environment vars are set:
#
# AZURE_TENANT_ID: with your Azure Active Directory tenant id or domain
# AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID
# AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret
# AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id
#
This sample shows how to create a Azure virtual machines with Managed Service Identity using the Azure Resource Manager APIs for Ruby.
This script expects that the following environment vars are set.

with your Azure Active Directory tenant id or domain
AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID
AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret
AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id
This sample shows how to create a Azure virtual machines with Managed Service Identity using the Azure Resource Manager APIs for Ruby . This script expects that the following environment vars are set . with your Azure Active Directory tenant id or domain AZURE_CLIENT_ID : with your Azure Active Directory Application Client ID AZURE_CLIENT_SECRET : with your Azure Active Directory Application Secret AZURE_SUBSCRIPTION_ID : with your Azure Subscription Id
def run_example
   Create the Resource Manager Client with an Application (service principal) token provider
  subscription_id = ENV['AZURE_SUBSCRIPTION_ID'] || '11111111-1111-1111-1111-111111111111'  your Azure Subscription Id
  MsRest.use_ssl_cert
  options = {
      tenant_id: ENV['AZURE_TENANT_ID'],
      client_id: ENV['AZURE_CLIENT_ID'],
      client_secret: ENV['AZURE_CLIENT_SECRET'],
      subscription_id: subscription_id,
  }
  resource_client = Resources::Client.new(options)
  network_client = Network::Client.new(options)
  storage_client = Storage::Client.new(options)
  compute_client = Compute::Client.new(options)
  authorization_client = Authorization::Client.new(options)
   Managing resource groups
  resource_group_params = resource_client.model_classes.resource_group.new.tap do |rg|
    rg.location = LOCATION
  end
   Create Resource group
  puts 'Create Resource Group'
  print_group resource_group = resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)
   Create a user assigned identity
  if USER_ASSIGNED_IDENTITY
    puts 'Create User Assigned Identity'
    msi_client = Msi::Client.new(options)
    identity_create_params = MsiModels::Identity.new.tap do |identity|
      identity.location = LOCATION
    end
    print_item user_assigned_identity = msi_client.user_assigned_identities.create_or_update(GROUP_NAME, "myMsiIdentity", identity_create_params)
  end
   Create storage account
  postfix = rand(1000)
  storage_account_name = "rubystor{postfix}"
  puts "Creating a premium storage account with encryption off named {storage_account_name} in resource group {GROUP_NAME}"
  storage_create_params = StorageModels::StorageAccountCreateParameters.new.tap do |account|
    account.location = LOCATION
    account.sku = StorageModels::Sku.new.tap do |sku|
      sku.name = StorageModels::SkuName::PremiumLRS
      sku.tier = StorageModels::SkuTier::Premium
    end
    account.kind = StorageModels::Kind::Storage
    account.encryption = StorageModels::Encryption.new.tap do |encrypt|
      encrypt.services = StorageModels::EncryptionServices.new.tap do |services|
        services.blob = StorageModels::EncryptionService.new.tap do |service|
          service.enabled = false
        end
      end
    end
  end
  print_item storage_account = storage_client.storage_accounts.create(GROUP_NAME, storage_account_name, storage_create_params)
  puts 'Creating a virtual network for the VM'
  vnet_create_params = NetworkModels::VirtualNetwork.new.tap do |vnet|
    vnet.location = LOCATION
    vnet.address_space = NetworkModels::AddressSpace.new.tap do |addr_space|
      addr_space.address_prefixes = ['10.0.0.0/16']
    end
    vnet.dhcp_options = NetworkModels::DhcpOptions.new.tap do |dhcp|
      dhcp.dns_servers = ['8.8.8.8']
    end
    vnet.subnets = [
        NetworkModels::Subnet.new.tap do |subnet|
          subnet.name = 'rubySampleSubnet'
          subnet.address_prefix = '10.0.0.0/24'
        end
    ]
  end
  print_item vnet = network_client.virtual_networks.create_or_update(GROUP_NAME, 'sample-ruby-vnet', vnet_create_params)
  puts 'Creating a public IP address for the VM'
  public_ip_params = NetworkModels::PublicIPAddress.new.tap do |ip|
    ip.location = LOCATION
    ip.public_ipallocation_method = NetworkModels::IPAllocationMethod::Dynamic
    ip.dns_settings = NetworkModels::PublicIPAddressDnsSettings.new.tap do |dns|
      dns.domain_name_label = 'msi-vm-domain-name-label'
    end
  end
  print_item public_ip = network_client.public_ipaddresses.create_or_update(GROUP_NAME, 'sample-ruby-pubip', public_ip_params)
  puts 'Creating VM'
  vm = create_vm(compute_client, network_client, authorization_client, LOCATION, 'msi-vm', storage_account, vnet.subnets[0], public_ip, user_assigned_identity)
   By default, the MSI accounts have no permissions
   Next is the assignment of permissions to the account
   example is Resource group access as Contributor, but
   you can assign any permissions you'd want
  msi_accounts_to_assign = []
  if SYSTEM_ASSIGNED_IDENTITY
    msi_accounts_to_assign.push(vm.identity.principal_id)
  end
  if USER_ASSIGNED_IDENTITY
    msi_accounts_to_assign.push(user_assigned_identity.principal_id)
  end
  puts "Getting the Role ID of Contributor of a Resource group: {GROUP_NAME}"
  role_name = 'Contributor'
  roles = authorization_client.role_definitions.list(resource_group.id, "roleName eq '{role_name}'")
  print_item contributor_role = roles.first
   Adding resource group scope to the MSI identities
  puts 'Creating the role assignment for the VM'
  msi_accounts_to_assign.each do |msi_identity|
    role_assignment_params = AuthorizationModels::RoleAssignmentCreateParameters.new.tap do |role_param|
      role_param.properties = AuthorizationModels::RoleAssignmentProperties.new.tap do |property|
        property.principal_id = msi_identity
        property.role_definition_id = contributor_role.id
      end
    end
    authorization_client.role_assignments.create(resource_group.id, SecureRandom.uuid, role_assignment_params)
  end
  puts 'Listing all of the resources within the group'
  resource_client.resources.list_by_resource_group(GROUP_NAME).each do |res|
    print_item res
  end
  puts ''
  export_template(resource_client)
  puts "Thank you for creating managed service identity Azure VM."
  puts "Use `netstat -tlnp` command verify that MSI service is running at 127.0.0.1:50342 address."
  puts "Connect to your new virtual machine via: 'ssh -p 22 {vm.os_profile.admin_username}@{public_ip.dns_settings.fqdn}'. Admin Password is: Pa$$w0rd92"
  puts 'Press any key to continue and delete the sample resources'
  gets
   Delete Resource group and everything in it
  puts 'Delete Resource Group'
  resource_client.resource_groups.delete(GROUP_NAME)
  puts "\nDeleted: {GROUP_NAME}"
end
def run_example subscription_id = ENV [ 'AZURE_SUBSCRIPTION_ID' ] || '11111111-1111-1111-1111-111111111111' MsRest . use_ssl_cert options = { tenant_id : ENV [ 'AZURE_TENANT_ID' ] , client_id : ENV [ 'AZURE_CLIENT_ID' ] , client_secret : ENV [ 'AZURE_CLIENT_SECRET' ] , subscription_id : subscription_id , } resource_client = Resources :: Client . new ( options ) network_client = Network :: Client . new ( options ) storage_client = Storage :: Client . new ( options ) compute_client = Compute :: Client . new ( options ) authorization_client = Authorization :: Client . new ( options ) resource_group_params = resource_client . model_classes . resource_group . new . tap do | rg | rg . location = LOCATION end puts 'Create Resource Group' print_group resource_group = resource_client . resource_groups . create_or_update ( GROUP_NAME , resource_group_params ) if USER_ASSIGNED_IDENTITY puts 'Create User Assigned Identity' msi_client = Msi :: Client . new ( options ) identity_create_params = MsiModels :: Identity . new . tap do | identity | identity . location = LOCATION end print_item user_assigned_identity = msi_client . user_assigned_identities . create_or_update ( GROUP_NAME , "myMsiIdentity" , identity_create_params ) end postfix = rand ( 1000 ) storage_account_name = "rubystor#{postfix}" puts "Creating a premium storage account with encryption off named #{storage_account_name} in resource group #{GROUP_NAME}" storage_create_params = StorageModels :: StorageAccountCreateParameters . new . tap do | account | account . location = LOCATION account . sku = StorageModels :: Sku . new . tap do | sku | sku . name = StorageModels :: SkuName :: PremiumLRS sku . tier = StorageModels :: SkuTier :: Premium end account . kind = StorageModels :: Kind :: Storage account . encryption = StorageModels :: Encryption . new . tap do | encrypt | encrypt . services = StorageModels :: EncryptionServices . new . tap do | services | services . blob = StorageModels :: EncryptionService . new . tap do | service | service . enabled = false end end end end print_item storage_account = storage_client . storage_accounts . create ( GROUP_NAME , storage_account_name , storage_create_params ) puts 'Creating a virtual network for the VM' vnet_create_params = NetworkModels :: VirtualNetwork . new . tap do | vnet | vnet . location = LOCATION vnet . address_space = NetworkModels :: AddressSpace . new . tap do | addr_space | addr_space . address_prefixes = [ '10.0.0.0/16' ] end vnet . dhcp_options = NetworkModels :: DhcpOptions . new . tap do | dhcp | dhcp . dns_servers = [ '8.8.8.8' ] end vnet . subnets = [ NetworkModels :: Subnet . new . tap do | subnet | subnet . name = 'rubySampleSubnet' subnet . address_prefix = '10.0.0.0/24' end ] end print_item vnet = network_client . virtual_networks . create_or_update ( GROUP_NAME , 'sample-ruby-vnet' , vnet_create_params ) puts 'Creating a public IP address for the VM' public_ip_params = NetworkModels :: PublicIPAddress . new . tap do | ip | ip . location = LOCATION ip . public_ipallocation_method = NetworkModels :: IPAllocationMethod :: Dynamic ip . dns_settings = NetworkModels :: PublicIPAddressDnsSettings . new . tap do | dns | dns . domain_name_label = 'msi-vm-domain-name-label' end end print_item public_ip = network_client . public_ipaddresses . create_or_update ( GROUP_NAME , 'sample-ruby-pubip' , public_ip_params ) puts 'Creating VM' vm = create_vm ( compute_client , network_client , authorization_client , LOCATION , 'msi-vm' , storage_account , vnet . subnets [ 0 ] , public_ip , user_assigned_identity ) msi_accounts_to_assign = [ ] if SYSTEM_ASSIGNED_IDENTITY msi_accounts_to_assign . push ( vm . identity . principal_id ) end if USER_ASSIGNED_IDENTITY msi_accounts_to_assign . push ( user_assigned_identity . principal_id ) end puts "Getting the Role ID of Contributor of a Resource group: #{GROUP_NAME}" role_name = 'Contributor' roles = authorization_client . role_definitions . list ( resource_group . id , "roleName eq '#{role_name}'" ) print_item contributor_role = roles . first puts 'Creating the role assignment for the VM' msi_accounts_to_assign . each do | msi_identity | role_assignment_params = AuthorizationModels :: RoleAssignmentCreateParameters . new . tap do | role_param | role_param . properties = AuthorizationModels :: RoleAssignmentProperties . new . tap do | property | property . principal_id = msi_identity property . role_definition_id = contributor_role . id end end authorization_client . role_assignments . create ( resource_group . id , SecureRandom . uuid , role_assignment_params ) end puts 'Listing all of the resources within the group' resource_client . resources . list_by_resource_group ( GROUP_NAME ) . each do | res | print_item res end puts '' export_template ( resource_client ) puts "Thank you for creating managed service identity Azure VM." puts "Use `netstat -tlnp` command verify that MSI service is running at 127.0.0.1:50342 address." puts "Connect to your new virtual machine via: 'ssh -p 22 #{vm.os_profile.admin_username}@#{public_ip.dns_settings.fqdn}'. Admin Password is: Pa$$w0rd92" puts 'Press any key to continue and delete the sample resources' gets puts 'Delete Resource Group' resource_client . resource_groups . delete ( GROUP_NAME ) puts "\nDeleted: #{GROUP_NAME}" end
This sample shows how to create a Azure virtual machines with Managed Service Identity using the Azure Resource Manager APIs for Ruby.
This sample shows how to create a Azure virtual machines with Managed Service Identity using the Azure Resource Manager APIs for Ruby .
# # Create the Resource Manager Client with an Application (service principal) token provider # # your Azure Subscription Id # # Managing resource groups # # Create Resource group # Create a user assigned identity # Create storage account # By default, the MSI accounts have no permissions # Next is the assignment of permissions to the account # example is Resource group access as Contributor, but # you can assign any permissions you'd want # Adding resource group scope to the MSI identities # Delete Resource group and everything in it