skip to Main Content

I have a sting

crdemo01.australiaeast.data.azurecr.io

I want to build the following string from the above one:

crdemo01-test.australiaeast.data

The same logic should apply to the following input strings as well:

crdemo01.australiasoutheast.data.azurecr.io
crdemo01.azurecr.io

What I’m trying to achieve is to remove the .azurecr.io part from the input string and append -test to the first part of the remaining string.

How can I achieve this in Terraform?

3

Answers


  1. Using replace twice with regex patterns:

    > replace(replace("crdemo01.australiaeast.data.azurecr.io","/^[^.]+/","$0-test"),"/.azurecr.io$/","")
    "crdemo01-test.australiaeast.data"
    
    Login or Signup to reply.
  2. There are a couple of functions that can help with this:

    variable "input" {
      default = "crdemo01.australiaeast.data.azurecr.io"
    }
    
    locals {
      remove_str = replace(var.input, ".azurecr.io", "")
      first = split(".", local.remove_str)[0]
      final_str = replace(local.remove_str, local.first, "${local.first}-test")
    }
    
    output "data" {
      value = local.final_str
    }
    

    You can combine all that in one line, but is a nice visual to see the breakdown of the code.

    Login or Signup to reply.
  3. This goal seems to require partitioning the input into three parts:

    1. The first domain segment, which will have -test added to it.
    2. Any additional domain segments prior to the fixed suffix.
    3. The fixed suffix .azurecr.io

    Here’s a regular expression pattern that should extract the first two segments as long as the third segment is present:

    (?P<first>[^.]+)(?P<remain>..+)?.azurecr.io
    

    This uses named capture groups, so if you use it with Terraform’s regex function then you’ll obtain a map of strings with the keys first and remain, which correspond to the first and second segments I listed above.

    Using that pattern, you can achieve the result you described in two steps by first extracting the two relevant parts and then concatenating them back together in a slightly different way:

    locals {
      parts  = regex("(?P<first>[^\.]+)(?P<remain>\..+)?\.azurecr\.io", var.input)
      result = "${local.parts.first}-test${local.parts.remain != null ? local.parts.remain : "")}"
    }
    

    In the last case of crdemo01.azurecr.io the remain result will be null because there isn’t any intermediate part, so the conditional expression arranges to treat that as the empty string instead. (A possible variation would be to use (?P<remain>\..+|) — a dot followed by other characters OR the empty string — which would then cause remain to be "" when there’s no middle part.)


    Example results from the regex call from terraform console, in case it’s useful to see the intermediate results to understand what’s going on here:

    > regex("(?P<first>[^\.]+)(?P<remain>\..+)?\.azurecr\.io", "crdemo01.australiaeast.data.azurecr.io")
    {
      "first" = "crdemo01"
      "remain" = ".australiaeast.data"
    }
    > regex("(?P<first>[^\.]+)(?P<remain>\..+)?\.azurecr\.io", "crdemo01.australiasoutheast.data.azurecr.io")
    {
      "first" = "crdemo01"
      "remain" = ".australiasoutheast.data"
    }
    > regex("(?P<first>[^\.]+)(?P<remain>\..+)?\.azurecr\.io", "crdemo01.azurecr.io")
    {
      "first" = "crdemo01"
      "remain" = tostring(null)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search