|
4 | 4 | import logging
|
5 | 5 | import re
|
6 | 6 | import time
|
| 7 | +from requests.exceptions import HTTPError, JSONDecodeError |
7 | 8 |
|
8 | 9 | from netfoundry.exceptions import UnknownResourceType, NetworkBoundaryViolation
|
9 | 10 |
|
@@ -597,17 +598,12 @@ def create_resource(self, type: str, post: dict, wait: int = 30, sleep: int = 2,
|
597 | 598 | headers=headers,
|
598 | 599 | json=post
|
599 | 600 | )
|
600 |
| - response_code = response.status_code |
601 |
| - except Exception as e: |
602 |
| - raise RuntimeError(f"error POST to {self.audience}core/v2/{type}, caught {e}") |
603 |
| - |
604 |
| - if response_code in [STATUS_CODES.codes.OK, STATUS_CODES.codes.ACCEPTED]: |
605 |
| - try: |
606 |
| - resource = response.json() |
607 |
| - except ValueError as e: |
608 |
| - raise RuntimeError(f"failed to load JSON from POST response, caught {e}") |
| 601 | + response.raise_for_status() |
| 602 | + except HTTPError as e: |
| 603 | + logging.error(f"failed POST to create resource, caught {e.response.text}") |
| 604 | + exit(1) |
609 | 605 | else:
|
610 |
| - raise RuntimeError(f"got unexpected HTTP code {STATUS_CODES._codes[response_code][0].upper()} ({response_code}) for post: {json.dumps(post, indent=2)}") |
| 606 | + resource = response.json() |
611 | 607 |
|
612 | 608 | if resource.get('_links') and resource['_links'].get('process-executions'):
|
613 | 609 | _links = resource['_links'].get('process-executions')
|
@@ -1955,25 +1951,29 @@ def delete_resource(self, type: str, id: str = None, wait: int = 0, sleep: int =
|
1955 | 1951 | if response_code not in expected_responses:
|
1956 | 1952 | raise RuntimeError(f"got unexpected HTTP code {STATUS_CODES._codes[response_code][0].upper()} ({response_code}) and response {response.text}")
|
1957 | 1953 | else:
|
1958 |
| - resource = response.json() |
1959 |
| - |
1960 |
| - if resource.get('_links') and resource['_links'].get('process-executions'): |
1961 |
| - _links = resource['_links'].get('process-executions') |
1962 |
| - if isinstance(_links, list): |
1963 |
| - process_id = _links[0]['href'].split('/')[6] |
1964 |
| - else: |
1965 |
| - process_id = _links['href'].split('/')[6] |
1966 |
| - if wait: |
1967 |
| - self.wait_for_statuses(expected_statuses=RESOURCES["process-executions"].status_symbols['complete'], type="process-executions", id=process_id, wait=wait, sleep=sleep) |
1968 |
| - return(True) |
1969 |
| - else: # only wait for the process to start, not finish, or timeout |
1970 |
| - self.wait_for_statuses(expected_statuses=RESOURCES['process-executions'].status_symbols['progress'] + RESOURCES['process-executions'].status_symbols['complete'], type="process-executions", id=process_id, wait=9, sleep=2) |
| 1954 | + try: |
| 1955 | + resource = response.json() |
| 1956 | + except JSONDecodeError as e: |
| 1957 | + logging.debug("ignoring {e}") |
1971 | 1958 | return(True)
|
1972 |
| - elif wait: |
1973 |
| - logging.warning("unable to wait for async complete because response did not provide a process execution id") |
1974 |
| - return(False) |
1975 |
| - else: |
1976 |
| - return(True) |
| 1959 | + else: |
| 1960 | + if resource.get('_links') and resource['_links'].get('process-executions'): |
| 1961 | + _links = resource['_links'].get('process-executions') |
| 1962 | + if isinstance(_links, list): |
| 1963 | + process_id = _links[0]['href'].split('/')[6] |
| 1964 | + else: |
| 1965 | + process_id = _links['href'].split('/')[6] |
| 1966 | + if wait: |
| 1967 | + self.wait_for_statuses(expected_statuses=RESOURCES["process-executions"].status_symbols['complete'], type="process-executions", id=process_id, wait=wait, sleep=sleep) |
| 1968 | + return(True) |
| 1969 | + else: # only wait for the process to start, not finish, or timeout |
| 1970 | + self.wait_for_statuses(expected_statuses=RESOURCES['process-executions'].status_symbols['progress'] + RESOURCES['process-executions'].status_symbols['complete'], type="process-executions", id=process_id, wait=9, sleep=2) |
| 1971 | + return(True) |
| 1972 | + elif wait: |
| 1973 | + logging.warning("unable to wait for async complete because response did not provide a process execution id") |
| 1974 | + return(False) |
| 1975 | + else: |
| 1976 | + return(True) |
1977 | 1977 |
|
1978 | 1978 |
|
1979 | 1979 | class Networks:
|
|
0 commit comments