def delete_all_snatpools(self, folder='Common'): """ Delete SNAT pools """ folder = str(folder).replace('/', '') request_url = self.bigip.icr_url + '/ltm/snatpool/' request_url += '?$select=name,selfLink' request_filter = 'partition eq ' + folder request_url += '&$filter=' + request_filter response = self.bigip.icr_session.get(request_url, timeout=const.CONNECTION_TIMEOUT) if response.status_code < 400: response_obj = json.loads(response.text) if 'items' in response_obj: for item in response_obj['items']: if item['name'].startswith(self.OBJ_PREFIX): response = self.bigip.icr_session.delete( self.bigip.icr_link(item['selfLink']), timeout=const.CONNECTION_TIMEOUT) if response.status_code > 400 and \ response.status_code != 404: Log.error('snatpool', response.text) raise exceptions.SNATDeleteException(response.text) return True elif response.status_code != 404: Log.error('snatpool', response.text) raise exceptions.SNATQueryException(response.text) return False
def remove_from_pool(self, name=None, member_name=None, folder='Common'): """ Remove from SNAT pool """ folder = str(folder).replace('/', '') request_url = self.bigip.icr_url + '/ltm/snatpool' request_url += '/~' + folder + '~' + name request_url += '?$select=members' response = self.bigip.icr_session.get(request_url, timeout=const.CONNECTION_TIMEOUT) if response.status_code < 400: response_obj = json.loads(response.text) sa_to_remove = None for member in response_obj['members']: member_base_name = os.path.basename(member) if member_base_name == member_name: sa_to_remove = member if not sa_to_remove: return True else: members = response_obj['members'] if len(members) == 1: request_url = self.bigip.icr_url + '/ltm/snatpool' request_url += '/~' + folder + '~' + name response = self.bigip.icr_session.delete( request_url, timeout=const.CONNECTION_TIMEOUT) if response.status_code: return True else: Log.error('snatpool', response.text) raise exceptions.SNATDeleteException(response.text) else: members.remove(sa_to_remove) payload = dict() payload['members'] = members request_url = self.bigip.icr_url request_url += '/ltm/snatpool' request_url += '/~' + folder + '~' + name response = self.bigip.icr_session.put( request_url, data=json.dumps(payload), timeout=const.CONNECTION_TIMEOUT) if response.status_code < 400: return True else: Log.error('snatpool', response.text) raise exceptions.SNATUpdateException(response.text) elif response.status_code != 404: Log.error('snatpool', response.text) raise exceptions.SNATQueryException(response.text) return False