Example #1
0
def tet_annotate(operation_type, API_ENDPOINT, CREDENTIALS_FILE,
                 annotation_csv_file, app_scope):
    rc = RestClient(API_ENDPOINT,
                    credentials_file=CREDENTIALS_FILE,
                    verify=False)
    if debug_mode:
        print('Operation Type: ' + operation_type)
        print('API Endpoint: ' + API_ENDPOINT)
        print('Credentials File: ' + CREDENTIALS_FILE)
        print('Annotation CSV file: ' + annotation_csv_file)
        print('App Scope: ' + app_scope)

    try:
        if operation_type == 'add':
            if debug_mode:
                print('Adding Annotation')
            req_payload = [
                tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')
            ]
            rc.upload(annotation_csv_file, '/assets/cmdb/upload/' + app_scope,
                      req_payload)
        elif operation_type == 'delete':
            if debug_mode:
                print('Deleting Annotation')
            req_payload = [
                tetpyclient.MultiPartOption(key='X-Tetration-Oper',
                                            val='delete')
            ]
            rc.upload(annotation_csv_file, '/assets/cmdb/upload/' + app_scope,
                      req_payload)
    except Exception:
        print("Oops - something went wrong")
        pass
Example #2
0
def SendAnnotationUpdates(rc, resolved_hosts):
    user_annotations = []
    headerFlag = 0

    for host in resolved_hosts:
        row = dict([(k if not k.startswith('user_') else k.split('user_')[1],
                     v) for k, v in host.items()
                    if k.startswith(('ip', 'vrf_name', 'user_'))])
        row['IP'] = row.pop('ip')
        row['VRF'] = row.pop('vrf_name')
        user_annotations.append(row)
        if headerFlag == 0:
            headers = [key for key in row if key != 'IP' and key != 'VRF']
            headers.insert(0, 'VRF')
            headers.insert(0, 'IP')
    with open('annotations.csv', 'wb') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=headers)
        writer.writeheader()
        writer.writerows(user_annotations)

    file_path = 'annotations.csv'
    keys = ['IP', 'VRF']

    req_payload = [
        tetpyclient.MultiPartOption(key='X-Tetration-Key', val=keys),
        tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')
    ]
    resp = rc.upload(file_path, '/assets/cmdb/upload', req_payload)
    if resp.status_code != 200:
        print("Error posting annotations to Tetration cluster")
    else:
        print("Successfully posted annotations to Tetration cluster")
Example #3
0
def uploadDeletions(rc, delFile):
    # upload added annotations to tetration
    keys = ['IP', 'VRF', 'BlackList']
    req_payload = [
        tetpyclient.MultiPartOption(key='X-Tetration-Key', val=keys),
        tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='delete')
    ]
    resp = rc.upload(delFile, '/assets/cmdb/upload', req_payload)
    if resp.status_code != 200:
        print("Error posting annotations to Tetration cluster")
    else:
        print(resp.text)
        print("Successfully posted annotations to Tetration cluster")
Example #4
0
def AnnotateHosts(rc,hosts,params):
    columns = [params["columns"][column] for column in params["columns"] if params["columns"][column]["enabled"] == True]    
    with open(params["csvParams"]["exportFilename"], "wb") as csv_file:
        fieldnames = ['IP','VRF']
        for column in columns:
            if column["infobloxName"] != 'extattrs':
                fieldnames.extend([column["annotationName"]])
            else:
                if column["overload"] == True:
                    fieldnames.extend([column["annotationName"]])
                else:
                    for attr in column["attributeList"]:
                        fieldnames.extend([str(column["annotationName"]) + '-' + str(attr)])
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writeheader()
        for host in hosts:
            hostDict = {}
            hostDict["IP"] = host["ip_address"]
            if params["vrf"]["type"] == 'static':
                hostDict["VRF"] = params["vrf"]["value"]
            else:
                if params["vrf"]["eaName"] not in host["extattrs"]:
                    print("EA: " + params["vrf"]["eaName"] + "not defined for host: " + host["ip_address"] + " skipping....")
                    continue
                hostDict["VRF"] = host["extattrs"][params["vrf"]["eaName"]]["value"]
            for column in columns:
                if column["infobloxName"] == 'extattrs':
                    for attr in column["attributeList"]:
                        if column["overload"] == True:
                            if attr in host["extattrs"]:
                                hostDict[column["annotationName"]] = str(attr) + '=' + str(host["extattrs"][attr]["value"]) + ';' if column["annotationName"] not in hostDict.keys() else hostDict[column["annotationName"]] + str(attr) + '=' + str(host["extattrs"][attr]["value"]) + ';'
                            else:
                                hostDict[column["annotationName"]] = str(attr) + '=;' if column["annotationName"] not in hostDict.keys() else str(hostDict[column["annotationName"]]) + str(attr) + '=;'
                        else:
                            if attr in host["extattrs"]:
                                hostDict[column["annotationName"] + '-' + attr] = host["extattrs"][attr]["value"]
                            else:
                                hostDict[column["annotationName"] + '-' + attr] = ''
                else:
                    hostDict[column["annotationName"]] = ",".join(host[column["infobloxName"]]).split('.')[0] if type(host[column["infobloxName"]]) is list else host[column["infobloxName"]]
            writer.writerow(hostDict)
    req_payload = [tetpyclient.MultiPartOption(key='X-Tetration-Key', val=keys), tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')]
    resp = rc.upload(params["csvParams"]["exportFilename"], '/assets/cmdb/upload', req_payload)
    if resp.status_code != 200:
        print("Error posting annotations to Tetration cluster")
    else:
        print("Successfully posted annotations to Tetration cluster")
Example #5
0
def uploadAdditions(rc, addFile):
    # upload added annotations to tetration
    keys = ['IP', 'VRF']
    req_payload = [
        tetpyclient.MultiPartOption(key='X-Tetration-Key', val=keys),
        tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')
    ]
    resp = rc.upload(addFile, '/assets/cmdb/upload', req_payload)
    if resp.status_code != 200:
        print("Error posting annotations to Tetration cluster")
        print(resp.status_code)
        print(resp.text)
    else:
        print(resp.status_code)
        print(resp.text)
        print("Successfully posted additions to Tetration cluster using file",
              addFile)
def SendAnnotationUpdates(rc, resolved_hosts):
    with open('annotations.csv', 'wb') as csvfile:
        writer = csv.DictWriter(
            csvfile, fieldnames=['IP', TETRATION_HOST_NAME_USER_ANNOTATION])
        writer.writeheader()
        writer.writerows(resolved_hosts)

    file_path = 'annotations.csv'
    req_payload = [
        tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')
    ]
    resp = rc.upload(file_path,
                     '/assets/cmdb/upload/%s' % TETRATION_SCOPE_NAME,
                     req_payload)
    if resp.status_code != 200:
        print("Error posting annotations to Tetration cluster")
    else:
        print("Successfully posted annotations to Tetration cluster")
Example #7
0
def upload_userdata( endpoint, file_path, app_scope):
    restclient = RestClient(endpoint,
                    credentials_file='api_key.json',
                    verify=False)

    #file_path = # source file contains hangul with UTF-8 encoding
    #app_scope = 'APJDemo' # name of scope  VRF independent 

    # upload user annotation file 
    req_payload = [tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')]
    resp = restclient.upload(file_path, '/assets/cmdb/upload/' + app_scope, req_payload)
    #print(resp, resp.text)

    #file_path = 'output.csv'
    #resp = restclient.download(file_path, '/assets/cmdb/download/' + app_scope)

    with open(file_path) as inf:
        # activate annotation with column names 
        # hdr = ['VM_Name', 'VM_Guest_OS', 'VM_Host', 'VM_Datastore', 'VM_Network', "VM_OWNER"]
        hdr = inf.readline().strip().split(',')
        print(hdr[1:])
        resp = restclient.put('/assets/cmdb/annotations/' + app_scope, json_body=json.dumps(hdr[1:]))
        print(resp, resp.text)
Example #8
0
def uploadAnnotation(rc):
    #Upload Annotation Tags to Tetration root Scope. Sample csv: need to have IP as anchor point, can add upto 32 annotations.
    vrfs = GetVRFs(rc)
    RootScopesList = GetRootScope(vrfs)
    print(
        Cyan +
        "\nHere are the names and VRF ID of all the root scopes in your cluster: "
        + CEND)
    print(*RootScopesList, sep="\n")
    root_scope_name = input(
        CGREEN + "\nWhat is the root scope you want to upload annotations: " +
        CEND)
    file_path = "sampleAnnotationUpload.csv"
    req_payload = [
        tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')
    ]
    resp = rc.upload(file_path, "/assets/cmdb/upload/" + root_scope_name,
                     req_payload)
    if resp.status_code == 200:
        print("\nUploaded sucessful!" + CEND)
    else:
        print("Error occured during upload annotation file")
        print("Error code: " + str(resp.status_code))
        sys.exit(3)
Example #9
0
if os.path.exists(ANNOTATIONS_DIFF_FILE):
    restclient = RestClient(os.environ['TETRATION_ENDPOINT'],
                            api_key=os.environ['TETRATION_API_KEY'],
                            api_secret=os.environ['TETRATION_API_SECRET'],
                            verify=False)

    requests.packages.urllib3.disable_warnings()

    # This is taken from the sample code in the Tetration documentation. The
    # URL is very important. There cannot be a slash (/) at the end of the URL.
    # That is why we add the slash to to the beginning of the APPSCOPE global.
    # When not in multitenant mode, APPSCOPE is just an empty string and the
    # URL will still end without a slash.
    file_path = '/<path_to_file>/user_annotations.csv'
    req_payload = [
        tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')
    ]
    resp = restclient.upload(ANNOTATIONS_DIFF_FILE,
                             '/assets/cmdb/upload' + APPSCOPE, req_payload)

    # if we get an error from Tetration, pass that through to ecohub
    if resp.status_code != 200:
        pigeon['status_code'] = resp.status_code
        pigeon['message'] = resp.text

# if the annotations file doesn't exist, there is nothing to do
else:
    pigeon['message'] = "Nothing to upload."

# send results to ecohub
print json.dumps(pigeon)
Example #10
0
 def AnnotateHosts(self, hosts, columns, csvFile):
     with open(csvFile, "wb") as csv_file:
         fieldnames = ['IP']
         for column in columns:
             if column["infobloxName"] != 'extattrs':
                 fieldnames.extend([column["annotationName"]])
             else:
                 if column["overload"] == "on":
                     fieldnames.extend([column["annotationName"]])
                 else:
                     for attr in column["attrList"]:
                         fieldnames.extend([
                             str(column["annotationName"]) + '-' + str(attr)
                         ])
         writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
         writer.writeheader()
         for host in hosts:
             hostDict = {}
             hostDict["IP"] = host["ip_address"]
             # hostDict["VRF"] = [ tetHost["vrf_name"] for tetHost in self.inventory.pagedData if tetHost["ip"] == host["ip_address"] ][0]
             if len(host["names"]) < 1:
                 continue
             for column in columns:
                 if column["infobloxName"] == 'extattrs':
                     for attr in column["attrList"]:
                         if column["overload"] == "on":
                             if attr["value"] in host["extattrs"]:
                                 hostDict[column["annotationName"]] = str(
                                     attr["value"]
                                 ) + '=' + str(
                                     host["extattrs"][
                                         attr["value"]]["value"]
                                 ) + ';' if column[
                                     "annotationName"] not in hostDict.keys(
                                     ) else hostDict[
                                         column["annotationName"]] + str(
                                             attr["value"]
                                         ) + '=' + str(host["extattrs"][
                                             attr["value"]]["value"]) + ';'
                             else:
                                 hostDict[column["annotationName"]] = str(
                                     attr["value"]
                                 ) + '=;' if column[
                                     "annotationName"] not in hostDict.keys(
                                     ) else str(hostDict[
                                         column["annotationName"]]) + str(
                                             attr["value"]) + '=;'
                         else:
                             if attr["value"] in host["extattrs"]:
                                 hostDict[column["annotationName"] + '-' +
                                          attr["value"]] = host["extattrs"][
                                              attr["value"]]["value"]
                             else:
                                 hostDict[column["annotationName"] + '-' +
                                          attr["value"]] = ''
                 elif column["infobloxName"] == 'zone':
                     hostDict[column["annotationName"]] = '.'.join(",".join(
                         host["names"]).split('.')[1:])
                 elif column["infobloxName"] == 'names':
                     hostDict[column["annotationName"]] = ",".join(
                         host[column["infobloxName"]]).split('.')[0]
                 else:
                     hostDict[column["annotationName"]] = host[
                         column["infobloxName"]]
             writer.writerow(hostDict)
     #keys = ['IP', 'VRF']
     #req_payload = [tetpyclient.MultiPartOption(key='X-Tetration-Key', val=keys), tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')]
     #resp = self.rc.upload(csvFile, '/assets/cmdb/upload', req_payload)
     req_payload = [
         tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')
     ]
     resp = self.rc.upload(csvFile,
                           '/assets/cmdb/upload/' + self.tenant_app_scope,
                           req_payload)
     if resp.status_code != 200:
         self.pigeon.note.update({
             'status_code': 403,
             'message': 'Error posting annotations to Tetration cluster',
             'data': {}
         })
         self.pigeon.send()
         return
     else:
         self.pigeon.note.update({
             'status_code': 100,
             'message':
             'Successfully posted annotations to Tetration cluster',
             'data': {}
         })
         self.pigeon.send()