Skip to content

Instantly share code, notes, and snippets.

@fndiaz
Last active November 6, 2019 21:30
Show Gist options
  • Save fndiaz/12d79ec532a69520c9a72bf316cd056a to your computer and use it in GitHub Desktop.
Save fndiaz/12d79ec532a69520c9a72bf316cd056a to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import requests, re, random, json
import optparse
import time
def get_config(headers, conn, connectors):
path = 'http://localhost:8083/connectors/%s' %(conn)
r = requests.get(path, headers=headers)
config = r.json()
names = name(conn)
config['name'] = names
config['config']['snapshot.mode'] = "schema_only"
del config['tasks']
del config['type']
del config['config']['name']
if conn in connectors:
register(config, headers, names)
def name(conn):
conn_split = conn.split('_')[0]
name = "%s_%s" %(conn_split, ''.join(random.sample('0123456789', 5)))
return name
def register(config, headers, names):
print "registering... %s" %(names)
config_json = json.dumps(config)
res = requests.post('http://localhost:8083/connectors/', headers=headers, data=config_json)
time.sleep(5)
print "registered successfully/\n"
def status(headers, conn):
print conn
path = 'http://localhost:8083/connectors/%s/status' %(conn)
r = requests.get(path, headers=headers)
print "%s/\n" %(r.text)
def delete(connectors):
for dado in connectors:
print "delete %s" %(dado)
requests.delete('http://localhost:8083/connectors/%s' %(dado))
def config(headers, connectors):
for dado in connectors:
r = requests.get("http://localhost:8083/connectors/%s" %(dado), headers=headers)
print dado
print "%s/\n" %(r.text)
def restart(connectors):
for dado in connectors:
r = requests.post('http://localhost:8083/connectors/%s/tasks/0/restart' %(dado))
print "restart task %s" %(dado)
def main(connectors, act):
headers = {
'Accept': 'application/json',
'Content-Type':'application/json'
}
response = requests.get('http://localhost:8083/connectors/', headers=headers)
conns = response.json()
if act == "register":
for conn in conns:
get_config(headers, conn, connectors)
if act == "status":
for conn in conns:
status(headers, conn)
if act == "list":
print conns
if act == "delete":
delete(connectors)
if act == "config":
config(headers, connectors)
if act == "restart":
restart(connectors)
def args_callback(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option('-c', '--connectors', dest='connectors', help='connector1,connector2', type='string', action='callback', callback=args_callback)
parser.add_option('-a', '--action', dest='act', help='status or list or register or config or delete', type='string')
(options, args) = parser.parse_args()
#print options.connectors
if not options.act:
raise Exception("action (--action or -a) option is mandatory")
main(options.connectors, options.act)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment