Module: NSXDriver::NSXRule::NSXVRule

Included in:
NSXDriver::NSXRule
Defined in:
lib/nsxv_rule.rb

Overview

Module NSXVRule

Instance Method Summary collapse

Instance Method Details

#nsxv_rule_spec(rule, vm_data, nic_data) ⇒ Object



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/nsxv_rule.rb', line 23

def nsxv_rule_spec(rule, vm_data, nic_data)
    rule_name = "#{rule[:id]}-#{rule[:name]}-#{vm_data[:id]}"
    rule_name << "-#{vm_data[:deploy_id]}-#{nic_data[:id]}"

    # rubocop:disable Layout/LineLength
    builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
        # rubocop:enable Layout/LineLength
        xml.rule('disabled' => 'false', 'logged' => 'false') do
            xml.name rule_name
            xml.action 'allow'
            xml.appliedToList do
                xml.appliedTo do
                    xml.name nic_data[:name]
                    xml.value nic_data[:lp]
                    xml.type 'Vnic'
                    xml.isValid 'true'
                end
            end
            xml.sectionId @one_section_id

            # SOURCES / DESTINATIONS: Any | IP Address | Vnet

            unless rule[:network_id].empty? && rule[:subnets].empty?

                if rule[:direction] == 'IN'
                    xml.sources('excluded' => 'false') do
                        if !rule[:network_id].empty?
                            xml.source do
                                xml.name rule[:network_name]
                                xml.value rule[:network_nsxid]
                                xml.type 'VirtualWire'
                                xml.isValid 'true'
                            end
                        elsif !rule[:subnets].empty?
                            rule[:subnets].each do |subnet|
                                xml.source do
                                    # rubocop:disable Layout/LineLength
                                    ip_version = IPAddr.new(subnet).ipv4? ? 'Ipv4Address' : 'Ipv6Address'
                                    # rubocop:enable Layout/LineLength
                                    xml.value subnet
                                    xml.type ip_version
                                    xml.isValid 'true'
                                end
                            end
                        end
                    end
                else
                    xml.destinations('excluded' => 'false') do
                        # Target network: Vnet
                        if !rule[:network_id].empty?
                            xml.destination do
                                xml.name nic_data[:network_name]
                                xml.value rule[:network_nsxid]
                                xml.type 'VirtualWire'
                                xml.isValid 'true'
                            end
                        # Target network: Manual network(IP Address)
                        elsif !rule[:subnets].empty?
                            rule[:subnets].each do |subnet|
                                xml.destination do
                                    # rubocop:disable Layout/LineLength
                                    ip_version = IPAddr.new(subnet).ipv4? ? 'Ipv4Address' : 'Ipv6Address'
                                    # rubocop:enable Layout/LineLength
                                    xml.value subnet
                                    xml.type ip_version
                                    xml.isValid 'true'
                                end
                            end
                        end
                    end
                end
            end

            ##### SERVICES #####
            unless rule[:protocol].empty?
                xml.services do
                    case rule[:protocol]
                    when 'TCP'
                        xml.service do
                            xml.isValid 'true'
                            xml.protocol '6'
                            xml.protocolName 'TCP'
                            # rubocop:disable Layout/LineLength
                            xml.sourcePort parse_ports(rule[:ports]) \
                                if rule[:direction] == 'IN'
                            xml.destinationPort parse_ports(rule[:ports]) \
                                if rule[:direction] == 'OUT'
                            # rubocop:enable Layout/LineLength
                        end
                    when 'UDP'
                        xml.service do
                            xml.isValid 'true'
                            xml.protocol '17'
                            xml.protocolName 'UDP'
                            # rubocop:disable Layout/LineLength
                            xml.sourcePort parse_ports(rule[:ports]) \
                                if rule[:direction] == 'IN'
                            xml.destinationPort parse_ports(rule[:ports]) \
                                if rule[:direction] == 'OUT'
                            # rubocop:enable Layout/LineLength
                        end
                    when 'ICMP'
                        xml.service do
                            xml.isValid 'true'
                            xml.protocol '1'
                            xml.protocolName 'ICMP'
                        end
                    when 'ICMPv6'
                        xml.service do
                            xml.isValid 'true'
                            xml.protocol '58'
                            xml.protocolName 'IPV6ICMP'
                        end
                    when 'IPSEC'
                        ports = NSXConstants::NSX_RULE_IPSEC_PORTS
                        xml.service do
                            xml.isValid 'true'
                            xml.protocol '50'
                            xml.protocolName 'ESP'
                        end
                        xml.service do
                            xml.isValid 'true'
                            xml.protocol '51'
                            xml.protocolName 'AH'
                        end
                        xml.service do
                            xml.isValid 'true'
                            xml.protocol '17'
                            xml.protocolName 'UDP'
                            xml.sourcePort parse_ports(ports) \
                                if rule[:direction] == 'IN'
                            xml.destinationPort parse_ports(ports) \
                                if rule[:direction] == 'OUT'
                        end
                    end
                end
            end

            xml.direction rule[:direction].downcase
            xml.packetType 'any'
        end
    end
    builder.to_xml
end