mirror of
https://github.com/jbowdre/phpIPAM-for-vRA8.git
synced 2024-11-21 22:42:20 +00:00
work on ip allocation
This commit is contained in:
parent
f0b6ae317b
commit
2b9ab9a710
107 changed files with 60 additions and 18 deletions
|
@ -12,6 +12,7 @@ conditions of the subcomponent's license, as noted in the LICENSE file.
|
||||||
import requests
|
import requests
|
||||||
from vra_ipam_utils.ipam import IPAM
|
from vra_ipam_utils.ipam import IPAM
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Example payload
|
Example payload
|
||||||
|
@ -76,16 +77,37 @@ def handler(context, inputs):
|
||||||
|
|
||||||
return ipam.allocate_ip()
|
return ipam.allocate_ip()
|
||||||
|
|
||||||
def do_allocate_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!')
|
||||||
|
print('Auth success.')
|
||||||
|
token = {"token": req.json()['data']['token']}
|
||||||
|
return token
|
||||||
|
|
||||||
|
def do_allocate_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
|
||||||
|
}
|
||||||
|
|
||||||
allocation_result = []
|
allocation_result = []
|
||||||
try:
|
try:
|
||||||
resource = self.inputs["resourceInfo"]
|
resource = self.inputs["resourceInfo"]
|
||||||
for allocation in self.inputs["ipAllocations"]:
|
for allocation in self.inputs["ipAllocations"]:
|
||||||
allocation_result.append(allocate(resource, allocation, self.context, self.inputs["endpoint"]))
|
allocation_result.append(allocate(resource, allocation, self.context, self.inputs["endpoint"], bundle))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
try:
|
try:
|
||||||
rollback(allocation_result)
|
rollback(allocation_result)
|
||||||
|
@ -99,14 +121,14 @@ def do_allocate_ip(self, auth_credentials, cert):
|
||||||
"ipAllocations": allocation_result
|
"ipAllocations": allocation_result
|
||||||
}
|
}
|
||||||
|
|
||||||
def allocate(resource, allocation, context, endpoint):
|
def allocate(resource, allocation, context, endpoint, bundle):
|
||||||
|
|
||||||
last_error = None
|
last_error = None
|
||||||
for range_id in allocation["ipRangeIds"]:
|
for range_id in allocation["ipRangeIds"]:
|
||||||
|
|
||||||
logging.info(f"Allocating from range {range_id}")
|
logging.info(f"Allocating from range {range_id}")
|
||||||
try:
|
try:
|
||||||
return allocate_in_range(range_id, resource, allocation, context, endpoint)
|
return allocate_in_range(range_id, resource, allocation, context, endpoint, bundle)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
last_error = e
|
last_error = e
|
||||||
logging.error(f"Failed to allocate from range {range_id}: {str(e)}")
|
logging.error(f"Failed to allocate from range {range_id}: {str(e)}")
|
||||||
|
@ -115,11 +137,27 @@ def allocate(resource, allocation, context, endpoint):
|
||||||
raise last_error
|
raise last_error
|
||||||
|
|
||||||
|
|
||||||
def allocate_in_range(range_id, resource, allocation, context, endpoint):
|
def allocate_in_range(range_id, resource, allocation, context, endpoint, bundle):
|
||||||
|
if int(allocation['size']) ==1:
|
||||||
|
vmName = resource['name']
|
||||||
|
owner = resource['owner']
|
||||||
|
uri = bundle['uri']
|
||||||
|
token = bundle['token']
|
||||||
|
cert = bundle['cert']
|
||||||
|
payload = {
|
||||||
|
'hostname': vmName,
|
||||||
|
'description': f'Reserved by vRA for {owner} at {datetime.now()}'
|
||||||
|
}
|
||||||
|
allocate_uri = f'{uri}/addresses/first_free/{str(range_id)}/'
|
||||||
|
req = requests.post(allocate_uri, data=payload, token=token, verify=cert)
|
||||||
|
result = {
|
||||||
|
"ipAllocationId": allocation['id'],
|
||||||
|
"ipRangeId": range_id,
|
||||||
|
"ipVersion":
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Plug your implementation here to allocate an ip address
|
|
||||||
## ...
|
|
||||||
## Allocation successful
|
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
"ipAllocationId": allocation["id"],
|
"ipAllocationId": allocation["id"],
|
||||||
|
@ -131,6 +169,10 @@ def allocate_in_range(range_id, resource, allocation, context, endpoint):
|
||||||
result["properties"] = {"customPropertyKey1": "customPropertyValue1"}
|
result["properties"] = {"customPropertyKey1": "customPropertyValue1"}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
else:
|
||||||
|
# TODO: implement allocation of continuous block of IPs
|
||||||
|
pass
|
||||||
|
raise Exception("Not implemented")
|
||||||
|
|
||||||
## Rollback any previously allocated addresses in case this allocation request contains multiple ones and failed in the middle
|
## Rollback any previously allocated addresses in case this allocation request contains multiple ones and failed in the middle
|
||||||
def rollback(allocation_result):
|
def rollback(allocation_result):
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue