Pages

Thursday, March 20, 2025

OAC Agents set up through API

 

Step 1: Set Up API Authentication

  1. Obtain API Credentials:
    • Ensure you have an Oracle Cloud account with necessary permissions.
    • Obtain authentication credentials (Client ID, Client Secret, or JWT Token).
  2. Authenticate with Oracle API Gateway:
    • Use OAuth 2.0 or Basic Authentication.
    • Obtain an access token using the /v1/auth/token endpoint.

Step 2: Check Available OAC Agents

  1. List existing agents:
    • Use the GET /api/v1/agents endpoint to retrieve the available agents.
    • Validate the agent you need to configure or create a new one.

Step 3: Create an OAC Agent

  1. Define Agent Properties:
    • Set name, description, execution frequency, and data source.
    • Define scheduling options (time-based or event-based triggers).
  2. API Call to Create an Agent:
    • Use POST /api/v1/agents with a JSON payload defining the agent.
    • Example JSON:

      {
      "name": "Sales Data Refresh",
      "description": "Automates sales data pipeline refresh",
      "schedule": {
      "frequency": "daily",
      "startTime": "08:00:00"
      },
      "enabled": true
      }

  • Execute the API call and confirm the agent is created successfully.


Step 4: Assign Agent to a Job or Task
  1. Define the task to execute (data pipeline, report, or model update).
  2. Use PUT /api/v1/agents/{agentId}/jobs to attach a job to the agent.
  3. Example Payload
  1. { "jobId": "data_refresh_task", "parameters": { "source": "Sales_DB", "destination": "OAC_Reports" } }


Step 5: Test and Run the Agent

  1. Trigger execution manually (optional) using:
  2. POST /api/v1/agents/{agentId}/run
  3. Monitor execution logs:Use GET /api/v1/agents/{agentId}/logs
  4. Check for errors and resolve issues if needed.

Step 6: Manage and Monitor OAC Agents

  1. Update an agent (PUT /api/v1/agents/{agentId})
  2. Disable/Enable an agent (PATCH /api/v1/agents/{agentId})
  3. Delete an agent (DELETE /api/v1/agents/{agentId})

 Highlevel Steps



Step 1: Authenticate & Get Access Token

Use OAuth 2.0 or Basic Authentication to obtain an access token.

API Request (OAuth 2.0 Example):

curl -X POST https://<OAC_URL>/api/v1/auth/token \

-H "Content-Type: application/x-www-form-urlencoded" \

-d "grant_type=client_credentials&client_id=<YOUR_CLIENT_ID>&client_secret=<YOUR_CLIENT_SECRET>"

Response Example:

json


  "access_token": "eyJhbGciOiJIUz...",

  "token_type": "Bearer",

  "expires_in": 3600

}

Save the access_token for subsequent API calls.

Step 2: List Existing OAC Agents

Retrieve existing agents to check if one already exists before creating a new one.


API Request:


curl -X GET https://<OAC_URL>/api/v1/agents \

-H "Authorization: Bearer <ACCESS_TOKEN>" \

-H "Content-Type: application/json"

Response Example:


json


[

  {

    "id": "12345",

    "name": "Sales Data Refresh",

    "status": "ACTIVE"

  }

]

Step 3: Create an OAC Agent

If no existing agent matches your needs, create a new one.

API Request:


curl -X POST https://<OAC_URL>/api/v1/agents \

-H "Authorization: Bearer <ACCESS_TOKEN>" \

-H "Content-Type: application/json" \

-d '{

  "name": "Sales Data Refresh",

  "description": "Automates sales data pipeline refresh",

  "schedule": {

    "frequency": "daily",

    "startTime": "08:00:00"

  },

  "enabled": true

}'

Response Example:

json


{

  "id": "67890",

  "name": "Sales Data Refresh",

  "status": "CREATED"

}

Step 4: Assign a Job to the Agent

Now, assign a data refresh or reporting job to the agent.

API Request:


curl -X PUT https://<OAC_URL>/api/v1/agents/67890/jobs \

