Schedule a SWIM Image Collection Job
Scenario Overview
This use case demonstrates how to schedule a SWIM (Software Image Management) job to upload a software image in CNC using different options:
- Uploading a file from your local system.
- Copying an image from an external server using a URL.
- Importing an image from a device. Once the image is uploaded, it will be parsed to verify its details, and it can then be used in other SWIM jobs.
The operation in the following API references is used in this example.
Prerequisites
Before running this example, ensure you have obtained an access token. Refer to the 'Getting Started' section of the CNC SWIM API documentation for instructions on obtaining the token.
Schedule Image Collection Job Using URL Option
Update the image_collection.json
input file with the required details (e.g., job name, URL, and collection parameters), then run the image_collection.sh
script from the example directory.
Navigate to the cnc-swim-api-examples
directory and run the image_collection.sh
script using the following command:
cd cnc-swim-api-examples;./image_collection.sh
Script Details
This script reads input from the image_collection.json file located in the input directory. Upon successful execution, a new collection job is scheduled and the output is saved to the image_collection_schedule_status.json file in the output directory.
#!/bin/bash
# Import common API functions and environment variables
. ./cnc-api-common.sh
image_collection() {
# Step 1: Ensure the CNC JWT is obtained by running the get-cnc-jwt.sh script.
# Read the JWT from the file and export it as the AUTH_TOKEN_HDR environment variable.
export_jwt
# STEP 1 : Create collection-status payload with collectionstatus `completed`, `failed`, or `inprogress`(input json file: CNC_API_INPUT)
# STEP 2 : Retrieve devices with collection status provided in the payload
# STEP 2.1 : Copy the output device list in CNC_API_OUTPUT file
CNC_API_URL=$CNC_SWIM_API_CTX/collect
CNC_API_INPUT="@$PRJ/input/image_collection.json"
CNC_API_OUTPUT="$PRJ/output/image_collection_schedule_status.json"
# Step 3: Replace the `jobName` field in the input JSON with a unique name using a timestamp.
# The `jq` command safely modifies the input file.
jq --arg ts "$(date +%s)" '.collectionJobDTO.jobDTO.jobName = "Job_Swim_Collection_" + $ts' input/image_collection.json > temp.json && mv temp.json input/image_collection.json
# Step 4: Send the POST request to schedule the image collection job and store the response in the output file.
http_post $CNC_API_URL $CNC_API_INPUT $CNC_API_OUTPUT
}
image_collection