Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(cv_device_v3): Dotted hostname support #680

Closed
wants to merge 2 commits into from

Conversation

sugetha24
Copy link
Contributor

@sugetha24 sugetha24 commented Oct 24, 2023

Change Summary

Adding support for hostname with dots(s1-leaf.1.aristanetworks.com) to cv_device_v3 module.

Related Issue(s)

Fixes #654

Component(s) name

arista.cvp.cv_device_v3

Proposed changes

Previously, get_device_by_name() cvprac function was used to get info on a device.
This was called for each device. Now, get_inventory() is called once to get the entire CVP inventory and cache it.
get_inventory() reports all information, especially fqdn and hostname as opposed to get_device_by_name() only returning fqdn.
The module no longer derivese hostname from fqdn field. If the module is not provided hostname as input, then we get it from the inventory.
The schema now supports hostname knob.

How to test

NOTE: This PR is dependent on some changes to get_inventory() function on cvprac. While we wait for that change to merge into cvprac, please make sure to comment line #600-626 in the latest cvprac: https://github.com/aristanetworks/cvprac/blob/b5306b22770848a32d5927620e804bd1482932c1/cvprac/cvp_api.py#L600
and run the following playbook

- name: Playbook to demonstrate cvp modules.
  hosts: CVP
  connection: local
  gather_facts: no
  vars:
    # Container definition
    cvp_devices:
      - hostname: s1-leaf.1
      # - fqdn: s1-leaf.1.aristanetworks.com
        parentContainerName: SITE1_LEAFS
        configlets:
          - Add-VLAN-50
  tasks:
    - name: "Configure devices on {{inventory_hostname}}"
      arista.cvp.cv_device_v3:
        devices: '{{cvp_devices}}'
        state: present
        # search_key: fqdn
        search_key: hostname
      register: CVP_DEVICES_RESULTS
    - name: debug
      debug:
        msg: "{{CVP_DEVICES_RESULTS}}"

Use search_key knob to specify if you want to serach the inventory by hostname or fqdn.
Note: if search_key: hostname, then hostname should be specified in cvp_device variable.
Similarly, if search_key: fqdn, then fqdn should be specified in cvp_device variable

Checklist

User Checklist

  • N/A

Repository Checklist

  • My code has been rebased from devel before I start
  • I have read the CONTRIBUTING document.
  • My change requires a change to the documentation and documentation have been updated accordingly. (check the box if not applicable)
  • I have updated molecule CI testing accordingly. (check the box if not applicable)

@github-actions github-actions bot added module: cv_device_v3 Issue related to cv_device module in v3 module_utils: cv_client cv_client implementation issue labels Oct 24, 2023
Copy link
Contributor

@ClausHolbechArista ClausHolbechArista left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried about performance on a scaled setup. If you have many devices (hundreds or more), but only run an operation for a single device, you end up parsing the full inventory many times across the code.

if inventory_mode == ModuleOptionValues.INVENTORY_MODE_LOOSE:
# Remove missing devices on CV from inventory (ignore missing)
user_inventory = self.__remove_missing_devices(
user_inventory=user_inventory
)
else:
# Check if all devices are present on CV (fail on missing)
self.__check_devices_exist(user_inventory=user_inventory)
self.__check_devices_exist(cvp_inventory, user_inventory=user_inventory)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of getting cvp_inventory in every function, just to pass it along to another method of self, you could just let those functions look up the inventory directly in self.cvp_inventory().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I will make that change.

MODULE_LOGGER.warning("Update list is: %s", str(user_result))
return DeviceInventory(data=user_result)

def refresh_hostname(self, cvp_inventory: list, user_inventory: DeviceInventory):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you still need all these "refresh_xxx" when you have all the information already? It would be more efficient to have one function (__refresh_user_inventory) that updates all missing info on user_inventory from cvp_inventory instead of looping through the entire inventory several times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Will make that change.

Data from CloudVision
"""
return self.__cv_client.api.get_inventory(start=0, end=0)

# Updated as per issue #365 to set default search with hostname field
@lru_cache
def __get_device(self, search_value: str, search_by: str = Api.device.HOSTNAME):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It is being called by a few other functions.

@@ -128,6 +134,16 @@
"CV-ANSIBLE-EOS01"
]
},
"hostname": {
"$id": "#/items/anyOf/0/properties/fqdn",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"$id": "#/items/anyOf/0/properties/fqdn",
"$id": "#/items/anyOf/0/properties/hostname",

"$id": "#/items/anyOf/0/properties/fqdn",
"type": "string",
"title": "The hostname schema",
"description": "An explanation about the purpose of this instance.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have no good description, just remove the field.

@sugetha24 sugetha24 marked this pull request as draft November 1, 2023 18:33
Copy link

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 15 days

@github-actions github-actions bot added the state: stale Staled issues waiting for closure label Jan 31, 2024
@noredistribution noredistribution removed the state: stale Staled issues waiting for closure label Jan 31, 2024
Copy link

sonarcloud bot commented May 21, 2024

Quality Gate Passed Quality Gate passed

Issues
7 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@sugetha24
Copy link
Contributor Author

Not fixing!

@sugetha24 sugetha24 closed this Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: cv_device_v3 Issue related to cv_device module in v3 module_utils: cv_client cv_client implementation issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cv_device_v3: hostname with dots
3 participants