|
| 1 | += Coding conventions |
| 2 | +ifdef::env-github[] |
| 3 | +:tip-caption: :bulb: |
| 4 | +:note-caption: :information_source: |
| 5 | +:important-caption: :heavy_exclamation_mark: |
| 6 | +:caution-caption: :fire: |
| 7 | +:warning-caption: :warning: |
| 8 | +endif::[] |
| 9 | +:sectnums: |
| 10 | +:toc: |
| 11 | + |
| 12 | +:uri-terraform-standard-module-structure: https://www.terraform.io/docs/language/modules/develop/structure.html |
| 13 | + |
| 14 | +This is a list of coding conventions you should observe when contributing code for this project. |
| 15 | + |
| 16 | +Current conventions: |
| 17 | + |
| 18 | +- module structure |
| 19 | +- adopting the right case type |
| 20 | +- good names for files and terraform objects (resources, variables, outputs) |
| 21 | +
|
| 22 | +It is not an exhaustive list and it will be updated as we go. In the mean time, your PR reviewer may suggest conventions that are not listed here. |
| 23 | + |
| 24 | +Use PR comments and the GitHub suggestion feature to agree on the final result. |
| 25 | + |
| 26 | +== Module Structure |
| 27 | + |
| 28 | +- We adhere to the {uri-terraform-standard-module-structure}[Terraform Standard Module Structure] |
| 29 | +- Any nested module calls should be in the main file: `main.tf` is the primary entrypoint of the module |
| 30 | +- All variables and ouputs declarations should be in `variables.tf` and `outputs.tf` |
| 31 | +- All variables and outputs should have descriptions |
| 32 | +- Nested modules should exist under the `modules/` folder |
| 33 | +- Examples of using the module should exist under the `examples/` folder and have a README to explain the goal and usage of the example |
| 34 | + |
| 35 | +== Documentation format |
| 36 | + |
| 37 | +We use https://asciidoc.org/[AsciiDoc] with the `*.adoc` file extension. The only exception is for the README files that need to be displayed on the Terraform Registry: as the Terraform Registry does not support AsciiDoc, the `README` and certain other documentation must be available in Markdown format. |
| 38 | + |
| 39 | +[NOTE] |
| 40 | +==== |
| 41 | +As GitHub renders AsciiDoc files using https://asciidoctor.org/[AsciiDoctor], it is the current reference regarding AsciiDoc syntax we use in the documentation. |
| 42 | +==== |
| 43 | + |
| 44 | +=== Internal links |
| 45 | + |
| 46 | +For internal links to AsciiDoc file (repo documentation content), we use relative links with the `xref:` macro. |
| 47 | + |
| 48 | +``` |
| 49 | +xref:CONTRIBUTING.adoc[CONTRIBUTING] |
| 50 | +``` |
| 51 | + |
| 52 | +For internal links to non-AsciiDoc file (e.g: README files), we use relative links with the `link:` macro. |
| 53 | + |
| 54 | +``` |
| 55 | +link:examples/README.md[examples README] |
| 56 | +``` |
| 57 | + |
| 58 | +=== External links |
| 59 | + |
| 60 | +For external links, we use https://docs.asciidoctor.org/asciidoc/latest/macros/autolinks/[autolinks]. |
| 61 | + |
| 62 | +[TIP] |
| 63 | +==== |
| 64 | +Links that are used multiple times must be defined in the header using the `:uri-xxx:` reference format. This makes any future updates to the URI much simpler. |
| 65 | +
|
| 66 | +. Defining a link in the document header |
| 67 | +. Calling the reference |
| 68 | +
|
| 69 | +---- |
| 70 | +:uri-repo: https://github.com/oracle-terraform-modules/terraform-oci-vcn |
| 71 | +
|
| 72 | +{uri-repo}[link caption] |
| 73 | +---- |
| 74 | +
|
| 75 | +==== |
| 76 | + |
| 77 | +=== Terraform registry requirements |
| 78 | + |
| 79 | +- README files must be in Markdown format |
| 80 | +- All links must use absolute path, relative links are not supported |
| 81 | + |
| 82 | +== Terraform code |
| 83 | + |
| 84 | +=== Case type, Files, Names |
| 85 | + |
| 86 | +- Use `snake_case` when naming Terraform files, variables and resources |
| 87 | +- If you need a new .tf file for better clarity, use this naming scheme: `<resources_group>`: e.g. `subnets.tf`, `nsgs.tf` |
| 88 | +- If your variable is controlling a behaviour, use imperative style to name it: e.g. `create_internet_gateway`, `use_encryption` |
| 89 | + |
| 90 | +=== Variable blocks |
| 91 | + |
| 92 | +Variables should always be in the format below: |
| 93 | + |
| 94 | +---- |
| 95 | +variable "xyz" { |
| 96 | + default = "A default value" |
| 97 | + description: "Add (Updatable) at the begining of the description if this value do not triggers a resource recreate" |
| 98 | + type: string |
| 99 | +---- |
| 100 | + |
| 101 | +When defining variables: |
| 102 | + |
| 103 | +. do not hesitate to insert a brief comment in the variable block if it helps to clarify your intention. |
| 104 | +. the type should always be specified. We prefer: |
| 105 | +.. string |
| 106 | +.. boolean |
| 107 | +.. number |
| 108 | +.. collection (list, set, map, tuples) of the above |
| 109 | +. if you specify a default value, it should: |
| 110 | +.. reflect a sensible default |
| 111 | +.. match the type specified |
| 112 | +.. be reflected in terraform.tfvars.example |
| 113 | + |
| 114 | +WARNING: No default value for `compartment_id` or any other variables related to provider authentication in module or examples files. The user will have to explicitly set these values. |
| 115 | + |
| 116 | +=== Examples |
| 117 | + |
| 118 | +Examples should promote good practices as much as possible e.g. avoid creating resources in the tenancy root compartment. |
0 commit comments