-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from pagopa/IOPLT-385_add_support_for_managed_…
…identity_on_evh_internal_client [#IOPLT-385] Add managed identity support for evh internal client
- Loading branch information
Showing
6 changed files
with
754 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,128 @@ | ||
import { | ||
KafkaConsumerCompact, | ||
ReadType, | ||
RunnerConfig, | ||
defaultRunner, | ||
read | ||
} from "@pagopa/fp-ts-kafkajs/dist/lib/KafkaConsumerCompact"; | ||
import * as E from "fp-ts/Either"; | ||
import * as TE from "fp-ts/TaskEither"; | ||
import { pipe } from "fp-ts/function"; | ||
import { EachMessageHandler } from "kafkajs"; | ||
import { getEventHubConsumer } from "./eventhub/utils"; | ||
export type MessageHandler = EachMessageHandler; | ||
export type QueueConsumer = KafkaConsumerCompact; | ||
export interface IQueueService { | ||
readonly consumeMessage: ( | ||
topic: string, | ||
runnerConfig: RunnerConfig | ||
) => TE.TaskEither<Error, void>; | ||
} | ||
import * as AR from "fp-ts/Array"; | ||
import * as J from "fp-ts/Json"; | ||
import { constVoid, flow, pipe } from "fp-ts/function"; | ||
import { earliestEventPosition } from "@azure/event-hubs"; | ||
import { EventHubConsumerClient } from "@azure/event-hubs"; | ||
import { toJsonObject } from "../utils/data"; | ||
import { | ||
IQueueService, | ||
KafkaParams, | ||
NativeEvhParams, | ||
PasswordLessNativeEvhParams | ||
} from "../types/evh"; | ||
import { | ||
getEventHubConsumer, | ||
getNativeEventHubConsumer, | ||
getPasswordLessNativeEventHubConsumer | ||
} from "./eventhub/utils"; | ||
|
||
const defaultConsumeMessage = (consumer: KafkaConsumerCompact) => ( | ||
topic: string, | ||
runnerConfig: RunnerConfig | ||
): TE.TaskEither<Error, void> => | ||
read(consumer)({ topics: [topic] }, defaultRunner, runnerConfig); | ||
|
||
export const eventHubService = { | ||
consumeMessage: read | ||
}; | ||
|
||
export const createEventHubService = ( | ||
connectionString: string | ||
export const createEventHubService = (params: KafkaParams) => ( | ||
messageHandler: ( | ||
message: Record<string, unknown> | ||
) => TE.TaskEither<Error, void> | ||
): E.Either<Error, IQueueService> => | ||
pipe( | ||
getEventHubConsumer(connectionString), | ||
getEventHubConsumer(params.connectionString), | ||
E.map(consumer => ({ | ||
consumeMessage: defaultConsumeMessage(consumer) | ||
consumeMessage: defaultConsumeMessage(consumer)(params.topicName, { | ||
...params.runnerConfigOptions, | ||
handler: eachBatchPayload => | ||
pipe( | ||
eachBatchPayload.batch.messages, | ||
AR.map( | ||
flow( | ||
msg => msg.value, | ||
buf => buf.toString(), | ||
J.parse, | ||
E.mapLeft(err => | ||
Error(`Cannot decode Kafka Message|ERROR=${String(err)}`) | ||
), | ||
E.map(toJsonObject), | ||
TE.fromEither, | ||
TE.chain(messageHandler) | ||
) | ||
), | ||
AR.sequence(TE.ApplicativeSeq), | ||
TE.mapLeft(err => { | ||
throw err; | ||
}), | ||
TE.map(constVoid), | ||
TE.toUnion | ||
)(), | ||
readType: ReadType.Message | ||
}) | ||
})) | ||
); | ||
|
||
const getEvhSubscriber = ( | ||
messageHandler: ( | ||
message: Record<string, unknown> | ||
) => TE.TaskEither<Error, void> | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
) => (consumer: EventHubConsumerClient): TE.TaskEither<Error, void> => | ||
pipe( | ||
consumer.subscribe( | ||
{ | ||
processError: async (_err, _context) => { | ||
// error reporting/handling code here | ||
}, | ||
processEvents: async (events, _) => | ||
pipe( | ||
events, | ||
AR.map(msgEvt => messageHandler(msgEvt.body)), | ||
AR.sequence(TE.ApplicativeSeq), | ||
TE.mapLeft(err => { | ||
throw err; | ||
}), | ||
TE.map(constVoid), | ||
TE.toUnion | ||
)() | ||
}, | ||
{ maxBatchSize: 1, startPosition: earliestEventPosition } | ||
), | ||
TE.of, | ||
TE.map(constVoid) | ||
); | ||
|
||
export const createNativeEventHubService = (params: NativeEvhParams) => ( | ||
messageHandler: ( | ||
message: Record<string, unknown> | ||
) => TE.TaskEither<Error, void> | ||
): E.Either<Error, IQueueService> => | ||
pipe( | ||
getNativeEventHubConsumer(params.connectionString), | ||
E.map(getEvhSubscriber(messageHandler)), | ||
E.map(consumeMessage => ({ | ||
consumeMessage | ||
})) | ||
); | ||
|
||
export const createPasswordlessNativeEventHubService = ( | ||
params: PasswordLessNativeEvhParams | ||
) => ( | ||
messageHandler: ( | ||
message: Record<string, unknown> | ||
) => TE.TaskEither<Error, void> | ||
): E.Either<Error, IQueueService> => | ||
pipe( | ||
getPasswordLessNativeEventHubConsumer(params.hostName, params.topicName), | ||
E.map(getEvhSubscriber(messageHandler)), | ||
E.map(consumeMessage => ({ | ||
consumeMessage | ||
})) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { KafkaConsumerCompact } from "@pagopa/fp-ts-kafkajs/dist/lib/KafkaConsumerCompact"; | ||
import { EachMessageHandler } from "kafkajs"; | ||
import * as TE from "fp-ts/TaskEither"; | ||
import * as t from "io-ts"; | ||
|
||
const BaseQueueParams = t.type({ | ||
connectionString: t.string | ||
}); | ||
export type BaseQueueParams = t.TypeOf<typeof BaseQueueParams>; | ||
|
||
export const RunnerConfigOptions = t.type({ | ||
autoCommit: t.boolean, | ||
autoCommitInterval: t.number, | ||
autoCommitThreshold: t.number, | ||
eachBatchAutoResolve: t.boolean, | ||
partitionsConsumedConcurrently: t.number | ||
}); | ||
export type RunnerConfigOptions = t.TypeOf<typeof RunnerConfigOptions>; | ||
|
||
export const KafkaParams = t.intersection([ | ||
BaseQueueParams, | ||
t.type({ | ||
queueType: t.literal("BASIC_KAFKA"), | ||
runnerConfigOptions: RunnerConfigOptions, | ||
topicName: t.string | ||
}) | ||
]); | ||
export type KafkaParams = t.TypeOf<typeof KafkaParams>; | ||
|
||
export const NativeEvhParams = t.intersection([ | ||
BaseQueueParams, | ||
t.type({ | ||
queueType: t.literal("NATIVE_EVH") | ||
}) | ||
]); | ||
export type NativeEvhParams = t.TypeOf<typeof NativeEvhParams>; | ||
|
||
export const PasswordLessNativeEvhParams = t.type({ | ||
hostName: t.string, | ||
queueType: t.literal("PASSWORDLESS_EVH"), | ||
topicName: t.string | ||
}); | ||
export type PasswordLessNativeEvhParams = t.TypeOf< | ||
typeof PasswordLessNativeEvhParams | ||
>; | ||
|
||
export const QueueParams = t.union([ | ||
KafkaParams, | ||
NativeEvhParams, | ||
PasswordLessNativeEvhParams | ||
]); | ||
export type QueueParams = t.TypeOf<typeof QueueParams>; | ||
|
||
export type MessageHandler = EachMessageHandler; | ||
export type QueueConsumer = KafkaConsumerCompact; | ||
|
||
export interface IQueueService { | ||
readonly consumeMessage: TE.TaskEither<Error, void>; | ||
} |
Oops, something went wrong.