Skip to content

Commit 7e89995

Browse files
Update apiview prod to staging sync job to use managed identity (#8673)
* Update apiview prod to staging sync job to use managed identity * Update job name * Add python install step and update as per review comment * Connect to cosmos DB using key * apiview_sync_pipeline * Changes to return exit code * Add new line * Add chain credential to support cli credential
1 parent 5c604ce commit 7e89995

3 files changed

Lines changed: 30 additions & 24 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
azure-cosmos
2-
azure-storage-blob
2+
azure-storage-blob
3+
azure-identity

eng/scripts/python/apiview-syncdb/sync_cosmosdb.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import traceback
99
from ast import literal_eval
1010
from azure.cosmos import CosmosClient
11+
from azure.identity import AzurePowerShellCredential, ChainedTokenCredential, AzureCliCredential
1112
from azure.storage.blob import BlobServiceClient
1213

1314
logging.getLogger().setLevel(logging.INFO)
@@ -25,13 +26,14 @@
2526
BACKUP_CONTAINER = "backups"
2627
BLOB_NAME_PATTERN ="cosmos/{0}/{1}"
2728

29+
# Create a AzurePowerShellCredential()
30+
credential_chain = ChainedTokenCredential(AzureCliCredential(), AzurePowerShellCredential())
2831

32+
def restore_data_from_backup(backup_storage_url, dest_url, db_name):
2933

30-
def restore_data_from_backup(connection_string, dest_url, dest_key, db_name):
31-
32-
dest_db_client = get_db_client(dest_url, dest_key, db_name)
34+
dest_db_client = get_db_client(dest_url, db_name)
3335

34-
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
36+
blob_service_client = BlobServiceClient(backup_storage_url, credential = credential_chain)
3537
container_client = blob_service_client.get_container_client(BACKUP_CONTAINER)
3638
for cosmos_container_name in COSMOS_CONTAINERS:
3739
# Load source records from backup file
@@ -68,10 +70,10 @@ def get_backup_contents(container_client, blob_name):
6870

6971

7072
# Create cosmosdb clients
71-
def get_db_client(dest_url, dest_key, db_name):
73+
def get_db_client(dest_url, db_name):
7274

7375
# Create cosmosdb client for destination db
74-
dest_cosmos_client = CosmosClient(dest_url, credential=dest_key)
76+
dest_cosmos_client = CosmosClient(dest_url, credential=credential_chain)
7577
if not dest_cosmos_client:
7678
logging.error("Failed to create cosmos client for destination db")
7779
exit(1)
@@ -83,7 +85,7 @@ def get_db_client(dest_url, dest_key, db_name):
8385
dest_db_client = dest_cosmos_client.get_database_client(db_name)
8486
logging.info("Created database clients")
8587
except:
86-
logging.error("Failed to create databae client using CosmosClient")
88+
logging.error("Failed to create database client using CosmosClient")
8789
traceback.print_exc()
8890
exit(1)
8991
return dest_db_client
@@ -122,20 +124,15 @@ def fetch_records(container_client, container_name):
122124
)
123125

124126
parser.add_argument(
125-
"--backup-connection-string",
127+
"--backup-storage-url",
126128
required=True,
127-
help=("Connection string to backup storage account"),
129+
help=("URL to backup storage account"),
128130
)
129131
parser.add_argument(
130132
"--dest-url",
131133
required=True,
132134
help=("URL to destination cosmosdb"),
133135
)
134-
parser.add_argument(
135-
"--dest-key",
136-
required=True,
137-
help=("Destination cosmosdb account key"),
138-
)
139136
parser.add_argument(
140137
"--db-name",
141138
required=True,
@@ -145,4 +142,4 @@ def fetch_records(container_client, container_name):
145142
args = parser.parse_args()
146143

147144
logging.info("Syncing database..")
148-
restore_data_from_backup(args.backup_connection_string, args.dest_url, args.dest_key, args.db_name)
145+
restore_data_from_backup(args.backup_storage_url, args.dest_url, args.db_name)

src/dotnet/APIView/apiview-sync-staging.yml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ stages:
1212
- stage: 'Main'
1313
jobs:
1414
- job: 'BlobCopy'
15+
displayName: 'Sync API reviews'
1516
pool:
1617
name: azsdk-pool-mms-win-2022-general
1718
vmImage: windows-2022
@@ -40,13 +41,20 @@ stages:
4041
env:
4142
AZCOPY_AUTO_LOGIN_TYPE: 'PSCRED'
4243

43-
- task: UsePythonVersion@0
44-
displayName: 'Use Python 3.6'
45-
inputs:
46-
versionSpec: 3.6
44+
- template: /eng/pipelines/templates/steps/use-python-version.yml
45+
parameters:
46+
versionSpec: '3.10'
4747

48-
- script: |
49-
cd $(Build.SourcesDirectory)/eng/scripts/python/apiview-syncdb/
50-
python -m pip install -r requirements.txt
51-
python ./sync_cosmosdb.py --dest-url $(apiview-staging-cosmos-url) --dest-key $(apiview-staging-cosmos-key) --db-name $(apiview-cosmosdb-name) --backup-connection-string $(apiview-cosmos-backup-connection)
48+
- task: AzurePowerShell@5
5249
displayName: Sync CosmosDB
50+
inputs:
51+
azureSubscription: 'Azure SDK Engineering System'
52+
ScriptType: 'InlineScript'
53+
azurePowerShellVersion: LatestVersion
54+
workingDirectory: $(Build.BinariesDirectory)
55+
pwsh: true
56+
Inline: |
57+
cd $(Build.SourcesDirectory)/eng/scripts/python/apiview-syncdb/
58+
python -m pip install -r requirements.txt
59+
python ./sync_cosmosdb.py --dest-url $(apiview-staging-cosmos-url) --db-name $(apiview-cosmosdb-name) --backup-storage-url $(apiview-cosmos-backup-url)
60+
exit $LASTEXITCODE

0 commit comments

Comments
 (0)