jeremmfr / terraform-provider-junos

Terraform provider for Junos devices
https://registry.terraform.io/providers/jeremmfr/junos
MIT License
62 stars 22 forks source link

Bug: Issue with updating configuration via resource junos_policyoptions_policy_statement #528

Closed AndriiPysmenko closed 1 year ago

AndriiPysmenko commented 1 year ago

Hi,

After upgrade terraform provider to version 2.1.0 resource junos_policyoptions_policy_statement starts to work incorrect.

Terraform and Provider Versions

Terraform v1.5.5
on linux_amd64

Terraform Configuration Files

    {
      name = "BGP-in",
      term = [
        {
          name             = "BGP",
          routing_instance = null,
          interface        = null,
          family           = null,
          prefix_list      = null,
          bgp_community    = null,
          protocol         = ["bgp"],
          local_preference = null,
          neighbor         = null,
          evpn_tag         = null,
          routes = [
            {
              route        = "10.1.0.0/16",
              option       = "orlonger",
              option_value = null
            },
            {
              route        = "10.11.0.0/16",
              option       = "orlonger",
              option_value = null
            },
            {
              route        = "10.150.0.0/16",
              option       = "orlonger",
              option_value = null
            },
            // ONLY THIS WAS ADDED TO THE EXIST CONFIRATION
            {
              route        = "10.199.0.0/16",
              option       = "orlonger",
              option_value = null
            }
           // ONLY THIS WAS ADDED TO THE EXIST CONFIRATION
          ],
          as_path_prepend         = null,
          load_balance            = null,
          local_preference_action = "add",
          local_preference_value  = 0,
          metric_action           = "add",
          metric_value            = 0,
          community_action        = "add",
          community_value         = "NC-all",
          next_hop                = null,
          action                  = "accept"
        }
      ]
    }
  ]

resource "junos_policyoptions_policy_statement" "policy_statement_routing_filter" {
  for_each = {
    for ps in local.policy_statement_routing_filter : ps.name => ps
  }
  name = each.value.name
  dynamic "term" {
    for_each = each.value.term
    content {
      name = term.value.name
      from {
        routing_instance = term.value.routing_instance
        interface        = term.value.interface
        family           = term.value.family
        prefix_list      = term.value.prefix_list
        bgp_community    = term.value.bgp_community
        protocol         = term.value.protocol
        local_preference = term.value.local_preference
        neighbor         = term.value.neighbor
        evpn_tag         = term.value.evpn_tag
        dynamic "route_filter" {
          for_each = term.value.routes
          content {
            route        = route_filter.value.route
            option       = route_filter.value.option
            option_value = route_filter.value.option_value
          }
        }
      }
      then {
        as_path_prepend = term.value.as_path_prepend
        load_balance    = term.value.load_balance
        local_preference {
          action = term.value.local_preference_action
          value  = term.value.local_preference_value
        }
        community {
          action = term.value.community_action
          value  = term.value.community_value
        }
        metric {
          action = term.value.metric_action
          value  = term.value.metric_value
        }
        next_hop = term.value.next_hop
        action   = term.value.action
      }
    }
  }
  term {
    name = "last"
    then {
      action = "reject"
    }
  }
  lifecycle {
    create_before_destroy = true
  }
  depends_on = [
    junos_routing_instance.routing_instances
  ]
}

Expected Behavior

Should created next configuration on device:

set policy-options policy-statement BGP-in term BGP from protocol bgp
set policy-options policy-statement BGP-in term BGP from route-filter 10.1.0.0/16 orlonger
set policy-options policy-statement BGP-in term BGP from route-filter 10.11.0.0/16 orlonger
set policy-options policy-statement BGP-in term BGP from route-filter 10.150.0.0/16 orlonger
set policy-options policy-statement BGP-in term BGP from route-filter 10.199.0.0/16 orlonger
set policy-options policy-statement BGP-in term BGP then metric add 0
set policy-options policy-statement BGP-in term BGP then local-preference add 0
set policy-options policy-statement BGP-in term BGP then community add NC-all
set policy-options policy-statement BGP-in term BGP then accept
set policy-options policy-statement BGP-in term last then reject

Actual Behavior

Plan:

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:

  # module.device_config.junos_policyoptions_policy_statement.policy_statement_routing_filter["BGP-in"] will be updated in-place
  ~ resource "junos_policyoptions_policy_statement" "policy_statement_routing_filter" {
        id   = "BGP-in"
        name = "BGP-in"

      ~ term {
            name = "BGP"

          ~ from {
                # (1 unchanged attribute hidden)

              + route_filter {
                  + option = "orlonger"
                  + route  = "10.199.0.0/16"
                }

                # (3 unchanged blocks hidden)
            }

            # (1 unchanged block hidden)
        }

        # (1 unchanged block hidden)
    }

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

Apply:

