File size: 2,818 Bytes
19da2df
 
 
 
 
4e0c319
19da2df
4e0c319
19da2df
 
 
4e0c319
19da2df
 
4e0c319
19da2df
 
 
 
 
2ffecbc
19da2df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ffecbc
19da2df
 
 
2ffecbc
19da2df
2ffecbc
19da2df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
947ade2
19da2df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from fastapi import FastAPI, HTTPException
import requests
import oci
import json
from datetime import datetime

app = FastAPI()

# Oracle Cloud Configurations (Ensure your OCI config file is set up correctly)
config = oci.config.from_file("~/.oci/config", "DEFAULT")
signer = oci.auth.signers.get_resource_principals_signer()

# Endpoint URL for Oracle AI Service (Replace region appropriately)
oci_endpoint = "https://anomalydetection.aiservice.{{region}}.oci.oraclecloud.com/20210101/aiPrivateEndpoints"

# Request headers including authentication (ensure the correct signing/authentication process)
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer {access_token}"  # Use the appropriate method to authenticate
}

@app.post("/create-private-endpoint")
async def create_private_endpoint(compartment_id: str, subnet_id: str, display_name: str, dns_zones: list):
    """Creates a private reverse connection endpoint in Oracle Cloud"""
    
    # Prepare the request body
    request_body = {
        "compartmentId": compartment_id,
        "subnetId": subnet_id,
        "displayName": display_name,
        "dnsZones": dns_zones,
        "definedTags": {},
        "freeformTags": {}
    }
    
    response = requests.post(
        oci_endpoint,
        headers=headers,
        data=json.dumps(request_body)
    )
    
    if response.status_code == 202:
        return {"message": "Private endpoint created successfully", "status": response.status_code}
    else:
        raise HTTPException(status_code=response.status_code, detail=response.json())

@app.put("/update-private-endpoint/{endpoint_id}")
async def update_private_endpoint(endpoint_id: str, display_name: str, dns_zones: list):
    """Updates a private reverse connection endpoint"""
    
    update_url = f"{oci_endpoint}/{endpoint_id}"
    
    request_body = {
        "displayName": display_name,
        "dnsZones": dns_zones,
        "definedTags": {},
        "freeformTags": {}
    }
    
    response = requests.put(
        update_url,
        headers=headers,
        data=json.dumps(request_body)
    )
    
    if response.status_code == 202:
        return {"message": "Private endpoint updated successfully", "status": response.status_code}
    else:
        raise HTTPException(status_code=response.status_code, detail=response.json())

@app.get("/get-private-endpoint/{endpoint_id}")
async def get_private_endpoint(endpoint_id: str):
    """Retrieves a private reverse connection endpoint by its ID"""
    
    get_url = f"{oci_endpoint}/{endpoint_id}"
    
    response = requests.get(
        get_url,
        headers=headers
    )
    
    if response.status_code == 200:
        return response.json()
    else:
        raise HTTPException(status_code=response.status_code, detail=response.json())