Skip to content
Permalink
Browse files

adding of function for delete object

  • Loading branch information
skublik committed Oct 3, 2018
1 parent 38f496d commit c94165719e8d818387692b4ba0ca7236ff1df130
Showing with 60 additions and 14 deletions.
  1. +4 −3 demo/complex/tests/main.bats
  2. +56 −11 library.bash
@@ -53,7 +53,7 @@ load ../../../library
add_object users /tmp/test110.xml
rm /tmp/test110.xml
search_and_check_object users test110
# TODO delete user after
delete_object_by_name users test110
}

@test "200 Upload objects" {
@@ -105,12 +105,13 @@ load ../../../library
rm /tmp/testuser230.xml
search_and_check_object users TestUser230

execute_bulk_action tests/resources/bulk-action/recom-role-grouper-sysadmin.xml

execute_bulk_action tests/resources/bulk-action/recom-role-grouper-sysadmin.xml
execute_bulk_action tests/resources/bulk-action/assign-role-grouper-sysadmin-to-test-user.xml

check_ldap_account_by_user_name TestUser230 complex_directory_1
check_of_ldap_membership TestUser230 sysadmingroup complex_directory_1

delete_object_by_name users TestUser230
}


@@ -12,7 +12,7 @@ function generic_wait_for_log () {
FAILURE="$4"
ADDITIONAL_CONTAINER_NAME=$5
ATTEMPT=0
MAX_ATTEMPTS=20
MAX_ATTEMPTS=40
DELAY=10

until [[ $ATTEMPT = $MAX_ATTEMPTS ]]; do
@@ -115,27 +115,29 @@ function get_and_check_object () {
function add_object () {
local TYPE=$1
local FILE=$2
TMPFILE=$(mktemp /tmp/execbulkaction.XXXXXX)
echo "Adding to $TYPE from $FILE..."

local response=$(curl -k -sD - --silent --write-out "%{http_code}" --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/$TYPE" -d @$FILE)
local http_code=$(sed '$!d' <<<"$response")
curl -k -sD - --silent --write-out "%{http_code}" --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/$TYPE" -d @$FILE >$TMPFILE
local HTTP_CODE=$(sed '$!d' $TMPFILE)

if [ "$http_code" -eq 201 ] || [ "$http_code" -eq 202 ]; then
if [ "$HTTP_CODE" -eq 201 ] || [ "$HTTP_CODE" -eq 202 ]; then

# get the real Location
local location=$(grep -oP "Location: \K.*" <<<"$response")
OID=$(sed 's/.*\///' <<<"$location")
OID=$(grep -oP "Location: \K.*" $TMPFILE | awk -F "$TYPE/" '{print $2}') || (echo "Couldn't extract oid from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)

echo "Oid created object: $OID"
rm $TMPFILE
return 0
else
echo "Error code: $http_code"
if [ "$http_code" -eq 500 ]; then
echo "Error code: $HTTP_CODE"
if [ "$HTTP_CODE" -ge 500 ]; then
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
else
local error_message=$(grep 'message' <<<"$response" | head -1 | awk -F">" '{print $2}' | awk -F"<" '{print $1}')
echo "Error message: $error_message"
local ERROR_MESSAGE=$(xmllint --xpath "/*/*[local-name()='error']/text()" $TMPFILE) || (echo "Couldn't extract error message from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
echo "Error message: $ERROR_MESSAGE"
fi
rm $TMPFILE
return 1
fi
}
@@ -153,6 +155,8 @@ function execute_bulk_action () {

local STATUS=$(xmllint --xpath "/*/*/*[local-name()='status']/text()" $TMPFILE) || (echo "Couldn't extract status from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
if [ $STATUS = "success" ]; then
local CONSOLE_OUTPUT=$(xmllint --xpath "/*/*/*[local-name()='consoleOutput']/text()" $TMPFILE) || (echo "Couldn't extract console output from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
echo "Console output: $CONSOLE_OUTPUT"
rm $TMPFILE
return 0
else
@@ -165,13 +169,54 @@ function execute_bulk_action () {

else
echo "Error code: $HTTP_CODE"
if [ "$http_code" -eq 500 ]; then
if [ "$HTTP_CODE" -ge 500 ]; then
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
else
local ERROR_MESSAGE=$(xmllint --xpath "/*/*[local-name()='error']/text()" $TMPFILE) || (echo "Couldn't extract error message from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
echo "Error message: $ERROR_MESSAGE"
fi
rm $TMPFILE
return 1
fi
}

function delete_object_by_name () {
local TYPE=$1
local NAME=$2
search_objects_by_name users $NAME
local OID=$(xmllint --xpath "/*/*[local-name()='object']/@oid" $SEARCH_RESULT_FILE | awk -F"\"" '{print $2}' ) || (echo "Couldn't extract oid from file:" ; cat $SEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE; return 1)
delete_object $TYPE $OID
}

function delete_object () {
local TYPE=$1
local OID=$2
echo "Deleting object with type $TYPE and oid $OID..."
TMPFILE=$(mktemp /tmp/delete.XXXXXX)

curl -k --silent --write-out "%{http_code}" --user administrator:5ecr3t -H "Content-Type: application/xml" -X DELETE "https://localhost:8443/midpoint/ws/rest/$TYPE/$OID" >$TMPFILE
local HTTP_CODE=$(sed '$!d' $TMPFILE)
sed -i '$ d' $TMPFILE

if [ "$HTTP_CODE" -eq 204 ]; then

echo "Object with type $TYPE and oid $OID was deleted"
rm $TMPFILE
return 0
else
echo "Error code: $HTTP_CODE"
if [ "$HTTP_CODE" -ge 500 ]; then
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
else
local ERROR_MESSAGE=$(xmllint --xpath "/*/*[local-name()='error']/text()" $TMPFILE) || (echo "Couldn't extract error message from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
echo "Error message: $ERROR_MESSAGE"
fi
rm $TMPFILE
return 1
fi
}



# Tries to find an object with a given name
# Results of the search are in the $SEARCH_RESULT_FILE

0 comments on commit c941657

Please sign in to comment.
You can’t perform that action at this time.