Skip to content

Commit

Permalink
refactor: unify main resource into a single file (#6)
Browse files Browse the repository at this point in the history
* docs: update copyright message in license

* docs: update gitignore

* chore: update required versions in terraform settings

* style: terraform fmt

* chore: now output the id of virtual machine

* docs: update readme

* refactor: unify virtual machine resource into a single file
  • Loading branch information
jortfal authored Apr 8, 2021
1 parent aa2a561 commit f860d3d
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 321 deletions.
16 changes: 12 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
# Crash log files
crash.log

# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
# .tfvars files are managed as part of configuration and so should be included in
# version control.
# Exclude all .tfvars files, which are likely to contain sentitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
#
# example.tfvars
*.tfvars

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
Expand All @@ -27,3 +28,10 @@ override.tf.json

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc

# Ignore terraform init lock file
.terraform.lock.hcl
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2021 jortfal | José Mnauel Ortega Falcón

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
120 changes: 105 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# azure_virtual_machine

![Release Date](https://img.shields.io/github/release-date/jortfal/azure_vitual_machine)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/jortfal/azure_virtual_machine)
![GitHub Release Date](https://img.shields.io/github/release-date/jortfal/azure_virtual_machine)
![GitHub license](https://img.shields.io/github/license/jortfal/azure_virtual_machine)
![Maintenance](https://img.shields.io/maintenance/yes/2020?color=green)
![Maintenance](https://img.shields.io/maintenance/yes/2021?color=green)
![Maintainer](https://img.shields.io/badge/maintainer-jortfal-green)

Module of Terraform to created a Virtual Machine, linux or windows, in Microsoft Azure.
Expand All @@ -11,12 +12,6 @@ Module of Terraform to created a Virtual Machine, linux or windows, in Microsoft

TO DO

## Terraform versions

Terraform 0.13.x

No support Terraform <= 0.12

## Usage

TO DO
Expand All @@ -34,27 +29,122 @@ No issue is creating limit on this module.

| Name | Version |
|------|---------|
| terraform | >= 0.13.0, < 0.14 |
| azure-cli | >= 2.12.1 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0, <= 0.14.10 |
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | >= 2.30.0, <= 2.54.0 |

## Providers

| Name | Version |
|------|---------|
| azurerm | >= 2.20.0, <= 2.30.0 |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | >= 2.30.0, <= 2.54.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [azurerm_network_interface.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) | resource |
| [azurerm_virtual_machine.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine) | resource |
| [azurerm_resource_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source |

## Inputs

TO DO
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_delete_data_disks_on_termination"></a> [delete\_data\_disks\_on\_termination](#input\_delete\_data\_disks\_on\_termination) | True to delete the data disks automatically when deleting the VM | `bool` | `false` | no |
| <a name="input_delete_os_disk_on_termination"></a> [delete\_os\_disk\_on\_termination](#input\_delete\_os\_disk\_on\_termination) | True to delete the OS disk automatically when deleting the VM | `bool` | `false` | no |
| <a name="input_name"></a> [name](#input\_name) | The name of virtual machine | `string` | n/a | yes |
| <a name="input_network_interface_list"></a> [network\_interface\_list](#input\_network\_interface\_list) | A list of network interfaces | <pre>list(<br> object({<br> name = string<br> ip_configuration = object({<br> name = string<br> subnet_id = string<br> private_ip_address_allocation = string<br> primary = bool<br> })<br> })<br> )</pre> | `[]` | no |
| <a name="input_os_profile"></a> [os\_profile](#input\_os\_profile) | Settings of virtual machine | <pre>object({<br> computer_name = string<br> admin_username = string<br> admin_password = string<br> })</pre> | n/a | yes |
| <a name="input_os_profile_linux_config"></a> [os\_profile\_linux\_config](#input\_os\_profile\_linux\_config) | Config profile of linux | `map(bool)` | <pre>{<br> "disable_password_authentication": false<br>}</pre> | no |
| <a name="input_os_profile_windows_config"></a> [os\_profile\_windows\_config](#input\_os\_profile\_windows\_config) | Config profile of windows os | `map(bool)` | <pre>{<br> "enable_automatic_upgrades": false,<br> "provision_vm_agent": false<br>}</pre> | no |
| <a name="input_os_type"></a> [os\_type](#input\_os\_type) | The type of os, it can be 'linux' or 'windows' | `string` | `null` | no |
| <a name="input_primary_network_interface_id"></a> [primary\_network\_interface\_id](#input\_primary\_network\_interface\_id) | The ID of the primary network interface | `string` | `null` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | The name of resource group where to create the virtual machine | `string` | n/a | yes |
| <a name="input_storage_data_disk_list"></a> [storage\_data\_disk\_list](#input\_storage\_data\_disk\_list) | A list of data disk | <pre>list(object({<br> name = string<br> caching = string<br> create_option = string<br> disk_size_gb = number<br> lun = number<br> write_accelerator_enabled = bool<br> managed_disk_type = string<br> managed_disk_id = string<br> }))</pre> | `[]` | no |
| <a name="input_storage_image_reference"></a> [storage\_image\_reference](#input\_storage\_image\_reference) | The image reference of virtual machine | <pre>object({<br> publisher = string<br> offer = string<br> sku = string<br> version = string<br> })</pre> | n/a | yes |
| <a name="input_storage_image_reference_id"></a> [storage\_image\_reference\_id](#input\_storage\_image\_reference\_id) | The ID of the Custom Image which the Virtual Machine should be created from | `string` | `null` | no |
| <a name="input_storage_os_disk"></a> [storage\_os\_disk](#input\_storage\_os\_disk) | The storage os disk settings | <pre>object({<br> name = string<br> caching = string<br> create_option = string<br> managed_disk_type = string<br> })</pre> | <pre>{<br> "caching": null,<br> "create_option": null,<br> "managed_disk_type": null,<br> "name": null<br>}</pre> | no |
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to security group | `map(string)` | `{}` | no |
| <a name="input_vm_size"></a> [vm\_size](#input\_vm\_size) | The size of virtual machine | `string` | n/a | yes |

## Outputs

TO DO
| Name | Description |
|------|-------------|
| <a name="output_id"></a> [id](#output\_id) | The ID of the Virtual Machine |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Tests

### Terraform fmt - Style Conventions

Rewrites all Terraform configuration files to a canonical format. Both configuration files (.tf) and variables files (.tfvars) are updated.

- [Documentation](https://www.terraform.io/docs/cli/commands/fmt.html)

#### To display diffs of formatting changes

```bash
# $ terraform fmt --help
# Usage:
# terraform fmt [options] [DIR]

$ terraform fmt -check --recursive --diff
```

#### To rewrite Terraform configuration files to a canonical format and style

```bash
# $ terraform fmt --help
# Usage:
# terraform fmt [options] [DIR]

$ terraform fmt --recursive
```

### TFLint

The configuration of TFLint can be found in the file `.tflint.-hcl`

- [Documentation](https://github.com/terraform-linters/tflint/blob/master/README.md)

- [User Guide](https://github.com/terraform-linters/tflint/tree/master/docs/user-guide)

```bash
# $ tflint --help
# Usage:
# tflint [OPTIONS] [FILE or DIR...]

$ tflint
```

### Terraform validate

Validate the configuration files in a directory, referring only to the configuration
and not accessing any remote services such as remote state, provider APIs, etc.

- [Documentation](https://www.terraform.io/docs/cli/commands/validate.html)

```bash
# $ terraform validate --help
# Usage:
# terraform validate [options] [dir]
$ terraform init -backend=false
$ terraform validate
```

### Terraform TFSec

### Terraform Docs

## Authors

Module managed by [José Manuel Ortega Falcón](https://github.com/jortfal).
Module managed by [jortfal | Jose Manuel Ortega Falcon](https://github.com/jortfal).

## License

Apache 2 Licensed. See LICENSE for full details.
Apache 2 Licensed. See `LICENSE` file for full details.
2 changes: 1 addition & 1 deletion data_sources.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 José Mnauel Ortega Falcón
Copyright 2021 jortfal | José Mnauel Ortega Falcón
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
54 changes: 37 additions & 17 deletions main_linux.tf → main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 José Mnauel Ortega Falcón
Copyright 2021 jortfal | José Mnauel Ortega Falcón
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@ limitations under the License.

# Linux Virtual Machine
resource "azurerm_virtual_machine" "this" {
count = var.os_type == "linux" ? 1 : 0

name = var.name
location = data.azurerm_resource_group.this.location
Expand All @@ -28,11 +27,21 @@ resource "azurerm_virtual_machine" "this" {
delete_os_disk_on_termination = var.delete_os_disk_on_termination
delete_data_disks_on_termination = var.delete_data_disks_on_termination

storage_image_reference {
publisher = var.storage_image_reference.publisher
offer = var.storage_image_reference.offer
sku = var.storage_image_reference.sku
version = var.storage_image_reference.version
dynamic "storage_image_reference" {
for_each = var.storage_image_reference_id == null ? [1] : []
content {
publisher = var.storage_image_reference.publisher
offer = var.storage_image_reference.offer
sku = var.storage_image_reference.sku
version = var.storage_image_reference.version
}
}

dynamic "storage_image_reference" {
for_each = var.storage_image_reference_id != null ? [1] : []
content {
id = var.storage_image_reference_id
}
}

storage_os_disk {
Expand All @@ -45,14 +54,14 @@ resource "azurerm_virtual_machine" "this" {
dynamic "storage_data_disk" {
for_each = var.storage_data_disk_list
content {
name = storage_data_disk.value["name"]
caching = storage_data_disk.value["caching"]
create_option = storage_data_disk.value["create_option"]
disk_size_gb = storage_data_disk.value["disk_size_gb"]
lun = storage_data_disk.value["lun"]
name = storage_data_disk.value["name"]
caching = storage_data_disk.value["caching"]
create_option = storage_data_disk.value["create_option"]
disk_size_gb = storage_data_disk.value["disk_size_gb"]
lun = storage_data_disk.value["lun"]
write_accelerator_enabled = storage_data_disk.value["write_accelerator_enabled"]
managed_disk_type = storage_data_disk.value["managed_disk_type"]
managed_disk_id = storage_data_disk.value["managed_disk_id"]
managed_disk_type = storage_data_disk.value["managed_disk_type"]
managed_disk_id = storage_data_disk.value["managed_disk_id"]
}
}

Expand All @@ -61,9 +70,20 @@ resource "azurerm_virtual_machine" "this" {
admin_username = var.os_profile.admin_username
admin_password = var.os_profile.admin_password
}

os_profile_linux_config {
disable_password_authentication = var.os_profile_linux_config.disable_password_authentication

dynamic "os_profile_linux_config" {
for_each = var.os_type == "linux" ? [1] : []
content {
disable_password_authentication = var.os_profile_linux_config.disable_password_authentication
}
}

dynamic "os_profile_windows_config" {
for_each = var.os_type == "windows" ? [1] : []
content {
provision_vm_agent = var.os_profile_windows_config.provision_vm_agent
enable_automatic_upgrades = var.os_profile_windows_config.enable_automatic_upgrades
}
}

tags = merge(
Expand Down
73 changes: 0 additions & 73 deletions main_linux_custom.tf

This file was deleted.

Loading

0 comments on commit f860d3d

Please sign in to comment.