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

foreman_host: Replace the "name" attribute with "fqdn" (computed) and "shortname" (required) #121

Merged
merged 10 commits into from
Jul 19, 2023

Conversation

bitkeks
Copy link
Collaborator

@bitkeks bitkeks commented May 17, 2023

As mentioned in #116 the "name" field is problematic in Foreman. The setting "append_domain_name_for_hosts" converts a short hostname into an FQDN. This leads to "inconsistent plan" errors in Terraform.

In earlier versions, a custom unmarshaller removed the domain part from the name attribute:

if fh.DomainName != "" && strings.Contains(fh.ForemanObject.Name, fh.DomainName) {
    fh.ForemanObject.Name = strings.Replace(fh.ForemanObject.Name, "."+fh.DomainName, "", 1)
}

In this case, Foreman always used the FQDN internally, but Terraform received only the shortname in return and had no issues. Since this function is removed, the conflict appears.

For expected behaviour and consistency I propose a switch to "fqdn" and "shortname" instead of the "name" attribute. This PR is the implementation.

The result will look like this:

  • name: Read-only, used in Foreman API as a unique name for a host. Can be either the short name or the expanded FQDN, depending on the respective Foreman setting.
  • shortname: The "new" "name" argument that you should use in Terraform for your hosts. In combination with the domain_id Foreman knows what the domain part should be.
  • fqdn: Read-only, only exists in the Terraform provider. Is constructed from the name received from Foreman. Can be used as a consistent variable value in other parts of the Terraform module files and blocks.

This commit introduces the new attributes "fqdn" and "shortname" to the
Terraform provider for foreman_host objects. Formerly, the "name"
attribute was used to configure and read the hostname of a host. But
this value can be inconsistent, because Foreman might expand a short
hostname to an FQDN, depending on a setting. (this is not a bug)

Because of this unexpected behaviour (results in "inconstistent plan" in
Terraform) the name attribute is now bypassed. Shortname is the new
required input variable.

Summary:
* name: was required, is now optional and computed
* fqdn: new, read-only, always returns the FQDN
* shortname: required and checked for dots (".") to issue warnings

Refs terraform-coop#116
"Shortname" does not exist in the Foreman API, only "name" which is
either shortname or FQDN depending on a setting. For a more expected
behaviour the shortname and fqdn fields were introduced. This commit
fills the Shortname field in the ForemanHost API struct.
@bitkeks bitkeks changed the title Host name fix foreman_host: Replace the "name" attribute with "fqdn" (computed) and "shortname" (required) May 17, 2023
@bitkeks bitkeks marked this pull request as ready for review June 21, 2023 06:53
@bitkeks bitkeks requested review from lhw and agriffit79 June 21, 2023 13:02
bitkeks and others added 4 commits July 10, 2023 15:47
The tests use an API mock response file to check against a mocked
Terraform schema. Since neither "shortname" nor "fqdn" host attributes
are in the Foreman API, we exclude them in the tests. They are
constructed from the "name" given by Foreman, or passed in in case of
the shortname.

To enforce the switch from "name" to "shortname", name is now a
deprecated argument. It is still accessible via the name attribute,
which is filled when the resource is read from the API.
foreman/resource_foreman_host.go Outdated Show resolved Hide resolved
foreman/resource_foreman_host.go Outdated Show resolved Hide resolved
@bitkeks bitkeks requested review from lhw and removed request for agriffit79 July 11, 2023 11:22
@bitkeks bitkeks merged commit d72e9c0 into terraform-coop:master Jul 19, 2023
7 checks passed
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

2 participants