Skip to content

Instantly share code, notes, and snippets.

@f5-rahm
Created January 13, 2021 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f5-rahm/078dde59c10660fe8c8f14cff217e181 to your computer and use it in GitHub Desktop.
Save f5-rahm/078dde59c10660fe8c8f14cff217e181 to your computer and use it in GitHub Desktop.
###################################################################################
# dgmgmt.py
#
# author: jason rahm
#
# usage: dgmgmt.py [-h] host user {add,modify,delete} datagroup dgvalues
#
# positional arguments:
# host BIG-IP IP/FQDN
# user BIG-IP Username
# {add,modify,delete,replace} add | modify | delete | replace-all-with
# datagroup Data-Group name you wish to change
# dgvalues Key or KV Pairs, in this format: "k1,k2,k3=v3,k4=v4,k5"
#
# optional arguments:
# -h, --help show this help message and exit
###################################################################################
from bigrest.bigip import BIGIP
# from pprintjson import pprintjson as ppjson
import argparse
import getpass
import sys
def build_parser():
parser = argparse.ArgumentParser()
parser.add_argument("host", help="BIG-IP IP/FQDN")
parser.add_argument("user", help="BIG-IP Username")
parser.add_argument(
"action",
help="add | modify | delete",
choices=["add", "modify", "delete", "replace-all-with"],
)
parser.add_argument("datagroup", help="Data-Group name you wish to change")
parser.add_argument(
"dgvalues", help='Key or KV Pairs, in this format: "k1,k2,k3=v3,k4=v4,k5"'
)
return parser.parse_args()
def format_records(action, records):
# records action { key1 key2 { data value2 } key3 key4 }
recs = ""
for record in records.split(","):
x = record.split("=")
record_key = x[0]
if len(x) == 1 and action != "modify":
recs += f"{record_key} "
elif len(x) == 1 and action == "modify":
recs += f'{record_key} {{ data "" }} '
elif len(x) == 2:
record_value = x[1]
recs += f'{record_key} {{ data "{record_value}" }} '
else:
raise ValueError("Max record items is 2: key or key/value pair.")
return f"records {action} {{ {recs} }}"
def instantiate_bigip(host, user):
pw = getpass.getpass(prompt=f"\n\tWell hello {user}, please enter your password: ")
try:
obj = BIGIP(host, user, pw)
except Exception as e:
print(f"Failed to connect to {args.host} due to {type(e).__name__}:\n")
print(f"{e}")
sys.exit()
return obj
def dg_update(bigip, dgname, dgaction, dgvalues):
if not bigip.exist(f"/mgmt/tm/ltm/data-group/internal/{dgname}"):
print(
f"\n\tThe {dgname} data-group doesn't exist. Please specify an existing data-group.\n"
)
sys.exit()
else:
record_data = format_records(dgaction, dgvalues)
try:
return bigip.modify(
f"/mgmt/tm/ltm/data-group/internal/{dgname}?options={record_data}", {}
)
except Exception as e:
print(f"Failed to modify the data-group due to {type(e).__name__}:\n")
print(f"{e}")
sys.exit()
if __name__ == "__main__":
args = build_parser()
b = instantiate_bigip(args.host, args.user)
result = dg_update(b, args.datagroup, args.action, args.dgvalues)
# ppjson(result.properties.get("records"))
print(result.properties.get("records"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment