Referrencing objects in Go

Hi,

I am creating a Datadog monitor with the CDK in Go:

import (
   datadogMonitor "github.com/cdktf/cdktf-provider-datadog-go/datadog/v3/monitor"
)

...

monitor := datadogMonitor.NewMonitor(*stack, &Id, &config)

Now I want to attach a monitor_downtime resource to it, for which I need the ID of my monitor:

datadogMonitorDowntime.NewDowntime(*stack, &downtimeId, 
     &datadogMonitorDowntime.DowntimeConfig{
			MonitorId: ???,
     })

How do I get a reference to that monitor that was/will be created? I need to provide the Datadog ID, clearly, not the TF id of the resource … How do I establish the relationship?

Thanks!

  • A

Hi @schapirama :wave:

while I haven’t tested this, I think MonitorId: monitor.Id() could be it. The id property is not necessarily Terraform specific but rather depends on the resource.

– Ansgar

Thanks @ansgarm !

monitor.Id() I think returns the TF-specific name of the resource (e.g. my-monitor), not the Datadog Id.

We have tried monitor.IdInput() and that seems to be working, but I am not 100% sure this is indeed the way to go.

Thanks!

  • A

Do you happen to have an HCL example doing this? Is the Id user defined? (IdInput and Id should ideally resolve to the same value eventually)

In HCL I do this:

resource "datadog_monitor" "errors" {
  name  = "My monitor"
  ...
}

resource "datadog_downtime" "errors_downtime" {
  monitor_id = datadog_monitor.errors.id
  scope = ["*"]
  ...
}

and the plan is then

  # datadog_monitor.errors will be created
  + resource "datadog_monitor" "errors" {
        ...
    }

   # datadog_downtime.errors_downtime will be created
   + resource "datadog_downtime" "errors_downtime" {
      + monitor_id                       = (known after apply)    
      ...
    }  

(and if the monitor already exists, the monitor_id for the downtime resource is correctly grabbed from the state file (it’s a number, the ID of the monitor in Datadog)

Thanks a lot,

  • A

To do something similar with the CDK in Golang, I have to do something like this:

// the TF resource IDs/names
monitorId := "errors"
downtimeId := "errors_downtime" 

// the monitor
myMonitor := datadogMonitor.NewMonitor(*stack, &monitorId, &monitorConfig)

// myMonitor.Id() is a *string 
// and I need a *float for the downtime `monitor_id` field
floatId, _ := strconv.ParseFloat(*myMonitor.Id(), 64)

datadogMonitorDowntime.NewDowntime(*stack, &downtimeId, 
    &datadogMonitorDowntime.DowntimeConfig{
        MonitorId: &floatId,
        ...
    }
)

but I think that myMonitor.Id() returns errors, not the ID in the Datadog world of the monitor that will be created.

  • A

@ansgarm I found the solution! :wink:

It is the Token functionality that I am after --it’s all explained here: Tokens - CDK for Terraform | Terraform | HashiCorp Developer

So if I do the following:

// the TF resource IDs/names
monitorId := "errors"
downtimeId := "errors_downtime" 

// the monitor
myMonitor := datadogMonitor.NewMonitor(*stack, &monitorId, &monitorConfig)

then myMonitor.Id() resolves to an internal “token”, something like "#{TOKEN[TOKEN.1]}". Using the Token functionality, I can ask CDKTF to resolve the value (during synth) to the actual Id of the monitor that is created:

with this:

datadogMonitorDowntime.NewDowntime(*stack, &downtimeId, 
    &datadogMonitorDowntime.DowntimeConfig{
        MonitorId: cdktf.Token_AsNumber(myMonitor.Id()),
        ...
    }
)

… the result is the following block in the cdk.tf.json output file:

  "resource": {
    "datadog_downtime": {
      "errors_downtime": {
        "depends_on": [
          "errors"
        ],
        "monitor_id": "${datadog_monitor.errors.id}",

which is what we want.

Thanks for your help!

Best,

  • A
1 Like