Dynamic Targets

In some cases, you might want to allow your users to input the name of the target they are requesting access to instead of declaring them all in your resources. Dynamic Targets allow you to do just that!

Add the setting to Prompt Fields

For a Target's setting to be populated dynamically, you will need to define it as a Prompt Field. The name of the Prompt Field must match the Key of the setting. For example, if to allow users to input a GitHub repository name, the Prompt Field will be configured with name = repo_name.

In this example, we will be using a GitHub Access Strategy. The goal is to request and grant access to the GitHub repository requested by the user in the Slack Request modal.

resource "sym_flow" "this" {
  name  = "github_access"
  label = "Github Access"

  implementation = "${path.module}/impl.py"
  environment_id = var.sym_environment_id
  vars = var.flow_vars

  params {
    strategy_id = sym_strategy.this.id

    prompt_field {
      name     = "reason"
      type     = "string"
      required = true
    }
    
    # This field will be bound to repo_name in the target resource next
    prompt_field {
      name     = "repo_name"
      label    = "Repository Name"
      type     = "string"
      required = true
    }
  }
}

After applying this, the new "Repository Name" field will appear in the modal. In the next step, we will bind it to the actual GitHub Target setting repo_name!

564564

Create a Dynamic Target

For a Target to have Dynamic Settings, it needs to have a special attribute field_bindings. This attribute contains a list of settings that will be inputted by the user in the Slack Request modal. The setting key must match the setting key being replaced and the Prompt Field name.

In this example, the setting being dynamically populated is the repo_name setting of a github_repo Target.

resource "sym_target" "target" {
  type  = "github_repo"
  name = "private-repos"
  
  # This is binding the repo_name form input to the repo_name field in the target
  field_bindings = ["repo_name"]
  
  # This is not needed anymore because provided by the field_bindings!
  # settings = {
    # repo_name = "private-repo"
  #}
}

The fields_bindings will allow Sym to interpret the following configuration:

resource "sym_target" "target" {
    ...
  field_bindings = ["repo_name"]
}

So that it behaves like this:

resource "sym_target" "target" {
  settings = {
    repo_name = # The value the user typed in the "repo_name" field in the Slack modal
   }
}

Results

364364

Notes

  • field_bindings will take precedence over static fields.
  • Make sure to configure the Prompt Field with required = True if the setting is a required setting.
  • You are not limited to the repo_name. You can make any Target setting dynamic!