-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Org VDC import, data source and read fix #324
Conversation
Signed-off-by: Vaidotas Bauzys <[email protected]>
…roviders/terraform-provider-vcd
Merge branch 'master' of github.com:terraform-providers/terraform-provider-vcd
…roviders/terraform-provider-vcd
…roviders/terraform-provider-vcd
…roviders/terraform-provider-vcd
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
…roviders/terraform-provider-vcd
Signed-off-by: Vaidotas Bauzys <[email protected]> # Conflicts: # CHANGELOG.md
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Second pass
vcd/resource_vcd_org_vdc.go
Outdated
// capacityResource return schema for capacity | ||
func capacitySchema() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeSet, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that the set has only one element, why don't we use the simpler TypeList
instead?
That would save us the aggravation of detecting hash values while testing, and the decrease in test readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeSet handles naturally that only one type of resource is configured
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for this. We would just need to check if we are not breaking compatibility with previous version. At the moment this is forcing every user using this data source to do ${tolist(tolist(data.vcd_org_vdc.existingVdc.compute_capacity)[0].cpu)[0].allocated}
to pull out some the value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If everyone agrees please create new Issue and new PR
vcd/resource_vcd_org_vdc_test.go
Outdated
@@ -24,6 +24,7 @@ func TestAccVcdOrgVdcReservationPool(t *testing.T) { | |||
"AllocationModel": "ReservationPool", | |||
"ProviderVdc": testConfig.VCD.ProviderVdc.Name, | |||
"NetworkPool": testConfig.VCD.ProviderVdc.NetworkPool, | |||
"Allocated": "2048", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are adding an allocated value to compute_capacity.0.cpu.0.allocated
but we don't check the value in the tests. Same for the deep values of memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added checks for all test.
vcd/datasource_vcd_org_vdc_test.go
Outdated
{"memory", memoryHashInternalValue, "reserved"}, | ||
{"memory", memoryHashInternalValue, "used"}, | ||
} | ||
for _, td := range testData { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why one value is accessed with zero indexes, while the other needs the calculated hashes?
(I know that I suggested the code simplification, but the original went in the same way)
if attr["compute_capacity.0.memory.0.reserved"] != vdcResource.Primary.Attributes[fmt.Sprintf("compute_capacity.%s.memory.%s.reserved", mainHashValue, memoryHashInternalValue)] {
return fmt.Errorf("compute_capacity.0.memory.0.reserved is %#v; want %#v", attr["compute_capacity.0.memory.0.reserved"], vdcResource.Primary.Attributes[fmt.Sprintf("compute_capacity.%s.memory.%s.reserved", mainHashValue, memoryHashInternalValue)])
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This question goes to Hashicorp. Seems that datasource for some reason didn't use hashes
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Added convertion changes from typeSet to typeList |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The list thing looks much cleaner! Thanks for the effort
@@ -22,6 +23,8 @@ IMPROVEMENTS: | |||
* `resource/vcd_vapp_vm` allows to force guest customization [#310] | |||
* `resource/vcd_vapp` supports guest properties [#319] | |||
* `resource/vcd_vapp_vm` supports guest properties [#319] | |||
* Upgrade Terraform SDK dependency to 0.12.6 [#302] | |||
* `vcd_org_vdc` Add import capability - [#324] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth noting it gets the "read" capability as someone may be surprised getting changes after regular terraform apply
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add note
vcd/api_test.go
Outdated
// Given "parent.1234.child.5678.something", it will return "1234" and "5678" | ||
// for Example: compute_capacity.315866465.memory.508945747.limit | ||
// (returns 315866465 and 508945747) | ||
func getHashValuesFromKey(stateFileMap map[string]string, parentKey, childKey string) (string, string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess now this gets to a question if this is used at all. Although it may come in handy in some other place in future.
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccVcdVdcDatasource(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get the impression, that this test does not test the data source and its fields, but vice versa. That is - it uses a data source to generate some vdc config and checks that the resource is provisioned as specified from the data source. Wondering if this can mask some issues with data source itself (for example if it does not retrieve some fields correctly but resource provisioning still works)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test according Giuseppes recommendations, his done previously his investigation. It's best approach though it has own issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way to improve on this approach is to use output
constructs in the test, and compare them with known items in the existing setup.
For example, we can use output to show the provider VDC, and compare that one with the name from our testing configuration.
output "provider_vdc" {
value = data.vcd_org_vdc.existingVdc.provider_vdc_name
}
in the test, then use
resource.TestCheckOutput("provider_vdc", testConfig.VCD.ProviderVdc.Name),
See some examples in PR #333
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry don't get idea - what is difference if I would check : resource.TestCheckResourceAttr("vcd_org_vdc."+vdcName, "provider_vdc_name", testConfig.VCD.ProviderVdc.Name)
Why we would need output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your approach is equivalent.
Using output
shows explicitly that we are accessing the data source in the HCL script.
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some provisional comments. Running tests now
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccVcdVdcDatasource(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way to improve on this approach is to use output
constructs in the test, and compare them with known items in the existing setup.
For example, we can use output to show the provider VDC, and compare that one with the name from our testing configuration.
output "provider_vdc" {
value = data.vcd_org_vdc.existingVdc.provider_vdc_name
}
in the test, then use
resource.TestCheckOutput("provider_vdc", testConfig.VCD.ProviderVdc.Name),
See some examples in PR #333
Signed-off-by: Vaidotas Bauzys <[email protected]>
vcd/resource_vcd_org_vdc.go
Outdated
func resourceVcdOrgVdcImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
resourceURI := strings.Split(d.Id(), ".") | ||
if len(resourceURI) != 2 { | ||
return nil, fmt.Errorf("resource name must be specified as org.vdc.my_existing_vdc") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an extra element in the example path:
"resource name must be specified as org.vdc.my_existing_vdc"
should be
"resource name must be specified as org.my_existing_vdc"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, fixed
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Good job! This was a tough one!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful @vbauzysvmware and finally a solid LGTM!
Continuing changed for supporting data sources and imports.