From a502ca88f92ba15e44b958879e6df9b5efa81d2d Mon Sep 17 00:00:00 2001 From: Christopher Grote Date: Wed, 4 Dec 2024 01:49:50 +0000 Subject: [PATCH] Simplify usage of new initializeContext method Signed-off-by: Christopher Grote --- .../runtime/src/main/kotlin/com/atlan/pkg/Utils.kt | 6 +++--- .../src/main/kotlin/com/atlan/pkg/ae/AdminExporter.kt | 9 ++++----- .../kotlin/com/atlan/pkg/adoption/AdoptionExporter.kt | 9 ++++----- .../src/main/kotlin/ApiTokenConnectionAdmin.kt | 3 +-- .../asset-export-basic/src/main/kotlin/Exporter.kt | 3 +-- .../src/main/kotlin/com/atlan/pkg/aim/Importer.kt | 3 +-- .../src/main/kotlin/com/atlan/pkg/cab/Importer.kt | 3 +-- .../src/main/kotlin/CustomMetadataExtender.kt | 3 +-- .../src/main/kotlin/DuplicateDetector.kt | 4 +--- .../src/main/kotlin/EnrichmentMigrator.kt | 6 ++---- .../kotlin/com/atlan/pkg/lftag/LakeTagSynchronizer.kt | 3 +-- .../src/main/kotlin/com/atlan/pkg/lb/Loader.kt | 3 +-- .../src/main/kotlin/com/atlan/pkg/mdir/Reporter.kt | 9 ++++----- .../src/main/kotlin/OpenAPISpecLoader.kt | 9 ++++----- .../owner-propagator/src/main/kotlin/OwnerPropagator.kt | 4 +--- .../src/main/kotlin/com/atlan/pkg/rab/Importer.kt | 3 +-- .../tests-cleanup/src/main/kotlin/TestsCleanup.kt | 3 +-- 17 files changed, 32 insertions(+), 51 deletions(-) diff --git a/package-toolkit/runtime/src/main/kotlin/com/atlan/pkg/Utils.kt b/package-toolkit/runtime/src/main/kotlin/com/atlan/pkg/Utils.kt index 622c1421a5..6337f36f48 100644 --- a/package-toolkit/runtime/src/main/kotlin/com/atlan/pkg/Utils.kt +++ b/package-toolkit/runtime/src/main/kotlin/com/atlan/pkg/Utils.kt @@ -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 initializeContext( - config: T, + inline fun initializeContext( + config: T = setPackageOps(), reuseCtx: PackageContext<*>? = null, ): PackageContext { if (reuseCtx != null) config.runtime = reuseCtx.config.runtime diff --git a/samples/packages/admin-export/src/main/kotlin/com/atlan/pkg/ae/AdminExporter.kt b/samples/packages/admin-export/src/main/kotlin/com/atlan/pkg/ae/AdminExporter.kt index 615a624a2d..e5fc6a78dd 100644 --- a/samples/packages/admin-export/src/main/kotlin/com/atlan/pkg/ae/AdminExporter.kt +++ b/samples/packages/admin-export/src/main/kotlin/com/atlan/pkg/ae/AdminExporter.kt @@ -27,8 +27,7 @@ object AdminExporter { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> // Before we start processing, will pre-cache all glossaries, // so we can resolve them to meaningful names @@ -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", @@ -64,8 +63,8 @@ object AdminExporter { "CLOUD" -> { Utils.uploadOutputFile( exportFile, - Utils.getOrDefault(config.targetPrefix, ""), - Utils.getOrDefault(config.targetKey, ""), + ctx.config.targetPrefix, + ctx.config.targetKey, ) } } diff --git a/samples/packages/adoption-export/src/main/kotlin/com/atlan/pkg/adoption/AdoptionExporter.kt b/samples/packages/adoption-export/src/main/kotlin/com/atlan/pkg/adoption/AdoptionExporter.kt index 650d8526d9..af0c79fd3d 100644 --- a/samples/packages/adoption-export/src/main/kotlin/com/atlan/pkg/adoption/AdoptionExporter.kt +++ b/samples/packages/adoption-export/src/main/kotlin/com/atlan/pkg/adoption/AdoptionExporter.kt @@ -25,8 +25,7 @@ object AdoptionExporter { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> val exportFile = "$outputDirectory${File.separator}adoption-export.xlsx" ExcelWriter(exportFile).use { xlsx -> @@ -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", @@ -63,8 +62,8 @@ object AdoptionExporter { "CLOUD" -> { Utils.uploadOutputFile( exportFile, - Utils.getOrDefault(config.targetPrefix, ""), - Utils.getOrDefault(config.targetKey, ""), + ctx.config.targetPrefix, + ctx.config.targetKey, ) } } diff --git a/samples/packages/api-token-connection-admin/src/main/kotlin/ApiTokenConnectionAdmin.kt b/samples/packages/api-token-connection-admin/src/main/kotlin/ApiTokenConnectionAdmin.kt index b22fced9ef..2fd75ffe7a 100644 --- a/samples/packages/api-token-connection-admin/src/main/kotlin/ApiTokenConnectionAdmin.kt +++ b/samples/packages/api-token-connection-admin/src/main/kotlin/ApiTokenConnectionAdmin.kt @@ -18,8 +18,7 @@ object ApiTokenConnectionAdmin { */ @JvmStatic fun main(args: Array) { - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> val connectionQN = Utils.reuseConnection(ctx.client, ctx.config.connectionQualifiedName?.let { it[0] }) if (connectionQN.isBlank() || ctx.config.apiTokenGuid.isBlank()) { diff --git a/samples/packages/asset-export-basic/src/main/kotlin/Exporter.kt b/samples/packages/asset-export-basic/src/main/kotlin/Exporter.kt index 765ce0ca0a..3f85a1f140 100644 --- a/samples/packages/asset-export-basic/src/main/kotlin/Exporter.kt +++ b/samples/packages/asset-export-basic/src/main/kotlin/Exporter.kt @@ -14,8 +14,7 @@ object Exporter { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> export(ctx, outputDirectory) } } diff --git a/samples/packages/asset-import/src/main/kotlin/com/atlan/pkg/aim/Importer.kt b/samples/packages/asset-import/src/main/kotlin/com/atlan/pkg/aim/Importer.kt index fcff2938e6..1e4138c286 100644 --- a/samples/packages/asset-import/src/main/kotlin/com/atlan/pkg/aim/Importer.kt +++ b/samples/packages/asset-import/src/main/kotlin/com/atlan/pkg/aim/Importer.kt @@ -20,8 +20,7 @@ object Importer { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> import(ctx, outputDirectory)?.close() } } diff --git a/samples/packages/cube-assets-builder/src/main/kotlin/com/atlan/pkg/cab/Importer.kt b/samples/packages/cube-assets-builder/src/main/kotlin/com/atlan/pkg/cab/Importer.kt index ceee3a5917..0dde789f64 100644 --- a/samples/packages/cube-assets-builder/src/main/kotlin/com/atlan/pkg/cab/Importer.kt +++ b/samples/packages/cube-assets-builder/src/main/kotlin/com/atlan/pkg/cab/Importer.kt @@ -33,8 +33,7 @@ object Importer { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> import(ctx, outputDirectory) } } diff --git a/samples/packages/custom-metadata-extender/src/main/kotlin/CustomMetadataExtender.kt b/samples/packages/custom-metadata-extender/src/main/kotlin/CustomMetadataExtender.kt index fc6ba79e63..15b0663e80 100644 --- a/samples/packages/custom-metadata-extender/src/main/kotlin/CustomMetadataExtender.kt +++ b/samples/packages/custom-metadata-extender/src/main/kotlin/CustomMetadataExtender.kt @@ -19,8 +19,7 @@ object CustomMetadataExtender { */ @JvmStatic fun main(args: Array) { - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> val cmName = ctx.config.customMetadata val connectionQNs = ctx.config.connectionQualifiedName diff --git a/samples/packages/duplicate-detector/src/main/kotlin/DuplicateDetector.kt b/samples/packages/duplicate-detector/src/main/kotlin/DuplicateDetector.kt index 8f2b20001e..4885053be6 100644 --- a/samples/packages/duplicate-detector/src/main/kotlin/DuplicateDetector.kt +++ b/samples/packages/duplicate-detector/src/main/kotlin/DuplicateDetector.kt @@ -30,9 +30,7 @@ object DuplicateDetector { @JvmStatic fun main(args: Array) { - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> - + Utils.initializeContext().use { ctx -> val qnPrefix = ctx.config.qnPrefix val types = ctx.config.assetTypes val batchSize = 20 diff --git a/samples/packages/enrichment-migrator/src/main/kotlin/EnrichmentMigrator.kt b/samples/packages/enrichment-migrator/src/main/kotlin/EnrichmentMigrator.kt index 36656c1611..4306a4d894 100644 --- a/samples/packages/enrichment-migrator/src/main/kotlin/EnrichmentMigrator.kt +++ b/samples/packages/enrichment-migrator/src/main/kotlin/EnrichmentMigrator.kt @@ -26,8 +26,7 @@ object EnrichmentMigrator { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> val sourceConnectionQN = ctx.config.sourceConnection[0] val sourcePrefix = ctx.config.sourceQnPrefix @@ -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 -> @@ -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, diff --git a/samples/packages/lake-formation-tag-sync/src/main/kotlin/com/atlan/pkg/lftag/LakeTagSynchronizer.kt b/samples/packages/lake-formation-tag-sync/src/main/kotlin/com/atlan/pkg/lftag/LakeTagSynchronizer.kt index 47cf6b5a27..fa80c85a4b 100644 --- a/samples/packages/lake-formation-tag-sync/src/main/kotlin/com/atlan/pkg/lftag/LakeTagSynchronizer.kt +++ b/samples/packages/lake-formation-tag-sync/src/main/kotlin/com/atlan/pkg/lftag/LakeTagSynchronizer.kt @@ -31,8 +31,7 @@ object LakeTagSynchronizer { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> val results = sync(ctx, outputDirectory) if (!results && ctx.config.failOnErrors) { logger.error { "Some errors detected, failing the workflow." } diff --git a/samples/packages/lineage-builder/src/main/kotlin/com/atlan/pkg/lb/Loader.kt b/samples/packages/lineage-builder/src/main/kotlin/com/atlan/pkg/lb/Loader.kt index f1491907b9..161be95934 100644 --- a/samples/packages/lineage-builder/src/main/kotlin/com/atlan/pkg/lb/Loader.kt +++ b/samples/packages/lineage-builder/src/main/kotlin/com/atlan/pkg/lb/Loader.kt @@ -26,8 +26,7 @@ object Loader { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> import(ctx, outputDirectory) } } diff --git a/samples/packages/metadata-impact-report/src/main/kotlin/com/atlan/pkg/mdir/Reporter.kt b/samples/packages/metadata-impact-report/src/main/kotlin/com/atlan/pkg/mdir/Reporter.kt index 25a466556b..1066c1d668 100644 --- a/samples/packages/metadata-impact-report/src/main/kotlin/com/atlan/pkg/mdir/Reporter.kt +++ b/samples/packages/metadata-impact-report/src/main/kotlin/com/atlan/pkg/mdir/Reporter.kt @@ -89,8 +89,7 @@ object Reporter { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> val batchSize = 300 val glossary = @@ -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", @@ -118,8 +117,8 @@ object Reporter { "CLOUD" -> { Utils.uploadOutputFile( reportFile, - Utils.getOrDefault(config.targetPrefix, ""), - Utils.getOrDefault(config.targetKey, ""), + ctx.config.targetPrefix, + ctx.config.targetKey, ) } } diff --git a/samples/packages/openapi-spec-loader/src/main/kotlin/OpenAPISpecLoader.kt b/samples/packages/openapi-spec-loader/src/main/kotlin/OpenAPISpecLoader.kt index ef5425f26f..c1cc28bc2d 100644 --- a/samples/packages/openapi-spec-loader/src/main/kotlin/OpenAPISpecLoader.kt +++ b/samples/packages/openapi-spec-loader/src/main/kotlin/OpenAPISpecLoader.kt @@ -24,8 +24,7 @@ object OpenAPISpecLoader { */ @JvmStatic fun main(args: Array) { - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> val outputDirectory = if (args.isEmpty()) "tmp" else args[0] val importType = ctx.config.importType val specUrl = ctx.config.specUrl @@ -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) { @@ -58,7 +57,7 @@ object OpenAPISpecLoader { specFilename, outputDirectory, false, - Utils.getOrDefault(config.specPrefix, ""), + ctx.config.specPrefix, specKey, ) } diff --git a/samples/packages/owner-propagator/src/main/kotlin/OwnerPropagator.kt b/samples/packages/owner-propagator/src/main/kotlin/OwnerPropagator.kt index 010a9953a6..9260b7a970 100644 --- a/samples/packages/owner-propagator/src/main/kotlin/OwnerPropagator.kt +++ b/samples/packages/owner-propagator/src/main/kotlin/OwnerPropagator.kt @@ -17,10 +17,8 @@ object OwnerPropagator { */ @JvmStatic fun main(args: Array) { - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> val batchSize = 20 - val tables = findTables(ctx.client, ctx.config.qnPrefix, batchSize) propagateOwner(ctx.client, tables, batchSize) } diff --git a/samples/packages/relational-assets-builder/src/main/kotlin/com/atlan/pkg/rab/Importer.kt b/samples/packages/relational-assets-builder/src/main/kotlin/com/atlan/pkg/rab/Importer.kt index 63a05a02e3..bbafeeb1cf 100644 --- a/samples/packages/relational-assets-builder/src/main/kotlin/com/atlan/pkg/rab/Importer.kt +++ b/samples/packages/relational-assets-builder/src/main/kotlin/com/atlan/pkg/rab/Importer.kt @@ -38,8 +38,7 @@ object Importer { @JvmStatic fun main(args: Array) { val outputDirectory = if (args.isEmpty()) "tmp" else args[0] - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().use { ctx -> import(ctx, outputDirectory) } } diff --git a/samples/packages/tests-cleanup/src/main/kotlin/TestsCleanup.kt b/samples/packages/tests-cleanup/src/main/kotlin/TestsCleanup.kt index a8e1882b1d..dd9f410041 100644 --- a/samples/packages/tests-cleanup/src/main/kotlin/TestsCleanup.kt +++ b/samples/packages/tests-cleanup/src/main/kotlin/TestsCleanup.kt @@ -30,8 +30,7 @@ object TestsCleanup { */ @JvmStatic fun main(args: Array) { - val config = Utils.setPackageOps() - Utils.initializeContext(config).use { ctx -> + Utils.initializeContext().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." }