def uploading_data()

in source/Tools Integration/CE-Integration/CE-automation-scripts/0-Import-intake-form.py [0:0]


def uploading_data(data, token):
    auth = {"Authorization": token}
    try:
        waves = json.loads(requests.get(UserHOST + mfcommon.waveendpoint, headers=auth).text)
        apps = json.loads(requests.get(UserHOST + mfcommon.appendpoint, headers=auth).text)
    except requests.exceptions.ConnectionError as e:
            raise SystemExit("ERROR: Connecting to User API failed, please check User API in FactoryEndpoints.json file. "
                            "If the API endpoint is correct, please close cmd and open a new cmd to run the script again")
    wave_list_csv = []
    app_list_csv = []
    wave_ids = []
    app_list = []
    server_list = []

    for row in data:
        if row['wave_id'].strip() not in wave_list_csv:
            wave_list_csv.append(str(row['wave_id']).strip())
        match = False
        for app in app_list_csv:
            if row['app_name'].lower().strip() == app['app_name'].lower().strip():
                match = True
        if (match == False):
            app_item = {}
            app_item['app_name'] = row['app_name'].strip()
            app_item['wave_name'] = "Wave " + row['wave_id'].strip()
            app_item['cloudendure_projectname'] = row['cloudendure_projectname'].strip()
            app_item['aws_accountid'] = row['aws_accountid'].strip()
            app_list_csv.append(app_item)

    data_validation(data, app_list_csv)

    # Get Unique new Wave Id, add to the list if Wave Id doesn't exist in the factory
    for wave_id in wave_list_csv:
        match = False
        for wave in waves:
            wave_name = "Wave " + wave_id
            if str(wave_name) == str(wave['wave_name']):
                match = True
        if (match == False):
            wave_ids.append(wave_id)
    if len(wave_ids) != 0:
        wave_ids.sort()
        print("New Waves: ")
        print("")
        for wave in wave_ids:
            print("Wave " + wave)
        print("")
        # Creating new Waves in the migration factory
        for wave in wave_ids:
            wave_name = {}
            wave_name['wave_name'] = "Wave " + wave
            try:
                r = requests.post(UserHOST + mfcommon.waveendpoint, headers=auth, data=json.dumps(wave_name))
                if r.status_code == 200:
                    print("Wave " + wave + " created in the migration factory")
                else:
                    print("Wave " + wave + " failed : " + r.text + ".......")
            except requests.exceptions.ConnectionError as e:
                   raise SystemExit("ERROR: Connecting to User API failed, please check User API in FactoryEndpoints.json file. "
                                    "If the API endpoint is correct, please close cmd and open a new cmd to run the script again")
        print("")
        print("----------------------------------------")
        print("")

    # Get Unique new App Name, add to the resource list if App Name doesn't exist in the factory
    for app_csv in app_list_csv:
        try:
            new_waves = json.loads(requests.get(UserHOST + mfcommon.waveendpoint, headers=auth).text)
        except requests.exceptions.ConnectionError as e:
                raise SystemExit("ERROR: Connecting to User API failed, please check User API in FactoryEndpoints.json file. "
                                "If the API endpoint is correct, please close cmd and open a new cmd to run the script again")
        for wave in new_waves:
            if app_csv['wave_name'] == str(wave['wave_name']):
                app_csv['wave_id'] = wave['wave_id']
                del app_csv['wave_name']
                break
        match = False
        for app in apps:
            if app_csv['app_name'].lower().strip() == app['app_name'].lower().strip():
                match = True
                if app_csv['wave_id'] != app['wave_id']:
                    print("Error: Wave_id for app " + app_csv['app_name'] + " doesn't match the Wave_id for the same app in the factory")
                    sys.exit(2)
                if app_csv['cloudendure_projectname'] != app['cloudendure_projectname']:
                    print("Error: cloudendure_projectname for app " + app_csv['app_name'] + " doesn't match the cloudendure_projectname for the same app in the factory")
                    sys.exit(3)
                if app_csv['aws_accountid'] != app['aws_accountid']:
                    print("Error: aws_accountid for app " + app_csv['app_name'] + " doesn't match the aws_accountid for the same app in the factory")
                    sys.exit(4)
        if (match == False):
            app_list.append(app_csv)
    if len(app_list) != 0:
        print("New Apps: ")
        print("")
        for app in app_list:
            print(app["app_name"])
        print("")
        # Creating new Apps in the migration factory
        for app in app_list:
            try:
                r = requests.post(UserHOST + mfcommon.appendpoint, headers=auth, data=json.dumps(app))
                if r.status_code == 200:
                    print("App " + app['app_name'] + " created in the migration factory")
                else:
                    print("App " + app['app_name'] + " failed : " + r.text + ".......")
            except requests.exceptions.ConnectionError as e:
                   raise SystemExit("ERROR: Connecting to User API failed, please check User API in FactoryEndpoints.json file. "
                                    "If the API endpoint is correct, please close cmd and open a new cmd to run the script again")
        print("")
        print("----------------------------------------")
        print("")
    # Get Unique server names, add to the resource list if Server Name doesn't exist in the factory
    try:
        newapps = json.loads(requests.get(UserHOST + mfcommon.appendpoint, headers=auth).text)
    except requests.exceptions.ConnectionError as e:
            raise SystemExit("ERROR: Connecting to User API failed, please check User API in FactoryEndpoints.json file. "
                            "If the API endpoint is correct, please close cmd and open a new cmd to run the script again")
    for row in data:
        for app in newapps:
            if row['app_name'].lower().strip() == app['app_name'].lower().strip():
               row['app_id'] = app['app_id']
        tags = []
        tag = {}
        server_item = {}
        server_item['server_name'] = row['server_name'].strip()
        tag['key'] = 'Name'
        tag['value'] = row['server_name'].strip()
        tags.append(tag)
        server_item['tags'] = tags
        server_item['app_id'] = row['app_id'].strip()
        server_item['server_os_family'] = row['server_os_family'].strip()
        server_item['server_os_version'] = row['server_os_version'].strip()
        server_item['server_fqdn'] = row['server_fqdn'].strip()
        server_item['server_tier'] = row['server_tier'].strip()
        server_item['server_environment'] = row['server_environment'].strip()
        server_item['subnet_IDs'] = convert_string_to_list(row['subnet_IDs'].strip())
        server_item['securitygroup_IDs'] = convert_string_to_list(row['securitygroup_IDs'].strip())
        server_item['subnet_IDs_test'] = convert_string_to_list(row['subnet_IDs_test'].strip())
        server_item['securitygroup_IDs_test'] = convert_string_to_list(row['securitygroup_IDs_test'].strip())
        server_item['instanceType'] = row['instanceType'].strip()
        server_item['tenancy'] = row['tenancy'].strip()
        server_list.append(server_item)
    if len(server_list) != 0:
        print("New Servers: ")
        print("")
        for server in server_list:
            print(server['server_name'])
        print("")
        # Creating new Apps in the migration factory
        for server in server_list:
            try:
                r = requests.post(UserHOST + mfcommon.serverendpoint, headers=auth, data=json.dumps(server))
                if r.status_code == 200:
                    print("Server " + server['server_name'] + " created in the migration factory")
                else:
                    print("ERROR: " + server['server_name'] + " failed : " + r.text + ".......")
            except requests.exceptions.ConnectionError as e:
                   raise SystemExit("ERROR: Connecting to User API failed, please check User API in FactoryEndpoints.json file. "
                                    "If the API endpoint is correct, please close cmd and open a new cmd to run the script again")