blob: 3941643939de109e02e845b7c933a9ba1fd11f4a [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
Scott Baker22796cc2017-02-23 16:53:34 -080016from xosapi.orm import ORMWrapper, register_convenience_wrapper
17
Scott Baker22796cc2017-02-23 16:53:34 -080018
Zack Williams045b63d2019-01-22 16:30:57 -070019class ORMWrapperInstance(ORMWrapper):
Scott Baker22796cc2017-02-23 16:53:34 -080020 def all_ips(self):
Zack Williams045b63d2019-01-22 16:30:57 -070021 ips = {}
Scott Baker22796cc2017-02-23 16:53:34 -080022 for ns in self.ports.all():
Zack Williams045b63d2019-01-22 16:30:57 -070023 if ns.ip:
24 ips[ns.network.name] = ns.ip
Scott Baker22796cc2017-02-23 16:53:34 -080025 return ips
26
27 def all_ips_string(self):
28 result = []
29 ips = self.all_ips()
30 for key in sorted(ips.keys()):
Zack Williams045b63d2019-01-22 16:30:57 -070031 # result.append("%s = %s" % (key, ips[key]))
Scott Baker22796cc2017-02-23 16:53:34 -080032 result.append(ips[key])
33 return ", ".join(result)
34
35 def get_public_ip(self):
36 for ns in self.ports.all():
Zack Williams045b63d2019-01-22 16:30:57 -070037 if (
38 (ns.ip)
39 and (ns.network.template.visibility == "public")
40 and (ns.network.template.translation == "none")
41 ):
Scott Baker22796cc2017-02-23 16:53:34 -080042 return ns.ip
43 return None
44
45 # return an address on nat-net
46 def get_network_ip(self, pattern):
47 for ns in self.ports.all():
48 if pattern in ns.network.name.lower():
49 return ns.ip
50 return None
51
52 # return an address that the synchronizer can use to SSH to the instance
53 def get_ssh_ip(self):
54 # first look specifically for a management_local network
55 for ns in self.ports.all():
Zack Williams045b63d2019-01-22 16:30:57 -070056 if (
57 ns.network.template
58 and ns.network.template.vtn_kind == "MANAGEMENT_LOCAL"
59 ):
Scott Baker22796cc2017-02-23 16:53:34 -080060 return ns.ip
61
62 # for compatibility, now look for any management network
Zack Williams045b63d2019-01-22 16:30:57 -070063 management = self.get_network_ip("management")
Scott Baker22796cc2017-02-23 16:53:34 -080064 if management:
65 return management
66
67 # if all else fails, look for nat-net (for OpenCloud?)
68 return self.get_network_ip("nat")
69
70 @property
71 def controller(self):
72 if self.node and self.node.site_deployment:
73 return self.node.site_deployment.controller
74 else:
75 return None
76
Zack Williams045b63d2019-01-22 16:30:57 -070077
Scott Baker22796cc2017-02-23 16:53:34 -080078register_convenience_wrapper("Instance", ORMWrapperInstance)