implement deallocate_ip

This commit is contained in:
John Bowdre 2021-01-15 15:32:56 -06:00
parent 62ec0b89ad
commit 8ecb24dfda

View file

@ -65,31 +65,52 @@ def handler(context, inputs):
return ipam.deallocate_ip() return ipam.deallocate_ip()
def do_deallocate_ip(self, auth_credentials, cert): def auth_session(uri, auth, cert):
# Your implemention goes here auth_uri = f'{uri}/user/'
req = requests.post(auth_uri, auth=auth, verify=cert)
if req.status_code != 200:
raise requests.exceptions.RequestException('Authentication Failure!')
token = {"token": req.json()['data']['token']}
return token
def do_deallocate_ip(self, auth_credentials, cert):
# Build variables
username = auth_credentials["privateKeyId"] username = auth_credentials["privateKeyId"]
password = auth_credentials["privateKey"] password = auth_credentials["privateKey"]
hostname = self.inputs["endpoint"]["endpointProperties"]["hostName"]
apiAppId = self.inputs["endpoint"]["endpointProperties"]["apiAppId"]
uri = f'https://{hostname}/api/{apiAppId}/'
auth = (username, password)
# Auth to API
token = auth_session(uri, auth, cert)
bundle = {
'uri': uri,
'token': token,
'cert': cert
}
deallocation_result = [] deallocation_result = []
for deallocation in self.inputs["ipDeallocations"]: for deallocation in self.inputs["ipDeallocations"]:
deallocation_result.append(deallocate(self.inputs["resourceInfo"], deallocation)) deallocation_result.append(deallocate(self.inputs["resourceInfo"], deallocation, bundle))
assert len(deallocation_result) > 0 assert len(deallocation_result) > 0
return { return {
"ipDeallocations": deallocation_result "ipDeallocations": deallocation_result
} }
def deallocate(resource, deallocation): def deallocate(resource, deallocation, bundle):
uri = bundle['uri']
token = bundle['token']
cert = bundle['cert']
ip_range_id = deallocation["ipRangeId"] ip_range_id = deallocation["ipRangeId"]
ip = deallocation["ipAddress"] ip = deallocation["ipAddress"]
resource_id = resource["id"] resource_id = resource["id"]
logging.info(f"Deallocating ip {ip} from range {ip_range_id}") logging.info(f"Deallocating ip {ip} from range {ip_range_id}")
## Plug your implementation here to deallocate an already allocated ip address deallocate_uri = f'{uri}/addresses/{ip}/{ip_range_id}/'
## ... requests.delete(deallocate_uri, headers=token, verify=cert)
## Deallocation successful
return { return {
"ipDeallocationId": deallocation["id"], "ipDeallocationId": deallocation["id"],
"message": "Success" "message": "Success"