在Azure databricks API中,我试图使用Azure Databricks API对Databricks repos中每个文件夹中的主分支进行最新的更改。我指的是:
当我使用postman通过向以下端点发送请求来进行调用时,它成功地拉出了如下所示:
端点:
https://<databricks-workspace>.azuredatabricks.net/api/2.0/repos/<repo-id>
这是同一个请求的标题:
为了进一步解释,报头是通过发送一个承载令牌、一个管理令牌和另一个包含订阅、资源组和databricks工作区的字段来构造的,如下所示:
/subscriptions/<Azure-subscription-id>/resourceGroups/<resourcegroup-name>/providers/Microsoft.Databricks/workspaces/<databricks-workspace-name>
如上面所示,当我和邮递员在我的本地机器上调用它时,它工作得非常好。但是,当我使用Azure DevOps使用相同的东西时,它会失败,从而导致错误:
Exception: b'{"error_code":"PERMISSION_DENIED","message":"Missing Git provider credentials. Go to User Settings > Git Integration to add your personal access token."}'
请注意,在此链接之后,我已经在Azure DevOps中生成了一个PAT令牌,并将其添加到服务主体中,否则它不会在我的邮递员上工作。但是,它在DevOps管道中给出了这个错误,如下所示:
这条管道就像我和邮递员一样。此管道正在调用python脚本,该脚本正在构造请求头和正文,如上面postman中所示。python代码如下所示,但我几乎可以肯定,引起问题的不是python脚本,因为我使用了相同的方法来列出repos、获取特定的repo、创建集群等等。我认为这一定是一些行政问题,我无法指出这一点。
python脚本:
import requests
import os
import json
## Constructing the header request
DBRKS_REQ_HEADERS = {
'Authorization': 'Bearer ' + os.environ['DBRKS_BEARER_TOKEN'],
'X-Databricks-Azure-Workspace-Resource-Id': '/subscriptions/'+ os.environ['DBRKS_SUBSCRIPTION_ID'] +'/resourceGroups/'+ os.environ['DBRKS_RESOURCE_GROUP'] +'/providers/Microsoft.Databricks/workspaces/' + os.environ['DBRKS_WORKSPACE_NAME'],
'X-Databricks-Azure-SP-Management-Token': os.environ['DBRKS_MANAGEMENT_TOKEN']}
TRIGGERING_BRANCH = "\"" + os.environ["TRIGGERING_BRANCH"] + "\""
print("TRIGGERING_BRANCH path is {}".format(TRIGGERING_BRANCH))
## Constructing the body request
body_json = '''
{
"branch": "main"
}
'''
## Checking the request body format is correct
print("Request body in json format:")
print(body_json)
## The prints are only for code tracing
DBRKS_REPOS_LIST_JSON = os.environ["DBRKS_REPOS_LIST"]
print("Type of DBRKS_REPOS_LIST_JSON is {}".format(type(DBRKS_REPOS_LIST_JSON)))
## This section extracts repo Ids from the variable containing repo Ids and branches to later construct url endpoint
str_obj = DBRKS_REPOS_LIST_JSON.replace('[','').replace(']','').replace('(','').replace(')','').replace('\'','').replace(' ','').split(',')
output = {}
str_to_list = [str_obj[i:i+2] for i in range(0, len(str_obj), 2)]
print("str_to_list")
print(str_to_list)
for e in str_to_list:
output[e[0]] = e[1]
print("output")
print(output)
repo_ids_for_main_branch = []
for key, value in output.items():
if value == 'main':
repo_ids_for_main_branch.append(key)
print("repo_ids_for_main_branch")
print(repo_ids_for_main_branch)
## This is the main part which is making the API call like postman:
for repo_id in repo_ids_for_main_branch:
dbrks_pull_repo_url = "https://"+os.environ['DBRKS_INSTANCE']+".azuredatabricks.net/api/2.0/repos/"+str(repo_id)
print("endpoint url is {}".format(dbrks_pull_repo_url))
response = requests.patch(dbrks_pull_repo_url, headers=DBRKS_REQ_HEADERS, data=body_json)
if response.status_code == 200:
print("Branch changed or pulled successfully!")
print(response.status_code)
print(response.content)
print('####################')
else:
print("Couldn't pull or change branch!")
raise Exception(response.content)
代码中从Azure DevOps管道传递到脚本的所有os变量,我已经通过打印检查了它们的值,它们都是正确的。
我想知道问题的根源是什么,以及如何解决它.
发布于 2022-05-30 10:51:19
https://stackoverflow.com/questions/72415542
复制相似问题