Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
GÉANT Service Orchestrator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GÉANT Orchestration and Automation Team
GAP
GÉANT Service Orchestrator
Commits
d0387488
Commit
d0387488
authored
1 year ago
by
JORGE SASIAIN
Browse files
Options
Downloads
Patches
Plain Diff
NAT-152: minor refactoring/rearrangement
parent
7df2fddc
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!9
Ipam service
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gso/services/_ipam.py
+62
-55
62 additions, 55 deletions
gso/services/_ipam.py
with
62 additions
and
55 deletions
gso/services/_ipam.py
+
62
−
55
View file @
d0387488
...
...
@@ -65,25 +65,6 @@ def _ip_network_version(network):
assert
ip_version
in
[
4
,
6
]
return
ip_version
def
_find_containers
(
network
=
None
,
ip_version
=
4
):
"""
If network is not None, find the container that contains specified network.
Otherwise find all containers.
"""
assert
ip_version
in
[
4
,
6
]
oss
=
settings
.
load_oss_params
()
assert
oss
.
IPAM
.
INFOBLOX
infoblox_params
=
oss
.
IPAM
.
INFOBLOX
endpoint
=
'
networkcontainer
'
if
ip_version
==
4
else
'
ipv6networkcontainer
'
r
=
requests
.
get
(
f
'
{
_wapi
(
infoblox_params
)
}
/
{
endpoint
}
'
,
params
=
{
'
network
'
:
network
}
if
network
else
None
,
auth
=
HTTPBasicAuth
(
infoblox_params
.
username
,
infoblox_params
.
password
),
verify
=
False
)
assert
r
.
status_code
>=
200
and
r
.
status_code
<
300
,
f
"
HTTP error
{
r
.
status_code
}
:
{
r
.
reason
}
\n\n
{
r
.
text
}
"
return
r
.
json
()
def
_find_networks
(
network_container
=
None
,
network
=
None
,
ip_version
=
4
):
"""
If network_container is not None, find all networks within the specified container.
...
...
@@ -209,37 +190,6 @@ def allocate_service_ipv6_network(service_type) -> V6ServiceNetwork:
assert
hasattr
(
ipam_params
,
service_type
)
and
service_type
!=
'
INFOBLOX
'
,
"
Invalid service type.
"
return
_allocate_network
(
ipam_params
.
INFOBLOX
,
getattr
(
ipam_params
,
service_type
).
V6
,
6
)
def
_delete_network
(
network
)
->
Union
[
V4ServiceNetwork
,
V6ServiceNetwork
]:
"""
Delete IPv4 or IPv6 network by CIDR.
"""
# TODO: should we check that there are no hosts in this network before deleting?
# Deleting a network deletes the hosts in it, but not the associated DNS records.
oss
=
settings
.
load_oss_params
()
assert
oss
.
IPAM
.
INFOBLOX
infoblox_params
=
oss
.
IPAM
.
INFOBLOX
ip_version
=
_ip_network_version
(
network
)
network_info
=
_find_networks
(
network
=
network
,
ip_version
=
ip_version
)
assert
len
(
network_info
)
==
1
,
"
Network does not exist.
"
assert
'
_ref
'
in
network_info
[
0
]
r
=
requests
.
delete
(
f
'
{
_wapi
(
infoblox_params
)
}
/
{
network_info
[
0
][
"
_ref
"
]
}
'
,
auth
=
HTTPBasicAuth
(
infoblox_params
.
username
,
infoblox_params
.
password
),
verify
=
False
)
assert
r
.
status_code
>=
200
and
r
.
status_code
<
300
,
f
"
HTTP error
{
r
.
status_code
}
:
{
r
.
reason
}
\n\n
{
r
.
text
}
"
# Extract ipv4/ipv6 address from the network reference obtained in the response
r_json
=
r
.
json
()
network_address
=
ipaddress
.
ip_network
(
r_json
.
rsplit
(
"
/
"
,
1
)[
0
].
split
(
"
:
"
)[
1
].
replace
(
"
%3A
"
,
"
:
"
))
if
ip_version
==
4
:
return
V4ServiceNetwork
(
v4
=
network_address
)
else
:
return
V6ServiceNetwork
(
v6
=
network_address
)
def
_find_next_available_ip
(
infoblox_params
,
network_ref
):
r
=
requests
.
post
(
f
'
{
_wapi
(
infoblox_params
)
}
/
{
network_ref
}
?_function=next_available_ip&num=1
'
,
...
...
@@ -344,8 +294,9 @@ def allocate_service_host(hostname=None, service_type=None, service_networks: Se
"""
Allocate host with both IPv4 and IPv6 address (and respective DNS records).
The domain name is also taken from the service type and appended to specified hostname.
If service_networks is not provided, the first network with available space for this service type is used.
If service_networks is provided, that one is used.
If service_networks is not provided, the first network with available space for this service type is used.
Note that if WFO will always specify the network after creating it, this mode won
'
t be needed.
"""
oss
=
settings
.
load_oss_params
()
assert
oss
.
IPAM
...
...
@@ -373,8 +324,9 @@ def allocate_service_host(hostname=None, service_type=None, service_networks: Se
assert
first_nonfull_ipv4_network
,
"
No available IPv4 addresses for this service type.
"
v4_host
=
_allocate_host
(
hostname
=
hostname
+
domain_name
,
network
=
first_nonfull_ipv4_network
)
else
:
network
=
str
(
service_networks
.
v4
)
v4_host
=
_allocate_host
(
hostname
=
hostname
+
domain_name
,
network
=
network
)
network
=
service_networks
.
v4
assert
network
.
subnet_of
(
ipv4_container
)
v4_host
=
_allocate_host
(
hostname
=
hostname
+
domain_name
,
network
=
str
(
network
))
# IPv6
if
not
service_networks
:
...
...
@@ -386,11 +338,66 @@ def allocate_service_host(hostname=None, service_type=None, service_networks: Se
# TODO: if "no available IP" error, create a new network?
v6_host
=
_allocate_host
(
hostname
=
hostname
+
domain_name
,
network
=
ipv6_networks_info
[
0
][
'
network
'
])
else
:
network
=
str
(
service_networks
.
v6
)
v6_host
=
_allocate_host
(
hostname
=
hostname
+
domain_name
,
network
=
network
)
network
=
service_networks
.
v6
assert
network
.
subnet_of
(
ipv6_container
)
v6_host
=
_allocate_host
(
hostname
=
hostname
+
domain_name
,
network
=
str
(
network
))
return
HostAddresses
(
v4
=
v4_host
.
v4
,
v6
=
v6_host
.
v6
)
"""
Below methods are not used for supported outside calls
"""
def
_find_containers
(
network
=
None
,
ip_version
=
4
):
"""
If network is not None, find that container.
Otherwise find all containers.
"""
assert
ip_version
in
[
4
,
6
]
oss
=
settings
.
load_oss_params
()
assert
oss
.
IPAM
.
INFOBLOX
infoblox_params
=
oss
.
IPAM
.
INFOBLOX
endpoint
=
'
networkcontainer
'
if
ip_version
==
4
else
'
ipv6networkcontainer
'
r
=
requests
.
get
(
f
'
{
_wapi
(
infoblox_params
)
}
/
{
endpoint
}
'
,
params
=
{
'
network
'
:
network
}
if
network
else
None
,
auth
=
HTTPBasicAuth
(
infoblox_params
.
username
,
infoblox_params
.
password
),
verify
=
False
)
assert
r
.
status_code
>=
200
and
r
.
status_code
<
300
,
f
"
HTTP error
{
r
.
status_code
}
:
{
r
.
reason
}
\n\n
{
r
.
text
}
"
return
r
.
json
()
def
_delete_network
(
network
)
->
Union
[
V4ServiceNetwork
,
V6ServiceNetwork
]:
"""
Delete IPv4 or IPv6 network by CIDR.
"""
# TODO: should we check that there are no hosts in this network before deleting?
# Deleting a network deletes the hosts in it, but not the associated DNS records.
oss
=
settings
.
load_oss_params
()
assert
oss
.
IPAM
.
INFOBLOX
infoblox_params
=
oss
.
IPAM
.
INFOBLOX
ip_version
=
_ip_network_version
(
network
)
network_info
=
_find_networks
(
network
=
network
,
ip_version
=
ip_version
)
assert
len
(
network_info
)
==
1
,
"
Network does not exist.
"
assert
'
_ref
'
in
network_info
[
0
]
r
=
requests
.
delete
(
f
'
{
_wapi
(
infoblox_params
)
}
/
{
network_info
[
0
][
"
_ref
"
]
}
'
,
auth
=
HTTPBasicAuth
(
infoblox_params
.
username
,
infoblox_params
.
password
),
verify
=
False
)
assert
r
.
status_code
>=
200
and
r
.
status_code
<
300
,
f
"
HTTP error
{
r
.
status_code
}
:
{
r
.
reason
}
\n\n
{
r
.
text
}
"
# Extract ipv4/ipv6 address from the network reference obtained in the response
r_json
=
r
.
json
()
network_address
=
ipaddress
.
ip_network
(
r_json
.
rsplit
(
"
/
"
,
1
)[
0
].
split
(
"
:
"
)[
1
].
replace
(
"
%3A
"
,
"
:
"
))
if
ip_version
==
4
:
return
V4ServiceNetwork
(
v4
=
network_address
)
else
:
return
V6ServiceNetwork
(
v6
=
network_address
)
def
_delete_host_by_ip
(
addr
)
->
Union
[
V4HostAddress
,
V6HostAddress
]:
"""
Delete IPv4 or IPv6 host by its address.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment