1import * as obs from "@distilled.cloud/aws/observabilityadmin";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Binding from "../../Binding.ts";
5import { isBindingHost } from "../Lambda/Function.ts";
6import { GetTelemetryRule } from "./GetTelemetryRule.ts";
7import type { TelemetryRule } from "./TelemetryRule.ts";
8
9/**
10 * HTTP implementation of {@link GetTelemetryRule}: grants
11 * `observabilityadmin:GetTelemetryRule` on the bound rule's ARN and calls
12 * the Observability Admin HTTP API with the function's IAM credentials,
13 * injecting the rule's name as the `RuleIdentifier`.
14 */
15export const GetTelemetryRuleHttp = Layer.effect(
16 GetTelemetryRule,
17 Effect.gen(function* () {
18 const getTelemetryRule = yield* obs.getTelemetryRule;
19
20 return Effect.fn(function* (rule: TelemetryRule) {
21 const RuleIdentifier = yield* rule.ruleName;
22 if (!globalThis.__ALCHEMY_RUNTIME__) {
23 const host = yield* Binding.Host;
24 if (isBindingHost(host)) {
25 yield* host.bind`Allow(${host}, AWS.ObservabilityAdmin.GetTelemetryRule(${rule}))`(
26 {
27 policyStatements: [
28 {
29 Effect: "Allow",
30 Action: ["observabilityadmin:GetTelemetryRule"],
31 Resource: [rule.ruleArn],
32 },
33 ],
34 },
35 );
36 }
37 }
38 return Effect.fn(
39 `AWS.ObservabilityAdmin.GetTelemetryRule(${rule.LogicalId})`,
40 )(function* () {
41 return yield* getTelemetryRule({
42 RuleIdentifier: yield* RuleIdentifier,
43 });
44 });
45 });
46 }),
47);