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

Fixed type assertion panic in network ID retrieval #532

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

shurkys
Copy link

@shurkys shurkys commented Feb 26, 2024

Refactored network ID retrieval logic to include checks for nil values and to ensure that the 'id' field is of type float64 before conversion. This prevents a panic caused by improper type assertion and ensures robust error handling in case of missing or invalid network IDs.

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for PR followers and do not help prioritize the request

Description

References

#000

New or Affected Resource(s)

  • opennebula_XXXXX

Checklist

  • I have created an issue and I have mentioned it in References
  • My code follows the style guidelines of this project (use go fmt)
  • My changes generate no new warnings or errors
  • I have updated the unit tests and they pass succesfuly
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation (if needed)
  • I have updated the changelog file

Refactored network ID retrieval logic to include checks for nil values and to ensure that the 'id' field is of type float64 before conversion. This prevents a panic caused by improper type assertion and ensures robust error handling in case of missing or invalid network IDs.
@frousselet frousselet marked this pull request as draft February 29, 2024 09:07
@shurkys shurkys marked this pull request as ready for review March 23, 2024 00:22
Copy link

This pull request is stale because it has been open for 30 days with no activity and it is not in a milestone. Remove 'status: stale' label or comment, or this will be closed in 5 days.

@shurkys
Copy link
Author

shurkys commented May 19, 2024

Reopen plz:

terragrunt import module.talos.opennebula_service.talos 11
data.terraform_remote_state.infrastructure: Reading...
module.talos.opennebula_service.talos: Importing from ID "11"...
module.talos.opennebula_service.talos: Import prepared!
  Prepared opennebula_service for import
module.talos.opennebula_service.talos: Refreshing state... [id=11]
data.terraform_remote_state.infrastructure: Read complete after 1s
╷
│ Error: Plugin did not respond
│ 
│ The plugin encountered an error, and failed to respond to the
│ plugin.(*GRPCProvider).ReadResource call. The plugin logs may contain more
│ details.
╵


Stack trace from the terraform-provider-opennebula_v1.4.0 plugin:

panic: interface conversion: interface {} is float64, not string

goroutine 36 [running]:
github.com/OpenNebula/terraform-provider-opennebula/opennebula.resourceOpennebulaServiceRead({0xeb3880?, 0xc00027e930?}, 0xc0000cba80, {0xc668e0?, 0xc00047e100?})
        github.com/OpenNebula/terraform-provider-opennebula/opennebula/resource_opennebula_service.go:317 +0x116d
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).read(0xc0002cb420, {0xeb3880, 0xc00027e930}, 0xd?, {0xc668e0, 0xc00047e100})
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:724 +0x12e
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc0002cb420, {0xeb3880, 0xc00027e930}, 0xc000467e10, {0xc668e0, 0xc00047e100})
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:1015 +0x585
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ReadResource(0xc0001c64c8, {0xeb3880?, 0xc00027e810?}, 0xc00047efc0)
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/grpc_provider.go:613 +0x497
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ReadResource(0xc0001d8b40, {0xeb3880?, 0xc0001aff50?}, 0xc00047a720)
        github.com/hashicorp/[email protected]/tfprotov5/tf5server/server.go:748 +0x49e
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ReadResource_Handler({0xd2ce00?, 0xc0001d8b40}, {0xeb3880, 0xc0001aff50}, 0xc00043ab60, 0x0)
        github.com/hashicorp/[email protected]/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:349 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0000003c0, {0xeb6b00, 0xc00030ad00}, 0xc0003d5e60, 0xc0002af8c0, 0x13b6930, 0x0)
        google.golang.org/[email protected]/server.go:1335 +0xde3
google.golang.org/grpc.(*Server).handleStream(0xc0000003c0, {0xeb6b00, 0xc00030ad00}, 0xc0003d5e60, 0x0)
        google.golang.org/[email protected]/server.go:1712 +0xa1b
google.golang.org/grpc.(*Server).serveStreams.func1.1()
        google.golang.org/[email protected]/server.go:947 +0xca
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/[email protected]/server.go:958 +0x15c

Error: The terraform-provider-opennebula_v1.4.0 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

