Bash script for creating multiple projects in harbor - linux

I wanted to create multiple projects in harbor with the API. so i have 5 projects to create in the text file
projects.txt
projects: abcd
efgh
ijkl
mnop
qrst
create_projects.sh
#!/bin/bash
DIR=$( cd "$( dirname "$BASH_SOURCE[0]}" )" && pwd )
while read p
do
curl --location --request POST 'https://harbor.rl.com/api/v2.0/projects' --header 'Content-Type: application/json' --header 'X-Resource-Name-In-Location: true' --header 'Authorization: Basic jhgkyyktfkghfgfhfgh=' --data-raw '{
"project_name": $p,
"public": true,
"metadata": {
"public": "true",
"prevent_vul": "true",
"auto_scan": "true"
},
"storage_limit": -1
}'
done < "$DIR/../assets/projects.txt"
i created bash script like this. please suggest how we gonna create these five projects in the harbor repo through the script

Related

github's post pulls api throwing invalid head error for a forked head in same org

repo1: https://github.com/ORG_SPACE/MAIN_REPO
repo2(forked from repo1 in same workspace): https://github.com/ORG_SPACE/FORKED_REPO
feature branch created in forked repo: https://github.com/ORG_SPACE/FORKED_REPO/tree/FEATURE_BRANCH
Authorization details: used new finegrained PAT having pull:read&write,contents:read,Metadata:read access of both repos MAIN_REPO,FORKED_REPO
ISSUE: getting 422 error response
{
"message": "Validation Failed",
"errors": [
{
"resource": "PullRequest",
"field": "head",
"code": "invalid"
}
],
"documentation_url": "https://docs.github.com/rest/reference/pulls#create-a-pull-request"
}
while hitting below curl to raise pull request to merge 'ORG_SPACE/FORKED_REPO/tree/FEATURE_BRANCH' into 'ORG_SPACE/MAIN_REPO/tree/master'
curl --location --request POST 'https://api.github.com/repos/ORG_SPACE/MAIN_REPO/pulls' \
--header 'Authorization: Bearer MY_PAT' \
--header 'Accept: application/vnd.github+json' \
--header 'Content-Type: application/json' \
--data-raw '{
"title": "test",
"head": "ORG_SPACE:FEATURE_BRANCH",
"base": "master"
}'
tried this head value : "ORG_SPACE:FORKED_REPO/FEATURE_BRANCH" but didn't work.
raising pull request with same api working fine for both repos while head and base are in same repo.

How to use REST API to create a file in GitLab and if a file with the same name existed, overwrite the old file

How to use REST API to create a file in GitLab and if a file with the same name existed, overwrite the old file. Does the "create" action automatically does this for me?
https://docs.gitlab.com/ee/api/commits.html
E.g. I wanna upload XXX/YYY/A.txt, if XXX/YYY/A.txt already existed, replace the A.txt with the new A.txt, if not create one with the provided content.
When you make a request to Commits API you will have to specify the action, if you want to create a new file the action would be create.
If the file already exists, gitlab will deny the request and respond with
{"message":"A file with this name already exists"}
So the action in the payload should be consistent with the repo state.
We could use a script here that it will try to commit the file with 2 payloads.
One specifying the create action and the other the update action.
create.json
{
"branch": "target branch",
"commit_message": "some commit message",
"actions": [
{
"action": "create",
"file_path": "XXX/YYY/A.txt",
"content": "some content new"
}]
}
update.json
{
"branch": "target branch",
"commit_message": "some commit message",
"actions": [
{
"action": "update",
"file_path": "XXX/YYY/A.txt",
"content": "some content new"
}]
}
The script will originally try the create action, if it fails it will try the update action. E.g.
#!/bin/bash
curl -s -w -XPOST --header "Content-Type: application/json" --header "PRIVATE-TOKEN: <access_token" --data "#create.json" https://gitlab.com/api/v4/projects/<project_id>/repository/commits | grep -o message
if [ $? -eq 0 ]
then
curl -s -w -XPOST --header "Content-Type: application/json" --header "PRIVATE-TOKEN: <access_token" --data "#update.json" https://gitlab.com/api/v4/projects/<project_id>/repository/commits
fi

how to use loop with some conditions in shell script