-H "Authorization: Bearer <ACCESS_TOKEN>" \

-H "Content-Type: application/json" \

-d '{

  "jobId": "data_refresh_task",

  "parameters": {

    "source": "Sales_DB",

    "destination": "OAC_Reports"

  }

}'

Response Example:


json


{

  "message": "Job assigned successfully."

}

Step 5: Manually Trigger the Agent

If needed, manually start the agent instead of waiting for the scheduled time.

API Request:


curl -X POST https://<OAC_URL>/api/v1/agents/67890/run \

-H "Authorization: Bearer <ACCESS_TOKEN>" \

-H "Content-Type: application/json"

Response Example:


json

Copy

Edit

{

  "message": "Agent execution started."

}

Step 6: Monitor Agent Execution Logs

Retrieve logs to monitor execution status and troubleshoot errors.

API Request:

curl -X GET https://<OAC_URL>/api/v1/agents/67890/logs \

-H "Authorization: Bearer <ACCESS_TOKEN>" \

-H "Content-Type: application/json"

Response Example:

json


  "logs": [

    {

      "timestamp": "2025-03-20T08:00:05Z",

      "status": "SUCCESS",

      "message": "Sales data refreshed successfully."

    }

  ]

}

Step 7: Update, Disable, or Delete an Agent

Update Agent Settings:


curl -X PUT https://<OAC_URL>/api/v1/agents/67890 \

-H "Authorization: Bearer <ACCESS_TOKEN>" \

-H "Content-Type: application/json" \

-d '{

  "schedule": {

    "frequency": "weekly",

    "startTime": "09:00:00"

  }

}'

Disable an Agent:


curl -X PATCH https://<OAC_URL>/api/v1/agents/67890 \

-H "Authorization: Bearer <ACCESS_TOKEN>" \

-H "Content-Type: application/json" \

-d '{"enabled": false}'

Delete an Agent:


curl -X DELETE https://<OAC_URL>/api/v1/agents/67890 \

-H "Authorization: Bearer <ACCESS_TOKEN>"

Automation Script (Python)

Here’s a Python script to automate OAC Agent setup using APIs.


python


import requests

OAC_URL = "https://<OAC_URL>"

CLIENT_ID = "<YOUR_CLIENT_ID>"

CLIENT_SECRET = "<YOUR_CLIENT_SECRET>"


def get_access_token():

    url = f"{OAC_URL}/api/v1/auth/token"

    data = {"grant_type": "client_credentials", "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET}

    response = requests.post(url, data=data)

    return response.json().get("access_token")


def create_agent(token):

    url = f"{OAC_URL}/api/v1/agents"

    headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}

    data = {

        "name": "Sales Data Refresh",

        "description": "Automates sales data pipeline refresh",

        "schedule": {"frequency": "daily", "startTime": "08:00:00"},

        "enabled": True

    }

    response = requests.post(url, json=data, headers=headers)

    return response.json()


def assign_job_to_agent(token, agent_id):

    url = f"{OAC_URL}/api/v1/agents/{agent_id}/jobs"

    headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}

    data = {"jobId": "data_refresh_task", "parameters": {"source": "Sales_DB", "destination": "OAC_Reports"}}

    response = requests.put(url, json=data, headers=headers)

    return response.json()


def trigger_agent(token, agent_id):

    url = f"{OAC_URL}/api/v1/agents/{agent_id}/run"

    headers = {"Authorization": f"Bearer {token}"}

    response = requests.post(url, headers=headers)

    return response.json()


if __name__ == "__main__":

    token = get_access_token()

    agent = create_agent(token)

    agent_id = agent.get("id")

    

    if agent_id:

        assign_job_to_agent(token, agent_id)

        trigger_response = trigger_agent(token, agent_id)

        print("Agent triggered:", trigger_response)

    else:

        print("Failed to create agent.")

Summary

✔ Authenticate and obtain an access token

✔ Create an OAC agent

✔ Assign a job to the agent

✔ Trigger the agent manually

✔ Monitor logs

✔ Update, disable, or delete the agent

No comments:

Post a Comment