Setting an Schema item with a line change `\n`

When I set a item in the schema using the method func (d *ResourceData) Set(key string, value interface{}) error {, line changes (\n) are not properly escaped:

For example, the following line of code

d.Set("ibgp_peer_template_config", "   peer template test\n     remote-as 65001")

results in the state-file to have this value

"ibgp_peer_template_config": "\"  template peer TEST\\n    remote-as 65002"",

This ends up causing issues with the Terraform diffing calculation in when executing terraform apply

The strings are clearly not the same (different AS numbers amongst other things).

The supposed state file JSON isn’t valid JSON, making me think you’ve copy/pasted inaccurately.

Accounting for the above problems, the value shown in the state file looks correct.

You will have to describe your problem in more detail, without these issues, to enable people of this forum to offer more useful responses.

Sorry. I tried to oversimplify the configuration.

This is how my execution plan looks like

resource "dcnm_fabric" "test" {
  name                      = "TERRAFORM_FABRIC"
  asn                       = "65002"
  ibgp_peer_template_config = "  template peer TEST\n    remote-as 65002\n    address-family l2vpn evpn"

This is how the state file looks after executing terraform apply

  "version": 4,
  "terraform_version": "1.3.1",
  "serial": 13,
  "lineage": "e1d55485-d67c-5f34-4846-e9637c2415a2",
  "outputs": {},
  "resources": [
      "mode": "managed",
      "type": "dcnm_fabric",
      "name": "test",
      "provider": "provider[\"\"]",
      "instances": [
          "schema_version": 0,
          "attributes": {
            "asn": 65002,
            "fabric_id": "FABRIC-4",
            "ibgp_bfd": false,
            "ibgp_peer_template_config": "  template peer TEST\\n    remote-as 65002\\n    address-family l2vpn evpn"
          "sensitive_attributes": [],
          "private": "bnVsbA=="
  "check_results": []

At this point there is a drift between the value in the Execution Plan and the value in the State File for the key ibgp_peer_template_config. Therefore a new execution of terraform apply triggers an update:

  # dcnm_fabric.test will be updated in-place
  ~ resource "dcnm_fabric" "test" {
      ~ ibgp_peer_template_config          = <<-EOT
          - "  template peer TEST\n    remote-as 65002\n    address-family l2vpn evpn"
          +   template peer TEST
          +     remote-as 65002
          +     address-family l2vpn evpn
        id                                 = "4"
        name                               = "TERRAFORM_FABRIC"
        # (52 unchanged attributes hidden)

Plan: 0 to add, 1 to change, 0 to destroy.

I have the feeling there is something wrong when I set the value of the key ibgp_peer_template_config in the Read method.

This is how the method looks: (Some line are omitted)

func resourceDCNMFabricRead(d *schema.ResourceData, m interface{}) error {
   d.Set("ibgp_peer_template_config", cont.S("IBGP_PEER_TEMPLATE").String())

I have already debugged the code and can confirm that the value of the operation cont.S("IBGP_PEER_TEMPLATE").String() is " template peer TEST\n remote-as 65002\n address-family l2vpn evpn". I cannot find any other reason why the state file includes additional \ characters for this specific key

The behaviour you describe would be explained by the actual string value being exactly what you say it is, literally including the quotes and backslash-n sequence.

Perhaps that’s actually what this cont.S is returning?