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



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fog/azurerm/models/compute/server.rb', line 41

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

    subscription_id = get_subscription_id(vm.id)
    hash['os_disk_id'] = "/subscriptions/#{subscription_id}/resourceGroups/#{hash['resource_group']}/providers/Microsoft.Compute/disks/#{hash['os_disk_name']}"

    hash['os_disk_size'] = vm.storage_profile.os_disk.disk_size_gb
    if vm.storage_profile.os_disk.vhd.nil?
      hash['managed_disk_storage_type'] = vm.storage_profile.os_disk.managed_disk.
    else
      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]
    end

    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::Compute::AzureRM::DataDisk.new
      hash['data_disks'] << data_disk.merge_attributes(Fog::Compute::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['tags'] = vm.tags

  unless vm.instance_view.nil?
    hash['platform_update_domain'] = vm.instance_view.platform_update_domain
    hash['platform_fault_domain'] = vm.instance_view.platform_fault_domain
  end

  hash
end

Instance Method Details

#attach_data_disk(disk_name, disk_size, storage_account_name, async = false) ⇒ Object



166
167
168
169
# File 'lib/fog/azurerm/models/compute/server.rb', line 166

def attach_data_disk(disk_name, disk_size, , async = false)
  response = service.attach_data_disk_to_vm(data_disk_params(disk_name, disk_size, ), async)
  async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response))
end

#attach_managed_disk(disk_name, disk_resource_group, async = false, caching = 'None') ⇒ Object



176
177
178
179
# File 'lib/fog/azurerm/models/compute/server.rb', line 176

def attach_managed_disk(disk_name, disk_resource_group, async = false, caching = 'None')
  response = service.attach_data_disk_to_vm(data_disk_params(disk_name, nil, nil, disk_resource_group, caching), async)
  async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response))
end

#deallocate(async = false) ⇒ Object



147
148
149
150
# File 'lib/fog/azurerm/models/compute/server.rb', line 147

def deallocate(async = false)
  response = service.deallocate_virtual_machine(resource_group, name, async)
  async ? create_fog_async_response(response) : response
end

#delete_extra_resourcesObject



186
187
188
189
190
191
# File 'lib/fog/azurerm/models/compute/server.rb', line 186

def delete_extra_resources
  unless vhd_path.nil? || !managed_disk_storage_type.nil?
    service.delete_image(resource_group, name)
    (resource_group, , name)
  end
end

#destroy(async = false) ⇒ Object



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

def destroy(async = false)
  response = service.delete_virtual_machine(resource_group, name, async)
  async ? create_fog_async_response(response) : response
end

#detach_data_disk(disk_name, async = false) ⇒ Object



171
172
173
174
# File 'lib/fog/azurerm/models/compute/server.rb', line 171

def detach_data_disk(disk_name, async = false)
  response = service.detach_data_disk_from_vm(resource_group, name, disk_name, async)
  async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response))
end

#detach_managed_disk(disk_name, async = false) ⇒ Object



181
182
183
184
# File 'lib/fog/azurerm/models/compute/server.rb', line 181

def detach_managed_disk(disk_name, async = false)
  response = service.detach_data_disk_from_vm(resource_group, name, disk_name, async)
  async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response))
end

#generalize(async = false) ⇒ Object



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

def generalize(async = false)
  response = service.generalize_virtual_machine(resource_group, name, async)
  async ? create_fog_async_response(response) : response
end

#list_available_sizes(async = false) ⇒ Object



157
158
159
160
# File 'lib/fog/azurerm/models/compute/server.rb', line 157

def list_available_sizes(async = false)
  response = service.list_available_sizes_for_virtual_machine(resource_group, name, async)
  async ? create_fog_async_response(response) : response
end

#power_off(async = false) ⇒ Object



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

def power_off(async = false)
  response = service.power_off_virtual_machine(resource_group, name, async)
  async ? create_fog_async_response(response) : response
end

#redeploy(async = false) ⇒ Object



152
153
154
155
# File 'lib/fog/azurerm/models/compute/server.rb', line 152

def redeploy(async = false)
  response = service.redeploy_virtual_machine(resource_group, name, async)
  async ? create_fog_async_response(response) : response
end

#restart(async = false) ⇒ Object



142
143
144
145
# File 'lib/fog/azurerm/models/compute/server.rb', line 142

def restart(async = false)
  response = service.restart_virtual_machine(resource_group, name, async)
  async ? create_fog_async_response(response) : response
end

#save(async = false) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fog/azurerm/models/compute/server.rb', line 99

def save(async = false)
  requires :name, :location, :resource_group, :vm_size, :username, :network_interface_card_ids
  requires :publisher, :offer, :sku, :version if vhd_path.nil? && image_ref.nil?
  requires :storage_account_name if managed_disk_storage_type.nil?
  requires :managed_disk_storage_type if .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))
    vm = service.get_virtual_machine(resource_group, name, false)
    merge_attributes(Fog::Compute::AzureRM::Server.parse(vm))
  end
end

#start(async = false) ⇒ Object



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

def start(async = false)
  response = service.start_virtual_machine(resource_group, name, async)
  async ? create_fog_async_response(response) : response
end

#update_attributesObject



193
194
195
196
# File 'lib/fog/azurerm/models/compute/server.rb', line 193

def update_attributes
  vm = service.get_virtual_machine(resource_group, name, false)
  merge_attributes(Fog::Compute::AzureRM::Server.parse(vm))
end

#vm_status(async = false) ⇒ Object



162
163
164
# File 'lib/fog/azurerm/models/compute/server.rb', line 162

def vm_status(async = false)
  service.check_vm_status(resource_group, name, async)
end