Skip to content
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

Merged
merged 48 commits into from
Sep 18, 2019
Merged

Org VDC import, data source and read fix #324

merged 48 commits into from
Sep 18, 2019

Conversation

vbauzys
Copy link
Contributor

@vbauzys vbauzys commented Sep 6, 2019

Continuing changed for supporting data sources and imports.

  • Org VDC import added
  • Data source for Org VDC
  • Fixed read issue with complex types

Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>

# Conflicts:
#	CHANGELOG.md
Signed-off-by: Vaidotas Bauzys <[email protected]>
@vbauzys vbauzys requested review from Didainius, dataclouder and lvirbalas and removed request for Didainius, dataclouder and lvirbalas September 9, 2019 09:36
@vbauzys vbauzys self-assigned this Sep 9, 2019
@vbauzys vbauzys requested a review from lvirbalas September 9, 2019 09:36
Signed-off-by: Vaidotas Bauzys <[email protected]>
Signed-off-by: Vaidotas Bauzys <[email protected]>
Copy link
Contributor

@dataclouder dataclouder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second pass

// capacityResource return schema for capacity
func capacitySchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Contributor Author

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

@@ -24,6 +24,7 @@ func TestAccVcdOrgVdcReservationPool(t *testing.T) {
"AllocationModel": "ReservationPool",
"ProviderVdc": testConfig.VCD.ProviderVdc.Name,
"NetworkPool": testConfig.VCD.ProviderVdc.NetworkPool,
"Allocated": "2048",
Copy link
Contributor

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.

Copy link
Contributor Author

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.

{"memory", memoryHashInternalValue, "reserved"},
{"memory", memoryHashInternalValue, "used"},
}
for _, td := range testData {
Copy link
Contributor

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)])
	}

Copy link
Contributor Author

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]>
@vbauzys
Copy link
Contributor Author

vbauzys commented Sep 17, 2019

Added convertion changes from typeSet to typeList

Copy link
Collaborator

@Didainius Didainius left a 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

vcd/resource_vcd_org_vdc.go Outdated Show resolved Hide resolved
@@ -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]
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add note

website/docs/d/org_vdc.html.markdown Outdated Show resolved Hide resolved
vcd/resource_vcd_org_vdc.go Outdated Show resolved Hide resolved
vcd/resource_vcd_org_vdc.go Outdated Show resolved Hide resolved
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) {
Copy link
Collaborator

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) {
Copy link
Collaborator

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)

Copy link
Contributor Author

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.

Copy link
Contributor

@dataclouder dataclouder Sep 18, 2019

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

Copy link
Contributor Author

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?

Copy link
Contributor

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]>
@ghost ghost added size/XL and removed size/XXL labels Sep 18, 2019
Signed-off-by: Vaidotas Bauzys <[email protected]>
@ghost ghost added size/XXL and removed size/XL labels Sep 18, 2019
Signed-off-by: Vaidotas Bauzys <[email protected]>
Copy link
Contributor

@dataclouder dataclouder left a 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

vcd/datasource_vcd_org_vdc.go Show resolved Hide resolved
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccVcdVdcDatasource(t *testing.T) {
Copy link
Contributor

@dataclouder dataclouder Sep 18, 2019

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]>
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")
Copy link
Contributor

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"

Copy link
Contributor Author

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]>
Copy link
Contributor

@dataclouder dataclouder left a 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!

Copy link
Collaborator

@lvirbalas lvirbalas left a 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!

@vbauzys vbauzys merged commit 1ed718d into vmware:master Sep 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants