command: Update "terraform show -json" tests for changed provider contract

We now require a provider to populate all of its defaults -- including
unknown value placeholders -- during PlanResourceChange. That means the
mock provider for testing "terraform show -json" must now manage the
population of the computed "id" attribute during plan.

To make this logic a little easier, we also change the ApplyResourceChange
implementation to fill in a non-null id, since that makes it easier for
the mock PlanResourceChange to recognize when it needs to populate that
default value during an update.
This commit is contained in:
Martin Atkins 2019-02-08 11:58:08 -08:00
parent 82588af892
commit 7e186f72d9
5 changed files with 31 additions and 12 deletions

View File

@ -244,13 +244,29 @@ func showFixtureProvider() *terraform.MockProvider {
p := testProvider() p := testProvider()
p.GetSchemaReturn = showFixtureSchema() p.GetSchemaReturn = showFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse { p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
idVal := req.ProposedNewState.GetAttr("id")
amiVal := req.ProposedNewState.GetAttr("ami")
if idVal.IsNull() {
idVal = cty.UnknownVal(cty.String)
}
return providers.PlanResourceChangeResponse{ return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState, PlannedState: cty.ObjectVal(map[string]cty.Value{
"id": idVal,
"ami": amiVal,
}),
} }
} }
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse { p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
idVal := req.PlannedState.GetAttr("id")
amiVal := req.PlannedState.GetAttr("ami")
if !idVal.IsKnown() {
idVal = cty.StringVal("placeholder")
}
return providers.ApplyResourceChangeResponse{ return providers.ApplyResourceChangeResponse{
NewState: cty.UnknownAsNull(req.PlannedState), NewState: cty.ObjectVal(map[string]cty.Value{
"id": idVal,
"ami": amiVal,
}),
} }
} }
return p return p

View File

@ -18,7 +18,7 @@
"schema_version": 0, "schema_version": 0,
"values": { "values": {
"ami": "bar", "ami": "bar",
"id": null "id": "placeholder"
} }
} }
] ]
@ -37,11 +37,11 @@
], ],
"before": { "before": {
"ami": "foo", "ami": "foo",
"id": null "id": "placeholder"
}, },
"after": { "after": {
"ami": "bar", "ami": "bar",
"id": null "id": "placeholder"
}, },
"after_unknown": { "after_unknown": {
"ami": false, "ami": false,
@ -61,7 +61,7 @@
], ],
"before": { "before": {
"ami": "foo", "ami": "foo",
"id": null "id": "placeholder"
}, },
"after": null, "after": null,
"after_unknown": false "after_unknown": false

View File

@ -14,7 +14,8 @@
{ {
"schema_version": 0, "schema_version": 0,
"attributes": { "attributes": {
"ami": "foo" "ami": "foo",
"id": "placeholder"
} }
} }
] ]
@ -28,7 +29,8 @@
{ {
"schema_version": 0, "schema_version": 0,
"attributes": { "attributes": {
"ami": "foo" "ami": "foo",
"id": "placeholder"
} }
} }
] ]

View File

@ -18,7 +18,7 @@
"schema_version": 0, "schema_version": 0,
"values": { "values": {
"ami": "bar", "ami": "bar",
"id": null "id": "placeholder"
} }
} }
] ]
@ -37,11 +37,11 @@
], ],
"before": { "before": {
"ami": "bar", "ami": "bar",
"id": null "id": "placeholder"
}, },
"after": { "after": {
"ami": "bar", "ami": "bar",
"id": null "id": "placeholder"
}, },
"after_unknown": { "after_unknown": {
"ami": false, "ami": false,

View File

@ -14,7 +14,8 @@
{ {
"schema_version": 0, "schema_version": 0,
"attributes": { "attributes": {
"ami": "bar" "ami": "bar",
"id": "placeholder"
} }
} }
] ]