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

User Story 125263 #238

Merged
merged 8 commits into from
Jul 24, 2023
Merged

User Story 125263 #238

merged 8 commits into from
Jul 24, 2023

Conversation

TomArcherMsft
Copy link
Collaborator

@TomArcherMsft TomArcherMsft temporarily deployed to acctests July 17, 2023 19:30 — with GitHub Actions Inactive
Copy link
Member

@lonegunmanb lonegunmanb left a comment

Choose a reason for hiding this comment

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

Thanks @TomArcherMsft for opening this pr, a few comments.

resource "azapi_resource" "ssh_public_key" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
name = random_pet.ssh_key_name.id
location = "westus3"
Copy link
Member

Choose a reason for hiding this comment

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

Is there any special reason we use hard coded location rather than var.resource_group_location here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

response_export_values = ["publicKey"]
}

output "key_data" {
Copy link
Member

Choose a reason for hiding this comment

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

Could we move this output block to outputs.tf file?

And I've tried this output, it's value is empty. If we want to export the generated public key data as output, we can use the following code snippet:

output "key_data" {
  value     = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}

Since public key data is not a secret and meant to be shared, I just removed sensitive = true.

Copy link
Collaborator Author

@TomArcherMsft TomArcherMsft Jul 20, 2023

Choose a reason for hiding this comment

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

Right now, the ssh.tf file is fully encapsulated, meaning that I can drop it into any configuration and it works. I want to keep this pattern instead of having functionality spread across multiple files.

Copy link
Member

Choose a reason for hiding this comment

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

Right now, the ssh.tf file is fully encapsulated, meaning that I can drop it into any configuration and it works. I want to keep this pattern instead of having functionality spread across multiple files.

That makes sense to me.

But I think we still need to ensure that value = azapi_resource.ssh_public_key.body could output the public key, and I still think we could remove this sensitive = true because the public key is meant to be shared.

variable "username" {
type = string
description = "The username for the local account that will be created on the new VM."
default = "azureadmin"
Copy link
Member

Choose a reason for hiding this comment

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

The default value is different than the corresponding default value in coalesce function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

admin_username = "azureuser"
disable_password_authentication = true
computer_name = "hostname"
admin_username = coalesce(var.username, "azureuser")
Copy link
Member

Choose a reason for hiding this comment

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

Since we have set default value for var.username already I think we can skip coalesce here and use var.username directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using the coalesce() function allows us to give the reader two options: variable or random. If the reader blanks out the var, they get a random value - such as if they're testing the code by running it multiple times and can't use the same value twice.

Copy link
Member

Choose a reason for hiding this comment

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

I understand that you'd like to set a default value for admin_username when var.username is absent, but things would go differently than you thought.

Using the coalesce() function allows us to give the reader two options: variable or random. If the reader blanks out the var, they get a random value

I didn't see any random factors in this case. According to the document:

coalesce takes any number of arguments and returns the first one that isn't null or an empty string.

So when var.username is null or empty string, coalesce function would return azureuser as default value.

variable "username" {
  type        = string
  description = "The username for the local account that will be created on the new VM."
  default     = "azureadmin"
}

If the module's caller omits var.username, then var.username's value would be azureadmin.

admin_username                  = coalesce(var.username, "azureuser")

So when the caller omits var.username, admin_username would be azureadmin, not azureuser.

Btw this is also the reason I suggested a nullable = false for var.username, if we do so, when user set var.username to null on purpose, the var.username's value would still be azureadmin.


admin_ssh_key {
username = "azureuser"
public_key = tls_private_key.example_ssh.public_key_openssh
username = coalesce(var.username, "azureuser")
Copy link
Member

Choose a reason for hiding this comment

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

Same issue here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using the coalesce() function allows us to give the reader two options: variable or random. If the reader blanks out the var, they get a random value - such as if they're testing the code by running it multiple times and can't use the same value twice.

Copy link
Member

Choose a reason for hiding this comment

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

variable "username" {
  type        = string
  description = "The username for the local account that will be created on the new VM."
  default     = "azureadmin"
}

If the module's caller omits var.username, then var.username's value would be azureadmin.

admin_username                  = coalesce(var.username, "azureuser")

So when the caller omits var.username, admin_username would be azureadmin, not azureuser.

}


variable "username" {
Copy link
Member

Choose a reason for hiding this comment

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

Since we've set default value for this variable I think we can set nullable = false for this block too.

Copy link
Contributor

@grayzu grayzu left a comment

Choose a reason for hiding this comment

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

LGTM

@TomArcherMsft TomArcherMsft temporarily deployed to acctests July 20, 2023 19:43 — with GitHub Actions Inactive
Copy link
Member

@lonegunmanb lonegunmanb left a comment

Choose a reason for hiding this comment

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

Hi @TomArcherMsft:

  1. It looks like you've made changes in 101-vm-cluster-linux folder, is that correct modification?
  2. The issues I addressed in 101-vm-with-infrastructure are still there.
  3. It'll be unnecessary to use coalesce along with variable's default value.
  4. I've tested and verified that output "key_data" block would output an empty object {}. Could we change the value to the following code snippet?:
output "key_data" {
  value     = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}

admin_username = "azureuser"
disable_password_authentication = true
computer_name = "hostname"
admin_username = coalesce(var.username, "azureuser")
Copy link
Member

Choose a reason for hiding this comment

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

I understand that you'd like to set a default value for admin_username when var.username is absent, but things would go differently than you thought.

Using the coalesce() function allows us to give the reader two options: variable or random. If the reader blanks out the var, they get a random value

I didn't see any random factors in this case. According to the document:

coalesce takes any number of arguments and returns the first one that isn't null or an empty string.

So when var.username is null or empty string, coalesce function would return azureuser as default value.

variable "username" {
  type        = string
  description = "The username for the local account that will be created on the new VM."
  default     = "azureadmin"
}

If the module's caller omits var.username, then var.username's value would be azureadmin.

admin_username                  = coalesce(var.username, "azureuser")

So when the caller omits var.username, admin_username would be azureadmin, not azureuser.

Btw this is also the reason I suggested a nullable = false for var.username, if we do so, when user set var.username to null on purpose, the var.username's value would still be azureadmin.


admin_ssh_key {
username = "azureuser"
public_key = tls_private_key.example_ssh.public_key_openssh
username = coalesce(var.username, "azureuser")
Copy link
Member

Choose a reason for hiding this comment

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

variable "username" {
  type        = string
  description = "The username for the local account that will be created on the new VM."
  default     = "azureadmin"
}

If the module's caller omits var.username, then var.username's value would be azureadmin.

admin_username                  = coalesce(var.username, "azureuser")

So when the caller omits var.username, admin_username would be azureadmin, not azureuser.

response_export_values = ["publicKey"]
}

output "key_data" {
Copy link
Member

Choose a reason for hiding this comment

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

Right now, the ssh.tf file is fully encapsulated, meaning that I can drop it into any configuration and it works. I want to keep this pattern instead of having functionality spread across multiple files.

That makes sense to me.

But I think we still need to ensure that value = azapi_resource.ssh_public_key.body could output the public key, and I still think we could remove this sensitive = true because the public key is meant to be shared.

@TomArcherMsft
Copy link
Collaborator Author

@lonegunmanb I misspoke about the random value. I see your point now :)

@TomArcherMsft TomArcherMsft temporarily deployed to acctests July 21, 2023 02:37 — with GitHub Actions Inactive
@TomArcherMsft TomArcherMsft temporarily deployed to acctests July 21, 2023 14:42 — with GitHub Actions Inactive
@TomArcherMsft TomArcherMsft temporarily deployed to acctests July 22, 2023 15:28 — with GitHub Actions Inactive
Copy link
Member

@lonegunmanb lonegunmanb left a comment

Choose a reason for hiding this comment

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

Thanks @TomArcherMsft for the update, LGTM!

@lonegunmanb lonegunmanb merged commit 58245fa into Azure:master Jul 24, 2023
3 checks passed
@TomArcherMsft TomArcherMsft deleted the UserStory125263 branch July 24, 2023 13:06
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.

3 participants