-
Notifications
You must be signed in to change notification settings - Fork 11
ECI-395 Search Resources for Logging #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f18ea00
e29b294
fcf68c9
e18b960
3eb865b
ef81ad9
d519333
06fdbfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "external" "logging_services" { | ||
program = ["bash", "logging_services.sh"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
|
||
output_file="oci_logging_services.json" | ||
echo "[]" > $output_file # Initialize the output file with an empty JSON array | ||
|
||
# Fetch logging services using OCI CLI | ||
response=$(oci logging service list --all --query "data[].{id:id, resourceTypes:\"resource-types\"[].{name:name, categories:categories[].{name:name}}}" --output json) | ||
|
||
# Write the response to the output file | ||
echo "$response" > "$output_file" | ||
|
||
# Output the response in a valid JSON map for Terraform's external data source | ||
content=$(jq -c . < "$output_file") # Ensure the file's content is compact JSON | ||
rm -f "$output_file" | ||
echo "{\"content\": \"$(echo "$content" | sed 's/"/\\"/g')\"}" # Escape quotes for JSON compatibility |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "external" "find_resources" { | ||
program = ["bash", "modules/resourcediscovery/search_resources.sh", var.compartment_ocid, var.group_id, local.resource_types_string] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
locals { | ||
resource_types_string = join(",", [for rt in var.resource_types : rt]) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "response" { | ||
value = jsondecode(data.external.find_resources.result["content"]) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/bin/bash | ||
|
||
export COMPARTMENT_ID="${1}" | ||
export GROUP_ID="${2}" | ||
export RESOURCE_TYPES="${3}" | ||
|
||
output_file="oci_resources_${GROUP_ID}_${COMPARTMENT_ID}.json" | ||
echo "[]" > $output_file # Initialize the output file with an empty JSON array | ||
IFS=',' read -ra types <<< "$RESOURCE_TYPES" | ||
|
||
# Perform OCI CLI query for each resource type | ||
for rt in "${types[@]}"; do | ||
# Initialize variables for pagination | ||
next_page="init" | ||
|
||
# Loop through pages | ||
while [ "$next_page" != "null" ]; do | ||
if [ "$next_page" == "init" ]; then | ||
# First query without next page token | ||
response=$(oci search resource structured-search --query-text \ | ||
"QUERY $rt resources where compartmentId = '$COMPARTMENT_ID' && lifeCycleState != 'TERMINATED' && lifeCycleState != 'FAILED'" \ | ||
--output json) | ||
else | ||
# Query with next page token | ||
response=$(oci search resource structured-search --query-text \ | ||
"QUERY $rt resources where compartmentId = '$COMPARTMENT_ID' && lifeCycleState != 'TERMINATED' && lifeCycleState != 'FAILED'" \ | ||
--page "$next_page" \ | ||
--output json) | ||
fi | ||
|
||
# Check if the response contains an error | ||
if [ -z "$response" ]; then | ||
# Log ServiceError responses | ||
echo "ServiceError for $GROUP_ID, resource type: $rt" >> error.log | ||
break | ||
fi | ||
|
||
# Extract next page token | ||
next_page=$(echo "$response" | jq -r '."opc-next-page" // "null"') | ||
|
||
# Extract and transform items | ||
items=$(echo "$response" | jq --arg group_id "$GROUP_ID" -c '[.data.items[] | {compartmentId: ."compartment-id", displayName: ."display-name", identifier: ."identifier", resourceType: ."resource-type", groupId: $group_id}]') | ||
# Append items to the output file if not empty | ||
if [ "$(echo "$items" | jq length)" -gt 0 ]; then | ||
temp_items_file="temp_items_$${GROUP_ID}_$${COMPARTMENT_ID}.json" | ||
echo "$items" > "$temp_items_file" | ||
jq -s '.[0] + .[1]' "$output_file" "$temp_items_file" > tmp_${GROUP_ID}_${COMPARTMENT_ID}.json && mv tmp_${GROUP_ID}_${COMPARTMENT_ID}.json "$output_file" | ||
rm -f "$temp_items_file" | ||
fi | ||
done | ||
done | ||
|
||
# Read the file's content and return it as a JSON-encoded string | ||
content=$(jq -c . < "$output_file") | ||
rm -f "$output_file" | ||
echo "{\"content\": \"$(echo "$content" | sed 's/"/\\"/g')\"}" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
variable "group_id" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if "service_id" or "service_name"? might be a better name for this? I see "group_id" as an OCI Group's OCID (eg. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The purpose of this variable is to specify This ensures that the abstraction for any caller code to use |
||
type = string | ||
description = "Unique Name for the group of resource types" | ||
} | ||
|
||
variable "compartment_ocid" { | ||
type = string | ||
description = "The OCID of the compartment where resources exist" | ||
} | ||
|
||
variable "resource_types" { | ||
type = list(string) | ||
description = "List of resource types" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.