Skip to content

Commit

Permalink
Add PreviousAddress field to ResourceChange to support moved block (#95)
Browse files Browse the repository at this point in the history
* Add PreviousAddress field to ResourceChange to support moved block

* Add test for PreviousAddress field
  • Loading branch information
takaishi committed Sep 12, 2023
1 parent cd7b46b commit 9901d28
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ type ResourceChange struct {
// The absolute resource address.
Address string `json:"address,omitempty"`

// The absolute address that this resource instance had
// at the conclusion of a previous plan.
PreviousAddress string `json:"previous_address,omitempty"`

// The module portion of the above address. Omitted if the instance
// is in the root module.
ModuleAddress string `json:"module_address,omitempty"`
Expand Down
21 changes: 21 additions & 0 deletions plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,24 @@ func TestPlan_withChecks(t *testing.T) {
}
}
}

func TestPlan_movedBlock(t *testing.T) {
f, err := os.Open("testdata/moved_block/plan.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()

var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}

if err := plan.Validate(); err != nil {
t.Fatal(err)
}

if plan.ResourceChanges[0].PreviousAddress != "random_id.test" {
t.Fatalf("unexpected previous address %s, expected is random_id.test", plan.ResourceChanges[0].PreviousAddress)
}
}
8 changes: 8 additions & 0 deletions testdata/moved_block/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
moved {
from = random_id.test
to = random_id.test2
}

