-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathalicloud_slb.rb
128 lines (109 loc) · 4.7 KB
/
alicloud_slb.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require 'alicloud_backend'
class AliCloudSlb < AliCloudResourceBase
name 'alicloud_slb'
desc 'Verifies properties for an individual AliCloud Application Load Balancer.'
example <<-EXAMPLE
describe alicloud_slb('slb-123456') do
it { should exist }
end
EXAMPLE
attr_reader :load_balancer_id, :load_balancer_name, :status, :resource_group_id, :address, :listener_ports_and_protocol, :backend_servers,
:has_reserved_info, :business_status, :listener_ports, :vswitch_id, :pay_type, :internet_charge_type,
:vpc_id, :delete_protection, :end_time_stamp, :end_time, :support_private_link, :address_ip_version,
:network_type, :bandwidth, :primary_zone_id, :create_time, :secondary_zone_id, :region_id_alias,
:region_id, :address_type, :create_time_stamp
def initialize(opts = {})
opts = { slb_id: opts } if opts.is_a?(String)
opts[:slb_id] = opts.delete(:id) if opts.key?(:id)
@opts = opts
super(opts)
validate_parameters(required: %i(slb_id region))
catch_alicloud_errors(ignore: 'InvalidLoadBalancerId.NotFound') do
@resp = @alicloud.slb_client.request(
action: 'DescribeLoadBalancerAttribute',
params: {
'RegionId': opts[:region],
'LoadBalancerId': opts[:slb_id],
},
)
end
if @resp.nil?
@slb_id = 'empty response'
return
end
@slb_info = @resp
@load_balancer_id = @slb_info['LoadBalancerId']
@load_balancer_name = @slb_info['LoadBalancerName']
@status = @slb_info['LoadBalancerStatus']
@resource_group_id = @slb_info['ResourceGroupId']
@address = @slb_info['Address']
@listener_ports_and_protocol = @slb_info['ListenerPortsAndProtocol']['ListenerPortAndProtocol']
@backend_servers = @slb_info['BackendServers']
@has_reserved_info = @slb_info['HasReservedInfo']
@business_status = @slb_info['BusinessStatus']
@listener_ports = @slb_info['ListenerPorts']
@vswitch_id = @slb_info['VSwitchId']
@pay_type = @slb_info['PayType']
@internet_charge_type = @slb_info['InternetChargeType']
@vpc_id = @slb_info['VpcId']
@delete_protection = @slb_info['DeleteProtection']
@end_time_stamp = @slb_info['EndTimeStamp']
@end_time = @slb_info['EndTime']
@support_private_ling = @slb_info['SupportPrivateLink']
@address_ip_version = @slb_info['AddressIPVersion']
@network_type = @slb_info['NetworkType']
@bandwidth = @slb_info['Bandwidth']
@primary_zone_id = @slb_info['MasterZoneId']
@create_time = @slb_info['CreateTime']
@secondary_zone_id = @slb_info['SlaveZoneId']
@region_id_alias = @slb_info['RegionIdAlias']
@region_id = @slb_info['RegionId']
@address_type = @slb_info['AddressType']
@create_time_stamp = @slb_info['CreateTimeStamp']
end
def exists?
!@slb_info.nil?
end
def https_listeners?
!@listener_ports_and_protocol.select { |lpp| lpp['ListenerProtocol'] == 'https' }.empty?
end
def http_listeners?
!@listener_ports_and_protocol.select { |lpp| lpp['ListenerProtocol'] == 'http' }.empty?
end
def udp_listeners?
!@listener_ports_and_protocol.select { |lpp| lpp['ListenerProtocol'] == 'udp' }.empty?
end
def tcp_listeners?
!@listener_ports_and_protocol.select { |lpp| lpp['ListenerProtocol'] == 'tcp' }.empty?
end
def listening_ports
@listener_ports_and_protocol.select { |lpp| lpp['ListenerPort'] }
end
def https_ports
@listener_ports_and_protocol.map { |lpp| lpp['ListenerPort'] if lpp['ListenerProtocol'] == 'https' }.compact
end
def http_ports
@listener_ports_and_protocol.map { |lpp| lpp['ListenerPort'] if lpp['ListenerProtocol'] == 'http' }.compact
end
def udp_ports
@listener_ports_and_protocol.map { |lpp| lpp['ListenerPort'] if lpp['ListenerProtocol'] == 'udp' }.compact
end
def tcp_ports
@listener_ports_and_protocol.map { |lpp| lpp['ListenerPort'] if lpp['ListenerProtocol'] == 'tcp' }.compact
end
def https_only?
https_ports.length == listening_ports.length
end
def resource_id
@load_balancer_id
end
def to_s
if @load_balancer_id
slb = " ID: #{@load_balancer_id}"
slb += " Name: #{@load_balancer_name}" if @load_balancer_name
else
slb = " #{@opts[:slb_id]}"
end
opts.key?(:region) ? "Server Load Balancer:#{slb} in #{opts[:region]}" : "Server Load Balancer:#{slb}"
end
end