5 lock
= threading
.Lock()
10 def __init__(self
, name
, ip_address
=None, mac_address
=None, floating_ip
=None):
13 self
.id = str(uuid
.uuid4())
14 self
.template_name
= name
16 ip_address is structured like 10.0.0.1/24
18 self
.ip_address
= ip_address
19 self
.mac_address
= mac_address
20 self
.floating_ip
= floating_ip
23 def set_name(self
, name
):
27 :param name: New port name.
33 # Delete old interface name
36 if intf_names
[self
.intf_name
][0] == self
.id and intf_names
[self
.intf_name
][1] is False:
37 del intf_names
[self
.intf_name
]
41 # Create new interface name
42 self
.create_intf_name()
44 def create_intf_name(self
):
46 Creates the interface name, while using the first 4 letters of the port name, the specification, if it is an
47 'in' / 'out' port or something else, and a counter value if the name is already used. The counter starts
48 for each name at 0 and can go up to 999. After creating the name each port will post its interface name
49 into the global dictionary and adding his full name. Thus each port can determine if his desired interface
50 name is already used and choose the next one.
52 split_name
= self
.name
.split(':')
53 if len(split_name
) >= 3:
54 if split_name
[2] == 'input' or split_name
[2] == 'in':
55 self
.intf_name
= split_name
[0][:4] + '-' + \
57 elif split_name
[2] == 'output' or split_name
[2] == 'out':
58 self
.intf_name
= split_name
[0][:4] + '-' + \
61 self
.intf_name
= split_name
[0][:4] + '-' + \
64 self
.intf_name
= self
.name
[:9]
70 intf_len
= len(self
.intf_name
)
71 self
.intf_name
= self
.intf_name
+ '-' + str(counter
)[:4]
72 while self
.intf_name
in intf_names
and counter
< 999 and not intf_names
[self
.intf_name
][0] == self
.id:
74 self
.intf_name
= self
.intf_name
[:intf_len
] + '-' + str(counter
)[:4]
77 logging
.ERROR("Port %s could not create unique interface name (%s)", self
.name
, self
.intf_name
)
82 if self
.intf_name
in intf_names
and intf_names
[self
.intf_name
][0] == self
.id:
85 intf_names
[self
.intf_name
] = [self
.id, updated
]
88 def get_short_id(self
):
90 Gets a shortened ID which only contains first 6 characters.
92 :return: The first 6 characters of the UUID.
95 return str(self
.id)[:6]
97 def create_port_dict(self
, compute
):
99 Creates the port description dictionary.
101 :param compute: Requires the compute resource to determine the used network.
102 :type compute: :class:`heat.compute`
103 :return: Returns the description dictionary.
107 port_dict
["admin_state_up"] = True # TODO is it always true?
108 port_dict
["device_id"] = "257614cc-e178-4c92-9c61-3b28d40eca44" # TODO find real values
109 port_dict
["device_owner"] = "" # TODO do we have such things?
110 net
= compute
.find_network_by_name_or_id(self
.net_name
)
111 port_dict
["fixed_ips"] = [
113 "ip_address": self
.ip_address
.rsplit('/', 1)[0] if self
.ip_address
is not None else "",
114 "subnet_id": net
.subnet_id
if net
is not None else ""
117 port_dict
["id"] = self
.id
118 port_dict
["mac_address"] = self
.mac_address
119 port_dict
["name"] = self
.name
120 port_dict
["network_id"] = net
.id if net
is not None else ""
121 port_dict
["status"] = "ACTIVE" # TODO do we support inactive port?
122 port_dict
["tenant_id"] = "abcdefghijklmnopqrstuvwxyz123456" # TODO find real tenant_id
125 def compare_attributes(self
, other
):
127 Does NOT compare ip_address because this function only exists to check if we can
128 update the IP address without any changes
130 :param other: The port to compare with
131 :type other: :class:`heat.resources.port`
132 :return: True if the attributes are the same, else False.
138 if self
.name
== other
.name
and self
.floating_ip
== other
.floating_ip
and \
139 self
.net_name
== other
.net_name
:
143 def __eq__(self
, other
):
147 if self
.name
== other
.name
and self
.ip_address
== other
.ip_address
and \
148 self
.mac_address
== other
.mac_address
and \
149 self
.floating_ip
== other
.floating_ip
and \
150 self
.net_name
== other
.net_name
:
155 return hash((self
.name
,
165 if self
.intf_name
in intf_names
and intf_names
[self
.intf_name
][0] == self
.id:
166 if intf_names
[self
.intf_name
][1] is False:
167 del intf_names
[self
.intf_name
]
169 intf_names
[self
.intf_name
][1] = False