How to creatte an index pattern in Opensearch use the API? - amazon-web-services

I want to create an index pattern using the Opensearch API. The issue i'm having is with the message body type. I'm getting the following message when running my script
I'm running opensearch 1.3
Invoke-RestMethod: {"statusCode":400,"error":"Bad Request","message":"[request body.attributes]: expected value of type [object] but got [undefined]"}
$baseuri = "https://<clusterendpoint>.<region>.es.amazonaws.com"
#obtain auth cookie
$authbody = #"
{"username":"$apiUserName", "password":"$apiPassword"}
"#
$authUri = "$baseuri/_dashboards/auth/login"
Invoke-RestMethod -Method Post -Uri $authUri -Headers #{"osd-xsrf"="true"} -ContentType "application/json" -Body $authbody -SessionVariable S1
#### create index pattern
$indexid = "indexname"
$body=#"
{
"index_pattern": {
"title": "indexname-*",
"timeFieldName": "#timestamp"
}
}
"#
$uri = "$baseuri/_dashboards/api/saved_objects/index-pattern/$indexid"
Invoke-RestMethod -Method Post -Uri $uri -Headers #{"osd-xsrf"="true"} -ContentType "application/json" -WebSession $S1 -Body $body -Verbose
i've attempted various ways of defining an object with no luck. when I run the invoke with ($body | convertfrom-json) i get a different error message.
Invoke-RestMethod: {"statusCode":400,"error":"Bad Request","message":"[request body]: expected a plain object value, but found [null] instead."}
UPDATE: I did originally attempt to use the create index pattern API for elastic, but this results in a new index being created, not an opensearch index_pattern.
$body=#{
"index_pattern"= #{
"title"= "ai-summary"
}
}
$uri = "$baseuri/api/index_patterns/$indexid"
Invoke-RestMethod -Method Post -Uri $uri -Headers #{"osd-xsrf"="true"} -ContentType "application/json" -WebSession $S1 -Body ($body | ConvertTo-Json) -Verbose -Credential $credObject

Related

how to take latest backupid to JSON for restore data to an instance in a different project GCP

I am working on restore the latest backup restore data to an instance into a different project GCP in Windows power shell.
Using below queries using power shell getting backup id's list
$cred = gcloud auth print-access-token
$headers = #{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/source-project/instances/source-instance/backupRuns" | Select-Object -Expand Content
Restore using below command
$cred = gcloud auth print-access-token
$headers = #{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/target-project/instances/target-instance/restoreBackup" | Select-Object -Expand Content
In the request.json file updating latest backupid to restore latest backup up.
How to take latest backup id from the list automatically using power shell
Please help any one.

Gateway Binding using the API

I get an Error 400 (Bad Request) using the code below. I have looked at it for hours. Does anyone see anything wrong? Any suggestions? I've tried the suggestions in this link, but no luck: https://community.powerbi.com/t5/Developer/BindToGateway-Powershell-REST-API-POST-failing-Need-Help/m-p/852851
#Login to Power BI using PowerBIPS App registration.
Write-Host "Start Login"
$ApplicationId = "abc123"
$SecurePassword = "password" | ConvertTo-SecureString -AsPlainText -force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList
$ApplicationId, $SecurePassword
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId "ten123"
Write-Host "Login Success"
Write-Host "Get Workspace info for workspace: AC Common_TST"
$workspaceName = "AC Common_TST"
$datasetName = "DateDim"
$workspace = Get-PowerBIWorkspace -Name $workspaceName
$workspaceObject = ( Get-PowerBIWorkspace -Name "AC Common_TST" )
$groupid=$workspaceObject.id
$newGatewayId = "gatewayid" # the ID of PRDGateway1
$dataset = Get-PowerBIDataset -WorkspaceId $workspace.Id | Where-Object Name -eq $datasetName
$datasetid = $dataset.id
$workspaceId = $workspace.Id
Write-Host "GroupID is "$workspace.id
Write-Host "Dataset ID is "$dataset.id
# POST body
$postParams = #'
{
"gatewayObjectId": "gatewayid",
"datasourceObjectIds": [
"dsid"
]
}
'#
$jsonPostBody = $postParams | ConvertTo-JSON
$uri = "https://api.powerbi.com/v1.0/myorg/groups/$workspaceid/datasets/$datasetId/Default.BindToGateway"
Invoke-PowerBIRestMethod -URL $uri -Method POST -Body $jsonPostBody
Resolve-PowerBIError -Last

