From ea0a9a97671cb460fb52a252d057ebe9a14c7d9c Mon Sep 17 00:00:00 2001
From: Jorge Sasiain <jorge.sasiain@ehu.eus>
Date: Wed, 14 Jun 2023 11:57:08 +0000
Subject: [PATCH] IPAM: add README with usage

---
 gso/services/README.md | 109 +++++++++++++++++++++++++++++++++++++++++
 gso/services/ipam.py   |  60 -----------------------
 2 files changed, 109 insertions(+), 60 deletions(-)
 create mode 100644 gso/services/README.md

diff --git a/gso/services/README.md b/gso/services/README.md
new file mode 100644
index 00000000..d393e74e
--- /dev/null
+++ b/gso/services/README.md
@@ -0,0 +1,109 @@
+## IPAM
+
+### Example configuration
+The following kind of configuration file needs to be exported in the variable OSS_PARAMS_FILENAME:
+
+```
+        "LO": {
+            "V4": {"containers": [], "networks": ["10.255.255.32/32", "10.255.255.0/28", "10.255.255.16/28"], "mask": 0},
+            "V6": {"containers": [], "networks": ["dead:beef::/80", "dead:beef:0:1::/80"], "mask": 0},
+            "domain_name": ".gso"
+        },
+        "TRUNK": {
+            "V4": {"containers": ["10.255.255.0/24", "10.255.254.0/24"], "networks": [], "mask": 31},
+            "V6": {"containers": ["dead:beef::/64", "dead:beee::/64"], "networks": [], "mask": 126},
+            "domain_name": ".gso"
+        },
+```
+Either a non-empty list of `networks` or a non-empty list of `containers` is required in the params file per service type. Having both is redundant, but in that case containers take precedence. The `mask` parameter is irrelevant if the service type uses networks instead of containers.
+
+### Host/network allocation
+Hosts can be allocated through `ipam.new_service_host()` resulting in a host record (IPv4, IPv6, A, AAAA) and zero or more cname records. This function can be called by passing domain name, hostname, and service type (mandatory). Optinally, you can pass either ipv4/ipv6 addresses, ipv4/ipv6 networks, or nothing. Passing nothing is considered default. In that case:
+
+-   If the service type has any container specified in the params file, networks have to be allocated before allocating hosts (through `ipam.new_service_networks()`). The mask parameter is used to create the new networks. Containers are filled up in the order that they appear in the params file.
+-   If the service type has any network specified in the params file, those networks are used directly to allocate hosts. Networks are filled up in the order that they appear in the params file.
+
+If you pass addresses or networks, the module will always attempt to use those, but will fail if they don't match the configuration in the params file for the service type (i.e. if you request an address or network that places outside of the configured containers/networks).
+
+Networks and hosts can be allocated with extensible attributes. Networks can be created with a comment. CNAME records can be optionally created.
+
+### Host/network deletion
+The code checks that the resource you are trying to delete is allocated (in terms of IP address space) to the service as per `containers` or `networks` in the service's configuration. If not, you are not allowed to delete it.
+
+No host or cname records are deleted via the `delete_service_host()` function until all passed arguments match the hostname, IP, and CNAME data of the host. If anything doesn't match, no record is deleted.
+
+### Usage examples
+
+#### Host/network allocation
+ The following is a sample call flow to allocate two loopback interfaces and a trunk service. new_service_host() can be called passing networks, addresses, or nothing.
+ - Host hA for service TRUNK uses a specific ipv4/ipv6 address pair.
+ - Host hB for service TRUNK uses the ipv4/ipv6 network pair.
+ - Service LO uses nothing.
+
+```
+    hostname_A = 'hA'
+    hostname_B = 'hB'
+
+    # hA LO (loopback)
+    loA_v4_host_address = ipaddress.ip_address('10.255.255.0')
+    loA_v6_host_address = ipaddress.ip_address('dead:beef::0')
+    loA_host_addresses = HostAddresses(v4=loA_v4_host_address,
+                                       v6=loA_v6_host_address)
+    new_service_host(hostname=hostname_A+"_LO",
+                     host_addresses=loA_host_addresses,
+                     cname_aliases=["alias1.hA", "alias2.hA"],
+                     service_type='LO')
+
+    # hB LO (loopback)
+    new_service_host(hostname=hostname_B+"_LO",
+                     cname_aliases=["alias1.hB"],
+                     service_type='LO')
+
+    # hA-hB TRUNK
+    trunkAB_network_extattrs = {
+        "vrf_name": {"value": "dummy_vrf"},
+    }
+    trunkAB_host_extattrs = {
+        "Site": {"value": "dummy_site"},
+    }
+    trunkAB_service_networks = new_service_networks(
+        service_type='TRUNK',
+        extattrs=trunkAB_network_extattrs,
+        comment="Network for hA-hB TRUNK"
+    )
+
+    trunkAB_v4_host_address = trunkAB_service_networks.v4.network_address
+    trunkAB_v6_host_address = trunkAB_service_networks.v6.network_address
+    trunkAB_host_addresses = HostAddresses(v4=trunkAB_v4_host_address,
+                                           v6=trunkAB_v6_host_address)
+
+    new_service_host(hostname=hostname_A+"_TRUNK",
+                     service_type='TRUNK',
+                     host_addresses=trunkAB_host_addresses,
+                     extattrs=trunkAB_host_extattrs)
+
+    new_service_host(hostname=hostname_B+"_TRUNK",
+                     service_type='TRUNK',
+                     service_networks=trunkAB_service_networks,
+                     extattrs=trunkAB_host_extattrs)
+```
+
+#### Host/network deletion
+```
+    # Delete network
+    service_network = ipam.delete_service_network(
+        network=ipaddress.ip_network('10.255.255.0/26'), service_type='LO'
+    )
+
+    # Delete host
+    input_host_addresses = ipam.HostAddresses(
+        v4=ipaddress.ip_address('10.255.255.1'),
+        v6=ipaddress.ip_address('dead:beef::1')
+    )
+    host_addresses = ipam.delete_service_host(
+        hostname='ha_lo',
+        host_addresses=input_host_addresses,
+        cname_aliases=['alias1.ha', 'alias2.ha'],
+        service_type='LO'
+    )
+```
diff --git a/gso/services/ipam.py b/gso/services/ipam.py
index dd85f7b8..724db901 100644
--- a/gso/services/ipam.py
+++ b/gso/services/ipam.py
@@ -79,63 +79,3 @@ def delete_service_host(
         cname_aliases=cname_aliases,
         service_type=service_type
     )
