Skip to content

Commit

Permalink
feat(apply): adds template func to get interface ip
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Oct 22, 2024
1 parent ddc0f1a commit 8339e9e
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions riocli/apply/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Filters to use in the manifests.
"""

import os

from riocli.config import new_client
from riocli.device.util import find_device_guid


def getenv(default: str, env_var: str) -> str:
"""
Get the value of an environment variable.
"""Get the value of an environment variable.
Usage:
"foo" : {{ "bar" | getenv('FOO') }}
Expand All @@ -36,6 +36,41 @@ def getenv(default: str, env_var: str) -> str:
return os.getenv(env_var, default)


def get_interface_ip(device_name: str, interface: str) -> str:
"""Get the IP address of an interface on a device.
Usage:
"ip" : {{ get_intf_ip('device_name', 'interface') }}
Args:
device_name: The name of the device.
interface: The name of the interface.
Returns:
The IP address of the interface
Raises:
Exception: If the interface is not available on the device.
"""
client = new_client(with_project=True)
device_id = find_device_guid(client, device_name)

device = client.get_device(device_id)
try:
device.poll_till_ready(retry_count=50, sleep_interval=10)
except Exception as e:
raise e

device.refresh()

for name in device.ip_interfaces:
if name == interface:
return device.ip_interfaces[name][0]

raise Exception(f'interface "{interface}" not found on device "{device_name}"')


FILTERS = {
"getenv": getenv,
"get_intf_ip": get_interface_ip,
}

0 comments on commit 8339e9e

Please sign in to comment.