Skip to content

Instantly share code, notes, and snippets.

@JacobCallahan
Created June 15, 2021 14:36
Show Gist options
  • Save JacobCallahan/73ebccf1acaf4222aca24200f7719c1b to your computer and use it in GitHub Desktop.
Save JacobCallahan/73ebccf1acaf4222aca24200f7719c1b to your computer and use it in GitHub Desktop.
This is a script that exports all "entities" in a Satellite system to json and yaml files
from requests.exceptions import HTTPError
from nailgun import entities
from nailgun.entity_mixins import EntitySearchMixin
from nailgun.config import ServerConfig
import json
import yaml
ServerConfig(url="<satellite url>", auth=("admin", "<passowrd>"), verify=False).save()
def search_entity(ent, **kwargs):
print(f"Lookng for {name}s")
try:
return [json.loads(item.to_json()) for item in ent(**kwargs).search()]
except HTTPError as err:
if "organization ID" in err.response.text:
return "org_id"
except Exception as err:
print(err)
to_search = {}
results = {}
for name, obj in entities.__dict__.items():
try:
if EntitySearchMixin in obj.mro() and obj is not EntitySearchMixin:
to_search[name] = obj
except AttributeError:
# not everything has an mro method, we don't care about them
continue
org_required = []
to_del = []
for name, ent in to_search.items():
res = search_entity(ent)
if res == "org_id":
org_required.append(name)
elif res:
print(f"Found results for: {name}")
results[name] = res
to_del.append(name)
for name in to_del:
del to_search[name]
to_del = []
for org in results.get("Organization", []):
for name in org_required:
results[name] = []
res = search_entity(to_search[name], organization_id=org.get("id"))
if res and isinstance(res, list):
print(f"Found results for: {name}")
results[name].append(res)
to_del.append(name)
for name in to_del:
del to_search[name]
to_del = []
print(f"Unable to find results for: {to_search.keys()}")
with open("export.json", "w") as f_out:
json.dump(results, f_out, indent=4, sort_keys=True)
with open("export.yaml", "w") as f_out:
yaml.dump(results, f_out, sort_keys=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment