GitHub user StanleyShen added a comment to the discussion: how to create own 
variable resolver

I didn't use it, I just use the `VaultVariableResolver` for reference, so it's 
just with annotations.

```
package org.apache.hop.core.variables.resolver.secrets;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest;
import com.amazonaws.services.secretsmanager.model.GetSecretValueResult;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.hop.core.exception.HopException;
import org.apache.hop.core.gui.plugin.GuiElementType;
import org.apache.hop.core.gui.plugin.GuiPlugin;
import org.apache.hop.core.gui.plugin.GuiWidgetElement;
import org.apache.hop.core.logging.LogChannel;
import org.apache.hop.core.variables.IVariables;
import org.apache.hop.core.variables.resolver.IVariableResolver;
import org.apache.hop.core.variables.resolver.VariableResolver;
import org.apache.hop.core.variables.resolver.VariableResolverPlugin;
import org.apache.hop.metadata.api.HopMetadataProperty;

@Getter
@Setter
@GuiPlugin
@VariableResolverPlugin(
    id = "AWS-Secret-Variable-Resolver",
    name = "AWS Secret Variable Resolver",
    description = "Look up values of secrets in AWS Secret Manager",
    documentationUrl = "/variables/resolvers/AWS-Secret.html" // TODO: write 
this documentation
    )
public class SecretVariableResolver implements IVariableResolver {

  @GuiWidgetElement(
      id = "awsAccessId",
      order = "01",
      label =
          
"i18n:org.apache.hop.core.variables.resolver.aws:SecretVariableResolver.label.awsAccessId",
      type = GuiElementType.TEXT,
      password = true,
      parentId = VariableResolver.GUI_PLUGIN_ELEMENT_PARENT_ID)
  @HopMetadataProperty
  private String awsAccessId;

  @GuiWidgetElement(
      id = "awsAccessKey",
      order = "02",
      label =
          
"i18n:org.apache.hop.core.variables.resolver.aws:SecretVariableResolver.label.awsAccessKey",
      type = GuiElementType.TEXT,
      password = true,
      parentId = VariableResolver.GUI_PLUGIN_ELEMENT_PARENT_ID)
  @HopMetadataProperty
  private String awsAccessKey;

  @GuiWidgetElement(
      id = "awsRegion",
      order = "03",
      label =
          
"i18n:org.apache.hop.core.variables.resolver.aws:SecretVariableResolver.label.awsRegion",
      type = GuiElementType.TEXT,
      parentId = VariableResolver.GUI_PLUGIN_ELEMENT_PARENT_ID)
  @HopMetadataProperty
  private String awsRegion;

  @GuiWidgetElement(
      id = "secretName",
      order = "04",
      label =
          
"i18n:org.apache.hop.core.variables.resolver.aws:SecretVariableResolver.label.secretName",
      type = GuiElementType.TEXT,
      parentId = VariableResolver.GUI_PLUGIN_ELEMENT_PARENT_ID)
  @HopMetadataProperty
  private String secretName;

  @Override
  public String resolve(String variableName, IVariables variables) throws 
HopException {
    try {
      return "hello";
    } catch (Exception e) {
      LogChannel.GENERAL.logError(
          "Error looking up variable '" + variableName + "' in the AWS Secret", 
e);
      return null;
    }
  }

  @Override
  public void setPluginId() {
    // Nothing to set
  }

  @Override
  public void init() {
    // Not used today
  }

  @Override
  public String getPluginId() {
    return "AWS-Secret-Variable-Resolver";
  }

  @Override
  public void setPluginName(String pluginName) {
    // Nothing to set
  }

  @Override
  public String getPluginName() {
    return "AWS Secret Variable Resolver";
  }
}
```

Did some search, it seems this annotation is enough, there is no other code to 
registry it.
And I see this resolver in UI

<img width="1466" alt="image" 
src="https://github.com/user-attachments/assets/fd4d0d86-69ef-4150-96b9-09cf95ae3c72";
 />

<img width="1110" alt="image" 
src="https://github.com/user-attachments/assets/747276bb-4452-41c6-87eb-1683e6438a2d";
 />

<img width="1046" alt="image" 
src="https://github.com/user-attachments/assets/c2d272fd-0fa9-48a6-8b1f-5a691a101e8d";
 />


But when running workflow, it seems the resolve didn't happen, I get value in 
log like this.

```
2025/05/14 08:12:10 - test_log - hello, ${awssecret:test}
```

GitHub link: 
https://github.com/apache/hop/discussions/5312#discussioncomment-13141886

----
This is an automatically sent email for users@hop.apache.org.
To unsubscribe, please send an email to: users-unsubscr...@hop.apache.org

Reply via email to