I need to run a curl command several times, and check the result every time. I used a for loop to check the output from curl, and I want to exit the loop when the correct status is reached.
The very first time the curl command is run, it will show me this output :
{
"name": "organizations/org_name/operations/long_running_operation_ID",
"metadata": {
"#type": "type.googleapis.com/google.cloud.apigee.v1.OperationMetadata",
"operationType": "INSERT",
"targetResourceName": "organizations/org_name",
"state": "IN_PROGRESS"
}
}
It takes time to complete, that's why it says IN_PROGRESS. It took around 30 to 60 sec, and if we run the same curl command after 30 to 60 sec then it will show the output below :
{
"error": {
"code": 409,
"message": "org graceful-path-310004 already associated with another project",
"status": "ALREADY_EXISTS",
"details": [
{
"#type": "type.googleapis.com/google.rpc.RequestInfo",
"requestId": "11430211642328568827"
}
]
}
}
Here is the script I have so far :
test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.error.status')
echo $test
while [ $test = "ALREADY EXISTS" || $test != "IN_PROGRESS" ]
do
echo $test
echo "Organization is creating"
test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.error.status')
done
echo "exit from loop"
To reduce the duplicated code, use functions.
Also, best practice to use jq to generate json -- it will safely handle any embedded quotes in the data:
json() {
jq --arg name "$ORG_NAME" \
--arg displayName "$ORG_DISPLAY_NAME" \
--arg description "$ORGANIZATION_DESCRIPTION" \
--arg runtimeType "$RUNTIMETYPE" \
--arg analyticsRegion "$ANALYTICS_REGION" \
--null-input --compact-output \
'$ARGS.named'
}
errorStatus() {
curl -H "Authorization: Bearer $TOKEN" \
-X POST \
-H "content-type:application/json" \
-d "$(json)" \
"${URL}?parent=projects/$PROJECT_ID" \
| jq -r '.error.status'
}
while true; do
status=$(errorStatus)
[ "$status" = "ALREADY EXISTS" ] && break
done
echo "exit from loop"
The while true; do some code; condition && break; done bit is the bash version of a do-while loop

parse JSON out from curl response using Sed or awk and assign to variable for later use

I am writing a bash script to be run on a linux appliance so I do not have access to install jq, I need to be able to use native Linux tools
curl --location --request POST "https://api-mp.meraki.com/api/v1/networks/12345/switch/stacks/12345/routing/interfaces" \
--header "X-Cisco-Meraki-API-Key: 123key" \
--header "Content-Type: application/json" \
--data '{
"name": "L3 int",
"subnet": "192.168.249.0/24",
"interfaceIp": "192.168.249.2",
"multicastRouting": "disabled",
"vlanId": 140,
"ospfSettings": {
"area": "0",
"cost": 1,
"isPassiveEnabled": true
}
}' | sed -n 's|.*"interfaceID":"\([^"]*\)".*|\1|p' > $newVar
the response from the server returns this:
{
"interfaceId": "12345",
"name": "PA L3 DR Int",
"subnet": "192.168.249.0/24",
"interfaceIp": "192.168.249.2",
"multicastRouting": "disabled",
"vlanId": 140,
"ospfSettings": {
"area": "0",
"cost": 1,
"isPassiveEnabled": true
}
}
I then need to be able to access the interfaceID new variable in a query string paramter
You just got the case wrong (ID vs Id in interfaceId) and forgot about the space after :, but you were close:
$ sed -n 's|.*"interfaceId": *"\([^"]*\)".*|\1|p' file
12345
Using cat file in place of your curl command to show how to save that to a variable in case you don't know:
$ var=$(cat file | sed -n 's|.*"interfaceId": *"\([^"]*\)".*|\1|p')
$ echo "$var"
12345
Could you please try following. Tested successfully in link
https://ideone.com/EsdCQp
your_command |
awk '
/"interfaceId": "/{
gsub(/[^0-9]+|"\,/,"")
print
}
'

escape variable in bash script curl request

I am trying to curl via bash script but unable to pass the value of var1 in curl request and getting error upon execution ...
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d '
{"msgtype": "text",
"text": {
"content": $var1
}
}'
Your variable $var1 doesn't get expanded by the shell because it is inside single quote '.
You need to use double quote to let bash do the parameter expansion, and escape your json data:
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d "
{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"$var1\"
}
}"
Or you can use inline document (without the escaping hell, but the command becomes awkward):
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d "$(cat <<EOT
{
"msgtype": "text",
"text": {
"content": "$var1"
}
}
EOT
)"
In general, don't use parameter expansion to define JSON dynamically like this. There is no guarantee that your template, combined with the contents of the variable, produces well-formed JSON. (This is for the same reasons you don't use string interpolation to create dynamic SQL queries.) Use a tool like jq instead.
curl 'https://link' ... \
-d "$(jq --argjson x "$var" \
-n '{msgtype: "text", text: {content: $x}}')"
Use below script.
https://aaa.com' -H 'Content-Type: application/json' -d '{"msgtype": "text", "text": { "content": '"'$var1'"' }}'

Resources