fog-arubacloud 0.0.3
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 +7 -0
- data/.codeclimate.yml +26 -0
- data/.gitignore +15 -0
- data/.rubocop.yml +1168 -0
- data/.ruby-version +1 -0
- data/.travis.yml +18 -0
- data/CONTRIBUTING.md +28 -0
- data/CONTRIBUTORS.md +2 -0
- data/Gemfile +6 -0
- data/LICENSE.md +20 -0
- data/README.md +36 -0
- data/Rakefile +24 -0
- data/fog-arubacloud.gemspec +31 -0
- data/gemfiles/fog-arubacloud-0.0.1.gem +0 -0
- data/gemfiles/fog-arubacloud-0.0.2.gem +0 -0
- data/lib/fog/arubacloud.rb +31 -0
- data/lib/fog/arubacloud/compute.rb +96 -0
- data/lib/fog/arubacloud/error.rb +84 -0
- data/lib/fog/arubacloud/examples/create_pro_server.rb +53 -0
- data/lib/fog/arubacloud/examples/create_smart_server.rb +39 -0
- data/lib/fog/arubacloud/examples/get_objects.rb +44 -0
- data/lib/fog/arubacloud/examples/get_servers.rb +31 -0
- data/lib/fog/arubacloud/examples/vm_operations.rb +60 -0
- data/lib/fog/arubacloud/models/compute/disk.rb +47 -0
- data/lib/fog/arubacloud/models/compute/disks.rb +31 -0
- data/lib/fog/arubacloud/models/compute/ip.rb +45 -0
- data/lib/fog/arubacloud/models/compute/ips.rb +50 -0
- data/lib/fog/arubacloud/models/compute/network_adapter.rb +26 -0
- data/lib/fog/arubacloud/models/compute/network_adapters.rb +20 -0
- data/lib/fog/arubacloud/models/compute/server.rb +185 -0
- data/lib/fog/arubacloud/models/compute/servers.rb +43 -0
- data/lib/fog/arubacloud/models/compute/template.rb +33 -0
- data/lib/fog/arubacloud/models/compute/templates.rb +67 -0
- data/lib/fog/arubacloud/requests/compute/create_vm_pro.rb +88 -0
- data/lib/fog/arubacloud/requests/compute/create_vm_smart.rb +74 -0
- data/lib/fog/arubacloud/requests/compute/delete_vm.rb +50 -0
- data/lib/fog/arubacloud/requests/compute/get_hypervisors.rb +49 -0
- data/lib/fog/arubacloud/requests/compute/get_purchased_ip_addresses.rb +79 -0
- data/lib/fog/arubacloud/requests/compute/get_server_details.rb +51 -0
- data/lib/fog/arubacloud/requests/compute/get_servers.rb +86 -0
- data/lib/fog/arubacloud/requests/compute/power_off_vm.rb +51 -0
- data/lib/fog/arubacloud/requests/compute/power_on_vm.rb +49 -0
- data/lib/fog/arubacloud/requests/compute/purchase_ip.rb +62 -0
- data/lib/fog/arubacloud/requests/compute/reinitialize_vm.rb +56 -0
- data/lib/fog/arubacloud/requests/compute/remove_ip.rb +53 -0
- data/lib/fog/arubacloud/service.rb +54 -0
- data/lib/fog/arubacloud/version.rb +12 -0
- data/spec/fog/arubacloud/config_spec.rb +0 -0
- data/spec/fog/compute/arubacloud/server_spec.rb +26 -0
- data/spec/fog/compute/arubacloud/servers_spec.rb +11 -0
- data/spec/fog/compute/arubacloud_spec.rb +32 -0
- data/spec/model_setup.rb +16 -0
- data/spec/spec_helper.rb +6 -0
- data/tests/test_helper.rb +8 -0
- metadata +217 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'fog/arubacloud'
|
10
|
+
require 'securerandom'
|
11
|
+
require 'optparse'
|
12
|
+
|
13
|
+
options = {}
|
14
|
+
OptionParser.new do |opts|
|
15
|
+
opts.banner = 'Usage: get_servers.rb [options]'
|
16
|
+
opts.on('-uUSERNAME', '--username=USERNAME', 'ArubaCloud Username') { |n| options[:username] = n}
|
17
|
+
opts.on('-pPASSWORD', '--password=PASSWORD', 'ArubaCloud Password') { |p| options[:password] = p}
|
18
|
+
end.parse!
|
19
|
+
|
20
|
+
# Fog.mock!
|
21
|
+
service = Fog::Compute.new({
|
22
|
+
:provider => 'ArubaCloud',
|
23
|
+
:arubacloud_username => options[:username],
|
24
|
+
:arubacloud_password => options[:password]
|
25
|
+
})
|
26
|
+
|
27
|
+
## Generate random string to append to vm name
|
28
|
+
rnd_string = SecureRandom.hex(2)
|
29
|
+
|
30
|
+
# Create a Smart VM
|
31
|
+
server = service.servers.create({
|
32
|
+
:name => "testfog#{rnd_string}",
|
33
|
+
:vm_type => 'smart',
|
34
|
+
:admin_passwd => 'Prova123',
|
35
|
+
:cpu => 1,
|
36
|
+
:memory => 1,
|
37
|
+
:template_id => '415',
|
38
|
+
:package_id => 1
|
39
|
+
})
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'fog/arubacloud'
|
10
|
+
require 'optparse'
|
11
|
+
|
12
|
+
options = {}
|
13
|
+
OptionParser.new do |opts|
|
14
|
+
opts.banner = 'Usage: get_servers.rb [options]'
|
15
|
+
opts.on('-uUSERNAME', '--username=USERNAME', 'ArubaCloud Username') { |n| options[:username] = n}
|
16
|
+
opts.on('-pPASSWORD', '--password=PASSWORD', 'ArubaCloud Password') { |p| options[:password] = p}
|
17
|
+
end.parse!
|
18
|
+
|
19
|
+
# Fog.mock!
|
20
|
+
service = Fog::Compute.new({
|
21
|
+
:provider => 'ArubaCloud',
|
22
|
+
:arubacloud_username => options[:username],
|
23
|
+
:arubacloud_password => options[:password]
|
24
|
+
})
|
25
|
+
|
26
|
+
# Retrieve purchased IP list.
|
27
|
+
# ips = service.ips
|
28
|
+
# ips.each { |ip| puts ip.address }
|
29
|
+
|
30
|
+
# Retrieve template list
|
31
|
+
# Hypervisor Types:
|
32
|
+
# 1 -> Microsoft Hyper-V - Cloud Pro
|
33
|
+
# 2 -> VMWare - Cloud Pro
|
34
|
+
# 3 -> Microsoft Hyper-V Low Cost - Cloud Pro
|
35
|
+
# 4 -> VMWare - Cloud Smart
|
36
|
+
templates = service.templates
|
37
|
+
# Filter only smart templates
|
38
|
+
smart_templates = templates.select { |template| template.hypervisor.eql?(4) && template.enabled }
|
39
|
+
# Filter only templates which contains Centos in the name
|
40
|
+
centos_smart = smart_templates.select { |template| template.name.include?('Centos')}
|
41
|
+
centos_smart.each do |t|
|
42
|
+
puts t.name
|
43
|
+
puts t.id
|
44
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'fog/arubacloud'
|
10
|
+
require 'optparse'
|
11
|
+
|
12
|
+
options = {}
|
13
|
+
OptionParser.new do |opts|
|
14
|
+
opts.banner = 'Usage: get_servers.rb [options]'
|
15
|
+
opts.on('-uUSERNAME', '--username=USERNAME', 'ArubaCloud Username') { |n| options[:username] = n}
|
16
|
+
opts.on('-pPASSWORD', '--password=PASSWORD', 'ArubaCloud Password') { |p| options[:password] = p}
|
17
|
+
end.parse!
|
18
|
+
|
19
|
+
service = Fog::Compute.new({
|
20
|
+
:provider => 'ArubaCloud',
|
21
|
+
:arubacloud_username => options[:username],
|
22
|
+
:arubacloud_password => options[:password]
|
23
|
+
})
|
24
|
+
|
25
|
+
servers = service.servers
|
26
|
+
servers.all.each do |server|
|
27
|
+
server.get_server_details
|
28
|
+
server.get_public_ip
|
29
|
+
puts server.id
|
30
|
+
puts server.name
|
31
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'fog/arubacloud'
|
10
|
+
require 'optparse'
|
11
|
+
|
12
|
+
options = {}
|
13
|
+
OptionParser.new do |opts|
|
14
|
+
opts.banner = 'Usage: get_servers.rb [options]'
|
15
|
+
opts.on('-uUSERNAME', '--username=USERNAME', 'ArubaCloud Username') { |n| options[:username] = n}
|
16
|
+
opts.on('-pPASSWORD', '--password=PASSWORD', 'ArubaCloud Password') { |p| options[:password] = p}
|
17
|
+
end.parse!
|
18
|
+
|
19
|
+
# Fog.mock!
|
20
|
+
service = Fog::Compute.new({
|
21
|
+
:provider => 'ArubaCloud',
|
22
|
+
:arubacloud_username => options[:username],
|
23
|
+
:arubacloud_password => options[:password]
|
24
|
+
})
|
25
|
+
|
26
|
+
|
27
|
+
servers = service.servers
|
28
|
+
|
29
|
+
# Poweroff and delete every VM in a Datacenter
|
30
|
+
servers.each do |s|
|
31
|
+
puts("Powering Off and Deleting VM: #{s.name}...")
|
32
|
+
next if s.state == Fog::Compute::ArubaCloud::Server::CREATING
|
33
|
+
s.power_off unless s.stopped?
|
34
|
+
until s.stopped?
|
35
|
+
s.get_server_details
|
36
|
+
puts(" --- Waiting for VM: #{s.name} to power off...")
|
37
|
+
end
|
38
|
+
begin
|
39
|
+
puts(' --- Enqueued Server Deletion')
|
40
|
+
s.delete
|
41
|
+
rescue
|
42
|
+
next
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Delete all vm in a datacenter
|
47
|
+
#servers.each do |s|
|
48
|
+
# begin
|
49
|
+
# s.delete
|
50
|
+
# rescue
|
51
|
+
# next
|
52
|
+
# end
|
53
|
+
#end
|
54
|
+
|
55
|
+
# Release all unassociated IP
|
56
|
+
# ips = service.ips
|
57
|
+
# ips.each do |i|
|
58
|
+
# puts i.address
|
59
|
+
# i.remove
|
60
|
+
# end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'fog/core/model'
|
9
|
+
require 'fog/arubacloud/error'
|
10
|
+
|
11
|
+
module Fog
|
12
|
+
module Compute
|
13
|
+
class ArubaCloud
|
14
|
+
class Disk < Fog::Model
|
15
|
+
attribute :size
|
16
|
+
attribute :virtual_disk_type
|
17
|
+
|
18
|
+
# Fake create method, it returns only an hash representing the json scheme of a disk
|
19
|
+
# @return [Hash] hash object representing the disk
|
20
|
+
# @raise [Fog::ArubaCloud::Errors::BadDiskSize]
|
21
|
+
# @raise [Fog::ArubaCloud::Errors::BadDiskNumber]
|
22
|
+
def create
|
23
|
+
requires :size, :virtual_disk_type
|
24
|
+
|
25
|
+
raise Fog::ArubaCloud::Errors::BadDiskSize.new('Maximum disk size: 500GB') unless
|
26
|
+
size <= 500 # maximum disk size: 500GB
|
27
|
+
raise Fog::ArubaCloud::Errors::BadDiskNumber.new('Only 4 disks are supported per VM') unless
|
28
|
+
virtual_disk_type < 3 # only 4 disks are supported per VM
|
29
|
+
end
|
30
|
+
|
31
|
+
def save
|
32
|
+
create
|
33
|
+
end
|
34
|
+
|
35
|
+
# Return an hash representation of the object in order to be used in json request
|
36
|
+
def get_hash
|
37
|
+
requires :size, :virtual_disk_type
|
38
|
+
{
|
39
|
+
:Size => size,
|
40
|
+
:VirtualDiskType => virtual_disk_type
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
end #Disk
|
45
|
+
end #ArubaCloud
|
46
|
+
end #Compute
|
47
|
+
end #Fog
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'fog/core/collection'
|
9
|
+
require 'fog/arubacloud/models/compute/ip'
|
10
|
+
|
11
|
+
module Fog
|
12
|
+
module Compute
|
13
|
+
class ArubaCloud
|
14
|
+
|
15
|
+
class Disks < Fog::Collection
|
16
|
+
model Fog::Compute::ArubaCloud::Disk
|
17
|
+
|
18
|
+
# Dummy Method, only to maintain fog structure, it returns nothings.
|
19
|
+
# @return [Nil]
|
20
|
+
def all
|
21
|
+
end
|
22
|
+
|
23
|
+
# Another dummy method, it returns nothings.
|
24
|
+
# @return [Nil]
|
25
|
+
def get
|
26
|
+
end
|
27
|
+
end #Disks
|
28
|
+
|
29
|
+
end #ArubaCloud
|
30
|
+
end #Compute
|
31
|
+
end #Fog
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'fog/core/model'
|
9
|
+
require 'fog/arubacloud/error'
|
10
|
+
|
11
|
+
module Fog
|
12
|
+
module Compute
|
13
|
+
class ArubaCloud
|
14
|
+
class IP < Fog::Model
|
15
|
+
identity :id, :aliases => 'ResourceId'
|
16
|
+
|
17
|
+
attribute :gateway, :aliases => 'Gateway'
|
18
|
+
attribute :server , :aliases => 'ServerId'
|
19
|
+
attribute :address, :aliases => 'Value'
|
20
|
+
attribute :netmask, :aliases => 'SubNetMask'
|
21
|
+
attribute :product_id, :aliases => 'ProductId'
|
22
|
+
attribute :loadbalancer_id, :aliases => 'LoadBalancerID'
|
23
|
+
ignore_attributes :CompanyId, :ResourceType
|
24
|
+
|
25
|
+
def initialize(attributes = {})
|
26
|
+
@service = attributes[:service]
|
27
|
+
super
|
28
|
+
end
|
29
|
+
|
30
|
+
def purchase
|
31
|
+
@service.purchase_ip
|
32
|
+
end
|
33
|
+
|
34
|
+
def remove
|
35
|
+
requires :id, :server
|
36
|
+
unless server.nil?
|
37
|
+
raise Fog::ArubaCloud::Errors::RequestError.new('Cannot remove an address attached to a vm.')
|
38
|
+
end
|
39
|
+
@service.remove_ip(id)
|
40
|
+
end
|
41
|
+
|
42
|
+
end #IP
|
43
|
+
end #ArubaCloud
|
44
|
+
end #Compute
|
45
|
+
end #Fog
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'fog/core/collection'
|
9
|
+
require 'fog/arubacloud/models/compute/ip'
|
10
|
+
|
11
|
+
module Fog
|
12
|
+
module Compute
|
13
|
+
class ArubaCloud
|
14
|
+
|
15
|
+
class Ips < Fog::Collection
|
16
|
+
model Fog::Compute::ArubaCloud::IP
|
17
|
+
|
18
|
+
# Returns list of Ip Addresses
|
19
|
+
# @return [Fog::Compute::ArubaCloud::Ips]
|
20
|
+
# @raise [Fog::ArubaCloud::Errors::NotFound]
|
21
|
+
def all
|
22
|
+
data = service.get_purchased_ip_addresses
|
23
|
+
objects = data['Value']
|
24
|
+
load(objects)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns a single IpAddress object
|
28
|
+
# @param ip [String]
|
29
|
+
# @return [Fog::Compute::ArubaCloud::IP]
|
30
|
+
# @raise [Fog::ArubaCloud::Errors::NotFound]
|
31
|
+
def get(ip)
|
32
|
+
# TODO: Implement single item retrieve
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns list of Ip Addresses related to a Virtual Machine
|
36
|
+
def filter_by_vmid(vmid)
|
37
|
+
# TODO: Implement filter ip per ID
|
38
|
+
end
|
39
|
+
|
40
|
+
# Purchase a new IpAddress
|
41
|
+
# @return [Hash] hash containing the response of the request
|
42
|
+
# @raise [Fog::ArubaCloud::Errors::RequestError]
|
43
|
+
def purchase
|
44
|
+
self << new(service.purchase_ip['Value'])
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'fog/core/model'
|
9
|
+
require 'fog/arubacloud/error'
|
10
|
+
|
11
|
+
module Fog
|
12
|
+
module Compute
|
13
|
+
class ArubaCloud
|
14
|
+
class NetworkAdapter < Fog::Model
|
15
|
+
identity :id, :aliases => 'Id'
|
16
|
+
|
17
|
+
attribute :mac_address, :aliases => 'MacAddress', :type => String
|
18
|
+
attribute :adapter_type, :aliases => 'NetworkAdapterType', :type => Integer
|
19
|
+
attribute :server_id, :aliases => 'ServerId', :type => Integer
|
20
|
+
attribute :vlan, :aliases => 'VLan', :type => Integer
|
21
|
+
|
22
|
+
has_many :ip_addresses, :ips, aliases: 'IPAddresses'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'fog/core/collection'
|
9
|
+
require 'fog/arubacloud/models/compute/network_adapter'
|
10
|
+
|
11
|
+
|
12
|
+
module Fog
|
13
|
+
module Compute
|
14
|
+
class ArubaCloud
|
15
|
+
class NetworkAdapters < Fog::Collection
|
16
|
+
model Fog::Compute::ArubaCloud::NetworkAdapter
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alessio Rocchi (<alessio.rocchi@staff.aruba.it>)
|
3
|
+
# © Copyright ArubaCloud.
|
4
|
+
#
|
5
|
+
# LICENSE: MIT (http://opensource.org/licenses/MIT)
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'fog/compute/models/server'
|
9
|
+
require 'fog/arubacloud/error'
|
10
|
+
|
11
|
+
module Fog
|
12
|
+
module Compute
|
13
|
+
class ArubaCloud
|
14
|
+
class Server < Fog::Compute::Server
|
15
|
+
CREATING = 1
|
16
|
+
STOPPED = 2
|
17
|
+
RUNNING = 3
|
18
|
+
ARCHIVED = 4
|
19
|
+
DELETED = 5
|
20
|
+
|
21
|
+
# This is the instance ID which is unique per Datacenter
|
22
|
+
identity :id, :aliases => 'ServerId'
|
23
|
+
|
24
|
+
attribute :name, :aliases => 'Name'
|
25
|
+
attribute :state, :aliases => 'ServerStatus'
|
26
|
+
attribute :memory, :aliases => 'RAMQuantity', :squash => 'Quantity'
|
27
|
+
attribute :cpu, :aliases => 'CPUQuantity', :squash => 'Quantity'
|
28
|
+
attribute :hypervisor, :aliases => 'HypervisorType'
|
29
|
+
attribute :datacenter_id, :aliases => 'DatacenterId'
|
30
|
+
attribute :hd_qty, :aliases => 'HDQuantity'
|
31
|
+
attribute :hd_total_size, :aliases => 'HDTotalSize'
|
32
|
+
attribute :smart_ipv4, :aliases => 'EasyCloudIPAddress', :squash => 'Value'
|
33
|
+
attribute :smart_package, :aliases => 'EasyCloudPackageID'
|
34
|
+
attribute :vnc_port, :aliases => 'VncPort'
|
35
|
+
attribute :admin_passwd
|
36
|
+
attribute :vm_type
|
37
|
+
attribute :ipv4_addr
|
38
|
+
attribute :package_id
|
39
|
+
attribute :template_id, :aliases => 'OSTemplateId'
|
40
|
+
attribute :template_description, :aliases => 'OSTemplate', :squash => :Description
|
41
|
+
|
42
|
+
#has_many :network_adapters, :network_adapters, aliases: 'NetworkAdapters'
|
43
|
+
|
44
|
+
ignore_attributes :CompanyId, :Parameters, :VirtualDVDs, :RenewDateSmart, :Note, :CreationDate,
|
45
|
+
:ControlToolActivationDate, :ControlToolInstalled, :UserId, :ScheduledOperations, :Snapshots,
|
46
|
+
:ActiveJobs
|
47
|
+
|
48
|
+
def initialize(attributes = {})
|
49
|
+
@service = attributes[:service]
|
50
|
+
|
51
|
+
if attributes[:vm_type].nil?
|
52
|
+
self.vm_type = 'smart' if attributes['HypervisorType'].eql? '4' || 'pro'
|
53
|
+
end
|
54
|
+
|
55
|
+
super
|
56
|
+
end
|
57
|
+
|
58
|
+
# Is server in ready state
|
59
|
+
# @param [String] ready_state By default state is RUNNING
|
60
|
+
# @return [Boolean] return true if server is in ready state
|
61
|
+
def ready?(ready_state=RUNNING)
|
62
|
+
state == ready_state
|
63
|
+
end
|
64
|
+
|
65
|
+
# Is server in stopped state
|
66
|
+
# @param [String] stopped_state By default stopped state is STOPPED
|
67
|
+
# @return [Boolean] return true if server is in stopped state
|
68
|
+
def stopped?(stopped_state=STOPPED)
|
69
|
+
state == stopped_state
|
70
|
+
end
|
71
|
+
|
72
|
+
# Is server in creating state
|
73
|
+
# @param [String] creating_state By default creating state is CREATING
|
74
|
+
# @return [Boolean] return true if server is in creating state
|
75
|
+
def creating?(creating_state=CREATING)
|
76
|
+
state == creating_state
|
77
|
+
end
|
78
|
+
|
79
|
+
def save
|
80
|
+
if persisted?
|
81
|
+
update
|
82
|
+
else
|
83
|
+
create
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def create
|
88
|
+
requires :name, :cpu, :memory, :admin_passwd, :vm_type
|
89
|
+
data = attributes
|
90
|
+
|
91
|
+
if vm_type.eql? 'pro'
|
92
|
+
# Automatically purchase a public ip address
|
93
|
+
data[:ipv4_id] = service.purchase_ip['Value']['ResourceId']
|
94
|
+
service.create_vm_pro(data)
|
95
|
+
elsif vm_type.eql? 'smart'
|
96
|
+
service.create_vm_smart(data)
|
97
|
+
else
|
98
|
+
raise Fog::ArubaCloud::Errors::BadParameters.new('VM Type can be smart or pro.')
|
99
|
+
end
|
100
|
+
|
101
|
+
# Retrieve new server list and filter the current virtual machine
|
102
|
+
# in order to retrieve the ID
|
103
|
+
# In case of failure, I will try it 3 for 3 times
|
104
|
+
try = 0
|
105
|
+
server = nil
|
106
|
+
while server.nil? && try <= 3
|
107
|
+
servers = service.get_servers
|
108
|
+
#server = servers['Value'].select { |v| v['Name'].include?(data[:name]) }.first
|
109
|
+
servers['Value'].each do |s|
|
110
|
+
if s['Name'].to_s.include? data[:name].to_s
|
111
|
+
server = s
|
112
|
+
end
|
113
|
+
end
|
114
|
+
Fog::Logger.debug("Fog::Compute::ArubaCloud::Server.create, #{data[:name]} server: #{server.inspect}")
|
115
|
+
if server
|
116
|
+
merge_attributes(server)
|
117
|
+
else
|
118
|
+
message = "error during attribute merge, `server` object is not ready, try n.#{try}"
|
119
|
+
Fog::Logger.warning("Fog::Compute::ArubaCloud::Server.create, #{message}")
|
120
|
+
Fog::Logger.warning(servers.inspect.to_yaml)
|
121
|
+
sleep(1)
|
122
|
+
end
|
123
|
+
try += 1
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def get_server_details
|
128
|
+
requires :id
|
129
|
+
response = service.get_server_details(id)
|
130
|
+
new_attributes = response['Value']
|
131
|
+
merge_attributes(new_attributes)
|
132
|
+
end
|
133
|
+
|
134
|
+
def get_public_ip
|
135
|
+
requires :id
|
136
|
+
if self.vm_type.eql? 'smart'
|
137
|
+
self.smart_ipv4
|
138
|
+
else
|
139
|
+
self.get_server_details unless self.attributes['NetworkAdapters']
|
140
|
+
self.attributes['NetworkAdapters'].each do |na|
|
141
|
+
na['IPAddresses'].each do |ipa|
|
142
|
+
if ipa['ProductId'].eql? 20
|
143
|
+
self.ipv4_addr = ipa['Value'].to_s
|
144
|
+
return self.ipv4_addr
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
def power_off
|
153
|
+
requires :id, :state
|
154
|
+
unless state.eql? RUNNING
|
155
|
+
raise Fog::ArubaCloud::Errors::VmStatus.new("Cannot poweroff vm in current state: #{state}")
|
156
|
+
end
|
157
|
+
@service.power_off_vm(id)
|
158
|
+
end
|
159
|
+
|
160
|
+
def power_on
|
161
|
+
requires :id, :state
|
162
|
+
unless state.eql? STOPPED
|
163
|
+
raise Fog::ArubaCloud::Errors::VmStatus.new("Cannot poweron vm in current state: #{state}")
|
164
|
+
end
|
165
|
+
@service.power_on_vm(id)
|
166
|
+
end
|
167
|
+
|
168
|
+
def delete
|
169
|
+
requires :id
|
170
|
+
state == STOPPED ? service.delete_vm(id) : raise(Fog::ArubaCloud::Errors::VmStatus.new(
|
171
|
+
"Cannot delete vm in current state: #{state}"
|
172
|
+
))
|
173
|
+
end
|
174
|
+
|
175
|
+
def reinitialize
|
176
|
+
requires :id, :hypervisor
|
177
|
+
state == STOPPED ? service.reinitialize_vm(id) : raise(Fog::ArubaCloud::Errors::VmStatus.new(
|
178
|
+
"Cannot reinitialize vm in current state: #{state}"
|
179
|
+
))
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|