Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # 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 Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 16 | from __future__ import absolute_import |
| 17 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 18 | from xosapi.orm import ORMWrapper, register_convenience_wrapper |
| 19 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 20 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 21 | class ORMWrapperInstance(ORMWrapper): |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 22 | def all_ips(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 23 | ips = {} |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 24 | for ns in self.ports.all(): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 25 | if ns.ip: |
| 26 | ips[ns.network.name] = ns.ip |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 27 | return ips |
| 28 | |
| 29 | def all_ips_string(self): |
| 30 | result = [] |
| 31 | ips = self.all_ips() |
| 32 | for key in sorted(ips.keys()): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 33 | # result.append("%s = %s" % (key, ips[key])) |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 34 | result.append(ips[key]) |
| 35 | return ", ".join(result) |
| 36 | |
| 37 | def get_public_ip(self): |
| 38 | for ns in self.ports.all(): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 39 | if ( |
| 40 | (ns.ip) |
| 41 | and (ns.network.template.visibility == "public") |
| 42 | and (ns.network.template.translation == "none") |
| 43 | ): |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 44 | 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 58 | if ( |
| 59 | ns.network.template |
| 60 | and ns.network.template.vtn_kind == "MANAGEMENT_LOCAL" |
| 61 | ): |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 62 | return ns.ip |
| 63 | |
| 64 | # for compatibility, now look for any management network |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 65 | management = self.get_network_ip("management") |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 66 | 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 79 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 80 | register_convenience_wrapper("Instance", ORMWrapperInstance) |