|
| 1 | +# PowerShell script to handle Secret Manager import for local development |
| 2 | +# This script mirrors the logic in the GitHub Actions workflow |
| 3 | + |
| 4 | +param( |
| 5 | + [Parameter(Mandatory=$true)] |
| 6 | + [string]$ProjectId |
| 7 | +) |
| 8 | + |
| 9 | +Write-Host "Checking for existing Secret Manager resources..." -ForegroundColor Green |
| 10 | + |
| 11 | +# Change to terraform directory |
| 12 | +Set-Location "h:\My Drive\Github\Agentic Data Science\terraform" |
| 13 | + |
| 14 | +try { |
| 15 | + # Check if the secret exists |
| 16 | + $secretExists = $false |
| 17 | + try { |
| 18 | + gcloud secrets describe gemini-api-key --project=$ProjectId 2>$null |
| 19 | + if ($LASTEXITCODE -eq 0) { |
| 20 | + $secretExists = $true |
| 21 | + Write-Host "Secret 'gemini-api-key' already exists" -ForegroundColor Yellow |
| 22 | + } |
| 23 | + } |
| 24 | + catch { |
| 25 | + Write-Host "Secret 'gemini-api-key' does not exist" -ForegroundColor Green |
| 26 | + } |
| 27 | + |
| 28 | + if ($secretExists) { |
| 29 | + Write-Host "Attempting to import existing secret..." -ForegroundColor Yellow |
| 30 | + |
| 31 | + # Try to import the secret |
| 32 | + try { |
| 33 | + terraform import google_secret_manager_secret.gemini_api_key "projects/$ProjectId/secrets/gemini-api-key" |
| 34 | + Write-Host "Successfully imported secret" -ForegroundColor Green |
| 35 | + } |
| 36 | + catch { |
| 37 | + Write-Host "Secret import failed or resource already in state: $($_.Exception.Message)" -ForegroundColor Yellow |
| 38 | + } |
| 39 | + |
| 40 | + # Try to import the secret version |
| 41 | + try { |
| 42 | + $versionId = gcloud secrets versions list gemini-api-key --project=$ProjectId --limit=1 --format="value(name)" 2>$null |
| 43 | + if ($versionId -and $LASTEXITCODE -eq 0) { |
| 44 | + Write-Host "Attempting to import secret version: $versionId" -ForegroundColor Yellow |
| 45 | + terraform import google_secret_manager_secret_version.gemini_api_key_version "projects/$ProjectId/secrets/gemini-api-key/versions/$versionId" |
| 46 | + Write-Host "Successfully imported secret version" -ForegroundColor Green |
| 47 | + } |
| 48 | + } |
| 49 | + catch { |
| 50 | + Write-Host "Secret version import failed or already in state: $($_.Exception.Message)" -ForegroundColor Yellow |
| 51 | + } |
| 52 | + } else { |
| 53 | + Write-Host "Secret does not exist - Terraform will create it" -ForegroundColor Green |
| 54 | + } |
| 55 | + |
| 56 | + Write-Host "`nRunning Terraform plan to check configuration..." -ForegroundColor Green |
| 57 | + terraform plan -no-color |
| 58 | + |
| 59 | +} catch { |
| 60 | + Write-Error "Error occurred: $($_.Exception.Message)" |
| 61 | + exit 1 |
| 62 | +} |
| 63 | + |
| 64 | +Write-Host "`nSecret Manager import process completed!" -ForegroundColor Green |
0 commit comments