3

I have the following code which I use to acquire a secret, use secret to log into portal and download a csv table. This works ok outside a function.

import pandas as pd
import pandas as pd
from arcgis.gis import GIS
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url="https://xxxx-dev-vault.vault.azure.net/", credential=credential)
secret = secret_client.get_secret("Secret")

#Log into portal
gis = GIS("https://url.com", "Username", secret.value)

#Extracting Table
item=gis.content.get('content_id')
table=item.layers[0].query().sdf 

I need to include this bit of code in in my httptrigger so that the function logs into portal, extracts csv/table so that the table is returned as a json body of the trigger response or is stored into a blob. How can I achieve this?

I initially thought I could achieve this by integrating the vault in the http trigger in this post. However, I ended up with the Secret being returned instead and I have been unable to use the secret within the function.

I dont mind, even an example logging into an email account or any other portal will suffice provided the secret password is acquired within the function runtime. Ultimately, I am interested in understanding how to retrieve a secret and use it within a function to power/resource a function output.

wwnde
  • 26,119
  • 6
  • 18
  • 32
  • So `table` in last line of your code is a csv object ? – Hury Shen Dec 03 '20 at 04:16
  • @Hury Shen Yes, the table is a csv object. Can be converted into other formats if needed so don't be limited if csv poses a challenge – wwnde Dec 03 '20 at 04:18
  • @Hury Shen wont mind even if the csv is thrown into a blob. main thing here is the retrieval and use of vault secret within a function – wwnde Dec 03 '20 at 04:52
  • Haven't you solved the problem of how to get key vault secret in function ? Per my understanding of this post, the point you want to solve is how to response json from the function(or store the json in blob), right ? – Hury Shen Dec 03 '20 at 04:57
  • @Hury Shen, no . The issue here is; in one function, 1. I want to acquire secret. 2. Use secret to log into portal and 3. return data acquired from portal as the function's httpresponse body. What I had the other time acquired and returned secret as the httpreponse. The secret is not the final outcome here. Simply put, I am integrating the code provided in this post in a function and the function body should return data from portal or query response from portal. if you like to refer to it so – wwnde Dec 03 '20 at 05:05
  • 1
    Ok, understand. – Hury Shen Dec 03 '20 at 05:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225447/discussion-between-wwnde-and-hury-shen). – wwnde Dec 03 '20 at 05:27
  • @Hury Shen do you have a minute? – wwnde Dec 22 '20 at 06:47
  • Hi wwnde, I'm busy now. You can post your question here if the question is related to this post or create another post on stack overflow if the question is not related to this post. I will check your problem and provide you with solution later(if I can help). – Hury Shen Dec 22 '20 at 06:59

1 Answers1

3

The code is what I test in my side with a csv file in local. But I'm not sure if the line dict_reader = csv.DictReader(table) works in your side. You can do some test and modify the code by yourself if it show error.

import logging

import azure.functions as func
from arcgis.gis import GIS
import csv
import json

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    # do some configuration in application settings of your function app as previous post mentioned, and then we can get the secret of key vault.
    # secret = os.environ["testkeyvault"]

    # gis = GIS("https://url.com", "Username", secret)

    #Extracting Table
    # item=gis.content.get('content_id')
    # table=item.layers[0].query().sdf

    # The four lines below is what I test in my side, I use a csv file in local and convert it to json and use "return func.HttpResponse(json_from_csv)" at the end of the code. The function will response json.
    file = open("C:\\Users\\Administrator\\Desktop\\user.csv", "r")
    dict_reader = csv.DictReader(file)
    dict_from_csv = list(dict_reader)[0]
    json_from_csv = json.dumps(dict_from_csv)

    # Your code should be like the three lines below. But as I didn't test with the csv file from gis, so I'm not sure if "dict_reader = csv.DictReader(table)" can work.
    # dict_reader = csv.DictReader(table)
    # dict_from_csv = list(dict_reader)[0]
    # json_from_csv = json.dumps(dict_from_csv)
    
    return func.HttpResponse(json_from_csv)

=============================Update============================

Change the code to match OP's requirements(And do not forget deploy the function to azure, or we can't get the keyvault secret on azure): enter image description here

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • @wwnde Yes, anything I can help ? – Hury Shen Jan 05 '21 at 05:32
  • 1
    @wwnde I'm not focused on data warehourse, you can post the question on Stack Overflow, I will try to provide suggestions if I can help. – Hury Shen Jan 05 '21 at 05:43
  • `https://stackoverflow.com/questions/65573962/azure-webhooks-python`, really it is all about creating a webhook that will listen to a specific url. How is this done in Azure? Cant seem to find proper documentation – wwnde Jan 05 '21 at 06:07