def test_exhaust_pages_bad_json_response(mocker: MockerFixture) -> None: _establish_mocks(mocker) for test in [{}, {"not data": {}}]: responses.add(responses.GET, f"{_test_url}?page[size]=100&page[number]=1", match_querystring=True, json=test, status=200) assert exhaust_pages(_test_url, json_mapper=_simple_mapper) == []
def test_exhaust_pages_single_page(mocker: MockerFixture) -> None: _establish_mocks(mocker) json = {"data": [{"objectname": "test1"}, {"objectname": "test2"}]} responses.add(responses.GET, f"{_test_url}?page[size]=100&page[number]=1", match_querystring=True, json=json, status=200) assert exhaust_pages(_test_url, json_mapper=_simple_mapper) == [["test1", "test2"]]
def test_exhaust_pages_error_http_response(mocker: MockerFixture) -> None: _establish_mocks(mocker) status_code = 500 json = { "errors": [{ "detail": "Something bad happened.", "status": status_code, "title": "Oops" }] } responses.add(responses.GET, f"{_test_url}?page[size]=100&page[number]=1", match_querystring=True, json=json, status=status_code) assert exhaust_pages(_test_url, json_mapper=_simple_mapper) == []
def fetch_all( terraform_domain: str, organization: str, *, workspace_names: Optional[List[str]] = None, blacklist: bool = False, no_tls: bool = False, token: Optional[str] = None, write_error_messages: bool = False ) -> List[Workspace]: """ Fetch all workspaces (or a subset if desired) from a particular Terraform organization. :param terraform_domain: The domain corresponding to the targeted Terraform installation (either Terraform Cloud or Enterprise). :param organization: The organization for which to fetch workspace data. :param workspace_names: The name(s) of workspace(s) for which data should be fetched. If not specified, all workspace data will be fetched. :param blacklist: Whether to use the specified workspaces as a blacklist-style filter. :param no_tls: Whether to use SSL/TLS encryption when communicating with the Terraform API. :param token: A token suitable for authenticating against the Terraform API. If not specified, a token will be searched for in the documented locations. :param write_error_messages: Whether to write error messages to STDERR. :return: The workspace objects corresponding to the given criteria. """ lower_workspaces = [] if workspace_names is None else [ workspace.lower() for workspace in workspace_names ] def is_returnable(workspace: Workspace) -> bool: for name in lower_workspaces: if fnmatch(workspace.name.lower(), name): return not blacklist return blacklist base_url = f"{get_protocol(no_tls)}://{terraform_domain}/api/v2" return [ workspace for workspace in itertools.chain.from_iterable( pagination.exhaust_pages( f"{base_url}/organizations/{organization}/workspaces", json_mapper=_map_workspaces, token=token, write_error_messages=write_error_messages ) ) if workspace_names is None or is_returnable(workspace) ]
def test_exhaust_pages_multiple_pages(mocker: MockerFixture) -> None: _establish_mocks(mocker) json_page1 = { "data": [{ "objectname": "test1" }, { "objectname": "test2" }], "meta": { "pagination": { "next-page": 2 } } } json_page2 = { "data": [{ "objectname": "test3" }, { "objectname": "test4" }], "meta": { "pagination": { "next-page": None } } } responses.add(responses.GET, f"{_test_url}?page[size]=100&page[number]=1", match_querystring=True, json=json_page1, status=200) responses.add(responses.GET, f"{_test_url}?page[size]=100&page[number]=2", match_querystring=True, json=json_page2, status=200) assert exhaust_pages(_test_url, json_mapper=_simple_mapper) == [["test1", "test2"], ["test3", "test4"]]