132 | def _do_post(post_url, api_name, request_body): |
133 | """Helper to do HTTP POST. |
134 | |
135 | Note: |
136 | 1) On success, Kubernetes returns a success code of 201(CREATED) not 200(OK) |
137 | 2) A response code of 509(CONFLICT) is interpreted as a success code (since |
138 | the error is most likely due to the resource already existing). This makes |
139 | _do_post() idempotent which is semantically desirable. |
140 | """ |
141 | is_success = True |
142 | try: |
143 | r = requests.post(post_url, |
144 | data=request_body, |
145 | timeout=_REQUEST_TIMEOUT_SECS) |
146 | if r.status_code == requests.codes.conflict: |
147 | print('WARN: Looks like the resource already exists. Api: %s, url: %s' % |
148 | (api_name, post_url)) |
149 | elif r.status_code != requests.codes.created: |
150 | print('ERROR: %s API returned error. HTTP response: (%d) %s' % |
151 | (api_name, r.status_code, r.text)) |
152 | is_success = False |
153 | except (requests.exceptions.Timeout, |
154 | requests.exceptions.ConnectionError) as e: |
155 | is_success = False |
156 | _print_connection_error(str(e)) |
157 | return is_success |