Skip to content

Commit

Permalink
Simplify usage of new initializeContext method
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Grote <[email protected]>
  • Loading branch information
cmgrote committed Dec 4, 2024
1 parent 2d42183 commit a502ca8
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ object Utils {
* This will use an API token if found in ATLAN_API_KEY, and will fallback to attempting
* to impersonate a user if ATLAN_API_KEY is empty.
*
* @param config configuration for the custom package, which has already been initialized through {@code parseConfigFromEnv()}
* @param config (optional) configuration for the custom package (will be pulled through {@code setPackageOps} if not provided)
* @param reuseCtx (optional) existing context of a running package to reuse
* @return connectivity to the Atlan tenant
*/
fun <T : CustomConfig> initializeContext(
config: T,
inline fun <reified T : CustomConfig> initializeContext(
config: T = setPackageOps<T>(),
reuseCtx: PackageContext<*>? = null,
): PackageContext<T> {
if (reuseCtx != null) config.runtime = reuseCtx.config.runtime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ object AdminExporter {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<AdminExportCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<AdminExportCfg>().use { ctx ->

// Before we start processing, will pre-cache all glossaries,
// so we can resolve them to meaningful names
Expand All @@ -50,7 +49,7 @@ object AdminExporter {

when (ctx.config.deliveryType) {
"EMAIL" -> {
val emails = Utils.getAsList(config.emailAddresses)
val emails = Utils.getAsList(ctx.config.emailAddresses)
if (emails.isNotEmpty()) {
Utils.sendEmail(
"[Atlan] Admin Export results",
Expand All @@ -64,8 +63,8 @@ object AdminExporter {
"CLOUD" -> {
Utils.uploadOutputFile(
exportFile,
Utils.getOrDefault(config.targetPrefix, ""),
Utils.getOrDefault(config.targetKey, ""),
ctx.config.targetPrefix,
ctx.config.targetKey,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ object AdoptionExporter {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<AdoptionExportCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<AdoptionExportCfg>().use { ctx ->

val exportFile = "$outputDirectory${File.separator}adoption-export.xlsx"
ExcelWriter(exportFile).use { xlsx ->
Expand All @@ -49,7 +48,7 @@ object AdoptionExporter {

when (ctx.config.deliveryType) {
"EMAIL" -> {
val emails = Utils.getAsList(config.emailAddresses)
val emails = Utils.getAsList(ctx.config.emailAddresses)
if (emails.isNotEmpty()) {
Utils.sendEmail(
"[Atlan] Adoption Export results",
Expand All @@ -63,8 +62,8 @@ object AdoptionExporter {
"CLOUD" -> {
Utils.uploadOutputFile(
exportFile,
Utils.getOrDefault(config.targetPrefix, ""),
Utils.getOrDefault(config.targetKey, ""),
ctx.config.targetPrefix,
ctx.config.targetKey,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ object ApiTokenConnectionAdmin {
*/
@JvmStatic
fun main(args: Array<String>) {
val config = Utils.setPackageOps<APITokenConnectionAdminCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<APITokenConnectionAdminCfg>().use { ctx ->
val connectionQN = Utils.reuseConnection(ctx.client, ctx.config.connectionQualifiedName?.let { it[0] })

if (connectionQN.isBlank() || ctx.config.apiTokenGuid.isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ object Exporter {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<AssetExportBasicCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<AssetExportBasicCfg>().use { ctx ->
export(ctx, outputDirectory)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ object Importer {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<AssetImportCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<AssetImportCfg>().use { ctx ->
import(ctx, outputDirectory)?.close()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ object Importer {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<CubeAssetsBuilderCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<CubeAssetsBuilderCfg>().use { ctx ->
import(ctx, outputDirectory)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ object CustomMetadataExtender {
*/
@JvmStatic
fun main(args: Array<String>) {
val config = Utils.setPackageOps<CustomMetadataExtenderCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<CustomMetadataExtenderCfg>().use { ctx ->

val cmName = ctx.config.customMetadata
val connectionQNs = ctx.config.connectionQualifiedName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ object DuplicateDetector {

@JvmStatic
fun main(args: Array<String>) {
val config = Utils.setPackageOps<DuplicateDetectorCfg>()
Utils.initializeContext(config).use { ctx ->

Utils.initializeContext<DuplicateDetectorCfg>().use { ctx ->
val qnPrefix = ctx.config.qnPrefix
val types = ctx.config.assetTypes
val batchSize = 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ object EnrichmentMigrator {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<EnrichmentMigratorCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<EnrichmentMigratorCfg>().use { ctx ->

val sourceConnectionQN = ctx.config.sourceConnection[0]
val sourcePrefix = ctx.config.sourceQnPrefix
Expand Down Expand Up @@ -96,7 +95,6 @@ object EnrichmentMigrator {
}
start.toList()
}
val assetsFailOnErrors = Utils.getOrDefault(config.failOnErrors, true)
ctx.config.targetConnection.forEach { targetConnectionQN ->
val targetDatabaseNames = getTargetDatabaseName(ctx.client, targetConnectionQN, ctx.config.targetDatabasePattern)
targetDatabaseNames.forEach { targetDatabaseName ->
Expand Down Expand Up @@ -132,7 +130,7 @@ object EnrichmentMigrator {
AssetImportCfg(
assetsFile = transformedFile,
assetsUpsertSemantic = "update",
assetsFailOnErrors = assetsFailOnErrors,
assetsFailOnErrors = ctx.config.failOnErrors,
assetsBatchSize = ctx.config.batchSize,
assetsFieldSeparator = ctx.config.fieldSeparator,
assetsCaseSensitive = ctx.config.caseSensitive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ object LakeTagSynchronizer {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<LakeFormationTagSyncCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<LakeFormationTagSyncCfg>().use { ctx ->
val results = sync(ctx, outputDirectory)
if (!results && ctx.config.failOnErrors) {
logger.error { "Some errors detected, failing the workflow." }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ object Loader {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<LineageBuilderCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<LineageBuilderCfg>().use { ctx ->
import(ctx, outputDirectory)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ object Reporter {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<MetadataImpactReportCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<MetadataImpactReportCfg>().use { ctx ->
val batchSize = 300

val glossary =
Expand All @@ -104,7 +103,7 @@ object Reporter {

when (ctx.config.deliveryType) {
"EMAIL" -> {
val emails = Utils.getAsList(config.emailAddresses)
val emails = Utils.getAsList(ctx.config.emailAddresses)
if (emails.isNotEmpty()) {
Utils.sendEmail(
"[Atlan] Metadata Impact Report",
Expand All @@ -118,8 +117,8 @@ object Reporter {
"CLOUD" -> {
Utils.uploadOutputFile(
reportFile,
Utils.getOrDefault(config.targetPrefix, ""),
Utils.getOrDefault(config.targetKey, ""),
ctx.config.targetPrefix,
ctx.config.targetKey,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ object OpenAPISpecLoader {
*/
@JvmStatic
fun main(args: Array<String>) {
val config = Utils.setPackageOps<OpenAPISpecLoaderCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<OpenAPISpecLoaderCfg>().use { ctx ->
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val importType = ctx.config.importType
val specUrl = ctx.config.specUrl
Expand All @@ -34,11 +33,11 @@ object OpenAPISpecLoader {
val batchSize = 20

val inputQN =
config.connectionQualifiedName?.let {
ctx.config.connectionQualifiedName?.let {
if (it.isNotEmpty()) it[0] else null
}
val connectionQN =
Utils.createOrReuseConnection(ctx.client, config.connectionUsage, inputQN, config.connection)
Utils.createOrReuseConnection(ctx.client, ctx.config.connectionUsage, inputQN, ctx.config.connection)

val specFileProvided = (importType == "DIRECT" && specFilename.isNotBlank()) || (importType == "CLOUD" && specKey.isNotBlank()) || (importType == "URL" && specUrl.isNotBlank())
if (!specFileProvided) {
Expand All @@ -58,7 +57,7 @@ object OpenAPISpecLoader {
specFilename,
outputDirectory,
false,
Utils.getOrDefault(config.specPrefix, ""),
ctx.config.specPrefix,
specKey,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ object OwnerPropagator {
*/
@JvmStatic
fun main(args: Array<String>) {
val config = Utils.setPackageOps<OwnerPropagatorCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<OwnerPropagatorCfg>().use { ctx ->
val batchSize = 20

val tables = findTables(ctx.client, ctx.config.qnPrefix, batchSize)
propagateOwner(ctx.client, tables, batchSize)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ object Importer {
@JvmStatic
fun main(args: Array<String>) {
val outputDirectory = if (args.isEmpty()) "tmp" else args[0]
val config = Utils.setPackageOps<RelationalAssetsBuilderCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<RelationalAssetsBuilderCfg>().use { ctx ->
import(ctx, outputDirectory)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ object TestsCleanup {
*/
@JvmStatic
fun main(args: Array<String>) {
val config = Utils.setPackageOps<TestsCleanupCfg>()
Utils.initializeContext(config).use { ctx ->
Utils.initializeContext<TestsCleanupCfg>().use { ctx ->
val prefix = ctx.config.prefix
if (prefix.isBlank()) {
logger.error { "Missing required parameter - you must specify a prefix for objects to be purged." }
Expand Down

0 comments on commit a502ca8

Please sign in to comment.