Getting Tests Run on Build with Azure DevOps REST APIs

Is there a REST API for getting the number of tests run/failed for a build?
I see there's one for getting Code Coverage but unless I am missing something obvious I can't find an API for just getting the number of tests run for a build.
Looks like that all APIs available to get Test Results require a test runid, but I only have a buildid.
You should try to use the Runs - Query API. Pass the optional build id.
GET https://dev.azure.com/{organization}/{project}/_apis/test/runs?minLastUpdatedDate={minLastUpdatedDate}&maxLastUpdatedDate={maxLastUpdatedDate}&buildIds={buildIds}&api-version=6.0
It's worth noting that you can customize the title of the run (with the build number included) by setting that on the task step of the pipeline.
I'm not sure if there is endpoint dedicated for tests but you can get tests from logs
$AzureDevOpsAuthenicationHeader = #{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$(System.AccessToken)")) }
$uri = "https://dev.azure.com/$(devopsAccount)/$(projectName)/_apis/build/builds/$(Build.BuildId)/logs/$(logId)?api-version=5.1"
Write-Host $uri
# Invoke the REST call
$result = Invoke-RestMethod -Uri $uri -Method Get -Headers $AzureDevOpsAuthenicationHeader
Write-Host $result
$lines = $result.Split([Environment]::NewLine)
$passed = 0;
$failed = 0;
foreach($line in $lines) {
if ($line -match "Passed:.(\d+)") {
$passed = $matches[1]
}
if ($line -match "Failed:.(\d+)") {
$failed = $matches[1]
}
}
echo $passed
echo $failed
You need to pass your build id and log id. To get log id please get all logs and go trough them and find the one with your task with tests.
In terms of buildId vs runId, they are the same. I mean buildId = runId. With newer syntax there were a change in nomenclature.
You can use the Runs Query Api as Matt mentioned. However i found the buildId query parameter didnot work probably. You may need to filter the api results by the buildId. See below example:
$connectionToken="Personal Access Token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$url = "https://dev.azure.com/{organization}/{project}/_apis/test/runs?minLastUpdatedDate=2020-10-20&maxLastUpdatedDate=2020-10-22&api-version=6.0"
$results= Invoke-RestMethod -Uri $trurl -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
$run = $results.value | Where-Object{$_.buildConfiguration.id -eq $buildId}
$runId = $run.id
You can also check out Runs List Api. I found the buildId was always appended to the run title. You can filter api results by the run name. See below:
$connectionToken="Personal Access Token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$url = "https://dev.azure.com/{organization}/{project}/_apis/test/runs?api-version=6.0"
$results= Invoke-RestMethod -Uri $trurl -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
$run = $results.value | Where-Object{$_.name -match $buildId}
$runId = $run.id

How do I get the code reviewer(s) of a build in Azure DevOps pipeline?

