Skip to content

Commit

Permalink
Merge pull request #258 from hashicorp/bendbennett/rename-number
Browse files Browse the repository at this point in the history
Deprecating number in favour of numeric
  • Loading branch information
bendbennett committed Jun 6, 2022
2 parents d6c03c2 + 3e61cf7 commit 96bc479
Show file tree
Hide file tree
Showing 11 changed files with 982 additions and 166 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Expand Up @@ -65,6 +65,7 @@ jobs:
- '0.15.*'
- '1.0.*'
- '1.1.*'
- '1.2.*'
steps:

- name: Setup Go
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
## 3.3.0 (Unreleased)

ENHANCEMENTS:

* resource/random_password: `number` is now deprecated and `numeric` has been added to align attribute naming. `number` will be removed in the next major release ([#258](https://github.com/hashicorp/terraform-provider-random/pull/258)).
* resource/random_string: `number` is now deprecated and `numeric` has been added to align attribute naming. `number` will be removed in the next major release ([#258](https://github.com/hashicorp/terraform-provider-random/pull/258)).

## 3.2.0 (May 18, 2022)

NEW FEATURES:
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/password.md
Expand Up @@ -46,7 +46,8 @@ resource "aws_db_instance" "example" {
- `min_numeric` (Number) Minimum number of numeric characters in the result. Default value is `0`.
- `min_special` (Number) Minimum number of special characters in the result. Default value is `0`.
- `min_upper` (Number) Minimum number of uppercase alphabet characters in the result. Default value is `0`.
- `number` (Boolean) Include numeric characters in the result. Default value is `true`.
- `number` (Boolean, Deprecated) Include numeric characters in the result. Default value is `true`. **NOTE**: This is deprecated, use `numeric` instead.
- `numeric` (Boolean) Include numeric characters in the result. Default value is `true`.
- `override_special` (String) Supply your own list of special characters to use for string generation. This overrides the default character list in the special argument. The `special` argument must still be set to true for any overwritten characters to be used in generation.
- `special` (Boolean) Include special characters in the result. These are `!@#$%&*()-_=+[]{}<>:?`. Default value is `true`.
- `upper` (Boolean) Include uppercase alphabet characters in the result. Default value is `true`.
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/string.md
Expand Up @@ -41,7 +41,8 @@ resource "random_string" "random" {
- `min_numeric` (Number) Minimum number of numeric characters in the result. Default value is `0`.
- `min_special` (Number) Minimum number of special characters in the result. Default value is `0`.
- `min_upper` (Number) Minimum number of uppercase alphabet characters in the result. Default value is `0`.
- `number` (Boolean) Include numeric characters in the result. Default value is `true`.
- `number` (Boolean, Deprecated) Include numeric characters in the result. Default value is `true`. **NOTE**: This is deprecated, use `numeric` instead.
- `numeric` (Boolean) Include numeric characters in the result. Default value is `true`.
- `override_special` (String) Supply your own list of special characters to use for string generation. This overrides the default character list in the special argument. The `special` argument must still be set to true for any overwritten characters to be used in generation.
- `special` (Boolean) Include special characters in the result. These are `!@#$%&*()-_=+[]{}<>:?`. Default value is `true`.
- `upper` (Boolean) Include uppercase alphabet characters in the result. Default value is `true`.
Expand Down
29 changes: 26 additions & 3 deletions internal/provider/resource_password.go
Expand Up @@ -5,11 +5,20 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/crypto/bcrypt"
)

// resourcePassword and resourceString both use the same set of CustomizeDiffFunc(s) in order to handle the deprecation
// of the `number` attribute and the simultaneous addition of the `numeric` attribute. planDefaultIfAllNull handles
// ensuring that both `number` and `numeric` default to `true` when they are both absent from config.
// planSyncIfChange handles keeping number and numeric in-sync when either one has been changed.
func resourcePassword() *schema.Resource {
customizeDiffFuncs := planDefaultIfAllNull(true, "number", "numeric")
customizeDiffFuncs = append(customizeDiffFuncs, planSyncIfChange("number", "numeric"))
customizeDiffFuncs = append(customizeDiffFuncs, planSyncIfChange("numeric", "number"))

return &schema.Resource{
Description: "Identical to [random_string](string.html) with the exception that the result is " +
"treated as sensitive and, thus, _not_ displayed in console output. Read more about sensitive " +
Expand All @@ -19,18 +28,26 @@ func resourcePassword() *schema.Resource {
CreateContext: createPassword,
ReadContext: readNil,
DeleteContext: RemoveResourceFromState,
Schema: passwordSchemaV1(),
Schema: passwordSchemaV2(),
Importer: &schema.ResourceImporter{
StateContext: importPasswordFunc,
},
SchemaVersion: 1,
SchemaVersion: 2,
StateUpgraders: []schema.StateUpgrader{
{
Version: 0,
Type: resourcePasswordV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourcePasswordStateUpgradeV0,
},
{
Version: 1,
Type: resourcePasswordV1().CoreConfigSchema().ImpliedType(),
Upgrade: resourcePasswordStringStateUpgradeV1,
},
},
CustomizeDiff: customdiff.All(
customizeDiffFuncs...,
),
}
}

Expand Down Expand Up @@ -74,6 +91,12 @@ func importPasswordFunc(ctx context.Context, d *schema.ResourceData, meta interf
return []*schema.ResourceData{d}, nil
}

func resourcePasswordV1() *schema.Resource {
return &schema.Resource{
Schema: passwordSchemaV1(),
}
}

func resourcePasswordV0() *schema.Resource {
return &schema.Resource{
Schema: passwordSchemaV0(),
Expand All @@ -87,7 +110,7 @@ func resourcePasswordStateUpgradeV0(_ context.Context, rawState map[string]inter

result, ok := rawState["result"].(string)
if !ok {
return nil, fmt.Errorf("resource password state upgrade failed, result could not be asserted as string: %T", rawState["result"])
return nil, fmt.Errorf("resource password state upgrade failed, result is not a string: %T", rawState["result"])
}

hash, err := generateHash(result)
Expand Down

0 comments on commit 96bc479

Please sign in to comment.