ERRO[0002] terraform invocation failed in /home/shurkys/code-server/opennebula/providers/opennebula/services/talos 
ERRO[0002] 1 error occurred:
        * [/home/shurkys/code-server/opennebula/providers/opennebula/services/talos] exit status 1

@treywelsh
Copy link
Collaborator

treywelsh commented Aug 8, 2024

The problem seems to be related to the PR #509

Looking at OpenNebula documentation the schema is not clear on the type inside of networks_values

An other page in the doc show the id value as a string:
image

The type is not so easy to track, the code from Goca seems to unmarshal/marshal the datas several times

Don't hesitate to provide more context in a issue @shurkys (linking some documentation, adding some acceptance test, how to reproduce easily, CHANGELOG informations...), this would help to understand and fix the code more quickly
I assume that your code fix the problem ?

A bit of help to review this @sk4zuzu or @vickmp ?

Copy link

This pull request is stale because it has been open for 30 days with no activity and it is not in a milestone. Remove 'status: stale' label or comment, or this will be closed in 5 days.

@shurkys
Copy link
Author

shurkys commented Sep 30, 2024

variable "opennebula_endpoint" {
	default = "http://192.168.1.5:2633/RPC2"
}
variable "opennebula_flow_endpoint" {
	default = "http://192.168.1.5:2474"
}
variable "opennebula_username" {
	default = "oneadmin"
}
variable "opennebula_password" {
	default = "oneadmin"
}

terraform {
  required_providers {
    opennebula = {
      source = "OpenNebula/opennebula"
      version = "1.4.0"
    }
  }
}

provider "opennebula" {
  endpoint      = var.opennebula_endpoint
  flow_endpoint = var.opennebula_flow_endpoint
  username      = var.opennebula_username
  password      = var.opennebula_password
}

resource "opennebula_template" "test" {
  name        = "test"
  description = "VM template for test"
  cpu         = 1
  vcpu        = 1
  memory      = 1024

  disk {
    image_id = 11
  }
}



data "opennebula_virtual_network" "test" {
  name = "mi_br0" // name of real network
}

resource "opennebula_service_template" "test" {
    template    = jsonencode(
        {
            TEMPLATE = {
                BODY = {
                    deployment  = "straight"
                    description = "Test1"
                    name        = "dev"
                    networks    = {
                        test1 = "M|network|test1| |reserve_from:${data.opennebula_virtual_network.test.id}:SIZE=3"
                    }
                    roles       = [
                        {
                            cardinality          = 1
                            cooldown             = 5
                            max_vms              = 3
                            min_vms              = 1
                            name                 = "test1"
                            shutdown_action      = "terminate-hard"
                            vm_template          = tonumber(resource.opennebula_template.test.id)
                            vm_template_contents = <<-EOT
                                NIC = [
                                  NAME = "_NIC0",
                                  NETWORK_ID = "$test1" ]
                            EOT
                        }
                    ]
                }
            }
        }
    )
    uid         = 0
    uname       = "oneadmin"
}

resource "opennebula_service" "test" {
  name        = "test" // whatever
  template_id = resource.opennebula_service_template.test.id
  extra_template = jsonencode(
    {
      "custom_attrs_values" : {
      },
      "networks_values" : [{
        "test1" : {
          "reserve_from" : data.opennebula_virtual_network.test.id,
          "extra" : "NAME=Test1\nSIZE=3"
        }
      }]
    }
  )
}

Apply

terraform apply  
data.opennebula_virtual_network.test: Reading...
data.opennebula_virtual_network.test: Read complete after 0s [name=mi_br0]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # opennebula_service.test will be created
  + resource "opennebula_service" "test" {
      + extra_template = jsonencode(
            {
              + custom_attrs_values = {}
              + networks_values     = [
                  + {
                      + test1 = {
                          + extra        = <<-EOT
                                NAME=Test1
                                SIZE=3
                            EOT
                          + reserve_from = 0
                        }
                    },
                ]
            }
        )
      + gid            = (known after apply)
      + gname          = (known after apply)
      + id             = (known after apply)
      + name           = "test"
      + networks       = (known after apply)
      + permissions    = (known after apply)
      + roles          = (known after apply)
      + state          = (known after apply)
      + template_id    = (known after apply)
      + uid            = (known after apply)
      + uname          = (known after apply)
    }

  # opennebula_service_template.test will be created
  + resource "opennebula_service_template" "test" {
      + gid         = (known after apply)
      + gname       = (known after apply)
      + id          = (known after apply)
      + name        = (known after apply)
      + permissions = (known after apply)
      + template    = (known after apply)
      + uid         = 0
      + uname       = "oneadmin"
    }

  # opennebula_template.test will be created
  + resource "opennebula_template" "test" {
      + cpu          = 1
      + default_tags = (known after apply)
      + description  = "VM template for test"
      + gid          = (known after apply)
      + gname        = (known after apply)
      + id           = (known after apply)
      + memory       = 1024
      + name         = "test"
      + permissions  = (known after apply)
      + reg_time     = (known after apply)
      + tags_all     = (known after apply)
      + uid          = (known after apply)
      + uname        = (known after apply)
      + vcpu         = 1

      + disk {
          + image_id = 11
        }

      + vmgroup (known after apply)
    }

Plan: 3 to add, 0 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

opennebula_template.test: Creating...
opennebula_template.test: Creation complete after 1s [id=30]
opennebula_service_template.test: Creating...
opennebula_service_template.test: Creation complete after 1s [id=101]
opennebula_service.test: Creating...
opennebula_service.test: Still creating... [10s elapsed]

│ Error: Plugin did not respond

│   with opennebula_service.test,
│   on main.tf line 83, in resource "opennebula_service" "test":
│   83: resource "opennebula_service" "test" {

│ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ApplyResourceChange call. The plugin logs may contain more details.


Stack trace from the terraform-provider-opennebula_v1.4.0 plugin:

panic: interface conversion: interface {} is float64, not string

goroutine 84 [running]:
github.com/OpenNebula/terraform-provider-opennebula/opennebula.resourceOpennebulaServiceRead({0xa1f7c8?, 0x40000b9740?}, 0x4000550c80, {0x7d1700?, 0x40001c9f00?})
        github.com/OpenNebula/terraform-provider-opennebula/opennebula/resource_opennebula_service.go:317 +0xe74
github.com/OpenNebula/terraform-provider-opennebula/opennebula.resourceOpennebulaServiceCreate({0xa1f7c8, 0x40000b9740}, 0x4000550c80, {0x7d1700?, 0x40001c9f00})
        github.com/OpenNebula/terraform-provider-opennebula/opennebula/resource_opennebula_service.go:258 +0xdc4
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0x4000314d20, {0xa1f800, 0x4000219110}, 0xd?, {0x7d1700, 0x40001c9f00})
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:707 +0xec
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0x4000314d20, {0xa1f800, 0x4000219110}, 0x400059c820, 0x4000550a00, {0x7d1700, 0x40001c9f00})
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:837 +0x84c
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0x400000d788, {0xa1f800?, 0x4000218ff0?}, 0x40004d6230)
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/grpc_provider.go:1021 +0xbd0
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0x40002f70e0, {0xa1f800?, 0x40002189c0?}, 0x40001d2310)
        github.com/hashicorp/[email protected]/tfprotov5/tf5server/server.go:818 +0x3c4
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x897b40?, 0x40002f70e0}, {0xa1f800, 0x40002189c0}, 0x40001d2000, 0x0)
        github.com/hashicorp/[email protected]/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:385 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0x400031a1e0, {0xa22a80, 0x4000254000}, 0x4000000c60, 0x400042ab40, 0xf23820, 0x0)
        google.golang.org/[email protected]/server.go:1335 +0xc68
google.golang.org/grpc.(*Server).handleStream(0x400031a1e0, {0xa22a80, 0x4000254000}, 0x4000000c60, 0x0)
        google.golang.org/[email protected]/server.go:1712 +0x854
google.golang.org/grpc.(*Server).serveStreams.func1.1()
        google.golang.org/[email protected]/server.go:947 +0xb4
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/[email protected]/server.go:958 +0x184

Error: The terraform-provider-opennebula_v1.4.0 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants