Class: Fog::Compute::AzureRM::Server

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/azurerm/models/compute/server.rb

Overview

This class is giving implementation of create/save and delete/destroy for Virtual Machine.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(vm) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fog/azurerm/models/compute/server.rb', line 34

def self.parse(vm)
  hash = {}
  hash['id'] = vm.id
  hash['name'] = vm.name
  hash['location'] = vm.location
  hash['resource_group'] = get_resource_group_from_id(vm.id)
  hash['vm_size'] = vm.hardware_profile.vm_size unless vm.hardware_profile.vm_size.nil?
  unless vm.storage_profile.nil?
    hash['os_disk_name'] = vm.storage_profile.os_disk.name
    hash['os_disk_vhd_uri'] = vm.storage_profile.os_disk.vhd.uri
    hash['storage_account_name'] = hash['os_disk_vhd_uri'].split('/')[2].split('.')[0]
    hash['os_disk_caching'] = vm.storage_profile.os_disk.caching
    unless vm.storage_profile.image_reference.nil?
      hash['publisher'] = vm.storage_profile.image_reference.publisher
      hash['offer'] = vm.storage_profile.image_reference.offer
      hash['sku'] = vm.storage_profile.image_reference.sku
      hash['version'] = vm.storage_profile.image_reference.version
    end
  end
  hash['username'] = vm.os_profile.admin_username
  hash['custom_data'] = vm.os_profile.custom_data
  hash['data_disks'] = []

  unless vm.storage_profile.data_disks.nil?
    vm.storage_profile.data_disks.each do |disk|
      data_disk = Fog::Storage::AzureRM::DataDisk.new
      hash['data_disks'] << data_disk.merge_attributes(Fog::Storage::AzureRM::DataDisk.parse(disk))
    end
  end

  hash['disable_password_authentication'] = false
  hash['disable_password_authentication'] = vm.os_profile.linux_configuration.disable_password_authentication unless vm.os_profile.linux_configuration.nil?
  if vm.os_profile.windows_configuration
    hash['provision_vm_agent'] = vm.os_profile.windows_configuration.provision_vmagent
    hash['enable_automatic_updates'] = vm.os_profile.windows_configuration.enable_automatic_updates
  end
  hash['network_interface_card_ids'] = vm.network_profile.network_interfaces.map(&:id)
  hash['availability_set_id'] = vm.availability_set.id unless vm.availability_set.nil?

  hash
end

Instance Method Details

#attach_data_disk(disk_name, disk_size, storage_account_name) ⇒ Object



133
134
135
136
# File 'lib/fog/azurerm/models/compute/server.rb', line 133

def attach_data_disk(disk_name, disk_size, )
  vm = service.attach_data_disk_to_vm(resource_group, name, disk_name, disk_size, )
  merge_attributes(Fog::Compute::AzureRM::Server.parse(vm))
end

#deallocateObject



117
118
119
# File 'lib/fog/azurerm/models/compute/server.rb', line 117

def deallocate
  service.deallocate_virtual_machine(resource_group, name)
end

#destroyObject



97
98
99
# File 'lib/fog/azurerm/models/compute/server.rb', line 97

def destroy
  service.delete_virtual_machine(resource_group, name)
end

#detach_data_disk(disk_name) ⇒ Object



138
139
140
141
# File 'lib/fog/azurerm/models/compute/server.rb', line 138

def detach_data_disk(disk_name)
  vm = service.detach_data_disk_from_vm(resource_group, name, disk_name)
  merge_attributes(Fog::Compute::AzureRM::Server.parse(vm))
end

#generalizeObject



101
102
103
# File 'lib/fog/azurerm/models/compute/server.rb', line 101

def generalize
  service.generalize_virtual_machine(resource_group, name)
end

#list_available_sizesObject



125
126
127
# File 'lib/fog/azurerm/models/compute/server.rb', line 125

def list_available_sizes
  service.list_available_sizes_for_virtual_machine(resource_group, name)
end

#power_offObject



105
106
107
# File 'lib/fog/azurerm/models/compute/server.rb', line 105

def power_off
  service.power_off_virtual_machine(resource_group, name)
end

#redeployObject



121
122
123
# File 'lib/fog/azurerm/models/compute/server.rb', line 121

def redeploy
  service.redeploy_virtual_machine(resource_group, name)
end

#restartObject



113
114
115
# File 'lib/fog/azurerm/models/compute/server.rb', line 113

def restart
  service.restart_virtual_machine(resource_group, name)
end

#save(async = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fog/azurerm/models/compute/server.rb', line 76

def save(async = false)
  requires :name, :location, :resource_group, :vm_size, :storage_account_name,
           :username, :network_interface_card_ids
  requires :publisher, :offer, :sku, :version if vhd_path.nil?

  if platform_is_linux?(platform)
    requires :disable_password_authentication
  else
    requires :password
  end

  ssh_key_path = "/home/#{username}/.ssh/authorized_keys" unless ssh_key_data.nil?

  if async
    service.create_virtual_machine(virtual_machine_params(ssh_key_path), true)
  else
    vm = service.create_virtual_machine(virtual_machine_params(ssh_key_path))
    merge_attributes(Fog::Compute::AzureRM::Server.parse(vm))
  end
end

#startObject



109
110
111
# File 'lib/fog/azurerm/models/compute/server.rb', line 109

def start
  service.start_virtual_machine(resource_group, name)
end

#vm_statusObject



129
130
131
# File 'lib/fog/azurerm/models/compute/server.rb', line 129

def vm_status
  service.check_vm_status(resource_group, name)
end