resource "random_id" "test2" {
byte_length = 10
}
1 change: 1 addition & 0 deletions testdata/moved_block/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"1.2","terraform_version":"1.5.3","planned_values":{"root_module":{"resources":[{"address":"random_id.test2","mode":"managed","type":"random_id","name":"test2","provider_name":"registry.terraform.io/hashicorp/random","schema_version":0,"values":{"b64_std":"uBIJLwrgNTh6OQ==","b64_url":"uBIJLwrgNTh6OQ","byte_length":10,"dec":"869248136000969819847225","hex":"b812092f0ae035387a39","id":"uBIJLwrgNTh6OQ","keepers":null,"prefix":null},"sensitive_values":{}}]}},"resource_changes":[{"address":"random_id.test2","previous_address":"random_id.test","mode":"managed","type":"random_id","name":"test2","provider_name":"registry.terraform.io/hashicorp/random","change":{"actions":["no-op"],"before":{"b64_std":"uBIJLwrgNTh6OQ==","b64_url":"uBIJLwrgNTh6OQ","byte_length":10,"dec":"869248136000969819847225","hex":"b812092f0ae035387a39","id":"uBIJLwrgNTh6OQ","keepers":null,"prefix":null},"after":{"b64_std":"uBIJLwrgNTh6OQ==","b64_url":"uBIJLwrgNTh6OQ","byte_length":10,"dec":"869248136000969819847225","hex":"b812092f0ae035387a39","id":"uBIJLwrgNTh6OQ","keepers":null,"prefix":null},"after_unknown":{},"before_sensitive":{},"after_sensitive":{}}}],"prior_state":{"format_version":"1.0","terraform_version":"1.5.3","values":{"root_module":{"resources":[{"address":"random_id.test2","mode":"managed","type":"random_id","name":"test2","provider_name":"registry.terraform.io/hashicorp/random","schema_version":0,"values":{"b64_std":"uBIJLwrgNTh6OQ==","b64_url":"uBIJLwrgNTh6OQ","byte_length":10,"dec":"869248136000969819847225","hex":"b812092f0ae035387a39","id":"uBIJLwrgNTh6OQ","keepers":null,"prefix":null},"sensitive_values":{}}]}}},"configuration":{"provider_config":{"random":{"name":"random","full_name":"registry.terraform.io/hashicorp/random"}},"root_module":{"resources":[{"address":"random_id.test2","mode":"managed","type":"random_id","name":"test2","provider_config_key":"random","expressions":{"byte_length":{"constant_value":10}},"schema_version":0}]}},"timestamp":"2023-08-31T11:49:54Z"}
1 change: 1 addition & 0 deletions testdata/moved_block/schemas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"1.0","provider_schemas":{"registry.terraform.io/hashicorp/random":{"provider":{"version":0,"block":{"description_kind":"plain"}},"resource_schemas":{"random_id":{"version":0,"block":{"attributes":{"b64_std":{"type":"string","description":"The generated id presented in base64 without additional transformations.","description_kind":"plain","computed":true},"b64_url":{"type":"string","description":"The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters `_` and `-`.","description_kind":"plain","computed":true},"byte_length":{"type":"number","description":"The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.","description_kind":"plain","required":true},"dec":{"type":"string","description":"The generated id presented in non-padded decimal digits.","description_kind":"plain","computed":true},"hex":{"type":"string","description":"The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.","description_kind":"plain","computed":true},"id":{"type":"string","description":"The generated id presented in base64 without additional transformations or prefix.","description_kind":"plain","computed":true},"keepers":{"type":["map","string"],"description":"Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.","description_kind":"plain","optional":true},"prefix":{"type":"string","description":"Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.","description_kind":"plain","optional":true}},"description":"\nThe resource `random_id` generates random numbers that are intended to be\nused as unique identifiers for other resources.\n\nThis resource *does* use a cryptographic random number generator in order\nto minimize the chance of collisions, making the results of this resource\nwhen a 16-byte identifier is requested of equivalent uniqueness to a\ntype-4 UUID.\n\nThis resource can be used in conjunction with resources that have\nthe `create_before_destroy` lifecycle flag set to avoid conflicts with\nunique names during the brief period where both the old and new resources\nexist concurrently.\n","description_kind":"plain"}},"random_integer":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"The string representation of the integer result.","description_kind":"plain","computed":true},"keepers":{"type":["map","string"],"description":"Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.","description_kind":"plain","optional":true},"max":{"type":"number","description":"The maximum inclusive value of the range.","description_kind":"plain","required":true},"min":{"type":"number","description":"The minimum inclusive value of the range.","description_kind":"plain","required":true},"result":{"type":"number","description":"The random integer result.","description_kind":"plain","computed":true},"seed":{"type":"string","description":"A custom seed to always produce the same value.","description_kind":"plain","optional":true}},"description":"The resource `random_integer` generates random values from a given range, described by the `min` and `max` attributes of a given resource.\n\nThis resource can be used in conjunction with resources that have the `create_before_destroy` lifecycle flag set, to avoid conflicts with unique names during the brief period where both the old and new resources exist concurrently.","description_kind":"plain"}},"random_password":{"version":3,"block":{"attributes":{"bcrypt_hash":{"type":"string","description":"A bcrypt hash of the generated random string. **NOTE**: If the generated random string is greater than 72 bytes in length, `bcrypt_hash` will contain a hash of the first 72 bytes.","description_kind":"plain","computed":true,"sensitive":true},"id":{"type":"string","description":"A static value used internally by Terraform, this should not be referenced in configurations.","description_kind":"plain","computed":true},"keepers":{"type":["map","string"],"description":"Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.","description_kind":"plain","optional":true},"length":{"type":"number","description":"The length of the string desired. The minimum value for length is 1 and, length must also be \u003e= (`min_upper` + `min_lower` + `min_numeric` + `min_special`).","description_kind":"plain","required":true},"lower":{"type":"bool","description":"Include lowercase alphabet characters in the result. Default value is `true`.","description_kind":"plain","optional":true,"computed":true},"min_lower":{"type":"number","description":"Minimum number of lowercase alphabet characters in the result. Default value is `0`.","description_kind":"plain","optional":true,"computed":true},"min_numeric":{"type":"number","description":"Minimum number of numeric characters in the result. Default value is `0`.","description_kind":"plain","optional":true,"computed":true},"min_special":{"type":"number","description":"Minimum number of special characters in the result. Default value is `0`.","description_kind":"plain","optional":true,"computed":true},"min_upper":{"type":"number","description":"Minimum number of uppercase alphabet characters in the result. Default value is `0`.","description_kind":"plain","optional":true,"computed":true},"number":{"type":"bool","description":"Include numeric characters in the result. Default value is `true`. **NOTE**: This is deprecated, use `numeric` instead.","description_kind":"plain","deprecated":true,"optional":true,"computed":true},"numeric":{"type":"bool","description":"Include numeric characters in the result. Default value is `true`.","description_kind":"plain","optional":true,"computed":true},"override_special":{"type":"string","description":"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.","description_kind":"plain","optional":true},"result":{"type":"string","description":"The generated random string.","description_kind":"plain","computed":true,"sensitive":true},"special":{"type":"bool","description":"Include special characters in the result. These are `!@#$%\u0026*()-_=+[]{}\u003c\u003e:?`. Default value is `true`.","description_kind":"plain","optional":true,"computed":true},"upper":{"type":"bool","description":"Include uppercase alphabet characters in the result. Default value is `true`.","description_kind":"plain","optional":true,"computed":true}},"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 data handling in the [Terraform documentation](https://www.terraform.io/docs/language/state/sensitive-data.html).\n\nThis resource *does* use a cryptographic random number generator.","description_kind":"plain"}},"random_pet":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"The random pet name.","description_kind":"plain","computed":true},"keepers":{"type":["map","string"],"description":"Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.","description_kind":"plain","optional":true},"length":{"type":"number","description":"The length (in words) of the pet name. Defaults to 2","description_kind":"plain","optional":true,"computed":true},"prefix":{"type":"string","description":"A string to prefix the name with.","description_kind":"plain","optional":true},"separator":{"type":"string","description":"The character to separate words in the pet name. Defaults to \"-\"","description_kind":"plain","optional":true,"computed":true}},"description":"The resource `random_pet` generates random pet names that are intended to be used as unique identifiers for other resources.\n\nThis resource can be used in conjunction with resources that have the `create_before_destroy` lifecycle flag set, to avoid conflicts with unique names during the brief period where both the old and new resources exist concurrently.","description_kind":"plain"}},"random_shuffle":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"A static value used internally by Terraform, this should not be referenced in configurations.","description_kind":"plain","computed":true},"input":{"type":["list","string"],"description":"The list of strings to shuffle.","description_kind":"plain","required":true},"keepers":{"type":["map","string"],"description":"Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.","description_kind":"plain","optional":true},"result":{"type":["list","string"],"description":"Random permutation of the list of strings given in `input`.","description_kind":"plain","computed":true},"result_count":{"type":"number","description":"The number of results to return. Defaults to the number of items in the `input` list. If fewer items are requested, some elements will be excluded from the result. If more items are requested, items will be repeated in the result but not more frequently than the number of items in the input list.","description_kind":"plain","optional":true},"seed":{"type":"string","description":"Arbitrary string with which to seed the random number generator, in order to produce less-volatile permutations of the list.\n\n**Important:** Even with an identical seed, it is not guaranteed that the same permutation will be produced across different versions of Terraform. This argument causes the result to be *less volatile*, but not fixed for all time.","description_kind":"plain","optional":true}},"description":"The resource `random_shuffle` generates a random permutation of a list of strings given as an argument.","description_kind":"plain"}},"random_string":{"version":2,"block":{"attributes":{"id":{"type":"string","description":"The generated random string.","description_kind":"plain","computed":true},"keepers":{"type":["map","string"],"description":"Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.","description_kind":"plain","optional":true},"length":{"type":"number","description":"The length of the string desired. The minimum value for length is 1 and, length must also be \u003e= (`min_upper` + `min_lower` + `min_numeric` + `min_special`).","description_kind":"plain","required":true},"lower":{"type":"bool","description":"Include lowercase alphabet characters in the result. Default value is `true`.","description_kind":"plain","optional":true,"computed":true},"min_lower":{"type":"number","description":"Minimum number of lowercase alphabet characters in the result. Default value is `0`.","description_kind":"plain","optional":true,"computed":true},"min_numeric":{"type":"number","description":"Minimum number of numeric characters in the result. Default value is `0`.","description_kind":"plain","optional":true,"computed":true},"min_special":{"type":"number","description":"Minimum number of special characters in the result. Default value is `0`.","description_kind":"plain","optional":true,"computed":true},"min_upper":{"type":"number","description":"Minimum number of uppercase alphabet characters in the result. Default value is `0`.","description_kind":"plain","optional":true,"computed":true},"number":{"type":"bool","description":"Include numeric characters in the result. Default value is `true`. **NOTE**: This is deprecated, use `numeric` instead.","description_kind":"plain","deprecated":true,"optional":true,"computed":true},"numeric":{"type":"bool","description":"Include numeric characters in the result. Default value is `true`.","description_kind":"plain","optional":true,"computed":true},"override_special":{"type":"string","description":"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.","description_kind":"plain","optional":true},"result":{"type":"string","description":"The generated random string.","description_kind":"plain","computed":true},"special":{"type":"bool","description":"Include special characters in the result. These are `!@#$%\u0026*()-_=+[]{}\u003c\u003e:?`. Default value is `true`.","description_kind":"plain","optional":true,"computed":true},"upper":{"type":"bool","description":"Include uppercase alphabet characters in the result. Default value is `true`.","description_kind":"plain","optional":true,"computed":true}},"description":"The resource `random_string` generates a random permutation of alphanumeric characters and optionally special characters.\n\nThis resource *does* use a cryptographic random number generator.\n\nHistorically this resource's intended usage has been ambiguous as the original example used it in a password. For backwards compatibility it will continue to exist. For unique ids please use [random_id](id.html), for sensitive random values please use [random_password](password.html).","description_kind":"plain"}},"random_uuid":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"The generated uuid presented in string format.","description_kind":"plain","computed":true},"keepers":{"type":["map","string"],"description":"Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.","description_kind":"plain","optional":true},"result":{"type":"string","description":"The generated uuid presented in string format.","description_kind":"plain","computed":true}},"description":"The resource `random_uuid` generates random uuid string that is intended to be used as unique identifiers for other resources.\n\nThis resource uses [hashicorp/go-uuid](https://github.com/hashicorp/go-uuid) to generate a UUID-formatted string for use with services needed a unique string identifier.","description_kind":"plain"}}}}}}
1 change: 1 addition & 0 deletions testdata/moved_block/show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"1.0","terraform_version":"1.5.3","values":{"root_module":{"resources":[{"address":"random_id.test","mode":"managed","type":"random_id","name":"test","provider_name":"registry.terraform.io/hashicorp/random","schema_version":0,"values":{"b64_std":"uBIJLwrgNTh6OQ==","b64_url":"uBIJLwrgNTh6OQ","byte_length":10,"dec":"869248136000969819847225","hex":"b812092f0ae035387a39","id":"uBIJLwrgNTh6OQ","keepers":null,"prefix":null},"sensitive_values":{}}]}}}

0 comments on commit 9901d28

Please sign in to comment.