|
| 1 | +# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from vmwarelib import inventory |
| 17 | +from vmwarelib.serialize import PhysicalNicJSONEncoder |
| 18 | +from vmwarelib.actions import BaseAction |
| 19 | +import json |
| 20 | + |
| 21 | + |
| 22 | +class GetHostNetworkHints(BaseAction): |
| 23 | + def run(self, host_ids, host_names, vsphere=None): |
| 24 | + """ |
| 25 | + Retrieve Network Hintsfor given Hosts (ESXi) |
| 26 | +
|
| 27 | + Args: |
| 28 | + - host_ids: Moid of Host to retrieve |
| 29 | + - host_names: Name of Host to retrieve |
| 30 | + - vsphere: Pre-configured vsphere connection details (config.yaml) |
| 31 | +
|
| 32 | +
|
| 33 | + Returns: |
| 34 | + - dict: Host network hints details. |
| 35 | + """ |
| 36 | + |
| 37 | + # TODO review using propertspec for retrieving all Hosts's at onces. |
| 38 | + results = {} |
| 39 | + if not host_ids and not host_names: |
| 40 | + raise ValueError("No IDs nor Names provided.") |
| 41 | + |
| 42 | + self.establish_connection(vsphere) |
| 43 | + |
| 44 | + if host_ids: |
| 45 | + for hid in host_ids: |
| 46 | + host = inventory.get_hostsystem(self.si_content, moid=hid) |
| 47 | + if host: |
| 48 | + if host.name not in results: |
| 49 | + network_hints = host.configManager.networkSystem.QueryNetworkHint() |
| 50 | + results[host.name] = json.loads(json.dumps(network_hints, |
| 51 | + cls=PhysicalNicJSONEncoder)) |
| 52 | + if host_names: |
| 53 | + for host in host_names: |
| 54 | + host = inventory.get_hostsystem(self.si_content, name=host) |
| 55 | + if host: |
| 56 | + if host.name not in results: |
| 57 | + network_hints = host.configManager.networkSystem.QueryNetworkHint() |
| 58 | + results[host.name] = json.loads(json.dumps(network_hints, |
| 59 | + cls=PhysicalNicJSONEncoder)) |
| 60 | + return results |
0 commit comments