-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
258 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package azurecaf | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func dataName() *schema.Resource { | ||
resourceMapsKeys := make([]string, 0, len(ResourceDefinitions)) | ||
for k := range ResourceDefinitions { | ||
resourceMapsKeys = append(resourceMapsKeys, k) | ||
} | ||
|
||
return &schema.Resource{ | ||
ReadContext: dataNameRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: "", | ||
}, | ||
"prefixes": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"suffixes": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"random_length": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.IntAtLeast(0), | ||
Default: 0, | ||
}, | ||
"result": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"separator": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: "-", | ||
}, | ||
"clean_input": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: true, | ||
}, | ||
"passthrough": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: false, | ||
}, | ||
"resource_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringInSlice(resourceMapsKeys, false), | ||
ForceNew: true, | ||
}, | ||
"random_seed": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"use_slug": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataNameRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
getNameReadResult(d, meta) | ||
return diag.Diagnostics{} | ||
} | ||
|
||
func getNameReadResult(d *schema.ResourceData, meta interface{}) error { | ||
name := d.Get("name").(string) | ||
prefixes := convertInterfaceToString(d.Get("prefixes").([]interface{})) | ||
suffixes := convertInterfaceToString(d.Get("suffixes").([]interface{})) | ||
separator := d.Get("separator").(string) | ||
resourceType := d.Get("resource_type").(string) | ||
cleanInput := d.Get("clean_input").(bool) | ||
passthrough := d.Get("passthrough").(bool) | ||
useSlug := d.Get("use_slug").(bool) | ||
randomLength := d.Get("random_length").(int) | ||
randomSeed := int64(d.Get("random_seed").(int)) | ||
|
||
convention := ConventionCafClassic | ||
|
||
randomSuffix := randSeq(int(randomLength), &randomSeed) | ||
|
||
namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"} | ||
|
||
resourceName, err := getResourceName(resourceType, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, useSlug, namePrecedence) | ||
if err != nil { | ||
return err | ||
} | ||
d.Set("result", resourceName) | ||
|
||
d.SetId(resourceName) | ||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# azurecaf_name | ||
|
||
The data source azurecaf_name generate a name for a resource (recommended way as data sources are evaluated before resources got created). | ||
|
||
The azurecaf_name resource allows you to: | ||
|
||
* Clean inputs to make sure they remain compliant with the allowed patterns for each Azure resource | ||
* Generate random characters to append at the end of the resource name | ||
* Handle prefix, suffixes (either manual or as per the Azure cloud adoption framework resource conventions) | ||
* Allow passthrough mode (simply validate the output) | ||
|
||
## Exemple usage | ||
This example shows how to get the value of an environment variable. | ||
|
||
```hcl | ||
data "azurecaf_name" "rg_example" { | ||
name = "demogroup" | ||
resource_type = "azurerm_resource_group" | ||
prefixes = ["a", "b"] | ||
suffixes = ["y", "z"] | ||
random_length = 5 | ||
clean_input = true | ||
} | ||
output "rg_example" { | ||
value = data.azurecaf_name.rg_example.result | ||
} | ||
``` | ||
```bash | ||
data.azurecaf_name.rg_example: Reading... | ||
data.azurecaf_name.rg_example: Read complete after 0s [id=a-b-rg-demogroup-sjdeh-y-z] | ||
|
||
Changes to Outputs: | ||
+ rg_example = "a-b-rg-demogroup-sjdeh-y-z" | ||
``` | ||
|
||
## Combined with Azure resource | ||
The data source evaluates the name before the resource got created and is visible at plan time. | ||
|
||
```hcl | ||
data "azurecaf_name" "rg_example" { | ||
name = "demogroup" | ||
resource_type = "azurerm_resource_group" | ||
prefixes = ["a", "b"] | ||
suffixes = ["y", "z"] | ||
random_length = 5 | ||
clean_input = true | ||
} | ||
resource "azurerm_resource_group" "rg" { | ||
name = data.azurecaf_name.rg_example.result | ||
location = "southeastasia" | ||
} | ||
``` | ||
```bash | ||
data.azurecaf_name.rg_example: Reading... | ||
data.azurecaf_name.rg_example: Read complete after 0s [id=a-b-rg-demogroup-wjyhr-y-z] | ||
|
||
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: | ||
+ create | ||
|
||
Terraform will perform the following actions: | ||
|
||
# azurerm_resource_group.rg will be created | ||
+ resource "azurerm_resource_group" "rg" { | ||
+ id = (known after apply) | ||
+ location = "southeastasia" | ||
+ name = "a-b-rg-demogroup-wjyhr-y-z" | ||
} | ||
|
||
Plan: 1 to add, 0 to change, 0 to destroy. | ||
``` | ||
|
||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* **name** - (optional) the basename of the resource to create, the basename will be sanitized as per supported characters set for each Azure resources. | ||
* **prefixes** (optional) - a list of prefix to append as the first characters of the generated name - prefixes will be separated by the separator character | ||
* **suffixes** (optional) - a list of additional suffix added after the basename, this is can be used to append resource index (eg. vm-001). Suffixes are separated by the separator character | ||
* **random_length** (optional) - default to ``0`` : configure additional characters to append to the generated resource name. Random characters will remain compliant with the set of allowed characters per resources and will be appended before suffix(ess). | ||
* **random_seed** (optional) - default to ``0`` : Define the seed to be used for random generator. 0 will not be respected and will generate a seed based in the unix time of the generation. | ||
* **resource_type** (optional) - describes the type of azure resource you are requesting a name from (eg. azure container registry: azurerm_container_registry). See the Resource Type section | ||
* **separator** (optional) - defaults to ``-``. The separator character to use between prefixes, resource type, name, suffixes, random character | ||
* **clean_input** (optional) - defaults to ``true``. remove any noncompliant character from the name, suffix or prefix. | ||
* **passthrough** (optional) - defaults to ``false``. Enables the passthrough mode - in that case only the clean input option is considered and the prefixes, suffixes, random, and are ignored. The resource prefixe is not added either to the resulting string | ||
* **use_slug** (optional) - defaults to ``true``. If a slug should be added to the name - If you put false no slug (the few letters that identify the resource type) will be added to the name. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* **id** - The id of the naming convention object (same as the result value) | ||
* **result** - The generated named for an Azure Resource based on the input parameter and the selected naming convention |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters