blob: f373f12610e46187883aa777430fa1ed7e048f94 [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
Zack Williams5c2ea232019-01-30 15:23:01 -070016from __future__ import absolute_import
17
Scott Baker22796cc2017-02-23 16:53:34 -080018from xosapi.orm import ORMWrapper, register_convenience_wrapper
19
Scott Baker22796cc2017-02-23 16:53:34 -080020
Zack Williams045b63d2019-01-22 16:30:57 -070021class ORMWrapperInstance(ORMWrapper):
Scott Baker22796cc2017-02-23 16:53:34 -080022 def all_ips(self):
Zack Williams045b63d2019-01-22 16:30:57 -070023 ips = {}
Scott Baker22796cc2017-02-23 16:53:34 -080024 for ns in self.ports.all():
Zack Williams045b63d2019-01-22 16:30:57 -070025 if ns.ip:
26 ips[ns.network.name] = ns.ip
Scott Baker22796cc2017-02-23 16:53:34 -080027 return ips
28
29 def all_ips_string(self):
30 result = []
31 ips = self.all_ips()
32 for key in sorted(ips.keys()):
Zack Williams045b63d2019-01-22 16:30:57 -070033 # result.append("%s = %s" % (key, ips[key]))
Scott Baker22796cc2017-02-23 16:53:34 -080034 result.append(ips[key])
35 return ", ".join(result)
36
37 def get_public_ip(self):
38 for ns in self.ports.all():
Zack Williams045b63d2019-01-22 16:30:57 -070039 if (
40 (ns.ip)
41 and (ns.network.template.visibility == "public")
42 and (ns.network.template.translation == "none")
43 ):
Scott Baker22796cc2017-02-23 16:53:34 -080044 return ns.ip
45 return None
46
47 # return an address on nat-net
48 def get_network_ip(self, pattern):
49 for ns in self.ports.all():
50 if pattern in ns.network.name.lower():
51 return ns.ip
52 return None
53
54 # return an address that the synchronizer can use to SSH to the instance
55 def get_ssh_ip(self):
56 # first look specifically for a management_local network
57 for ns in self.ports.all():
Zack Williams045b63d2019-01-22 16:30:57 -070058 if (
59 ns.network.template
60 and ns.network.template.vtn_kind == "MANAGEMENT_LOCAL"
61 ):
Scott Baker22796cc2017-02-23 16:53:34 -080062 return ns.ip
63
64 # for compatibility, now look for any management network
Zack Williams045b63d2019-01-22 16:30:57 -070065 management = self.get_network_ip("management")
Scott Baker22796cc2017-02-23 16:53:34 -080066 if management:
67 return management
68
69 # if all else fails, look for nat-net (for OpenCloud?)
70 return self.get_network_ip("nat")
71
72 @property
73 def controller(self):
74 if self.node and self.node.site_deployment:
75 return self.node.site_deployment.controller
76 else:
77 return None
78
Zack Williams045b63d2019-01-22 16:30:57 -070079
Scott Baker22796cc2017-02-23 16:53:34 -080080register_convenience_wrapper("Instance", ORMWrapperInstance)