From 612446c3c992890d36880e29a234bd464072f9ef Mon Sep 17 00:00:00 2001 From: Owen Date: Thu, 11 Sep 2025 11:42:37 -0700 Subject: [PATCH] Work accross sites? --- blueprint.py | 15 +++------- blueprint.yaml | 45 ++++++++++++++-------------- server/lib/blueprints/resources.ts | 14 +++++++-- server/routers/org/applyBlueprint.ts | 1 + 4 files changed, 40 insertions(+), 35 deletions(-) diff --git a/blueprint.py b/blueprint.py index 6490ebd5..b3017b5b 100644 --- a/blueprint.py +++ b/blueprint.py @@ -29,18 +29,11 @@ def convert_and_send(file_path, url, headers): # This will be used to ensure the YAML is valid before sending parsed_yaml = yaml.safe_load(yaml_content) - # Create the JSON payload. The curl request shows the value - # of "blueprint" is a string, which means the raw YAML content - # should be sent as a string value for that key. - json_payload = { - "blueprint": yaml_content - } + # convert the parsed YAML to a JSON string + json_payload = json.dumps(parsed_yaml) - # Convert the payload to a JSON string - json_string = json.dumps(json_payload) - - # Base64 encode the JSON string - encoded_json = base64.b64encode(json_string.encode('utf-8')).decode('utf-8') + # Encode the JSON string to Base64 + encoded_json = base64.b64encode(json_payload.encode('utf-8')).decode('utf-8') # Create the final payload with the base64 encoded data final_payload = { diff --git a/blueprint.yaml b/blueprint.yaml index da1498cd..bb6cd531 100644 --- a/blueprint.yaml +++ b/blueprint.yaml @@ -1,10 +1,10 @@ resources: - resource-nice-id: + resource-nice-id-duce: name: this is my resource protocol: http full-domain: level1.test3.example.com - # host-header: example.com - # tls-server-name: example.com + host-header: example.com + tls-server-name: example.com auth: pincode: 123456 password: sadfasdfadsf @@ -16,22 +16,23 @@ resources: whitelist-users: - owen@fossorial.io targets: - # - site: glossy-plains-viscacha-rat - # - hostname: localhost - # method: http - # port: 8000 - # healthcheck: - # port: 8000 - # hostname: localhost - # - site: glossy-plains-viscacha-rat - # - hostname: localhost - # method: http - # port: 8001 - # resource-nice-id2: - # name: this is other resource - # protocol: tcp - # proxy-port: 3000 - # targets: - # # - site: glossy-plains-viscacha-rat - # - hostname: localhost - # port: 3000 \ No newline at end of file + - site: lively-yosemite-toad + hostname: localhost + method: http + port: 8000 + - site: slim-alpine-chipmunk + hostname: localhost + method: http + port: 8001 + - site: glossy-plains-viscacha-rat + hostname: localhost + method: http + port: 8001 + resource-nice-id2: + name: this is other resource + protocol: tcp + proxy-port: 3000 + targets: + - site: glossy-plains-viscacha-rat + hostname: localhost + port: 3000 \ No newline at end of file diff --git a/server/lib/blueprints/resources.ts b/server/lib/blueprints/resources.ts index b1556f18..8499156e 100644 --- a/server/lib/blueprints/resources.ts +++ b/server/lib/blueprints/resources.ts @@ -247,11 +247,12 @@ export async function updateResources( // Create new targets for (const [index, targetData] of resourceData.targets.entries()) { - if (!targetData) { + if (!targetData || (typeof targetData === 'object' && Object.keys(targetData).length === 0)) { // If targetData is null or an empty object, we can skip it continue; } const existingTarget = existingResourceTargets[index]; + if (existingTarget) { let targetSiteId = targetData.site; let site; @@ -281,7 +282,7 @@ export async function updateResources( ) .limit(1); } else { - throw new Error(`Target site ID is required`); + throw new Error(`Target site is required`); } if (!site) { @@ -336,7 +337,16 @@ export async function updateResources( const targetsToDelete = existingResourceTargets.slice( resourceData.targets.length ); + logger.debug(`Targets to delete: ${JSON.stringify(targetsToDelete)}`); for (const target of targetsToDelete) { + if (!target) { + continue; + } + if (siteId && target.siteId !== siteId) { + logger.debug(`Skipping target ${target.targetId} for deletion. Site ID does not match filter.`); + continue; // only delete targets for the specified siteId + } + logger.debug(`Deleting target ${target.targetId}`); await trx .delete(targets) .where(eq(targets.targetId, target.targetId)); diff --git a/server/routers/org/applyBlueprint.ts b/server/routers/org/applyBlueprint.ts index 3e97da09..982258ee 100644 --- a/server/routers/org/applyBlueprint.ts +++ b/server/routers/org/applyBlueprint.ts @@ -98,6 +98,7 @@ export async function applyBlueprint( const decoded = Buffer.from(blueprint, "base64").toString("utf-8"); // then parse the json const blueprintParsed = JSON.parse(decoded); + // Update the blueprint in the database await applyBlueprintFunc(orgId, blueprintParsed); } catch (error) {