def process_static_asset_config()

in 5-app-infra/3-artifact-publish/docker/cdmc/tag_engine_api/main.py [0:0]


def process_static_asset_config():
    
    if 'credentials' not in session:
        return redirect('/')
        
    template_id = request.form['template_id']
    template_project = request.form['template_project']
    template_region = request.form['template_region']
    service_account = request.form['service_account']
    included_assets_uris = request.form['included_assets_uris'].rstrip()
    excluded_assets_uris = request.form['excluded_assets_uris'].rstrip()
    refresh_mode = request.form['refresh_mode']
    refresh_frequency = request.form['refresh_frequency'].rstrip()
    refresh_unit = request.form['refresh_unit']
    action = request.form['action']
    
    print('included_assets_uris: ' + included_assets_uris)
    print('excluded_assets_uris: ' + excluded_assets_uris)
    print('service_account: ' + service_account)

    credentials, success = get_target_credentials(service_account)
    
    if success == False:
        print('Error acquiring credentials from', service_account)

    dcc = controller.DataCatalogController(credentials, None, None, template_id, template_project, template_region)
    template = dcc.get_template()
    
    if action == "Cancel Changes":
        
        return render_template(
            'tag_template.html',
            template_id=template_id,
            template_project=template_project,
            template_region=template_region, 
            service_account=service_account, 
            fields=template)
    
    if action == "View Existing Configs":

        configs = store.read_configs(service_account, 'ALL', template_id, template_project, template_region)

        return render_template(
            'view_configs.html',
            template_id=template_id,
            template_project=template_project,
            template_region=template_region,
            service_account=service_account,
            configs=configs)
    
    fields = []
    
    selected_fields = request.form.getlist("selected")
    print("selected_fields: " + str(selected_fields))
    
    for selected_field in selected_fields:
        selected_type = request.form.get(selected_field + "_datatype")

        if selected_type == 'bool':
            selected_value = request.form.get(selected_field)
            
            if selected_value.lower() == 'true':
                selected_value = True
            else:
                selected_value = False
        else:
            selected_value = request.form.get(selected_field)
        
        #print(selected_field + ", " + str(selected_value) + ", " + selected_type)
        
        for template_field in template:
            
            if template_field['field_id'] != selected_field:
                continue
            
            is_required = template_field['is_required']
            field = {'field_id': selected_field, 'field_value': selected_value, 'field_type': selected_type, 'is_required': is_required}
            fields.append(field)
            break
    
    #print('fields: ' + str(fields))
    
    if excluded_assets_uris == 'None':
        excluded_assets_uris = ''
    
    tag_history_option, _ = store.read_tag_history_settings()
    
    if tag_history_option == True:
        tag_history_display = "ON"
    else:
        tag_history_display = "OFF"
                    
    template_uuid = store.write_tag_template(template_id, template_project, template_region)
    
    # TO DO: decide how best to let users specify the overwrite field from the UI 
    config_uuid = store.write_static_asset_config(service_account, fields, included_assets_uris, excluded_assets_uris, \
                                                  template_uuid, template_id, template_project, template_region, \
                                                  refresh_mode, refresh_frequency, refresh_unit, tag_history_option, overwrite=True)
    
    # [START render_template]
    return render_template(
        'created_static_asset_config.html',
        config_uuid=config_uuid,
        config_type='STATIC_TAG_ASSET',
        template_id=template_id,
        template_project=template_project,
        template_region=template_region,
        service_account=service_account,
        fields=fields,
        included_assets_uris=included_assets_uris,
        excluded_assets_uris=excluded_assets_uris,
        tag_history=tag_history_display)