Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Random String and Password Import
Browse files Browse the repository at this point in the history
jahantech committed Mar 18, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 5f827cd commit b143df3
Showing 6 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions random/resource_password.go
Original file line number Diff line number Diff line change
@@ -10,5 +10,8 @@ func resourcePassword() *schema.Resource {
Read: readNil,
Delete: schema.RemoveFromState,
Schema: stringSchemaV1(true),
Importer: &schema.ResourceImporter{
State: ImportPassword,
},
}
}
3 changes: 3 additions & 0 deletions random/resource_string.go
Original file line number Diff line number Diff line change
@@ -12,5 +12,8 @@ func resourceString() *schema.Resource {
MigrateState: resourceRandomStringMigrateState,
SchemaVersion: 1,
Schema: stringSchemaV1(false),
Importer: &schema.ResourceImporter{
State: ImportString,
},
}
}
31 changes: 31 additions & 0 deletions random/string.go
Original file line number Diff line number Diff line change
@@ -185,3 +185,34 @@ func generateRandomBytes(charSet *string, length int) ([]byte, error) {
func readNil(d *schema.ResourceData, meta interface{}) error {
return nil
}

//ImportString will import string resource and will match the resource criteria
func ImportString(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {

val := d.Id()

d.Set("length", len(val))
d = UpperCharsInValue(d)
d = LowerCharsInValue(d)
d = SpecialCharsInValue(d)
d = NumbersInValue(d)
d.Set("result", val)

return []*schema.ResourceData{d}, nil
}

//ImportPassword will import password resource and will match any attributes that are required for the resource change
func ImportPassword(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {

val := d.Id()

d.Set("length", len(val))
d = UpperCharsInValue(d)
d = LowerCharsInValue(d)
d = SpecialCharsInValue(d)
d = NumbersInValue(d)
d.SetId("none")
d.Set("result", val)

return []*schema.ResourceData{d}, nil
}
56 changes: 56 additions & 0 deletions random/string_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package random

import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

const numChars = "0123456789"
const lowerChars = "abcdefghijklmnopqrstuvwxyz"
const upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const specialChars = "!@#$%&*()-_=+[]{}<>:?"

func updateStringResourceAttributes(d *schema.ResourceData, charSet string, charClass string, charClassMin string) *schema.ResourceData {
count := 0
val := d.Id()
for i := range val {
if strings.Contains(charSet, string(val[i])) {
count = count + 1
}
}
//Check for character set
if count > 0 {
d.Set(charClass, true)
} else {
d.Set(charClass, false)
}

//Check for minimum number of required chars from a character set
_, newMinClass := d.GetChange(charClassMin)

if count >= newMinClass.(int) {
d.Set(charClassMin, newMinClass.(int))
} else {
d.Set(charClassMin, count)
}
return d

}

func UpperCharsInValue(d *schema.ResourceData) *schema.ResourceData {

return updateStringResourceAttributes(d, upperChars, "upper", "min_upper")
}

func LowerCharsInValue(d *schema.ResourceData) *schema.ResourceData {
return updateStringResourceAttributes(d, lowerChars, "lower", "min_lower")
}

func SpecialCharsInValue(d *schema.ResourceData) *schema.ResourceData {
return updateStringResourceAttributes(d, specialChars, "special", "min_special")
}

func NumbersInValue(d *schema.ResourceData) *schema.ResourceData {
return updateStringResourceAttributes(d, numChars, "number", "min_numeric")
}
7 changes: 7 additions & 0 deletions website/docs/r/password.html.md
Original file line number Diff line number Diff line change
@@ -36,3 +36,10 @@ resource "aws_db_instance" "example" {
password = random_password.password.result
}
```
## Import

Random Password can be imported by specifying the value of the string:

```
terraform import random_password.password securepassword
```
8 changes: 8 additions & 0 deletions website/docs/r/string.html.md
Original file line number Diff line number Diff line change
@@ -72,3 +72,11 @@ The following arguments are supported:
The following attributes are exported:

* `result` - Random string generated.

## Import

Strings can be imported by just specifying the value of the string:

```
terraform import random_string.test test
```

0 comments on commit b143df3

Please sign in to comment.