Given a build id, how do I get the code reviewer(s) name in azure DevOps pipeline?
Assume, the build was off of a master branch - which devs merge their feature branch after code has been reviewed in a pull request. This is the policy and no one directly commit their change into master. So that means, every build has a code reviewer behind it. How do i get that?
Thanks!
You can use below Rest api to get the PR reviewers.
1, First call below build rest api with the buildId. And in the response you will get the commit id from the build's sourceVersion and the repository id.
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1
2, After you get the commit id and repository id. You can call commit rest api to get the associated PR id from the comments in the response.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=5.1
3, Then Call pull request reviewer rest api to get the Reviewers.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/reviewers?api-version=5.1
Below is the example scripts in powershell. See this link to get a Personal access token
$buildId= " "
$burl =" https://dev.azure.com/OrgName/ProjName/_apis/build/builds/$($buildId)?api-version=5.1"
$PAT="personel access token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$buildInfo = Invoke-RestMethod -Uri $curl -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
#get CommitId and repoId
$commitId = $buildInfo.sourceVersion
$repoId=$buildInfo.repository.id
#commit rest api
$curl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($repoId)/commits/$($commitId)?api-version=5.1"
$commitInfo = Invoke-RestMethod -Uri $curl -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
#get PR id
$prId = $commitInfo.comment.split(" ")[2].TrimEnd(":")
$prurl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($repoId)/pullRequests/$($prId)/reviewers?api-version=5.1"
Invoke-RestMethod -Uri $prurl -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
If you can find the build from the pipeline runs history in the UI page with a give buildId. It will be much easier. You can get the PR id from the title directly. See below pic.
You can also click on the commit id shown on above screenshot, to see the details of the commit, where you will get the associated PR.
Here is what I have finally working. Took Levi's code snippet above and just fixed a line to get pull request id working in various scenarios. Kudos to Levi's for helping! Hope it helps someone.
$PAT="personel access token"
$base64EncodedPAT = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$basicAuth = #{Authorization = "Basic $base64EncodedPAT" }
$buildId= "..."
function GetCodeReviewers() {
#Get build info
$buildUrl = "https://dev.azure.com/OrgName/ProjName/_apis/build/builds/$($buildId)?api-version=5.1"
$buildInfo = Invoke-RestMethod -Method Get -Uri $buildUrl -Headers $basicAuth
# Get Commit Info
$commitUrl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($buildInfo.repository.id)/commits/$($buildInfo.sourceVersion)?api-version=5.1"
$commitInfo = Invoke-RestMethod -Uri $commitUrl -Method Get -Headers $basicAuth
#Get Code Reviewers
$comment = $commitInfo.comment
#$pullRequestId = $comment.split(" ")[2].TrimEnd(":") # it turns out, the 3rd item may not always be the PullRequestID so the next line may not work for all scenarios
#note that, a comment could come as follows:
# case 1: Merge PR 1234: some other text here including story or bug numbers
# case 2: Merge pull request 1234 some additional text goes here including story or bug numbers
# The following will pick the first number - which I assume will always be the PullRequestID
$pullRequestId = $null
$pullRequestId = $comment.Replace(':', '').Split(" ").Trim() | Where-Object {[int]::TryParse($_, $pullRequestId)} | Select-Object -First 1
$pullRequestUrl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($buildInfo.repository.id)/pullRequests/$($pullRequestId)/reviewers?api-version=5.1"
$reviewers = Invoke-RestMethod -Uri $pullRequestUrl -Method Get -Headers $basicAuth
return $reviewers.value
}

How to send json data using powershell from S3 to Neptune instance?

I have a turtle file that I wish to send from S3 server to the Neptune instance on powershell. Below is the command I am using
Invoke-RestMethod -Uri http://edwardspoc.civwhymjvz19.us-east-1.neptune.amazonaws.com:8182 -ContentType application/json -body '
{
"source" : "s3://cedwardsneptune/ATC.ttl",
"format" : "turtle",
"iamRoleArn" : "arn:aws:s3:::cedwardsneptune",
"region" : "us-east-1",
"failOnError" : "FALSE",
"parserConfiguration" : {
"namedGraphUri" : "http://purl.bioontology.org/ontology/ATC"
}
}'
This is giving me the following error
Invoke-RestMethod : Cannot send a content-body with this verb-type.
I made subtle changes but nothing seems to be working. Any help is appreciated !
Error you having says that you are trying to send a BODY when your REQUEST VERB does not allow it.
Basically you are doing GET and trying to send a body. When GET verb is not allowed to send a body part.
Probably you need to use POST method (check specific webservice documentation)
For that I recommend you next format:
$Cred = Get-Credential (If you could have)
$Url = "http://edwardspoc.civwhymjvz19.us-east-1.neptune.amazonaws.com:8182"
$Body = '{
"source" : "s3://cedwardsneptune/ATC.ttl",
"format" : "turtle",
"iamRoleArn" : "arn:aws:s3:::cedwardsneptune",
"region" : "us-east-1",
"failOnError" : "FALSE",
"parserConfiguration" : {
"namedGraphUri" : "http://purl.bioontology.org/ontology/ATC"
}
}'
Invoke-RestMethod -Method 'Post' -Uri $url -Credential $Cred -ContentType $contentType -Body $body -ContentType 'application/json' -OutFile output.csv
(*)Of course -OutFile is optional
And about JSON There is a cmdlet called ConvertFrom-JSON you can use commonly when you need to parse an output(that is a JSON).
p.e:
Invoke-RestMethod -Method 'Post' -Uri $url -Credential $Cred -Body $body |
ConvertFrom-JSON | Select field1, field2, field3
If you expect an output (json) having field1, field2, field3 as fields