-
-
-if __name__ == '__main__':
-    # sample call flow to allocate two loopback interfaces and a trunk service
-    # new_service_host can be called passing networks, addresses, or nothing.
-    # - host h1 for service TRUNK uses a specific ipv4/ipv6 address pair
-    # - host h2 for service TRUNK uses the ipv4/ipv6 network pair
-    # - service LO uses nothing
-    # networks and hosts can be allocated with extensible attributes
-    # networks can be created with a comment
-    # CNAME records can be optionally created
-
-    hostname_A = 'hA'
-    hostname_B = 'hB'
-
-    # hA LO (loopback)
-    # loA_v4_host_address = ipaddress.ip_address('10.255.255.0')
-    # loA_v6_host_address = ipaddress.ip_address('dead:beef::0')
-    # loA_host_addresses = HostAddresses(v4=loA_v4_host_address,
-    #                                    v6=loA_v6_host_address)
-    # new_service_host(hostname=hostname_A+"_LO",
-    #                  host_addresses=loA_host_addresses,
-    #                  cname_aliases=["alias1.hA", "alias2.hA"],
-    #                  service_type='LO')
-    new_service_host(hostname=hostname_A+"_LO",
-                     cname_aliases=["alias1.hA", "alias2.hA"],
-                     service_type='LO')
-
-    # hB LO (loopback)
-    new_service_host(hostname=hostname_B+"_LO",
-                     cname_aliases=["alias1.hB"],
-                     service_type='LO')
-
-    # hA-hB TRUNK
-    trunkAB_network_extattrs = {
-        "vrf_name": {"value": "dummy_vrf"},
-    }
-    trunkAB_host_extattrs = {
-        "Site": {"value": "dummy_site"},
-    }
-    trunkAB_service_networks = new_service_networks(
-        service_type='TRUNK',
-        extattrs=trunkAB_network_extattrs,
-        comment="Network for hA-hB TRUNK"
-    )
-
-    trunkAB_v4_host_address = trunkAB_service_networks.v4.network_address
-    trunkAB_v6_host_address = trunkAB_service_networks.v6.network_address
-    trunkAB_host_addresses = HostAddresses(v4=trunkAB_v4_host_address,
-                                           v6=trunkAB_v6_host_address)
-
-    new_service_host(hostname=hostname_A+"_TRUNK",
-                     service_type='TRUNK',
-                     host_addresses=trunkAB_host_addresses,
-                     extattrs=trunkAB_host_extattrs)
-
-    new_service_host(hostname=hostname_B+"_TRUNK",
-                     service_type='TRUNK',
-                     service_networks=trunkAB_service_networks,
-                     extattrs=trunkAB_host_extattrs)
-- 
GitLab