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 23, 2024
1 parent a05e9db commit f8c3e63
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies = [
"python-magic>=0.4.27",
"pytz",
"pyyaml>=5.4.1",
"rapyuta-io>=2.0.0",
"rapyuta-io>=2.1.0",
"requests>=2.20.0",
"semver>=3.0.0",
"setuptools",
Expand Down
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" : {{ device_name | get_intf_ip(interface='intf-name') }}
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,
}
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f8c3e63

Please sign in to comment.