[2023-08-21T11:27:12.127Z] module.device_config.junos_policyoptions_policy_statement.policy_statement_routing_filter["BGP-in"]: Modifying... [id=BGP-in]
[2023-08-21T11:27:12.127Z] ╷
[2023-08-21T11:27:12.127Z] │ Error: Plugin did not respond
[2023-08-21T11:27:12.127Z] │ 
[2023-08-21T11:27:12.127Z] │   with module.device_config.junos_policyoptions_policy_statement.policy_statement_routing_filter["BGP-in"],
[2023-08-21T11:27:12.127Z] │   on modules/device_config/main.tf line 500, in resource "junos_policyoptions_policy_statement" "policy_statement_routing_filter":
[2023-08-21T11:27:12.127Z] │  500: resource "junos_policyoptions_policy_statement" "policy_statement_routing_filter" {
[2023-08-21T11:27:12.127Z] │ 
[2023-08-21T11:27:12.127Z] │ The plugin encountered an error, and failed to respond to the
[2023-08-21T11:27:12.127Z] │ plugin.(*GRPCProvider).ApplyResourceChange call. The plugin logs may
[2023-08-21T11:27:12.128Z] │ contain more details.
[2023-08-21T11:27:12.128Z] ╵
[2023-08-21T11:27:12.128Z] 
[2023-08-21T11:27:12.128Z] Stack trace from the terraform-provider-junos_v2.1.0 plugin:
[2023-08-21T11:27:12.128Z] 
[2023-08-21T11:27:12.128Z] panic: runtime error: invalid memory address or nil pointer dereference
[2023-08-21T11:27:12.128Z] [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x89c69d]
[2023-08-21T11:27:12.128Z] 
[2023-08-21T11:27:12.128Z] goroutine 16 [running]:
[2023-08-21T11:27:12.128Z] github.com/jeremmfr/terraform-provider-junos/internal/providerfwk.(*policyoptionsPolicyStatementBlockFrom).isEmpty(0x0)
[2023-08-21T11:27:12.128Z]  github.com/jeremmfr/terraform-provider-junos/internal/providerfwk/resource_policyoptions_policy_statement.go:995 +0x1d
[2023-08-21T11:27:12.128Z] github.com/jeremmfr/terraform-provider-junos/internal/providerfwk.(*policyoptionsPolicyStatementData).set(0xc0002b4310, {0x15d2a00?, 0xc0002b4380?}, 0xc0002b4380?)
[2023-08-21T11:27:12.128Z]  github.com/jeremmfr/terraform-provider-junos/internal/providerfwk/resource_policyoptions_policy_statement.go:2158 +0xd9c
[2023-08-21T11:27:12.128Z] github.com/jeremmfr/terraform-provider-junos/internal/providerfwk.defaultResourceUpdate({0x15d9b60, 0xc0006df800}, {0x15d5eb8, 0xc000012678}, {0x15d2a00?, 0xc0002b4380?}, {0x15d2a40?, 0xc0002b4310}, 0xc0005f32c0)
[2023-08-21T11:27:12.128Z]  github.com/jeremmfr/terraform-provider-junos/internal/providerfwk/default_resource_op.go:262 +0x8a5
[2023-08-21T11:27:12.128Z] github.com/jeremmfr/terraform-provider-junos/internal/providerfwk.(*policyoptionsPolicyStatement).Update(0xc0004f01e8?, {0x15d9b60, 0xc0006df800}, {{{{0x15df168, 0xc000819680}, {0x121c800, 0xc000967590}}, {0x15e0f40, 0xc0006fcbe0}}, {{{0x15df168, ...}, ...}, ...}, ...}, ...)
[2023-08-21T11:27:12.128Z]  github.com/jeremmfr/terraform-provider-junos/internal/providerfwk/resource_policyoptions_policy_statement.go:2033 +0x205
[2023-08-21T11:27:12.128Z] github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).UpdateResource(0xc0000e54a0, {0x15d9b60, 0xc0006df800}, 0xc0009332c8, 0xc0009331d8)
[2023-08-21T11:27:12.128Z]  github.com/hashicorp/terraform-plugin-framework@v1.3.3/internal/fwserver/server_updateresource.go:122 +0x731
[2023-08-21T11:27:12.128Z] github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ApplyResourceChange(0xc0009333e0?, {0x15d9b60, 0xc0006df800}, 0xc000676410, 0xc0009333e0)
[2023-08-21T11:27:12.129Z]  github.com/hashicorp/terraform-plugin-framework@v1.3.3/internal/fwserver/server_applyresourcechange.go:102 +0x1a5
[2023-08-21T11:27:12.129Z] github.com/hashicorp/terraform-plugin-framework/internal/proto5server.(*Server).ApplyResourceChange(0xc0000e54a0, {0x15d9b60?, 0xc0006df6b0?}, 0xc000676370)
[2023-08-21T11:27:12.129Z]  github.com/hashicorp/terraform-plugin-framework@v1.3.3/internal/proto5server/server_applyresourcechange.go:55 +0x41a
[2023-08-21T11:27:12.129Z] github.com/hashicorp/terraform-plugin-mux/tf5muxserver.muxServer.ApplyResourceChange({0xc00043cae0, 0xc000404fc0, 0xc00043cb10, 0xc00043cb40, {0xc00034bbc0, 0x2, 0x2}}, {0x15d9b60?, 0xc0006df380?}, 0xc000676370)
[2023-08-21T11:27:12.129Z]  github.com/hashicorp/terraform-plugin-mux@v0.11.2/tf5muxserver/mux_server_ApplyResourceChange.go:30 +0x139
[2023-08-21T11:27:12.129Z] github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc0005440a0, {0x15d9b60?, 0xc0006de990?}, 0xc0002b4380)
[2023-08-21T11:27:12.129Z]  github.com/hashicorp/terraform-plugin-go@v0.18.0/tfprotov5/tf5server/server.go:821 +0x574
[2023-08-21T11:27:12.129Z] github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x12f3b40?, 0xc0005440a0}, {0x15d9b60, 0xc0006de990}, 0xc0002b4310, 0x0)
[2023-08-21T11:27:12.129Z]  github.com/hashicorp/terraform-plugin-go@v0.18.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:422 +0x170
[2023-08-21T11:27:12.129Z] google.golang.org/grpc.(*Server).processUnaryRPC(0xc0004d6000, {0x15df4b8, 0xc0000e61a0}, 0xc0006a4120, 0xc00052f9b0, 0x1c8f000, 0x0)
[2023-08-21T11:27:12.129Z]  google.golang.org/grpc@v1.56.1/server.go:1337 +0xdf3
[2023-08-21T11:27:12.129Z] google.golang.org/grpc.(*Server).handleStream(0xc0004d6000, {0x15df4b8, 0xc0000e61a0}, 0xc0006a4120, 0x0)
[2023-08-21T11:27:12.129Z]  google.golang.org/grpc@v1.56.1/server.go:1714 +0xa36
[2023-08-21T11:27:12.129Z] google.golang.org/grpc.(*Server).serveStreams.func1.1()
[2023-08-21T11:27:12.129Z]  google.golang.org/grpc@v1.56.1/server.go:959 +0x98
[2023-08-21T11:27:12.129Z] created by google.golang.org/grpc.(*Server).serveStreams.func1
[2023-08-21T11:27:12.129Z]  google.golang.org/grpc@v1.56.1/server.go:957 +0x18c
[2023-08-21T11:27:12.129Z] 
[2023-08-21T11:27:12.129Z] Error: The terraform-provider-junos_v2.1.0 plugin crashed!
[2023-08-21T11:27:12.129Z] 
[2023-08-21T11:27:12.129Z] This is always indicative of a bug within the plugin. It would be immensely
[2023-08-21T11:27:12.129Z] helpful if you could report the crash with the plugin's maintainers so that it
[2023-08-21T11:27:12.130Z] can be fixed. The output above should help diagnose the issue.
[2023-08-21T11:27:12.130Z] 
[2023-08-21T11:27:12.130Z] 
command returned exit code 1

