Revision 347968ad src/vmm_mad/remotes/lib/vcenter_driver/network.rb

View differences:

src/vmm_mad/remotes/lib/vcenter_driver/network.rb
16 16
    def fetch!
17 17
        VIClient.get_entities(@item, "Network").each do |item|
18 18
            item_name = item._ref
19
            @items[item_name.to_sym] = Network.new(item)
19
            @items[item_name.to_sym] = PortGroup.new(item)
20
        end
21

  
22
        VIClient.get_entities(@item, "DistributedVirtualPortgroup").each do |item|
23
            item_name = item._ref
24
            @items[item_name.to_sym] = DistributedPortGroup.new(item)
20 25
        end
21 26
    end
22 27

  
......
38 43
class Network
39 44
    attr_accessor :item
40 45

  
46
    include Memoize
47

  
41 48
    def initialize(item, vi_client=nil)
42
        if !item.instance_of? RbVmomi::VIM::Network
49
        if !item.instance_of?(RbVmomi::VIM::Network)  &&
50
           !item.instance_of?(RbVmomi::VIM::DistributedVirtualPortgroup )
43 51
            raise "Expecting type 'RbVmomi::VIM::Network'. " <<
44 52
                  "Got '#{item.class} instead."
45 53
        end
......
48 56
        @item = item
49 57
    end
50 58

  
59

  
60
    def self.to_one_template(network_name, network_ref, network_type, vlan_id,
61
                             ccr_ref, ccr_name, vcenter_uuid)
62
        one_tmp = {}
63
        one_tmp[:name] = "#{network_name} - #{ccr_name}"
64
        one_tmp[:bridge] = network_name
65
        one_tmp[:type] = network_type
66
        one_tmp[:cluster] = ccr_name
67
        one_tmp[:vlan_id] = vlan_id
68
        one_tmp[:vcenter_ccr_ref] = ccr_ref
69
        one_tmp[:one] = to_one(network_name, network_ref, network_type, vlan_id,
70
                             ccr_ref, ccr_name, vcenter_uuid)
71
        return one_tmp
72
    end
73

  
74
    def self.to_one(network_name, network_ref, network_type, vlan_id,
75
                    ccr_ref, ccr_name, vcenter_uuid)
76
        template = "NAME=\"#{network_name} - #{ccr_name}\"\n"\
77
                   "BRIDGE=\"#{network_name}\"\n"\
78
                   "VN_MAD=\"dummy\"\n"\
79
                   "VCENTER_TYPE=\"#{network_type}\"\n"\
80
                   "VCENTER_NET_REF=\"#{network_ref}\"\n"\
81
                   "VCENTER_CCR_REF=\"#{ccr_ref}\"\n"\
82
                   "VCENTER_INSTANCE_ID=\"#{vcenter_uuid}\"\n"
83

  
84
        template << "VLAN_TAGGED_ID=#{vlan_id}\n" if !vlan_id.empty?
85

  
86
        return template
87
    end
88

  
51 89
    # This is never cached
52 90
    def self.new_from_ref(ref, vi_client)
53 91
        self.new(RbVmomi::VIM::Network.new(vi_client.vim, ref), vi_client)
54 92
    end
93

  
55 94
end # class Network
56 95

  
96
class PortGroup < Network
97

  
98
    def initialize(item, vi_client=nil)
99
        if !item.instance_of?(RbVmomi::VIM::Network)
100
            raise "Expecting type 'RbVmomi::VIM::Network'. " <<
101
                  "Got '#{item.class} instead."
102
        end
103

  
104
        @vi_client = vi_client
105
        @item = item
106
    end
107

  
108
    def clusters
109
        net_clusters = {}
110
        host_members =@item['host']
111
        host_members.each do |h|
112
            if !net_clusters.key?(h.parent._ref.to_s)
113
               net_clusters[h.parent._ref.to_s] = h.parent.name.to_s
114
            end
115
        end
116
        net_clusters
117
    end
118

  
119
    def vlan_id
120
        id = ""
121
        host_members = self['host']
122
        host = host_members.first
123
        # This is pretty slow as the host id subsystem has to be queried
124
        cm = host.configManager
125
        nws = cm.networkSystem
126
        nc = nws.networkConfig
127
        pgs = nc.portgroup
128
        pgs.each do |pg|
129
            if pg.spec.name == self["name"]
130
                id << pg.spec.vlanId.to_s  if pg.spec.vlanId != 0
131
                break
132
            end
133
        end
134
        id
135
    end
136

  
137
    def network_type
138
        "Port Group"
139
    end
140
end # class PortGroup
141

  
142
class DistributedPortGroup < Network
143

  
144
    def initialize(item, vi_client=nil)
145
        if !item.instance_of?(RbVmomi::VIM::DistributedVirtualPortgroup )
146
           raise "Expecting type 'RbVmomi::VIM::DistributedVirtualPortgroup'. " <<
147
                  "Got '#{item.class} instead."
148
        end
149

  
150
        @vi_client = vi_client
151
        @item = item
152
    end
153

  
154
    def clusters
155
        net_clusters = {}
156
        host_members = self['config.distributedVirtualSwitch.summary.hostMember']
157
        host_members.each do |h|
158
            if !net_clusters.key?(h.parent._ref.to_s)
159
               net_clusters[h.parent._ref.to_s] = h.parent.name.to_s
160
            end
161
        end
162
        net_clusters
163
    end
164

  
165
    def vlan_id
166
        id = ""
167
        pc = self['config.defaultPortConfig']
168
        if pc.respond_to?(:vlan) && pc.vlan.respond_to?(:vlanId)
169

  
170
            vlan = pc.vlan.vlanId
171

  
172
            if vlan.is_a? Array
173
                vlan.each do |v|
174
                    id << v.start.to_s
175
                    id << ".."
176
                    id << v.end.to_s
177
                    id << ","
178
                end
179
                id.chop!
180
            else
181
                id = vlan.to_s if vlan != 0
182
            end
183
        end
184
        return id
185
    end
186

  
187
    def network_type
188
        "Distributed Port Group"
189
    end
190
end # class DistributedPortGroup
191

  
57 192
end # module VCenterDriver
58 193

  

Also available in: Unified diff