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

Allow provider configuration to be passed by its caller #1052

Open
4 of 5 tasks
chanster opened this issue Dec 7, 2023 · 1 comment
Open
4 of 5 tasks

Allow provider configuration to be passed by its caller #1052

chanster opened this issue Dec 7, 2023 · 1 comment

Comments

@chanster
Copy link

chanster commented Dec 7, 2023

System Information

Linux distribution

Ubuntu 22.04

Terraform version

v1.6.4

Provider and libvirt versions

libvirt 0.7.6

Checklist

  • Is your issue/contribution related with enabling some setting/option exposed by libvirt that the plugin does not yet support, or requires changing/extending the provider terraform schema?

    • Make sure you explain why this option is important to you, why it should be important to everyone. Describe your use-case with detail and provide examples where possible.
    • If it is a very special case, consider using the XSLT support in the provider to tweak the definition instead of opening an issue
    • Maintainers do not have expertise in every libvirt setting, so please, describe the feature and how it is used. Link to the appropriate documentation
  • Is it a bug or something that does not work as expected? Please make sure you fill the version information below:

Description of Issue/Question

When calling libvirt resources within a module, it does not support the provider configuration provided by the caller. Having a module to create virtual machines provides a easy way to create them in mass rather than creating multiple similar resources at the root configuration.

lets say I have a module machine in {working_dir}/machine:

Setup

main.tf:

terraform {
  required_providers {
    libvirt = {
      source  = "registry.terraform.io/dmacvicar/libvirt"
      version = "0.7.6"
    }
  }
}

provider "libvirt" {
  uri = "qemu:///session"
}

module "vm" {
  source = "./machine"
  count  = 3
  name   = vm[count.index]
  ...
}

./machine/main.tf:

terraform {
  required_providers {
    libvirt = {
      source  = "registry.terraform.io/dmacvicar/libvirt"
      version = "0.7.6"
    }
  }
}

provider "libvirt" {
  uri = "qemu:///session"
}

resource "libvirt_volume" "root" {
  name           = "${var.name}.qcow2"
  source         = var.vm_image
  size           = 10 * 1024 * 1024 * 1024 # convert GB to bytes
  pool           = var.pool_name
}

resource "libvirt_domain" "main" {
  name = var.name
  vcpu = var.cpus

  cpu {
    mode = "host-passthrough"
  }

  network_interface {
    network_id     = var.network_id
    hostname       = var.hostname == null ? var.name : var.hostname
    addresses      = [var.ip_address]
    wait_for_lease = false
  }

  disk {
    volume_id = libvirt_volume.root.id
    scsi      = "true"
  }
}

Steps to Reproduce Issue

  1. Create a module that uses libvirt provider.
  2. Call the module created from terraform root configuration.
  3. run terraform init
    terraform init
    
    Initializing the backend...
    Initializing modules...
    ╷
    │ Error: Module is incompatible with count, for_each, and depends_on
    │ 
    │   on machines.tf line 16, in module "vm":
    │    16:   count = 3
    │ 
    │ The module at module.machine is a legacy module which contains its own local provider configurations, and so calls to it may not use the count, for_each, or depends_on arguments.
    │ 
    │ If you also control the module "./machine", consider updating this module to instead expect provider configurations to be passed by its caller.
  4. removing provider block from ./machines/main.tf allows module to work but no longer runs with correct context of qemu:///session, and defaults to qemu:///system.
    # removed this block
    provider "libvirt" {
      uri = "qemu:///session"
    }
  5. Removing terraform block from ./machines/main.tf causes module not correctly resolve.
    │ Error: Failed to query available provider packages
    │ 
    │ Could not retrieve the list of available versions for provider hashicorp/libvirt: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/libvirt
    │ 
    │ Did you intend to use dmacvicar/libvirt? If so, you must specify that source address in each module which requires that provider. To see which modules are currently depending on hashicorp/libvirt, run the following
    │ command:
    │     terraform providers

Additional information:

Do you have SELinux or Apparmor/Firewall enabled? Some special configuration? No
Have you tried to reproduce the issue without them enabled? n/a

@memetb
Copy link

memetb commented Feb 22, 2024

I am working on a PR for this (@dmacvicar)

I too see an important need for this: when writing modules, I may make use of virsh on multiple bare metal nodes to accomplish one orchestration event. I should not know as the caller that there are more than one endpoints.

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

No branches or pull requests

2 participants