example.rb
def create_vm(compute_client, network_client, authorization_client, location, vm_name, storage_acct, subnet, public_ip, user_assigned_identity)
  puts "Creating a network interface for the VM #{vm_name}"
  print_item nic = network_client.network_interfaces.create_or_update(
      GROUP_NAME,
      "sample-ruby-nic-#{vm_name}",
      NetworkModels::NetworkInterface.new.tap do |interface|
        interface.location = LOCATION
        interface.ip_configurations = [
            NetworkModels::NetworkInterfaceIPConfiguration.new.tap do |nic_conf|
              nic_conf.name = "sample-ruby-nic-#{vm_name}"
              nic_conf.private_ipallocation_method = NetworkModels::IPAllocationMethod::Dynamic
              nic_conf.subnet = subnet
              nic_conf.public_ipaddress = public_ip
            end
        ]
      end
  )

  puts 'Creating a Ubuntu 16.04.0-LTS Standard DS2 V2 virtual machine w/ a public IP'
  vm_create_params = ComputeModels::VirtualMachine.new.tap do |vm|
    vm.location = location
    vm.os_profile = ComputeModels::OSProfile.new.tap do |os_profile|
      os_profile.computer_name = vm_name
      os_profile.admin_username = 'notAdmin'
      os_profile.admin_password = 'Pa$$w0rd92'
    end

    vm.storage_profile = ComputeModels::StorageProfile.new.tap do |store_profile|
      store_profile.image_reference = ComputeModels::ImageReference.new.tap do |ref|
        ref.publisher = 'canonical'
        ref.offer = 'UbuntuServer'
        ref.sku = '16.04.0-LTS'
        ref.version = 'latest'
      end
      store_profile.os_disk = ComputeModels::OSDisk.new.tap do |os_disk|
        os_disk.name = "sample-os-disk-#{vm_name}"
        os_disk.caching = ComputeModels::CachingTypes::None
        os_disk.create_option = ComputeModels::DiskCreateOptionTypes::FromImage
        os_disk.vhd = ComputeModels::VirtualHardDisk.new.tap do |vhd|
          vhd.uri = "https://#{storage_acct.name}.blob.core.windows.net/rubycontainer/#{vm_name}.vhd"
        end
      end
    end

    vm.hardware_profile = ComputeModels::HardwareProfile.new.tap do |hardware|
      hardware.vm_size = ComputeModels::VirtualMachineSizeTypes::StandardDS2V2
    end

    vm.network_profile = ComputeModels::NetworkProfile.new.tap do |net_profile|
      net_profile.network_interfaces = [
          ComputeModels::NetworkInterfaceReference.new.tap do |ref|
            ref.id = nic.id
            ref.primary = true
          end
      ]
    end

    puts 'Create an identity'
    if USER_ASSIGNED_IDENTITY && SYSTEM_ASSIGNED_IDENTITY
      vm.identity = ComputeModels::VirtualMachineIdentity.new.tap do |identity|
        identity.type = ResourceIdentityType::SystemAssignedUserAssigned
        identity.identity_ids = [user_assigned_identity.id]
      end
    elsif USER_ASSIGNED_IDENTITY
      # Use User Assigned Identity for the VM
      vm.identity = ComputeModels::VirtualMachineIdentity.new.tap do |identity|
        identity.type = ResourceIdentityType::UserAssigned
        identity.identity_ids = [user_assigned_identity.id]
      end
    elsif SYSTEM_ASSIGNED_IDENTITY
      # Use System Assigned Identity for the VM
      vm.identity = ComputeModels::VirtualMachineIdentity.new.tap do |identity|
        identity.type = ResourceIdentityType::SystemAssigned
      end
    end
  end

  ssh_pub_location = File.expand_path('~/.ssh/id_rsa.pub')
  if File.exists? ssh_pub_location
    puts "Found SSH public key in #{ssh_pub_location}. Disabling password and enabling SSH authentication."
    key_data = File.read(ssh_pub_location)
    puts "Using public key: #{key_data}"
    vm_create_params.os_profile.linux_configuration = ComputeModels::LinuxConfiguration.new.tap do |linux|
      linux.disable_password_authentication = true
      linux.ssh = ComputeModels::SshConfiguration.new.tap do |ssh_config|
        ssh_config.public_keys = [
            ComputeModels::SshPublicKey.new.tap do |pub_key|
              pub_key.key_data = key_data
              pub_key.path = '/home/notAdmin/.ssh/authorized_keys'
            end
        ]
      end
    end
  end

  puts 'Created VM'
  print_item vm = compute_client.virtual_machines.create_or_update(GROUP_NAME, "sample-ruby-vm-#{vm_name}", vm_create_params)

  puts "Install Managed Service Identity Extension"
  ext_name = 'msiextension'
  vm_extension = ComputeModels::VirtualMachineExtension.new.tap do |extension|
    extension.publisher = 'Microsoft.ManagedIdentity'
    extension.virtual_machine_extension_type = 'ManagedIdentityExtensionForLinux'
    extension.type_handler_version = '1.0'
    extension.auto_upgrade_minor_version = true
    extension.settings = Hash.new.tap do |settings|
      settings['port'] = '50342'
    end
    extension.location = LOCATION
  end

  compute_client.virtual_machine_extensions.create_or_update(GROUP_NAME, "sample-ruby-vm-#{vm_name}", ext_name, vm_extension)

  compute_client.virtual_machines.get(GROUP_NAME, "sample-ruby-vm-#{vm_name}")
