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

Remove build flag and re-introduce provision method attribute #122

Merged

Conversation

bitkeks
Copy link
Collaborator

@bitkeks bitkeks commented May 17, 2023

The build flag was introduced as the replacement for the "method" attribute. Earlier, the method could be set to "build" or "image", which is then used by Foreman. The initial creation of a host also needed the build flag to be set in the API request to signal Foreman that it should create the machine.

This lead to the problem that Terraform configs containing "build = true" would reset the build flag in Foreman even for existing, already built machines, resetting them at the next boot. This is dangerous and should be avoided.

The commit removes the build flag completely from Terraform and uses the "provision_method" attribute from the Foreman API (again).

We will have to find an additional attribute to support the use case of "enforced rebuild" that originally lead to the switch.

The "managed" flag is not changed.

Refs #40
Refs #68
Refs #106

The build flag was introduced as the replacement for the "method"
attribute. Earlier, the method could be set to "build" or "image", which
is then used by Foreman. The initial creation of a host also needed the
build flag to be set in the API request to signal Foreman that it should
create the machine.

This lead to the problem that Terraform configs containing "build = true"
would reset the build flag in Foreman even for existing, already built
machines, resetting them at the next boot. This is dangerous and should
be avoided.

The commit removes the build flag completely from Terraform and uses the
"provision_method" attribute from the Foreman API (again).

We will have to find an additional attribute to support the use case of
"enforced rebuild" that originally lead to the switch.

The "managed" flag is not changed.

Refs terraform-coop#40
Refs terraform-coop#68
Refs terraform-coop#106
…ning

See examples/host/vmware_imagebased_host.tf for a VMware-backed image VM
Refs terraform-coop#103

Refs terraform-coop#102
Refs terraform-coop#105
@bitkeks bitkeks marked this pull request as ready for review June 20, 2023 17:19
@togoschi
Copy link
Contributor

togoschi commented Jun 26, 2023

My tests with self-compiled binary were successful either with network-based or image-based provisioning method (running against foreman v3.6.1)

My test code (image-based provisioning example with vSphere provider)

data "foreman_image" "ubuntu" {
  name ="Ubuntu 22.04 LTS"
  compute_resource_id = data.foreman_computeresource.vsphere.id
}

// [Foreman Hosts]--------------------------
resource "foreman_host" "image_based_machine" {
  count               = var.hosts.count
  shortname       = format(var.hosts.hostname_format, count.index + 1)
  provision_method = "image"
  image_id         = data.foreman_image.ubuntu.id

  compute_attributes = format(<<EOF
    {
        "image_id": "%s"
    }
  EOF
  ,
  data.foreman_image.ubuntu.uuid)

  hostgroup_id        = foreman_hostgroup.ubuntu.id
  compute_resource_id = data.foreman_computeresource.vsphere.id
}

@@ -953,6 +946,10 @@ func setResourceDataFromForemanHost(d *schema.ResourceData, fh *api.ForemanHost)
log.Printf("[WARN] error setting compute attributes: %s", err)
}

// See issue #115 for "Build" attribute
d.Set("managed", fh.Managed)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes #124

// log.Debugf("computeResource: [%+v]", computeResource)

var diags diag.Diagnostics
if h.ProvisionMethod == "image" { // && strings.ToLower(computeResource.Provider) == "vmware" {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might differ in other providers, so a hard fail when image_id is not given might introduce problems for other users. See #125 (reply in thread)

Removes an experimental check of the compute_attributes when using "image"-based
host creation. The backend providers need different values.

VMware for example needs the image_id as UUID, see the example file
in examples/host/vmware_imagebased_host.tf

Refs terraform-coop#125
Copy link
Collaborator

@agriffit79 agriffit79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick test and seems ok

foreman/resource_foreman_host.go Show resolved Hide resolved
foreman/resource_foreman_host.go Show resolved Hide resolved
diags = append(diags, diag)
return diags

} else if _, ok := h.ComputeAttributes["image_id"]; !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image_id might be required for vmware but I don't believe it is for other plugins. It can also be implicitly provided through a compute_profile. As in this example. Every virtualisation plugin is different, I think trying to validate the content of compute_attributes is going to be impossible. Every PR risks breaking existing behaviour as no one can test all possible plugins.

@agriffit79 agriffit79 merged commit 2e2fe78 into terraform-coop:master Jun 30, 2023
7 checks passed
bitkeks added a commit to mindfulsecurity/terraform-provider-foreman that referenced this pull request Jul 17, 2023
PR terraform-coop#122 removed the "build" argument, removing the possibility to
enforce setting the Foreman-internal build flag to "true". This commit
re-introduces this ability, but with a more explicit argument name:
"set_build_flag". And it defaults to "false", so that not specifying it
does not toggle the flag to "true" on accident.

Resolves terraform-coop#130

See terraform-coop#125
bitkeks added a commit that referenced this pull request Jul 19, 2023
PR #122 removed the "build" argument, removing the possibility to
enforce setting the Foreman-internal build flag to "true". This commit
re-introduces this ability, but with a more explicit argument name:
"set_build_flag". And it defaults to "false", so that not specifying it
does not toggle the flag to "true" on accident.

Resolves #130

See #125
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.

None yet

3 participants