def delete_tags(workspaces, action): print('Deleting tags...') to_delete = [] for item in workspaces: to_delete.append({ 'ip': item['ip'], 'Cloud Service': item['user_Cloud Service'] }) restclient = RestClient(tetration_url, api_key=tetration_api_key, api_secret=tetration_api_secret, verify=False) with NamedTemporaryFile() as tf: with open(tf.name, 'w') as temp_csv: writer = csv.DictWriter(temp_csv, fieldnames=['ip', 'Cloud Service']) writer.writeheader() for data in to_delete: writer.writerow(data) temp_csv.seek(0) req_payload = [MultiPartOption(key='X-Tetration-Oper', val=action)] resp = restclient.upload( temp_csv.name, '/openapi/v1/assets/cmdb/upload/{}'.format(tetration_tenant), req_payload) if resp.ok: print("INFO: Deleted {} Annotations".format(len(workspaces))) else: print("ERROR: Failed to Upload Annotations") print(resp.text)
def upload_tags(workspaces, action): restclient = RestClient( tetration_url, api_key=tetration_api_key, api_secret=tetration_api_secret, verify=False) with NamedTemporaryFile() as tf: with open(tf.name, 'w') as temp_csv: writer = csv.DictWriter(temp_csv, fieldnames=fields) writer.writeheader() for data in workspaces: writer.writerow(data) temp_csv.seek(0) req_payload = [ MultiPartOption( key='X-Tetration-Oper', val=action) ] resp = restclient.upload( temp_csv.name, '/openapi/v1/assets/cmdb/upload/{}'.format( tetration_tenant), req_payload) if resp.ok: print("Uploaded {} Annotations \n Action: {}".format( len(workspaces), action)) else: print("Failed to Upload Annotations") print(resp.text)
def annotate(api, vrf, prefixes): logger.info("Writing Annotations (Total: %s) " % len(prefixes)) with NamedTemporaryFile() as tf: wr = writer(tf) wr.writerow(('IP', 'SaaS Provider', 'SaaS Region', 'SaaS Component')) for pfx in prefixes: wr.writerow(pfx) tf.seek(0) req_payload = [MultiPartOption(key='X-Tetration-Oper', val='add')] resp = api.upload(tf.name, '/assets/cmdb/upload/%s' % vrf, req_payload) if resp.ok: logger.info("Uploaded Annotations") else: logger.error("Failed to Upload Annotations: %s", resp.text)
def upload_server_port_config(endpoint, file_path, scope_name): restclient = RestClient(endpoint, credentials_file='api_key.json', verify=False) scope_id = get_scope_id(restclient, scope_name) if scope_id is not None and os.path.exists(file_path): req_payload = [MultiPartOption(key='X-Tetration-Oper', val='add')] resp = restclient.upload( file_path, '/openapi/v1/adm/{0}/server_ports'.format(scope_id), req_payload, timeout=200) # seconds print(resp, resp.text) else: print( 'Wrong scope name {0} or server ports config file {1} does not exist' .format(scope_name, file_path))
def main(): restclient = RestClient(API_ENDPOINT, credentials_file='api_credentials.json', verify=False) file_path = '/your-path-to-file/upload.csv' root_app_scope_name = 'your-root-scope' req_payload = [MultiPartOption(key='X-Tetration-Oper', val='add')] restclient.upload(file_path, '/assets/cmdb/upload/' + root_app_scope_name, req_payload) # Basic Error handling if resp.status_code != 200: print("Unsuccessful request returned code: {} , response: {}".format(resp.status_code, resp.text)) exit(-1) results = resp.json() # check if results were returned, if so print them out, # sometimes the list is inside the json as result other times raw return. if hasattr(results, 'results'): print(json.dumps(results["results"], indent=2)) else: print(json.dumps(results, indent=2)) return
def upload_annotations(self): if 'creds' in self.config: restclient = RestClient(self.config["url"], credentials_file=self.config['creds'], verify=self.config["verify"]) else: restclient = RestClient(self.config["url"], api_key=self.config["key"], api_secret=self.config["secret"], verify=self.config["verify"]) # sleep for 30 seconds to stagger uploading sleep(30) labels = { "mac": 'ACI MAC', "bd": 'ACI Bridge Domain', "vrf": 'ACI VRF', "tenant": 'ACI Tenant', "app": 'ACI Application Profile', "epg": 'ACI End Point Group', "intf": 'ACI Attached Interface', "ts": 'ACI Last Endpoint Move Time Stamp', "epg_dn": 'ACI EPG DN', "leaf": 'ACI Leaf' } headers = [labels[key] for key in self.config['annotations']] headers.insert(0, "IP") while True: if self.stopped(): print "Cleaning up annotation thread" return if self.annotations: try: # Acquire the lock so we don't have a sync issue # if an endpoint receives an event while we upload # data to Tetration self.lock.acquire() print "Writing Annotations (Total: %s) " % len( self.annotations) with NamedTemporaryFile() as tf: wr = writer(tf) wr.writerow(headers) for att in self.annotations.values(): row = [ att[key] for key in self.config['annotations'] ] row.insert(0, att["ip"]) wr.writerow(row) tf.seek(0) req_payload = [ MultiPartOption(key='X-Tetration-Oper', val='add') ] print '/openapi/v1/assets/cmdb/upload/{}'.format( self.config["vrf"]) resp = restclient.upload( tf.name, '/openapi/v1/assets/cmdb/upload/{}'.format( self.config["vrf"]), req_payload) if resp.ok: print "Uploaded Annotations" self.log.append({ "timestamp": time(), "message": "{} annotations".format(len(self.annotations)) }) self.annotations.clear() else: print "Failed to Upload Annotations" print resp.text finally: self.lock.release() else: print "No new annotations to upload" print "Waiting {} seconds".format(int(self.config["frequency"])) sleep(int(self.config["frequency"]))