end
# Create a Virtual Machine, Install MSI extension and return it
Create a Virtual Machine, Install MSI extension and return it
Create a Virtual Machine Install MSI extension and return it
def create_vm(compute_client, network_client, authorization_client, location, vm_name, storage_acct, subnet, public_ip, user_assigned_identity)
  puts "Creating a network interface for the VM #{vm_name}"
  print_item nic = network_client.network_interfaces.create_or_update(
      GROUP_NAME,
      "sample-ruby-nic-#{vm_name}",
      NetworkModels::NetworkInterface.new.tap do |interface|
        interface.location = LOCATION
        interface.ip_configurations = [
            NetworkModels::NetworkInterfaceIPConfiguration.new.tap do |nic_conf|
              nic_conf.name = "sample-ruby-nic-#{vm_name}"
              nic_conf.private_ipallocation_method = NetworkModels::IPAllocationMethod::Dynamic
              nic_conf.subnet = subnet
              nic_conf.public_ipaddress = public_ip
            end
        ]
      end
  )
  puts 'Creating a Ubuntu 16.04.0-LTS Standard DS2 V2 virtual machine w/ a public IP'
  vm_create_params = ComputeModels::VirtualMachine.new.tap do |vm|
    vm.location = location
    vm.os_profile = ComputeModels::OSProfile.new.tap do |os_profile|
      os_profile.computer_name = vm_name
      os_profile.admin_username = 'notAdmin'
      os_profile.admin_password = 'Pa$$w0rd92'
    end
    vm.storage_profile = ComputeModels::StorageProfile.new.tap do |store_profile|
      store_profile.image_reference = ComputeModels::ImageReference.new.tap do |ref|
        ref.publisher = 'canonical'
        ref.offer = 'UbuntuServer'
        ref.sku = '16.04.0-LTS'
        ref.version = 'latest'
      end
      store_profile.os_disk = ComputeModels::OSDisk.new.tap do |os_disk|
        os_disk.name = "sample-os-disk-#{vm_name}"
        os_disk.caching = ComputeModels::CachingTypes::None
        os_disk.create_option = ComputeModels::DiskCreateOptionTypes::FromImage
        os_disk.vhd = ComputeModels::VirtualHardDisk.new.tap do |vhd|
          vhd.uri = "https://#{storage_acct.name}.blob.core.windows.net/rubycontainer/#{vm_name}.vhd"
        end
      end
    end
    vm.hardware_profile = ComputeModels::HardwareProfile.new.tap do |hardware|
      hardware.vm_size = ComputeModels::VirtualMachineSizeTypes::StandardDS2V2
    end
    vm.network_profile = ComputeModels::NetworkProfile.new.tap do |net_profile|
      net_profile.network_interfaces = [
          ComputeModels::NetworkInterfaceReference.new.tap do |ref|
            ref.id = nic.id
            ref.primary = true
          end
      ]
    end
    puts 'Create an identity'
    if USER_ASSIGNED_IDENTITY && SYSTEM_ASSIGNED_IDENTITY
      vm.identity = ComputeModels::VirtualMachineIdentity.new.tap do |identity|
        identity.type = ResourceIdentityType::SystemAssignedUserAssigned
        identity.identity_ids = [user_assigned_identity.id]
      end
    elsif USER_ASSIGNED_IDENTITY
      vm.identity = ComputeModels::VirtualMachineIdentity.new.tap do |identity|
        identity.type = ResourceIdentityType::UserAssigned
        identity.identity_ids = [user_assigned_identity.id]
      end
    elsif SYSTEM_ASSIGNED_IDENTITY
      vm.identity = ComputeModels::VirtualMachineIdentity.new.tap do |identity|
        identity.type = ResourceIdentityType::SystemAssigned
      end
    end
  end
  ssh_pub_location = File.expand_path('~/.ssh/id_rsa.pub')
  if File.exists? ssh_pub_location
    puts "Found SSH public key in #{ssh_pub_location}. Disabling password and enabling SSH authentication."
    key_data = File.read(ssh_pub_location)
    puts "Using public key: #{key_data}"
    vm_create_params.os_profile.linux_configuration = ComputeModels::LinuxConfiguration.new.tap do |linux|
      linux.disable_password_authentication = true
      linux.ssh = ComputeModels::SshConfiguration.new.tap do |ssh_config|
        ssh_config.public_keys = [
            ComputeModels::SshPublicKey.new.tap do |pub_key|
              pub_key.key_data = key_data
              pub_key.path = '/home/notAdmin/.ssh/authorized_keys'
            end
        ]
      end
    end
  end
  puts 'Created VM'
  print_item vm = compute_client.virtual_machines.create_or_update(GROUP_NAME, "sample-ruby-vm-#{vm_name}", vm_create_params)
  puts "Install Managed Service Identity Extension"
  ext_name = 'msiextension'
  vm_extension = ComputeModels::VirtualMachineExtension.new.tap do |extension|
    extension.publisher = 'Microsoft.ManagedIdentity'
    extension.virtual_machine_extension_type = 'ManagedIdentityExtensionForLinux'
    extension.type_handler_version = '1.0'
    extension.auto_upgrade_minor_version = true
    extension.settings = Hash.new.tap do |settings|
      settings['port'] = '50342'
    end
    extension.location = LOCATION
  end
  compute_client.virtual_machine_extensions.create_or_update(GROUP_NAME, "sample-ruby-vm-#{vm_name}", ext_name, vm_extension)
  compute_client.virtual_machines.get(GROUP_NAME, "sample-ruby-vm-#{vm_name}")
end
def create_vm ( compute_client , network_client , authorization_client , location , vm_name , storage_acct , subnet , public_ip , user_assigned_identity ) puts "Creating a network interface for the VM #{vm_name}" print_item nic = network_client . network_interfaces . create_or_update ( GROUP_NAME , "sample-ruby-nic-#{vm_name}" , NetworkModels :: NetworkInterface . new . tap do | interface | interface . location = LOCATION interface . ip_configurations = [ NetworkModels :: NetworkInterfaceIPConfiguration . new . tap do | nic_conf | nic_conf . name = "sample-ruby-nic-#{vm_name}" nic_conf . private_ipallocation_method = NetworkModels :: IPAllocationMethod :: Dynamic nic_conf . subnet = subnet nic_conf . public_ipaddress = public_ip end ] end ) puts 'Creating a Ubuntu 16.04.0-LTS Standard DS2 V2 virtual machine w/ a public IP' vm_create_params = ComputeModels :: VirtualMachine . new . tap do | vm | vm . location = location vm . os_profile = ComputeModels :: OSProfile . new . tap do | os_profile | os_profile . computer_name = vm_name os_profile . admin_username = 'notAdmin' os_profile . admin_password = 'Pa$$w0rd92' end vm . storage_profile = ComputeModels :: StorageProfile . new . tap do | store_profile | store_profile . image_reference = ComputeModels :: ImageReference . new . tap do | ref | ref . publisher = 'canonical' ref . offer = 'UbuntuServer' ref . sku = '16.04.0-LTS' ref . version = 'latest' end store_profile . os_disk = ComputeModels :: OSDisk . new . tap do | os_disk | os_disk . name = "sample-os-disk-#{vm_name}" os_disk . caching = ComputeModels :: CachingTypes :: None os_disk . create_option = ComputeModels :: DiskCreateOptionTypes :: FromImage os_disk . vhd = ComputeModels :: VirtualHardDisk . new . tap do | vhd | vhd . uri = "https://#{storage_acct.name}.blob.core.windows.net/rubycontainer/#{vm_name}.vhd" end end end vm . hardware_profile = ComputeModels :: HardwareProfile . new . tap do | hardware | hardware . vm_size = ComputeModels :: VirtualMachineSizeTypes :: StandardDS2V2 end vm . network_profile = ComputeModels :: NetworkProfile . new . tap do | net_profile | net_profile . network_interfaces = [ ComputeModels :: NetworkInterfaceReference . new . tap do | ref | ref . id = nic . id ref . primary = true end ] end puts 'Create an identity' if USER_ASSIGNED_IDENTITY && SYSTEM_ASSIGNED_IDENTITY vm . identity = ComputeModels :: VirtualMachineIdentity . new . tap do | identity | identity . type = ResourceIdentityType :: SystemAssignedUserAssigned identity . identity_ids = [ user_assigned_identity . id ] end elsif USER_ASSIGNED_IDENTITY vm . identity = ComputeModels :: VirtualMachineIdentity . new . tap do | identity | identity . type = ResourceIdentityType :: UserAssigned identity . identity_ids = [ user_assigned_identity . id ] end elsif SYSTEM_ASSIGNED_IDENTITY vm . identity = ComputeModels :: VirtualMachineIdentity . new . tap do | identity | identity . type = ResourceIdentityType :: SystemAssigned end end end ssh_pub_location = File . expand_path ( '~/.ssh/id_rsa.pub' ) if File . exists? ssh_pub_location puts "Found SSH public key in #{ssh_pub_location}. Disabling password and enabling SSH authentication." key_data = File . read ( ssh_pub_location ) puts "Using public key: #{key_data}" vm_create_params . os_profile . linux_configuration = ComputeModels :: LinuxConfiguration . new . tap do | linux | linux . disable_password_authentication = true linux . ssh = ComputeModels :: SshConfiguration . new . tap do | ssh_config | ssh_config . public_keys = [ ComputeModels :: SshPublicKey . new . tap do | pub_key | pub_key . key_data = key_data pub_key . path = '/home/notAdmin/.ssh/authorized_keys' end ] end end end puts 'Created VM' print_item vm = compute_client . virtual_machines . create_or_update ( GROUP_NAME , "sample-ruby-vm-#{vm_name}" , vm_create_params ) puts "Install Managed Service Identity Extension" ext_name = 'msiextension' vm_extension = ComputeModels :: VirtualMachineExtension . new . tap do | extension | extension . publisher = 'Microsoft.ManagedIdentity' extension . virtual_machine_extension_type = 'ManagedIdentityExtensionForLinux' extension . type_handler_version = '1.0' extension . auto_upgrade_minor_version = true extension . settings = Hash . new . tap do | settings | settings [ 'port' ] = '50342' end extension . location = LOCATION end compute_client . virtual_machine_extensions . create_or_update ( GROUP_NAME , "sample-ruby-vm-#{vm_name}" , ext_name , vm_extension ) compute_client . virtual_machines . get ( GROUP_NAME , "sample-ruby-vm-#{vm_name}" ) end
Create a Virtual Machine, Install MSI extension and return it
Create a Virtual Machine Install MSI extension and return it
# Use User Assigned Identity for the VM # Use System Assigned Identity for the VM
compute_client
network_client
authorization_client
location
vm_name
storage_acct
subnet
public_ip
user_assigned_identity