Steps to Reproduce

  1. terraform apply

Additional Context

Everything works as expected till version 2.1.0

References

jeremmfr commented 1 year ago

Thank you for reporting this issue, I'll take care of fixing this quickly.

computeracer commented 1 year ago

@jeremmfr thank you for your work on this. I was working more the the provider and found the state was never migrated and I still needed to do a state remove and import. for it to work. I at first attributed it to perhaps the state thinking I already upgraded to version 2 of this provider (Since I could not rollback to 1.33). When I got to look closer, I wonder if we would need to version the schema as mentioned here to trigger the migration: https://developer.hashicorp.com/terraform/plugin/framework/resources/state-upgrade#stateupgrader-with-priorschema

I could not find a mention of the version field in: https://github.com/jeremmfr/terraform-provider-junos/blob/8c4784690dbd35961e3447dab8fb78d375eb39c9/internal/providerfwk/upgradestate_forwardingoptions_sampling_instance.go

This is a new aspect of Terraform shema to me that I havent gotten to consider in my work with programming go Terraform. Do you think that may be what I am hitting?

jeremmfr commented 1 year ago

@jeremmfr thank you for your work on this. I was working more the the provider and found the state was never migrated and I still needed to do a state remove and import. for it to work. I at first attributed it to perhaps the state thinking I already upgraded to version 2 of this provider (Since I could not rollback to 1.33). When I got to look closer, I wonder if we would need to version the schema as mentioned here to trigger the migration: https://developer.hashicorp.com/terraform/plugin/framework/resources/state-upgrade#stateupgrader-with-priorschema

I could not find a mention of the version field in: https://github.com/jeremmfr/terraform-provider-junos/blob/8c4784690dbd35961e3447dab8fb78d375eb39c9/internal/providerfwk/upgradestate_forwardingoptions_sampling_instance.go

This is a new aspect of Terraform shema to me that I havent gotten to consider in my work with programming go Terraform. Do you think that may be what I am hitting?

@computeracer I think your comment concerns issue #536 and not this one.

If you have already updated the provider, the resource state has already upgraded to new version 1 (instead of 0). In this case, you need to launch the command terraform plan or terraform apply without -refresh=false or command terraform apply -refresh-only to update the value of routing_instance to the default default.