nutanix / calm-dsl

Keep Calm and DSL On!
https://nutanix.github.io/calm-dsl/
Apache License 2.0
33 stars 51 forks source link

[Bug] users/list api call returns 500 entries and DSL does not loop through the next offset of 500 if more than 500 users present in command calm update cache #230

Closed glover-chris closed 1 year ago

glover-chris commented 2 years ago

Describe the bug The users/list api call returns 500 and DSL does not loop through the next offset of 500 if more than 500 users present.

Branch/Container master

To Reproduce Steps to reproduce the behavior:

  1. run calm update cache against PC where, say 1000, users exist.
  2. Users above the 500 count will not be updated in DSL cache.

Expected behavior All users should be included in the update cache command.

Findings/Troubleshooting Done Issue may be in the code here where the user api is called but there is no logic for more than 500 users.

https://github.com/nutanix/calm-dsl/blob/884be6b2cf4298f97dc8b064efbd6c02d8018086/calm/dsl/db/table_config.py#L1197

Line 1200 client = get_api_client() Obj = get_resource_api("users", client.connection) res, err = Obj.list({"length": 1000})

Screenshots If applicable, add screenshots to help explain your problem.

System Configuration:

Additional context Temporary fix in code at line 1195 of calm/dsl/db/table_config.py is working.

        client = get_api_client()
        Obj = get_resource_api("users", client.connection)
        offset = 0
        length = 500
        while True:
            res, err = Obj.list({"length": length, "offset": offset})
            if err:
                raise Exception("[{}] - {}".format(err["code"], err["error"]))
            res = res.json()
            for entity in res["entities"]:
                name = entity["status"]["name"]
                uuid = entity["metadata"]["uuid"]
                display_name = entity["status"]["resources"].get("display_name") or ""
                directory_service_user = (
                    entity["status"]["resources"].get("directory_service_user") or dict()
                )
                directory_service_ref = (
                    directory_service_user.get("directory_service_reference") or dict()
                )
                directory_service_name = directory_service_ref.get("name", "LOCAL")
                if directory_service_name:
                    cls.create_entry(
                        name=name,
                        uuid=uuid,
                        display_name=display_name,
                        directory=directory_service_name,
                    )
            if offset + length >= res['metadata']['total_matches']:
                break
            else:
                offset += length