Lehel44 commented on code in PR #7051:
URL: https://github.com/apache/nifi/pull/7051#discussion_r1142673497


##########
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/src/main/java/org/apache/nifi/processors/aws/s3/AbstractS3Processor.java:
##########
@@ -198,6 +218,88 @@ protected AmazonS3Client createClient(final ProcessContext 
context, final AWSCre
         return s3;
     }
 
+    protected Region resolveRegion(final ProcessContext context, final 
Map<String, String> attributes) {
+        String regionValue = context.getProperty(S3_CUSTOM_REGION).getValue();
+
+        if (ATTRIBUTE_DRIVEN_REGION.getValue().equals(regionValue)) {
+            regionValue = attributes.get(S3_REGION_ATTRIBUTE);
+        }
+
+        return parseRegionValue(regionValue);
+    }
+
+    private Region parseRegionValue(String regionValue) {
+        if (regionValue == null) {
+            throw new ProcessException(format("[%s] was selected as region 
source but [%s] attribute does not exist", ATTRIBUTE_DRIVEN_REGION, 
S3_REGION_ATTRIBUTE));
+        }
+
+        try {
+            return Region.getRegion(Regions.fromName(regionValue));
+        } catch (Exception e) {
+            throw new ProcessException(format("The [%s] attribute contains an 
invalid region value [%s]", S3_REGION_ATTRIBUTE, regionValue), e);
+        }
+    }
+
+    protected void setClientBasedOnRegionAttribute(final ProcessContext 
context, final Map<String, String> attributes) {
+        String regionValue = context.getProperty(S3_CUSTOM_REGION).getValue();
+
+        if (ATTRIBUTE_DRIVEN_REGION.getValue().equals(regionValue)) {
+            regionValue = attributes.get(S3_REGION_ATTRIBUTE);
+            Region region = parseRegionValue(regionValue);
+
+            final AWSConfiguration awsConfiguration = 
getConfiguration(context, region);
+            this.client = awsConfiguration.getClient();
+            this.region = awsConfiguration.getRegion();
+        }
+    }
+
+    protected AWSConfiguration getConfiguration(final ProcessContext context, 
final Map<String, String> attributes) {
+        final Region region = resolveRegion(context, attributes);
+        return super.getConfiguration(context, region);
+    }
+
+    @Override
+    public List<ConfigVerificationResult> verify(final ProcessContext context, 
final ComponentLog verificationLogger, final Map<String, String> attributes) {
+        final List<ConfigVerificationResult> results = new ArrayList<>();
+
+        try {
+            getConfiguration(context, attributes);
+            results.add(new ConfigVerificationResult.Builder()
+                    .outcome(Outcome.SUCCESSFUL)
+                    .verificationStepName("Create S3 Client")
+                    .explanation("Successfully created S3 Client")
+                    .build());
+        } catch (final Exception e) {
+            verificationLogger.error("Failed to create S3 Client", e);
+            results.add(new ConfigVerificationResult.Builder()
+                    .outcome(Outcome.FAILED)
+                    .verificationStepName("Create S3 Client")
+                    .explanation("Failed to crete S3 Client: " + 
e.getMessage())
+                    .build());
+        }
+
+        return results;
+    }
+
+    @OnScheduled
+    public void onScheduled(final ProcessContext context) {
+        if (!isAttributeDrivenRegion(context)) {
+            setClientAndRegion(context);
+        }
+    }
+
+    private boolean isAttributeDrivenRegion(final ProcessContext context) {
+        String regionValue = context.getProperty(S3_CUSTOM_REGION).getValue();
+        return ATTRIBUTE_DRIVEN_REGION.getValue().equals(regionValue);
+    }
+
+    private static AllowableValue[] getCustomAvailableRegions() {
+        final List<AllowableValue> values = new ArrayList<>();
+        Collections.addAll(values, 
AbstractAWSCredentialsProviderProcessor.getAvailableRegions());
+        values.add(ATTRIBUTE_DRIVEN_REGION);
+        return values.toArray(new AllowableValue[0]);
+    }

Review Comment:
   ```suggestion
       private static AllowableValue[] getCustomAvailableRegions() {
       List<AllowableValue> values = new 
ArrayList<>(Arrays.asList(AbstractAWSCredentialsProviderProcessor.getAvailableRegions()));
       values.add(ATTRIBUTE_DRIVEN_REGION);
       return values.toArray(new AllowableValue[0]);
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to