Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Terraform Configuration Error with iosxe_restconf on CAT9K #203

Open
CuongDoManh0710 opened this issue Dec 20, 2024 · 1 comment
Open
Labels
question Further information is requested

Comments

@CuongDoManh0710
Copy link

CuongDoManh0710 commented Dec 20, 2024

Fixing Terraform Configuration Error with iosxe_restconf on CAT9K

We are using iosxe_restconf to configure the interface for a CAT9K device. Our code is as follows. After applying it in Terraform, the following error was reported: Can you help me fix it?
Thank you very much!

resource "iosxe_restconf" "gigabit_ethernet" {
  path = "Cisco-IOS-XE-native:native/interface"
 
  lists = [
    {
      name = "GigabitEthernet"
      key  = "name"
      items = [
        {
          name        = "1/0/10"
          description = "Configured via Terraform"
          switchport-config = jsonencode({
            switchport = {
              "Cisco-IOS-XE-switch:access" = {
                vlan = {
                  vlan = 18
                }
              },
              "Cisco-IOS-XE-switch:mode" = {
                access = {}
              }
            }
          })
 
        }
      ]
    }
  ]
}

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place
 
Terraform will perform the following actions:
 
  # iosxe_restconf.gigabit_ethernet will be updated in-place
  ~ resource "iosxe_restconf" "gigabit_ethernet" {
      ~ attributes = {} -> (known after apply)
        id         = "Cisco-IOS-XE-native:native/interface"
      ~ lists      = [
          ~ {
              ~ items = [
                  ~ {
                      ~ "description"       = "Configured via test" -> "Configured via Terraform"
                      + "switchport-config" = jsonencode(
                            {
                              + switchport = {
                                  + "Cisco-IOS-XE-switch:access" = {
                                      + vlan = {
                                          + vlan = 18
                                        }
                                    }
                                  + "Cisco-IOS-XE-switch:mode"   = {
                                      + access = {}
                                    }
                                }
                            }
                        )
                        # (1 unchanged element hidden)
                    },
                ]
                name  = "GigabitEthernet"
                # (1 unchanged attribute hidden)
            },
        ]
        # (2 unchanged attributes hidden)
    }
 
Plan: 0 to add, 1 to change, 0 to destroy.
 
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
 
  Enter a value: yes
 
iosxe_restconf.gigabit_ethernet: Modifying... [id=Cisco-IOS-XE-native:native/interface]
╷
│ Error: Client Error
│ 
│   with iosxe_restconf.gigabit_ethernet,
│   on interface_restconf.tf line 1, in resource "iosxe_restconf" "gigabit_ethernet":
│    1: resource "iosxe_restconf" "gigabit_ethernet" {
│ 
│ Failed to configure object (PATCH), got error: HTTP Request failed: StatusCode 400, RESTCONF errors {Error:[{ErrorType:application ErrorTag:malformed-message ErrorAppTag:
│ ErrorPath:/Cisco-IOS-XE-native:native/interface ErrorMessage:invalid value for: switchport-config in
│ /ios:native/ios:interface/ios:GigabitEthernet[ios:name='1/0/10']/ios:switchport-config ErrorInfo:}]} {PatchId: GlobalStatus:{Ok:false Errors:{Error:[]}}
│ EditStatus:{Edit:[]} Errors:{Error:[]}}
@danischm
Copy link
Member

You cannot use jsonencode() to provide nested attributes. You can use a path syntax to refer to nested leafs in YANG models:

"switchport-config/switchport/Cisco-IOS-XE-switch:access/vlan/vlan" = 100

@danischm danischm added the question Further information is requested label Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants