From cbbbd240d99c72c6913002210eb17f77535ba280 Mon Sep 17 00:00:00 2001 From: Mika Busch Date: Fri, 8 May 2020 14:12:01 +0200 Subject: [PATCH 1/4] Specify vrf_id for VC_Hosts Adds the Option to add a vrf_id to a vc Host. This vrf is used when requesting IP-Addresses/Prefixes from Netbox and also when creating or updating those. --- run.py | 6 +++++- settings.example.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/run.py b/run.py index 23601b2..8cfb600 100644 --- a/run.py +++ b/run.py @@ -1100,6 +1100,8 @@ def obj_exists(self, nb_obj_type, vc_data): quote_plus(vc_data["virtual_machine"]["name"]), query_key, vc_data[query_key] ) + elif self.vrf_id is not None and nb_obj_type in ("ip_addresses", "prefixes"): + query = "?{}={}&vrf_id={}".format(query_key, vc_data[query_key], self.vrf_id) else: query = "?{}={}".format( query_key, vc_data[query_key] @@ -1516,7 +1518,9 @@ def search_prefix(self, ip_addr): :rtype: dict """ result = {"tenant": None, "vrf": None} - query = "?contains={}".format(ip_addr) + query = ("?contains={}".format(ip_addr) + if self.vrf_id is None + else "?contains={}&vrf_id={}".format(ip_addr, self.vrf_id)) try: prefix_obj = self.request( req_type="get", nb_obj_type="prefixes", query=query diff --git a/settings.example.py b/settings.example.py index 632a657..310705b 100644 --- a/settings.example.py +++ b/settings.example.py @@ -33,7 +33,7 @@ # The USER argument supports SSO with @domain.tld suffix VC_HOSTS = [ # You can add more vCenter instances by duplicating the line below - {"HOST": "vcenter1.example.com", "PORT": 443, "USER": "", "PASS": ""}, + {"HOST": "vcenter1.example.com", "PORT": 443, "USER": "", "PASS": "", "VRF_ID": None}, ] From 3e90cd9256fc89b1985bfa18b53b2777026bacbe Mon Sep 17 00:00:00 2001 From: Mika Busch Date: Fri, 29 May 2020 15:53:29 +0200 Subject: [PATCH 2/4] Fixed missing vrf_id assignment VRF_ID from the vc_conn gets imported --- run.py | 1 + 1 file changed, 1 insertion(+) diff --git a/run.py b/run.py index 8cfb600..774578f 100644 --- a/run.py +++ b/run.py @@ -905,6 +905,7 @@ def __init__(self, vc_conn): }, } self.vc_tag = format_tag(vc_conn["HOST"]) + self.vrf_id = vc_conn["VRF_ID"] self.vc = vCenterHandler( format_vcenter_conn(vc_conn), nb_api_version=self.nb_api_version ) From fc08dffd02e646b6e0f632d952bd36bfa92992cd Mon Sep 17 00:00:00 2001 From: Mika Busch Date: Tue, 23 Jun 2020 15:48:17 +0200 Subject: [PATCH 3/4] Prevent KeyError by using get() --- run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.py b/run.py index 774578f..05c4b4c 100644 --- a/run.py +++ b/run.py @@ -905,7 +905,7 @@ def __init__(self, vc_conn): }, } self.vc_tag = format_tag(vc_conn["HOST"]) - self.vrf_id = vc_conn["VRF_ID"] + self.vrf_id = vc_conn.get("VRF_ID") self.vc = vCenterHandler( format_vcenter_conn(vc_conn), nb_api_version=self.nb_api_version ) From 761f4501d19afb0664ae80d98709e1f7e946a51f Mon Sep 17 00:00:00 2001 From: Mika Busch Date: Mon, 22 Jun 2020 12:12:18 +0200 Subject: [PATCH 4/4] Fixed Pylinter errors --- run.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/run.py b/run.py index 05c4b4c..f7fa16e 100644 --- a/run.py +++ b/run.py @@ -1519,9 +1519,9 @@ def search_prefix(self, ip_addr): :rtype: dict """ result = {"tenant": None, "vrf": None} - query = ("?contains={}".format(ip_addr) - if self.vrf_id is None - else "?contains={}&vrf_id={}".format(ip_addr, self.vrf_id)) + query = ("?contains={}".format(ip_addr) + if self.vrf_id is None + else "?contains={}&vrf_id={}".format(ip_addr, self.vrf_id)) try: prefix_obj = self.request( req_type="get", nb_obj_type="prefixes", query=query