-
Notifications
You must be signed in to change notification settings - Fork 9
/
alicloud_disk.rb
101 lines (88 loc) · 3.11 KB
/
alicloud_disk.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require 'alicloud_backend'
class AliCloudDisk < AliCloudResourceBase
name 'alicloud_disk'
desc 'Verifies properties for an individual AliCloud disk.'
example <<-EXAMPLE
describe alicloud_disk('disk-12345678') do
it { should exist }
its('encrypted') { should eq true }
its('size') { should cmp 100 }
its('delete_with_instance') { should eq false }
its('enable_auto_snapshot') { should eq true }
its('delete_auto_snapshot') { should eq false }
end
EXAMPLE
attr_reader :id, :name, :description, :size, :category, :encrypted, :kms_key_id,
:enable_auto_snapshot, :delete_auto_snapshot, :delete_with_instance
def initialize(opts = {})
opts = { disk_id: opts } if opts.is_a?(String)
opts[:disk_id] = opts.delete(:id) if opts.key?(:id) # id is an alias for disk_id
opts[:disk_name] = opts.delete(:name) if opts.key?(:name) # name is an alias for disk_name
@opts = opts
super(opts)
validate_parameters(require_any_of: %i(disk_id disk_name), required: %i(region))
if opts[:disk_id] && !opts[:disk_id].empty?
if opts[:disk_id] !~ /^d-[0-9a-z]+$/
raise ArgumentError,
"#{@__resource_name__}: disk ID must be in the format 'd- followed by alphanumeric characters."
end
raise ArgumentError, "#{@__resource_name__}: expected only one of `disk_id` or `disk_name`" if opts[:disk_name]
elsif !opts[:disk_name] || opts[:disk_name].empty?
raise ArgumentError, "#{@__resource_name__}: `disk_id` or `disk_name` must be provided"
end
@resp = fetch_disk_info(opts)
if @resp.nil?
@encrypted = false
return
end
@disk = @resp
@id = @disk['DiskId']
@name = @disk['DiskName']
@description = @disk['Description']
@size = @disk['Size']
@category = @disk['Category']
@encrypted = @disk['Encrypted']
@kms_key_id = @disk['KMSKeyId']
@enable_auto_snapshot = @disk['EnableAutoSnapshot']
@delete_auto_snapshot = @disk['DeleteAutoSnapshot']
@delete_with_instance = @disk['DeleteWithInstance']
end
def fetch_disk_info(opts)
filters = opts.key?(:disk_id) ? { DiskIds: [opts[:disk_id]] } : { DiskName: opts[:disk_name] }
filters[:RegionId] = opts[:region]
catch_alicloud_errors do
resp = @alicloud.ecs_client.request(
action: 'DescribeDisks',
params: filters,
opts: {
method: 'POST',
},
)['Disks']['Disk']
disk = if opts.key?(:disk_id)
resp.select { |d| d['DiskId'] == opts[:disk_id] }.first
else
resp.select { |d| d['DiskName'] == opts[:disk_name] }.first
end
return disk
end
end
def encrypted?
@encrypted
end
def exists?
end
def resource_id
@id
end
def to_s
if @opts[:disk_name]
d = "ECS Disk: Name: #{@opts[:disk_name]}"
d += " ID: #{@id}" if @id
else
d = "ECS Disk: ID: #{@opts[:disk_id]}"
d += " Name: #{@name}" if @name
end
"#{d} in #{opts[:region]}"
end
end