fog-azure-rm 0.3.9 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/fog/azurerm/compute.rb +3 -2
- data/lib/fog/azurerm/docs/compute.md +36 -1
- data/lib/fog/azurerm/models/compute/server.rb +10 -5
- data/lib/fog/azurerm/requests/compute/{create_generalized_image.rb → create_image.rb} +9 -8
- data/lib/fog/azurerm/requests/compute/create_virtual_machine.rb +207 -133
- data/lib/fog/azurerm/requests/compute/{delete_generalized_image.rb → delete_image.rb} +4 -4
- data/lib/fog/azurerm/requests/compute/get_image.rb +45 -0
- data/lib/fog/azurerm/utilities/general.rb +4 -0
- data/lib/fog/azurerm/version.rb +1 -1
- data/test/api_stub.rb +1 -1
- data/test/api_stub/requests/compute/{generalized_image.rb → image.rb} +3 -3
- data/test/integration/server.rb +37 -25
- data/test/integration/server_custom_image.rb +21 -12
- data/test/requests/compute/test_create_image.rb +27 -0
- data/test/requests/compute/test_delete_image.rb +23 -0
- data/test/requests/compute/test_get_image.rb +23 -0
- metadata +9 -7
- data/test/requests/compute/test_create_generalized_image.rb +0 -27
- data/test/requests/compute/test_delete_generalized_image.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a19921af4507084f3170dd448ce4193f7bcd9075b0fca9376b0270324a70664
|
4
|
+
data.tar.gz: f44723252fdfa0cafb164e416643c04e5b212d6954cec36572408c461add8b5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9102c6cb48c96fa42d47b38ae5d5b73843bc63640e43178e78059fead565569847d65d628a84aefeae61be8a56abd6cf8eee28dbeff5118a90a3a5e3708ec069
|
7
|
+
data.tar.gz: fc2ecb7465efca20e516a19ca555bf97f77aa50eee8bfd2b68a31f8fb59d6cbbb3e8d2031ab06f13cd5268a85243badb799aa30ff791e560c081e6bccc4cf419
|
data/CHANGELOG.md
CHANGED
data/lib/fog/azurerm/compute.rb
CHANGED
@@ -41,8 +41,9 @@ module Fog
|
|
41
41
|
request :list_managed_disks_in_subscription
|
42
42
|
request :revoke_access_to_managed_disk
|
43
43
|
request :grant_access_to_managed_disk
|
44
|
-
request :
|
45
|
-
request :
|
44
|
+
request :create_image
|
45
|
+
request :delete_image
|
46
|
+
request :get_image
|
46
47
|
|
47
48
|
model_path 'fog/azurerm/models/compute'
|
48
49
|
model :availability_set
|
@@ -38,7 +38,6 @@ fog_compute_service.servers.check_vm_exists('<Resource Group Name>', '<VM Name>'
|
|
38
38
|
- To create VM with managed OS disk, use the _managed_disk_storage_type_ argument
|
39
39
|
- To create VM with unmanaged OS disk, use the _storage_account_name_ argument
|
40
40
|
|
41
|
-
|
42
41
|
### Virtual Machine (Managed OS Disk)
|
43
42
|
|
44
43
|
```ruby
|
@@ -66,6 +65,42 @@ fog_compute_service.servers.create(
|
|
66
65
|
)
|
67
66
|
```
|
68
67
|
|
68
|
+
**Info:**
|
69
|
+
|
70
|
+
- To create VM from Image, pass in the Image ID in `image_ref` attribute
|
71
|
+
|
72
|
+
**Limitation:**
|
73
|
+
|
74
|
+
- Image should be in the same region
|
75
|
+
- ONLY managed VM can be created from image
|
76
|
+
|
77
|
+
### Virtual Machine (Managed OS Disk from Image)
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
fog_compute_service.servers.create(
|
81
|
+
name: '<VM Name>',
|
82
|
+
location: '<Location>',
|
83
|
+
resource_group: '<Resource Group Name>',
|
84
|
+
tags: { key1: 'value1', key2: 'value2', keyN: 'valueN' },
|
85
|
+
vm_size: '<Virtual Machine Size>',
|
86
|
+
username: '<Username>',
|
87
|
+
disable_password_authentication: <True/False>,
|
88
|
+
network_interface_card_ids: ['/subscriptions/<Subscription Id>/resourceGroups/<Resource Group Name>/providers/Microsoft.Network/networkInterfaces/<Network Interface Id>'],
|
89
|
+
publisher: '<Publisher Name>', # Not required if custom image is being used
|
90
|
+
offer: '<Offer Name>', # Not required if custom image is being used
|
91
|
+
sku: '<SKU Name>', # Not required if custom image is being used
|
92
|
+
version: '<Version>', # Not required if custom image is being used
|
93
|
+
platform: '<OS Type>',
|
94
|
+
availability_set_id: '<Availability Set Id>', # [Optional]
|
95
|
+
password: '<Password>', # [Optional], if 'platform' partameter is 'Linux'.
|
96
|
+
image_ref: '<Image ID>', # [Optional], if you want to create the VM from a custom image.
|
97
|
+
custom_data: '<Custom Data Value>', # [Optional], if you want to add custom data in this VM.
|
98
|
+
os_disk_caching: '<Caching Type>', # [Optional], can be one of None, ReadOnly, ReadWrite
|
99
|
+
managed_disk_storage_type: '<Storage Account Type>', # [Optional], if storage_account_name is passed, can be StandardLRS or PremiumLRS
|
100
|
+
os_disk_size: <Disk Size> # [Optional], size of the os disk in GB (upto 1023)
|
101
|
+
)
|
102
|
+
```
|
103
|
+
|
69
104
|
### Virtual Machine (Unmanaged OS Disk)
|
70
105
|
|
71
106
|
```ruby
|
@@ -35,6 +35,7 @@ module Fog
|
|
35
35
|
attribute :tags
|
36
36
|
attribute :platform_update_domain
|
37
37
|
attribute :platform_fault_domain
|
38
|
+
attribute :image_ref
|
38
39
|
|
39
40
|
def self.parse(vm)
|
40
41
|
hash = {}
|
@@ -92,7 +93,7 @@ module Fog
|
|
92
93
|
|
93
94
|
def save(async = false)
|
94
95
|
requires :name, :location, :resource_group, :vm_size, :username, :network_interface_card_ids
|
95
|
-
requires :publisher, :offer, :sku, :version if vhd_path.nil? &&
|
96
|
+
requires :publisher, :offer, :sku, :version if vhd_path.nil? && image_ref.nil?
|
96
97
|
requires :storage_account_name if managed_disk_storage_type.nil?
|
97
98
|
requires :managed_disk_storage_type if storage_account_name.nil?
|
98
99
|
|
@@ -179,7 +180,7 @@ module Fog
|
|
179
180
|
|
180
181
|
def delete_extra_resources
|
181
182
|
unless vhd_path.nil? || !managed_disk_storage_type.nil?
|
182
|
-
service.
|
183
|
+
service.delete_image(resource_group, name)
|
183
184
|
delete_storage_account_or_container(resource_group, storage_account_name, name)
|
184
185
|
end
|
185
186
|
end
|
@@ -226,7 +227,8 @@ module Fog
|
|
226
227
|
os_disk_caching: os_disk_caching,
|
227
228
|
managed_disk_storage_type: managed_disk_storage_type,
|
228
229
|
os_disk_size: os_disk_size,
|
229
|
-
tags: tags
|
230
|
+
tags: tags,
|
231
|
+
image_ref: image_ref
|
230
232
|
}
|
231
233
|
end
|
232
234
|
|
@@ -242,8 +244,11 @@ module Fog
|
|
242
244
|
end
|
243
245
|
|
244
246
|
def delete_storage_account_or_container(resource_group, storage_account_name, vm_name)
|
245
|
-
|
246
|
-
|
247
|
+
if storage_account_name.nil?
|
248
|
+
delete_storage_account(resource_group)
|
249
|
+
else
|
250
|
+
delete_storage_container(resource_group, storage_account_name, vm_name)
|
251
|
+
end
|
247
252
|
end
|
248
253
|
|
249
254
|
def delete_storage_container(resource_group, storage_account_name, vm_name)
|
@@ -3,32 +3,32 @@ module Fog
|
|
3
3
|
class AzureRM
|
4
4
|
# This class provides the actual implementation for service calls.
|
5
5
|
class Real
|
6
|
-
def
|
7
|
-
msg = "Creating/Updating
|
6
|
+
def create_image(image_config)
|
7
|
+
msg = "Creating/Updating Image: #{image_config[:vm_name]}-osImage"
|
8
8
|
Fog::Logger.debug msg
|
9
9
|
image_name = "#{image_config[:vm_name]}-osImage"
|
10
|
-
image =
|
10
|
+
image = setup_params(image_config)
|
11
11
|
begin
|
12
12
|
image_obj = @compute_mgmt_client.images.create_or_update(image_config[:resource_group], image_name, image)
|
13
13
|
rescue MsRestAzure::AzureOperationError => e
|
14
14
|
raise_azure_exception(e, msg)
|
15
15
|
end
|
16
|
-
Fog::Logger.debug "
|
16
|
+
Fog::Logger.debug "Image #{image_name} created/updated successfully."
|
17
17
|
image_obj
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
21
21
|
|
22
|
-
def
|
22
|
+
def setup_params(image_config)
|
23
23
|
storage_profile_image = Azure::ARM::Compute::Models::ImageStorageProfile.new
|
24
|
-
storage_profile_image.os_disk =
|
24
|
+
storage_profile_image.os_disk = create_generalized_os_disk_image(image_config)
|
25
25
|
image = Azure::ARM::Compute::Models::Image.new
|
26
26
|
image.storage_profile = storage_profile_image
|
27
27
|
image.location = image_config[:location]
|
28
28
|
image
|
29
29
|
end
|
30
30
|
|
31
|
-
def
|
31
|
+
def create_generalized_os_disk_image(image_config)
|
32
32
|
os_disk_image = Azure::ARM::Compute::Models::ImageOSDisk.new
|
33
33
|
os_disk_image.os_type = image_config[:platform]
|
34
34
|
os_disk_image.os_state = 'Generalized'
|
@@ -37,9 +37,10 @@ module Fog
|
|
37
37
|
os_disk_image
|
38
38
|
end
|
39
39
|
end
|
40
|
+
|
40
41
|
# This class provides the mock implementation for unit tests.
|
41
42
|
class Mock
|
42
|
-
def
|
43
|
+
def create_image(*)
|
43
44
|
image_obj = {
|
44
45
|
'location' => 'West US',
|
45
46
|
'tags' => {
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'base64'
|
2
|
+
|
2
3
|
WHITE_SPACE = ' '.freeze
|
3
4
|
|
4
5
|
module Fog
|
@@ -7,122 +8,237 @@ module Fog
|
|
7
8
|
# This class provides the actual implementation for service calls.
|
8
9
|
class Real
|
9
10
|
def create_virtual_machine(vm_config, async = false)
|
10
|
-
|
11
|
+
vm_name = vm_config[:name]
|
12
|
+
rg_name = vm_config[:resource_group]
|
13
|
+
|
14
|
+
msg = "Creating Virtual Machine '#{vm_name}' in Resource Group '#{rg_name}'..."
|
11
15
|
Fog::Logger.debug msg
|
12
|
-
virtual_machine = Azure::ARM::Compute::Models::VirtualMachine.new
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
vm = Azure::ARM::Compute::Models::VirtualMachine.new
|
18
|
+
vm.location = vm_config[:location]
|
19
|
+
vm.tags = vm_config[:tags]
|
20
|
+
vm.availability_set = get_vm_availability_set(vm_config[:availability_set_id])
|
21
|
+
vm.hardware_profile = get_hardware_profile(vm_config[:vm_size])
|
22
|
+
vm.os_profile = get_os_profile(vm_config)
|
23
|
+
vm.network_profile = get_network_profile(vm_config[:network_interface_card_ids])
|
24
|
+
vm.storage_profile = get_storage_profile(vm_config)
|
19
25
|
|
20
|
-
string_data = vm_config[:custom_data]
|
21
|
-
string_data = WHITE_SPACE if string_data.nil?
|
22
|
-
encoded_data = Base64.strict_encode64(string_data)
|
23
|
-
virtual_machine.hardware_profile = define_hardware_profile(vm_config[:vm_size])
|
24
|
-
virtual_machine.storage_profile = define_storage_profile(vm_config[:name],
|
25
|
-
vm_config[:storage_account_name],
|
26
|
-
vm_config[:publisher],
|
27
|
-
vm_config[:offer],
|
28
|
-
vm_config[:sku],
|
29
|
-
vm_config[:version],
|
30
|
-
vm_config[:vhd_path],
|
31
|
-
vm_config[:os_disk_caching],
|
32
|
-
vm_config[:platform],
|
33
|
-
vm_config[:resource_group],
|
34
|
-
vm_config[:managed_disk_storage_type],
|
35
|
-
vm_config[:os_disk_size],
|
36
|
-
vm_config[:location])
|
37
|
-
|
38
|
-
virtual_machine.os_profile = if vm_config[:platform].casecmp(WINDOWS).zero?
|
39
|
-
define_windows_os_profile(vm_config[:name],
|
40
|
-
vm_config[:username],
|
41
|
-
vm_config[:password],
|
42
|
-
vm_config[:provision_vm_agent],
|
43
|
-
vm_config[:enable_automatic_updates],
|
44
|
-
encoded_data)
|
45
|
-
else
|
46
|
-
define_linux_os_profile(vm_config[:name],
|
47
|
-
vm_config[:username],
|
48
|
-
vm_config[:password],
|
49
|
-
vm_config[:disable_password_authentication],
|
50
|
-
vm_config[:ssh_key_path],
|
51
|
-
vm_config[:ssh_key_data],
|
52
|
-
encoded_data)
|
53
|
-
end
|
54
|
-
virtual_machine.network_profile = define_network_profile(vm_config[:network_interface_card_ids])
|
55
|
-
virtual_machine.location = vm_config[:location]
|
56
|
-
virtual_machine.tags = vm_config[:tags]
|
57
26
|
begin
|
58
27
|
response = if async
|
59
|
-
@compute_mgmt_client.virtual_machines.create_or_update_async(
|
28
|
+
@compute_mgmt_client.virtual_machines.create_or_update_async(rg_name, vm_name, vm)
|
60
29
|
else
|
61
|
-
@compute_mgmt_client.virtual_machines.create_or_update(
|
30
|
+
@compute_mgmt_client.virtual_machines.create_or_update(rg_name, vm_name, vm)
|
62
31
|
end
|
63
32
|
rescue MsRestAzure::AzureOperationError => e
|
64
33
|
raise_azure_exception(e, msg)
|
65
34
|
end
|
35
|
+
|
66
36
|
unless async
|
67
37
|
unless vm_config[:vhd_path].nil? || vm_config[:managed_disk_storage_type].nil?
|
68
|
-
|
69
|
-
delete_storage_account_or_container(
|
38
|
+
delete_image(rg_name, vm_name)
|
39
|
+
delete_storage_account_or_container(rg_name, vm_config[:storage_account_name], vm_name)
|
70
40
|
end
|
71
41
|
end
|
72
|
-
|
42
|
+
|
43
|
+
Fog::Logger.debug "Virtual Machine #{vm_name} created successfully!" unless async
|
73
44
|
response
|
74
45
|
end
|
75
46
|
|
76
47
|
private
|
77
48
|
|
78
|
-
def
|
49
|
+
def get_vm_availability_set(availability_set_id)
|
50
|
+
sub_resource = nil
|
51
|
+
unless availability_set_id.nil?
|
52
|
+
sub_resource = MsRestAzure::SubResource.new
|
53
|
+
sub_resource.id = availability_set_id
|
54
|
+
end
|
55
|
+
sub_resource
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_hardware_profile(vm_size)
|
79
59
|
hw_profile = Azure::ARM::Compute::Models::HardwareProfile.new
|
80
60
|
hw_profile.vm_size = vm_size
|
81
61
|
hw_profile
|
82
62
|
end
|
83
63
|
|
84
|
-
def
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
64
|
+
def get_os_profile(vm_config)
|
65
|
+
# Arguments unpacking
|
66
|
+
platform = vm_config[:platform]
|
67
|
+
vm_name = vm_config[:name]
|
68
|
+
username = vm_config[:username]
|
69
|
+
password = vm_config[:password]
|
70
|
+
custom_data = vm_config[:custom_data]
|
71
|
+
provision_vm_agent = vm_config[:provision_vm_agent]
|
72
|
+
enable_automatic_updates = vm_config[:enable_automatic_updates]
|
73
|
+
disable_password_auth = vm_config[:disable_password_authentication]
|
74
|
+
ssh_key_path = vm_config[:ssh_key_path]
|
75
|
+
ssh_key_data = vm_config[:ssh_key_data]
|
76
|
+
|
77
|
+
# Build and return os profile object
|
78
|
+
os_profile = Azure::ARM::Compute::Models::OSProfile.new
|
79
|
+
os_profile.computer_name = vm_name
|
80
|
+
os_profile.admin_username = username
|
81
|
+
os_profile.admin_password = password
|
82
|
+
os_profile.custom_data = Base64.strict_encode64(custom_data.nil? ? WHITE_SPACE : custom_data)
|
83
|
+
|
84
|
+
if platform.casecmp(WINDOWS).zero?
|
85
|
+
os_profile.windows_configuration = get_windows_config(provision_vm_agent, enable_automatic_updates)
|
86
|
+
else
|
87
|
+
os_profile.linux_configuration = get_linux_config(disable_password_auth, ssh_key_path, ssh_key_data)
|
88
|
+
end
|
89
|
+
|
90
|
+
os_profile
|
91
|
+
end
|
92
|
+
|
93
|
+
def get_windows_config(provision_vm_agent, enable_automatic_updates)
|
94
|
+
windows_config = Azure::ARM::Compute::Models::WindowsConfiguration.new
|
95
|
+
windows_config.provision_vmagent = provision_vm_agent
|
96
|
+
windows_config.enable_automatic_updates = enable_automatic_updates
|
97
|
+
windows_config
|
91
98
|
end
|
92
99
|
|
93
|
-
def
|
100
|
+
def get_linux_config(disable_password_auth, ssh_key_path, ssh_key_data)
|
101
|
+
linux_config = Azure::ARM::Compute::Models::LinuxConfiguration.new
|
102
|
+
linux_config.disable_password_authentication = disable_password_auth
|
103
|
+
|
104
|
+
unless ssh_key_path.nil? || ssh_key_data.nil?
|
105
|
+
ssh_config = Azure::ARM::Compute::Models::SshConfiguration.new
|
106
|
+
ssh_public_key = Azure::ARM::Compute::Models::SshPublicKey.new
|
107
|
+
ssh_public_key.path = ssh_key_path
|
108
|
+
ssh_public_key.key_data = ssh_key_data
|
109
|
+
ssh_config.public_keys = [ssh_public_key]
|
110
|
+
linux_config.ssh = ssh_config
|
111
|
+
end
|
112
|
+
|
113
|
+
linux_config
|
114
|
+
end
|
115
|
+
|
116
|
+
def get_network_profile(network_interface_card_ids)
|
117
|
+
network_interface_cards = []
|
118
|
+
network_interface_card_ids.each_with_index do |id, index|
|
119
|
+
nic = Azure::ARM::Compute::Models::NetworkInterfaceReference.new
|
120
|
+
nic.id = id
|
121
|
+
nic.primary = index == PRIMARY_NIC_INDEX
|
122
|
+
network_interface_cards << nic
|
123
|
+
end
|
124
|
+
|
125
|
+
network_profile = Azure::ARM::Compute::Models::NetworkProfile.new
|
126
|
+
network_profile.network_interfaces = network_interface_cards
|
127
|
+
network_profile
|
128
|
+
end
|
129
|
+
|
130
|
+
def get_storage_profile(vm_config)
|
131
|
+
# Arguments unpacking
|
132
|
+
managed_disk_storage_type = vm_config[:managed_disk_storage_type]
|
133
|
+
|
134
|
+
storage_profile = if managed_disk_storage_type.nil?
|
135
|
+
get_unmanaged_vm_storage_profile(vm_config)
|
136
|
+
else
|
137
|
+
get_managed_vm_storage_profile(vm_config)
|
138
|
+
end
|
139
|
+
storage_profile
|
140
|
+
end
|
141
|
+
|
142
|
+
def get_unmanaged_vm_storage_profile(vm_config)
|
143
|
+
# Arguments unpacking
|
144
|
+
vm_name = vm_config[:name]
|
145
|
+
storage_account_name = vm_config[:storage_account_name]
|
146
|
+
publisher = vm_config[:publisher]
|
147
|
+
offer = vm_config[:offer]
|
148
|
+
sku = vm_config[:sku]
|
149
|
+
version = vm_config[:version]
|
150
|
+
vhd_path = vm_config[:vhd_path]
|
151
|
+
os_disk_caching = vm_config[:os_disk_caching]
|
152
|
+
platform = vm_config[:platform]
|
153
|
+
resource_group = vm_config[:resource_group]
|
154
|
+
os_disk_size = vm_config[:os_disk_size]
|
155
|
+
location = vm_config[:location]
|
156
|
+
image_ref = vm_config[:image_ref]
|
157
|
+
|
94
158
|
storage_profile = Azure::ARM::Compute::Models::StorageProfile.new
|
95
|
-
|
159
|
+
# Set OS disk VHD path
|
96
160
|
os_disk = Azure::ARM::Compute::Models::OSDisk.new
|
161
|
+
vhd = Azure::ARM::Compute::Models::VirtualHardDisk.new
|
162
|
+
vhd.uri = get_blob_endpoint(storage_account_name) + "/vhds/#{vm_name}_os_disk.vhd"
|
163
|
+
os_disk.vhd = vhd
|
164
|
+
|
165
|
+
if vhd_path.nil? && image_ref.nil?
|
166
|
+
# Marketplace
|
167
|
+
storage_profile.image_reference = image_reference(publisher, offer, sku, version)
|
168
|
+
elsif !vhd_path.nil? && image_ref.nil?
|
169
|
+
# VHD
|
170
|
+
new_vhd_path = copy_vhd_to_storage_account(resource_group, storage_account_name, vhd_path, location, vm_name)
|
171
|
+
img_vhd = Azure::ARM::Compute::Models::VirtualHardDisk.new
|
172
|
+
img_vhd.uri = new_vhd_path
|
173
|
+
os_disk.image = img_vhd
|
174
|
+
else
|
175
|
+
# Image
|
176
|
+
image_resource_group = get_resource_group_from_id(image_ref)
|
177
|
+
image_name = get_image_name(image_ref)
|
178
|
+
image = get_image(image_resource_group, image_name)
|
179
|
+
storage_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new
|
180
|
+
storage_profile.image_reference.id = image.id
|
181
|
+
end
|
97
182
|
|
98
|
-
|
183
|
+
storage_profile.os_disk = configure_os_disk_object(os_disk, os_disk_caching, os_disk_size, platform, vm_name)
|
184
|
+
storage_profile
|
185
|
+
end
|
99
186
|
|
100
|
-
|
101
|
-
|
102
|
-
|
187
|
+
def get_managed_vm_storage_profile(vm_config)
|
188
|
+
# Argument unpacking
|
189
|
+
managed_disk_storage_type = vm_config[:managed_disk_storage_type]
|
190
|
+
vhd_path = vm_config[:vhd_path]
|
191
|
+
resource_group = vm_config[:resource_group]
|
192
|
+
storage_account_name = vm_config[:storage_account_name]
|
193
|
+
location = vm_config[:location]
|
194
|
+
publisher = vm_config[:publisher]
|
195
|
+
offer = vm_config[:offer]
|
196
|
+
sku = vm_config[:sku]
|
197
|
+
version = vm_config[:version]
|
198
|
+
os_disk_caching = vm_config[:os_disk_caching]
|
199
|
+
os_disk_size = vm_config[:os_disk_size]
|
200
|
+
platform = vm_config[:platform]
|
201
|
+
vm_name = vm_config[:name]
|
202
|
+
image_ref = vm_config[:image_ref]
|
103
203
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
204
|
+
# Build storage profile
|
205
|
+
storage_profile = Azure::ARM::Compute::Models::StorageProfile.new
|
206
|
+
os_disk = Azure::ARM::Compute::Models::OSDisk.new
|
207
|
+
managed_disk = Azure::ARM::Compute::Models::ManagedDiskParameters.new
|
208
|
+
managed_disk.storage_account_type = managed_disk_storage_type
|
209
|
+
os_disk.managed_disk = managed_disk
|
109
210
|
|
110
|
-
|
211
|
+
if vhd_path.nil? && image_ref.nil?
|
212
|
+
# Marketplace
|
213
|
+
storage_profile.image_reference = image_reference(publisher, offer, sku, version)
|
214
|
+
elsif !vhd_path.nil? && image_ref.nil?
|
215
|
+
# VHD
|
216
|
+
new_vhd_path = copy_vhd_to_storage_account(resource_group, storage_account_name, vhd_path, location, vm_name)
|
217
|
+
image = create_image(image_config_params(location, new_vhd_path, platform, resource_group, vm_name))
|
218
|
+
storage_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new
|
219
|
+
storage_profile.image_reference.id = image.id
|
111
220
|
else
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
managed_disk = Azure::ARM::Compute::Models::ManagedDiskParameters.new
|
119
|
-
managed_disk.storage_account_type = managed_disk_storage_type
|
120
|
-
os_disk.managed_disk = managed_disk
|
221
|
+
# Image
|
222
|
+
image_resource_group = get_resource_group_from_id(image_ref)
|
223
|
+
image_name = get_image_name(image_ref)
|
224
|
+
image = get_image(image_resource_group, image_name)
|
225
|
+
storage_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new
|
226
|
+
storage_profile.image_reference.id = image.id
|
121
227
|
end
|
228
|
+
|
122
229
|
storage_profile.os_disk = configure_os_disk_object(os_disk, os_disk_caching, os_disk_size, platform, vm_name)
|
123
230
|
storage_profile
|
124
231
|
end
|
125
232
|
|
233
|
+
def image_reference(publisher, offer, sku, version)
|
234
|
+
image_reference = Azure::ARM::Compute::Models::ImageReference.new
|
235
|
+
image_reference.publisher = publisher
|
236
|
+
image_reference.offer = offer
|
237
|
+
image_reference.sku = sku
|
238
|
+
image_reference.version = version
|
239
|
+
image_reference
|
240
|
+
end
|
241
|
+
|
126
242
|
def image_config_params(location, new_vhd_path, platform, resource_group, vm_name)
|
127
243
|
{
|
128
244
|
location: location,
|
@@ -151,54 +267,19 @@ module Fog
|
|
151
267
|
os_disk
|
152
268
|
end
|
153
269
|
|
154
|
-
def define_windows_os_profile(vm_name, username, password, provision_vm_agent, enable_automatic_updates, encoded_data)
|
155
|
-
os_profile = Azure::ARM::Compute::Models::OSProfile.new
|
156
|
-
windows_config = Azure::ARM::Compute::Models::WindowsConfiguration.new
|
157
|
-
windows_config.provision_vmagent = provision_vm_agent
|
158
|
-
windows_config.enable_automatic_updates = enable_automatic_updates
|
159
|
-
os_profile.windows_configuration = windows_config
|
160
|
-
os_profile.computer_name = vm_name
|
161
|
-
os_profile.admin_username = username
|
162
|
-
os_profile.admin_password = password
|
163
|
-
os_profile.custom_data = encoded_data
|
164
|
-
os_profile
|
165
|
-
end
|
166
|
-
|
167
|
-
def define_linux_os_profile(vm_name, username, password, disable_password_authentication, ssh_key_path, ssh_key_data, encoded_data)
|
168
|
-
os_profile = Azure::ARM::Compute::Models::OSProfile.new
|
169
|
-
linux_config = Azure::ARM::Compute::Models::LinuxConfiguration.new
|
170
|
-
|
171
|
-
unless ssh_key_path.nil? || ssh_key_data.nil?
|
172
|
-
ssh_config = Azure::ARM::Compute::Models::SshConfiguration.new
|
173
|
-
ssh_public_key = Azure::ARM::Compute::Models::SshPublicKey.new
|
174
|
-
ssh_public_key.path = ssh_key_path
|
175
|
-
ssh_public_key.key_data = ssh_key_data
|
176
|
-
ssh_config.public_keys = [ssh_public_key]
|
177
|
-
linux_config.ssh = ssh_config
|
178
|
-
end
|
179
|
-
|
180
|
-
linux_config.disable_password_authentication = disable_password_authentication
|
181
|
-
os_profile.linux_configuration = linux_config
|
182
|
-
os_profile.computer_name = vm_name
|
183
|
-
os_profile.admin_username = username
|
184
|
-
os_profile.admin_password = password
|
185
|
-
os_profile.custom_data = encoded_data
|
186
|
-
os_profile
|
187
|
-
end
|
188
|
-
|
189
270
|
def copy_vhd_to_storage_account(resource_group, storage_account_name, vhd_path, location, vm_name)
|
190
271
|
# Copy if VHD does not exist belongs to same storage account.
|
191
|
-
|
192
|
-
if storage_account_name !=
|
272
|
+
vhd_storage_account_name = (vhd_path.split('/')[2]).split('.')[0]
|
273
|
+
if storage_account_name != vhd_storage_account_name
|
193
274
|
if storage_account_name.nil?
|
194
275
|
new_time = current_time
|
195
276
|
storage_account_name = "sa#{new_time}"
|
196
|
-
|
197
|
-
|
198
|
-
)
|
277
|
+
storage_account_config = storage_account_config_params(location, resource_group, storage_account_name)
|
278
|
+
storage_account = @storage_service.storage_accounts.create(storage_account_config)
|
199
279
|
else
|
200
280
|
storage_account = @storage_service.storage_accounts.get(resource_group, storage_account_name)
|
201
281
|
end
|
282
|
+
|
202
283
|
access_key = storage_account.get_access_keys.first.value
|
203
284
|
storage_data = Fog::Storage::AzureRM.new(azure_storage_account_name: storage_account_name, azure_storage_access_key: access_key)
|
204
285
|
new_time = current_time
|
@@ -206,15 +287,18 @@ module Fog
|
|
206
287
|
blob_name = "vhd_image#{new_time}.vhd"
|
207
288
|
storage_data.directories.create(key: container_name)
|
208
289
|
storage_data.copy_blob_from_uri(container_name, blob_name, vhd_path)
|
290
|
+
|
209
291
|
until storage_data.get_blob_properties(container_name, blob_name).properties[:copy_status] == 'success'
|
210
292
|
Fog::Logger.debug 'Waiting disk to ready'
|
211
293
|
sleep(10)
|
212
294
|
end
|
295
|
+
|
213
296
|
new_vhd_path = get_blob_endpoint(storage_account_name) + "/#{container_name}/#{blob_name}"
|
214
297
|
Fog::Logger.debug "Path:#{new_vhd_path}. | Copy done"
|
215
298
|
else
|
216
299
|
new_vhd_path = vhd_path
|
217
300
|
end
|
301
|
+
|
218
302
|
new_vhd_path
|
219
303
|
end
|
220
304
|
|
@@ -232,20 +316,6 @@ module Fog
|
|
232
316
|
}
|
233
317
|
end
|
234
318
|
|
235
|
-
def define_network_profile(network_interface_card_ids)
|
236
|
-
network_interface_cards = []
|
237
|
-
network_interface_card_ids.each_with_index do |id, index|
|
238
|
-
nic = Azure::ARM::Compute::Models::NetworkInterfaceReference .new
|
239
|
-
nic.id = id
|
240
|
-
nic.primary = index == PRIMARY_NIC_INDEX
|
241
|
-
network_interface_cards << nic
|
242
|
-
end
|
243
|
-
|
244
|
-
network_profile = Azure::ARM::Compute::Models::NetworkProfile.new
|
245
|
-
network_profile.network_interfaces = network_interface_cards
|
246
|
-
network_profile
|
247
|
-
end
|
248
|
-
|
249
319
|
def delete_storage_account_or_container(resource_group, storage_account_name, vm_name)
|
250
320
|
delete_storage_account(resource_group) if storage_account_name.nil?
|
251
321
|
delete_storage_container(resource_group, storage_account_name, vm_name) unless storage_account_name.nil?
|
@@ -258,9 +328,12 @@ module Fog
|
|
258
328
|
end
|
259
329
|
|
260
330
|
def delete_storage_account(resource_group)
|
261
|
-
@storage_service.storage_accounts.delete_storage_account_from_tag(resource_group,
|
331
|
+
@storage_service.storage_accounts.delete_storage_account_from_tag(resource_group,
|
332
|
+
TEMPORARY_STORAGE_ACCOUNT_TAG_KEY,
|
333
|
+
TEMPORARY_STORAGE_ACCOUNT_TAG_VALUE)
|
262
334
|
end
|
263
335
|
end
|
336
|
+
|
264
337
|
# This class provides the mock implementation for unit tests.
|
265
338
|
class Mock
|
266
339
|
def create_virtual_machine(*)
|
@@ -320,6 +393,7 @@ module Fog
|
|
320
393
|
'provisioningState' => 'Succeeded'
|
321
394
|
}
|
322
395
|
}
|
396
|
+
|
323
397
|
vm_mapper = Azure::ARM::Compute::Models::VirtualMachine.mapper
|
324
398
|
@compute_mgmt_client.deserialize(vm_mapper, vm, 'result.body')
|
325
399
|
end
|
@@ -3,8 +3,8 @@ module Fog
|
|
3
3
|
class AzureRM
|
4
4
|
# This class provides the actual implementation for service calls.
|
5
5
|
class Real
|
6
|
-
def
|
7
|
-
msg = "Deleting
|
6
|
+
def delete_image(resource_group, vm_name)
|
7
|
+
msg = "Deleting Image: #{vm_name}-osImage"
|
8
8
|
Fog::Logger.debug msg
|
9
9
|
image_name = "#{vm_name}-osImage"
|
10
10
|
begin
|
@@ -12,14 +12,14 @@ module Fog
|
|
12
12
|
rescue MsRestAzure::AzureOperationError => e
|
13
13
|
raise_azure_exception(e, msg)
|
14
14
|
end
|
15
|
-
Fog::Logger.debug "
|
15
|
+
Fog::Logger.debug "Image #{image_name} deleted successfully."
|
16
16
|
true
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
# This class provides the mock implementation for unit tests.
|
21
21
|
class Mock
|
22
|
-
def
|
22
|
+
def delete_image(*)
|
23
23
|
Fog::Logger.debug 'Image fog-test-server-osImage from Resource group fog-test-rg deleted successfully.'
|
24
24
|
true
|
25
25
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Fog
|
2
|
+
module Compute
|
3
|
+
class AzureRM
|
4
|
+
# Real class for Compute Request
|
5
|
+
class Real
|
6
|
+
def get_image(resource_group_name, image_name)
|
7
|
+
msg = "Getting Image #{image_name} in Resource Group #{resource_group_name}"
|
8
|
+
Fog::Logger.debug msg
|
9
|
+
begin
|
10
|
+
image = @compute_mgmt_client.images.get(resource_group_name, image_name)
|
11
|
+
rescue MsRestAzure::AzureOperationError => e
|
12
|
+
raise_azure_exception(e, msg)
|
13
|
+
end
|
14
|
+
Fog::Logger.debug "#{msg} successful"
|
15
|
+
image
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Mock class for Compute Request
|
20
|
+
class Mock
|
21
|
+
def get_image(*)
|
22
|
+
body = {
|
23
|
+
'id' => '/subscriptions/########-####-####-####-############/resourceGroups/TestRG/providers/Microsoft.Compute/images/TestImage',
|
24
|
+
'name' => 'TestImage',
|
25
|
+
'resource_group' => 'TestRG',
|
26
|
+
'location' => 'West US',
|
27
|
+
'storage_profile' => {
|
28
|
+
'os_disk' => {
|
29
|
+
'os_type' => 'Linux',
|
30
|
+
'os_state' => 'Generalized',
|
31
|
+
'blob_uri' => 'https://myblob.blob.core.windows.net/images/testimage.vhd',
|
32
|
+
'caching' => 'ReadWrite',
|
33
|
+
'disk_size_gb' => '5'
|
34
|
+
},
|
35
|
+
'data_disks' => []
|
36
|
+
},
|
37
|
+
'provisioning_state' => 'Succeeded'
|
38
|
+
}
|
39
|
+
image_mapper = Azure::ARM::Compute::Models::Image.mapper
|
40
|
+
@compute_mgmt_client.deserialize(image_mapper, body, 'result.body')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/fog/azurerm/version.rb
CHANGED
data/test/api_stub.rb
CHANGED
@@ -78,7 +78,7 @@ module ApiStub
|
|
78
78
|
autoload :VirtualMachine, File.expand_path('api_stub/requests/compute/virtual_machine', __dir__)
|
79
79
|
autoload :VirtualMachineExtension, File.expand_path('api_stub/requests/compute/virtual_machine_extension', __dir__)
|
80
80
|
autoload :ManagedDisk, File.expand_path('api_stub/requests/compute/managed_disk', __dir__)
|
81
|
-
autoload :
|
81
|
+
autoload :Image, File.expand_path('api_stub/requests/compute/image', __dir__)
|
82
82
|
end
|
83
83
|
|
84
84
|
# Load test Resources requests
|
@@ -2,8 +2,8 @@ module ApiStub
|
|
2
2
|
module Requests
|
3
3
|
module Compute
|
4
4
|
# Mock class for Virtual Machine Requests
|
5
|
-
class
|
6
|
-
def self.
|
5
|
+
class Image
|
6
|
+
def self.image_params
|
7
7
|
{
|
8
8
|
resource_group: 'fog-test-rg',
|
9
9
|
vm_name: 'fog-test-server',
|
@@ -13,7 +13,7 @@ module ApiStub
|
|
13
13
|
}
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.
|
16
|
+
def self.create_image(compute_client)
|
17
17
|
body = {
|
18
18
|
'location' => 'West US',
|
19
19
|
'tags' => {
|
data/test/integration/server.rb
CHANGED
@@ -57,10 +57,10 @@ begin
|
|
57
57
|
)
|
58
58
|
|
59
59
|
network.virtual_networks.create(
|
60
|
-
name:
|
61
|
-
location:
|
62
|
-
resource_group:
|
63
|
-
network_address_list:
|
60
|
+
name: 'testVnet',
|
61
|
+
location: LOCATION,
|
62
|
+
resource_group: 'TestRG-VM',
|
63
|
+
network_address_list: '10.1.0.0/16,10.2.0.0/16'
|
64
64
|
)
|
65
65
|
|
66
66
|
network.subnets.create(
|
@@ -79,7 +79,6 @@ begin
|
|
79
79
|
private_ip_allocation_method: Fog::ARM::Network::Models::IPAllocationMethod::Dynamic
|
80
80
|
)
|
81
81
|
|
82
|
-
# TODO: Create new NIC for managed VM
|
83
82
|
network.network_interfaces.create(
|
84
83
|
name: 'NetInt2',
|
85
84
|
resource_group: 'TestRG-VM',
|
@@ -94,12 +93,14 @@ begin
|
|
94
93
|
########################################################################################################################
|
95
94
|
|
96
95
|
flag = compute.servers.check_vm_exists('TestRG-VM', 'TestVM')
|
97
|
-
puts
|
96
|
+
puts 'Virtual Machine does NOT exist!' unless flag
|
98
97
|
|
99
98
|
########################################################################################################################
|
100
99
|
###################### Create Server ######################
|
101
100
|
########################################################################################################################
|
102
101
|
|
102
|
+
puts 'Creating un-managed Virtual Machine...'
|
103
|
+
|
103
104
|
tags = { key1: 'value1', key2: 'value2' }
|
104
105
|
|
105
106
|
virtual_machine = compute.servers.create(
|
@@ -126,6 +127,9 @@ begin
|
|
126
127
|
########################################################################################################################
|
127
128
|
###################### Create Managed Server ######################
|
128
129
|
########################################################################################################################
|
130
|
+
|
131
|
+
puts 'Creating managed Virtual Machine...'
|
132
|
+
|
129
133
|
managed_vm = compute.servers.create(
|
130
134
|
name: 'TestVM-Managed',
|
131
135
|
location: LOCATION,
|
@@ -151,6 +155,8 @@ begin
|
|
151
155
|
###################### Create Server Async ####################
|
152
156
|
########################################################################################################################
|
153
157
|
|
158
|
+
print 'Creating Virtual Machine asynchronously...'
|
159
|
+
|
154
160
|
async_response = compute.servers.create_async(
|
155
161
|
name: 'TestVM',
|
156
162
|
location: LOCATION,
|
@@ -172,17 +178,18 @@ begin
|
|
172
178
|
)
|
173
179
|
|
174
180
|
loop do
|
175
|
-
|
176
|
-
|
177
|
-
|
181
|
+
if async_response.pending?
|
182
|
+
sleep(2)
|
183
|
+
print '.'
|
184
|
+
end
|
178
185
|
|
179
186
|
if async_response.fulfilled?
|
180
|
-
puts async_response.value.
|
187
|
+
puts "\nCreated VM asynchronously! [#{async_response.value.name}]"
|
181
188
|
break
|
182
189
|
end
|
183
190
|
|
184
191
|
if async_response.rejected?
|
185
|
-
puts async_response.reason.inspect
|
192
|
+
puts "\nERROR: Async VM creation failed!\n#{async_response.reason.inspect}"
|
186
193
|
break
|
187
194
|
end
|
188
195
|
end
|
@@ -193,7 +200,7 @@ begin
|
|
193
200
|
|
194
201
|
virtual_machine = compute.servers.get('TestRG-VM', 'TestVM')
|
195
202
|
virtual_machine.attach_data_disk('datadisk1', 10, storage_account_name)
|
196
|
-
puts 'Attached Data Disk to VM'
|
203
|
+
puts 'Attached Data Disk to VM!'
|
197
204
|
|
198
205
|
########################################################################################################################
|
199
206
|
###################### Detach Data Disk from VM ######################
|
@@ -201,7 +208,7 @@ begin
|
|
201
208
|
|
202
209
|
virtual_machine = compute.servers.get('TestRG-VM', 'TestVM')
|
203
210
|
virtual_machine.detach_data_disk('datadisk1')
|
204
|
-
puts 'Detached Data Disk from VM'
|
211
|
+
puts 'Detached Data Disk from VM!'
|
205
212
|
|
206
213
|
########################################################################################################################
|
207
214
|
###################### Delete Data Disk ######################
|
@@ -218,6 +225,7 @@ begin
|
|
218
225
|
########################################################################################################################
|
219
226
|
###################### Create a Managed Data Disk ######################
|
220
227
|
########################################################################################################################
|
228
|
+
|
221
229
|
managed_disk = compute.managed_disks.create(
|
222
230
|
name: 'ManagedDataDisk',
|
223
231
|
location: LOCATION,
|
@@ -233,30 +241,32 @@ begin
|
|
233
241
|
########################################################################################################################
|
234
242
|
###################### Attach Managed Data Disk to VM ######################
|
235
243
|
########################################################################################################################
|
244
|
+
|
236
245
|
managed_vm.attach_managed_disk('ManagedDataDisk', 'TestRG-VM')
|
237
|
-
puts 'Attached Managed Data Disk to VM'
|
246
|
+
puts 'Attached Managed Data Disk to VM!'
|
238
247
|
|
239
248
|
########################################################################################################################
|
240
249
|
###################### Detach Data Disk from VM ######################
|
241
250
|
########################################################################################################################
|
242
251
|
|
243
252
|
managed_vm.detach_managed_disk('ManagedDataDisk')
|
244
|
-
puts 'Detached Managed Data Disk from VM'
|
253
|
+
puts 'Detached Managed Data Disk from VM!'
|
245
254
|
|
246
255
|
########################################################################################################################
|
247
256
|
###################### Delete Managed Data Disk ######################
|
248
257
|
########################################################################################################################
|
258
|
+
|
249
259
|
managed_disk.destroy
|
250
|
-
puts 'Deleted managed data disk'
|
260
|
+
puts 'Deleted managed data disk!'
|
251
261
|
|
252
262
|
########################################################################################################################
|
253
263
|
###################### List VM in a resource group ######################
|
254
264
|
########################################################################################################################
|
255
265
|
|
256
266
|
virtual_machines = compute.servers(resource_group: 'TestRG-VM')
|
257
|
-
puts 'List virtual machines
|
258
|
-
virtual_machines.each do |
|
259
|
-
puts
|
267
|
+
puts 'List virtual machines in a resource group:'
|
268
|
+
virtual_machines.each do |vm|
|
269
|
+
puts "- #{vm.name}"
|
260
270
|
end
|
261
271
|
|
262
272
|
#######################################################################################################################
|
@@ -270,10 +280,10 @@ begin
|
|
270
280
|
###################### List available sizes in VM ######################
|
271
281
|
########################################################################################################################
|
272
282
|
|
283
|
+
puts 'List of available sizes for Virtual Machine:'
|
273
284
|
available_sizes_for_vm = virtual_machine.list_available_sizes
|
274
|
-
puts 'List available sizes in virtual machines:'
|
275
285
|
available_sizes_for_vm.each do |available_size|
|
276
|
-
puts available_size.
|
286
|
+
puts "- #{available_size.name}"
|
277
287
|
end
|
278
288
|
|
279
289
|
########################################################################################################################
|
@@ -295,7 +305,7 @@ begin
|
|
295
305
|
########################################################################################################################
|
296
306
|
|
297
307
|
virtual_machine.deallocate
|
298
|
-
puts 'Virtual machine
|
308
|
+
puts 'Virtual machine de-allocated!'
|
299
309
|
|
300
310
|
########################################################################################################################
|
301
311
|
###################### Start VM ######################
|
@@ -316,12 +326,14 @@ begin
|
|
316
326
|
########################################################################################################################
|
317
327
|
|
318
328
|
puts "Deleted virtual machine: #{virtual_machine.destroy}"
|
319
|
-
puts "Deleted
|
329
|
+
puts "Deleted virtual machine: #{managed_vm.destroy}"
|
320
330
|
|
321
331
|
########################################################################################################################
|
322
332
|
###################### CleanUp ######################
|
323
333
|
########################################################################################################################
|
324
334
|
|
335
|
+
puts 'Cleaning up...'
|
336
|
+
|
325
337
|
nic = network.network_interfaces.get('TestRG-VM', 'NetInt')
|
326
338
|
nic.destroy
|
327
339
|
|
@@ -337,8 +349,8 @@ begin
|
|
337
349
|
resource_group = rs.resource_groups.get('TestRG-VM')
|
338
350
|
resource_group.destroy
|
339
351
|
|
340
|
-
puts 'Integration Test for virtual machine ran successfully'
|
352
|
+
puts 'Integration Test for virtual machine ran successfully!'
|
341
353
|
rescue
|
342
|
-
puts 'Integration Test for virtual machine is failing'
|
354
|
+
puts 'Integration Test for virtual machine is failing!'
|
343
355
|
resource_group.destroy unless resource_group.nil?
|
344
356
|
end
|
@@ -59,10 +59,10 @@ begin
|
|
59
59
|
)
|
60
60
|
|
61
61
|
network.virtual_networks.create(
|
62
|
-
name:
|
63
|
-
location:
|
64
|
-
resource_group:
|
65
|
-
network_address_list:
|
62
|
+
name: 'testVnet',
|
63
|
+
location: LOCATION,
|
64
|
+
resource_group: RG_NAME,
|
65
|
+
network_address_list: '10.1.0.0/16,10.2.0.0/16'
|
66
66
|
)
|
67
67
|
|
68
68
|
network.subnets.create(
|
@@ -103,6 +103,8 @@ begin
|
|
103
103
|
###################### Create Server ######################
|
104
104
|
########################################################################################################################
|
105
105
|
|
106
|
+
vhd_path = 'https://myblob.blob.core.windows.net/vhds/my_vhd.vhd'.freeze
|
107
|
+
|
106
108
|
custom_image_virtual_machine = compute.servers.create(
|
107
109
|
name: 'TestVM',
|
108
110
|
location: LOCATION,
|
@@ -114,7 +116,7 @@ begin
|
|
114
116
|
disable_password_authentication: false,
|
115
117
|
network_interface_card_ids: ["/subscriptions/#{azure_credentials['subscription_id']}/resourceGroups/#{RG_NAME}/providers/Microsoft.Network/networkInterfaces/NetInt"],
|
116
118
|
platform: 'linux',
|
117
|
-
vhd_path:
|
119
|
+
vhd_path: vhd_path
|
118
120
|
)
|
119
121
|
|
120
122
|
puts "Created custom image un-managed virtual machine: #{custom_image_virtual_machine.name}"
|
@@ -134,7 +136,7 @@ begin
|
|
134
136
|
disable_password_authentication: false,
|
135
137
|
network_interface_card_ids: ["/subscriptions/#{azure_credentials['subscription_id']}/resourceGroups/#{RG_NAME}/providers/Microsoft.Network/networkInterfaces/NetInt2"],
|
136
138
|
platform: 'linux',
|
137
|
-
vhd_path:
|
139
|
+
vhd_path: vhd_path,
|
138
140
|
managed_disk_storage_type: Azure::ARM::Compute::Models::StorageAccountTypes::StandardLRS
|
139
141
|
)
|
140
142
|
|
@@ -144,6 +146,8 @@ begin
|
|
144
146
|
############## Create Managed Server Async ################
|
145
147
|
########################################################################################################################
|
146
148
|
|
149
|
+
print 'Creating Virtual Machine asynchronously...'
|
150
|
+
|
147
151
|
async_response = compute.servers.create_async(
|
148
152
|
name: 'TestVM-ManagedAsync',
|
149
153
|
location: LOCATION,
|
@@ -155,22 +159,23 @@ begin
|
|
155
159
|
disable_password_authentication: false,
|
156
160
|
network_interface_card_ids: ["/subscriptions/#{azure_credentials['subscription_id']}/resourceGroups/#{RG_NAME}/providers/Microsoft.Network/networkInterfaces/NetInt3"],
|
157
161
|
platform: 'linux',
|
158
|
-
vhd_path:
|
162
|
+
vhd_path: vhd_path,
|
159
163
|
managed_disk_storage_type: Azure::ARM::Compute::Models::StorageAccountTypes::StandardLRS
|
160
164
|
)
|
161
165
|
|
162
166
|
loop do
|
163
|
-
|
164
|
-
|
165
|
-
|
167
|
+
if async_response.pending?
|
168
|
+
sleep(2)
|
169
|
+
print '.'
|
170
|
+
end
|
166
171
|
|
167
172
|
if async_response.fulfilled?
|
168
|
-
puts "
|
173
|
+
puts "\nCreated custom image managed virtual machine: #{async_response.value.name}"
|
169
174
|
break
|
170
175
|
end
|
171
176
|
|
172
177
|
if async_response.rejected?
|
173
|
-
puts async_response.reason.inspect
|
178
|
+
puts "\nERROR: Async VM creation failed!\n#{async_response.reason.inspect}"
|
174
179
|
break
|
175
180
|
end
|
176
181
|
end
|
@@ -195,6 +200,8 @@ begin
|
|
195
200
|
###################### CleanUp ######################
|
196
201
|
########################################################################################################################
|
197
202
|
|
203
|
+
puts 'Cleaning up...'
|
204
|
+
|
198
205
|
nic = network.network_interfaces.get(RG_NAME, 'NetInt')
|
199
206
|
nic.destroy
|
200
207
|
|
@@ -212,6 +219,8 @@ begin
|
|
212
219
|
|
213
220
|
resource_group = rs.resource_groups.get(RG_NAME)
|
214
221
|
resource_group.destroy
|
222
|
+
|
223
|
+
puts 'Integration Test for virtual machine ran successfully!'
|
215
224
|
rescue
|
216
225
|
puts 'Integration Test for custom image virtual machine is failing'
|
217
226
|
resource_group.destroy unless resource_group.nil?
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __dir__)
|
2
|
+
|
3
|
+
# Test class for Create Image Request
|
4
|
+
class TestCreateImage < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@service = Fog::Compute::AzureRM.new(credentials)
|
7
|
+
compute_client = @service.instance_variable_get(:@compute_mgmt_client)
|
8
|
+
@image = compute_client.images
|
9
|
+
@response = ApiStub::Requests::Compute::Image.create_image(compute_client)
|
10
|
+
@input_params = ApiStub::Requests::Compute::Image.image_params
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_create_image_success
|
14
|
+
@image.stub :create_or_update, @response do
|
15
|
+
assert_equal @service.create_image(@input_params), @response
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_create_image_failure
|
20
|
+
response = proc { raise MsRestAzure::AzureOperationError.new(nil, nil, 'error' => { 'message' => 'mocked exception' }) }
|
21
|
+
@image.stub :create_or_update, response do
|
22
|
+
assert_raises RuntimeError do
|
23
|
+
@service.create_image(@input_params)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __dir__)
|
2
|
+
|
3
|
+
# Test class for Delete Image Request
|
4
|
+
class TestDeleteImage < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@service = Fog::Compute::AzureRM.new(credentials)
|
7
|
+
compute_client = @service.instance_variable_get(:@compute_mgmt_client)
|
8
|
+
@image = compute_client.images
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_delete_image_success
|
12
|
+
@image.stub :delete, true do
|
13
|
+
assert @service.delete_image('fog-test-rg', 'fog-test-server-osImage')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_delete_image_failure
|
18
|
+
response = proc { raise MsRestAzure::AzureOperationError.new(nil, nil, 'error' => { 'message' => 'mocked exception' }) }
|
19
|
+
@image.stub :delete, response do
|
20
|
+
assert_raises(RuntimeError) { @service.delete_image('fog-test-rg', 'fog-test-server-osImage') }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __dir__)
|
2
|
+
|
3
|
+
# Test class for Get Image Request
|
4
|
+
class TestGetImage < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@service = Fog::Compute::AzureRM.new(credentials)
|
7
|
+
compute_client = @service.instance_variable_get(:@compute_mgmt_client)
|
8
|
+
@image = compute_client.images
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_get_image_success
|
12
|
+
@image.stub :get, true do
|
13
|
+
assert @service.get_image('fog-test-rg', 'TestImage')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_get_image_failure
|
18
|
+
response = proc { raise MsRestAzure::AzureOperationError.new(nil, nil, 'error' => { 'message' => 'mocked exception' }) }
|
19
|
+
@image.stub :get, response do
|
20
|
+
assert_raises(RuntimeError) { @service.get_image('fog-test-rg', 'TestImage') }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fog-azure-rm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shaffan Chaudhry
|
@@ -18,7 +18,7 @@ authors:
|
|
18
18
|
autorequire:
|
19
19
|
bindir: bin
|
20
20
|
cert_chain: []
|
21
|
-
date: 2017-
|
21
|
+
date: 2017-12-12 00:00:00.000000000 Z
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
24
24
|
name: rake
|
@@ -422,19 +422,20 @@ files:
|
|
422
422
|
- lib/fog/azurerm/requests/compute/check_vm_extension_exists.rb
|
423
423
|
- lib/fog/azurerm/requests/compute/check_vm_status.rb
|
424
424
|
- lib/fog/azurerm/requests/compute/create_availability_set.rb
|
425
|
-
- lib/fog/azurerm/requests/compute/
|
425
|
+
- lib/fog/azurerm/requests/compute/create_image.rb
|
426
426
|
- lib/fog/azurerm/requests/compute/create_or_update_managed_disk.rb
|
427
427
|
- lib/fog/azurerm/requests/compute/create_or_update_vm_extension.rb
|
428
428
|
- lib/fog/azurerm/requests/compute/create_virtual_machine.rb
|
429
429
|
- lib/fog/azurerm/requests/compute/deallocate_virtual_machine.rb
|
430
430
|
- lib/fog/azurerm/requests/compute/delete_availability_set.rb
|
431
|
-
- lib/fog/azurerm/requests/compute/
|
431
|
+
- lib/fog/azurerm/requests/compute/delete_image.rb
|
432
432
|
- lib/fog/azurerm/requests/compute/delete_managed_disk.rb
|
433
433
|
- lib/fog/azurerm/requests/compute/delete_virtual_machine.rb
|
434
434
|
- lib/fog/azurerm/requests/compute/delete_vm_extension.rb
|
435
435
|
- lib/fog/azurerm/requests/compute/detach_data_disk_from_vm.rb
|
436
436
|
- lib/fog/azurerm/requests/compute/generalize_virtual_machine.rb
|
437
437
|
- lib/fog/azurerm/requests/compute/get_availability_set.rb
|
438
|
+
- lib/fog/azurerm/requests/compute/get_image.rb
|
438
439
|
- lib/fog/azurerm/requests/compute/get_managed_disk.rb
|
439
440
|
- lib/fog/azurerm/requests/compute/get_virtual_machine.rb
|
440
441
|
- lib/fog/azurerm/requests/compute/get_vm_extension.rb
|
@@ -677,7 +678,7 @@ files:
|
|
677
678
|
- test/api_stub/models/traffic_manager/traffic_manager_profile.rb
|
678
679
|
- test/api_stub/requests/application_gateway/gateway.rb
|
679
680
|
- test/api_stub/requests/compute/availability_set.rb
|
680
|
-
- test/api_stub/requests/compute/
|
681
|
+
- test/api_stub/requests/compute/image.rb
|
681
682
|
- test/api_stub/requests/compute/managed_disk.rb
|
682
683
|
- test/api_stub/requests/compute/virtual_machine.rb
|
683
684
|
- test/api_stub/requests/compute/virtual_machine_extension.rb
|
@@ -821,18 +822,19 @@ files:
|
|
821
822
|
- test/requests/compute/test_check_vm_exists.rb
|
822
823
|
- test/requests/compute/test_check_vm_extension_exists.rb
|
823
824
|
- test/requests/compute/test_create_availability_set.rb
|
824
|
-
- test/requests/compute/
|
825
|
+
- test/requests/compute/test_create_image.rb
|
825
826
|
- test/requests/compute/test_create_or_update_managed_disk.rb
|
826
827
|
- test/requests/compute/test_create_virtual_machine.rb
|
827
828
|
- test/requests/compute/test_create_vm_extension.rb
|
828
829
|
- test/requests/compute/test_deallocate_virtual_machine.rb
|
829
830
|
- test/requests/compute/test_delete_availability_set.rb
|
830
|
-
- test/requests/compute/
|
831
|
+
- test/requests/compute/test_delete_image.rb
|
831
832
|
- test/requests/compute/test_delete_managed_disk.rb
|
832
833
|
- test/requests/compute/test_delete_virtual_machine.rb
|
833
834
|
- test/requests/compute/test_detach_data_disk_from_vm.rb
|
834
835
|
- test/requests/compute/test_generalize_virtual_machine.rb
|
835
836
|
- test/requests/compute/test_get_availability_set.rb
|
837
|
+
- test/requests/compute/test_get_image.rb
|
836
838
|
- test/requests/compute/test_get_managed_disk.rb
|
837
839
|
- test/requests/compute/test_get_virtual_machine.rb
|
838
840
|
- test/requests/compute/test_get_vm_extension.rb
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require File.expand_path '../../test_helper', __dir__
|
2
|
-
|
3
|
-
# Test class for Create Virtual Machine Request
|
4
|
-
class TestCreateGeneralizedImage < Minitest::Test
|
5
|
-
def setup
|
6
|
-
@service = Fog::Compute::AzureRM.new(credentials)
|
7
|
-
compute_client = @service.instance_variable_get(:@compute_mgmt_client)
|
8
|
-
@image = compute_client.images
|
9
|
-
@response = ApiStub::Requests::Compute::GeneralizedImage.create_generalized_image(compute_client)
|
10
|
-
@input_params = ApiStub::Requests::Compute::GeneralizedImage.generalized_image_params
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_create_generalized_image_success
|
14
|
-
@image.stub :create_or_update, @response do
|
15
|
-
assert_equal @service.create_generalized_image(@input_params), @response
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_create_generalized_image_failure
|
20
|
-
response = proc { raise MsRestAzure::AzureOperationError.new(nil, nil, 'error' => { 'message' => 'mocked exception' }) }
|
21
|
-
@image.stub :create_or_update, response do
|
22
|
-
assert_raises RuntimeError do
|
23
|
-
@service.create_generalized_image(@input_params)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require File.expand_path '../../test_helper', __dir__
|
2
|
-
|
3
|
-
# Test class for Delete Virtual Machine Request
|
4
|
-
class TestDeleteGeneralizedImage < Minitest::Test
|
5
|
-
def setup
|
6
|
-
@service = Fog::Compute::AzureRM.new(credentials)
|
7
|
-
compute_client = @service.instance_variable_get(:@compute_mgmt_client)
|
8
|
-
@generalized_image = compute_client.images
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_delete_generalized_image_success
|
12
|
-
@generalized_image.stub :delete, true do
|
13
|
-
assert @service.delete_generalized_image('fog-test-rg', 'fog-test-server-osImage')
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_delete_generalized_image_failure
|
18
|
-
response = proc { raise MsRestAzure::AzureOperationError.new(nil, nil, 'error' => { 'message' => 'mocked exception' }) }
|
19
|
-
@generalized_image.stub :delete, response do
|
20
|
-
assert_raises(RuntimeError) { @service.delete_generalized_image('fog-test-rg', 'fog-test-server-osImage') }
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|