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

Improve ignore_metadata_changes by adding support to OpenAPI entries #1164

Merged
merged 55 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
0b89202
Restart PR
adambarreiro Nov 20, 2023
219e80b
Fix tests
adambarreiro Nov 20, 2023
9da22b6
Self review
adambarreiro Nov 20, 2023
db3d926
Self review
adambarreiro Nov 20, 2023
2c20fd9
Add persistent attr
adambarreiro Nov 21, 2023
5d0464d
Fix
adambarreiro Nov 21, 2023
e988d47
#
adambarreiro Nov 21, 2023
080af72
Add note
adambarreiro Nov 22, 2023
a7352a3
Fixes
adambarreiro Nov 22, 2023
4130424
Fix
adambarreiro Nov 22, 2023
bc927bb
Fix
adambarreiro Nov 22, 2023
657ed4c
Improve test
adambarreiro Nov 22, 2023
b54dd16
self-review
adambarreiro Nov 22, 2023
68215d1
self-review
adambarreiro Nov 22, 2023
c6a0925
self-review
adambarreiro Nov 22, 2023
b07d663
self-review
adambarreiro Nov 22, 2023
1d02166
Remove version 10.4.0 checks
adambarreiro Nov 22, 2023
fdf3cf4
Apply suggestion
adambarreiro Nov 22, 2023
7e8bc3a
Improve error msg
adambarreiro Nov 22, 2023
de6c0ed
Fix
adambarreiro Nov 22, 2023
f59cf2b
#
adambarreiro Nov 22, 2023
b52704d
Change error msg
adambarreiro Nov 22, 2023
9211ae3
Add clarification
adambarreiro Nov 22, 2023
2fd2bb0
Add clarification
adambarreiro Nov 22, 2023
d9a8b54
Refactor and simplify code
adambarreiro Nov 22, 2023
67af8a5
Add test
adambarreiro Nov 22, 2023
3eab5ae
Simplify test
adambarreiro Nov 22, 2023
f75423e
Add ignore_metadata_changes note
adambarreiro Nov 23, 2023
1e8a2de
#
adambarreiro Nov 23, 2023
56414e4
Apply suggestion
adambarreiro Nov 23, 2023
e9b0d66
Merge
adambarreiro Nov 23, 2023
987e764
Add metadata conflict checker
adambarreiro Nov 23, 2023
8db90b9
gs
adambarreiro Nov 23, 2023
2fa0e02
Update docs
adambarreiro Nov 23, 2023
fc6da3c
Merge
adambarreiro Nov 23, 2023
6b946e8
Update docs
adambarreiro Nov 23, 2023
4bd0457
Merge
adambarreiro Nov 23, 2023
0088256
Fix diagnostics
adambarreiro Nov 23, 2023
8c78aba
Apply suggestions
adambarreiro Nov 23, 2023
1d97a21
Merge
adambarreiro Nov 23, 2023
aa9ad33
Refactoring
adambarreiro Nov 24, 2023
cf50f70
Add limit of 50
adambarreiro Nov 24, 2023
4758936
#
adambarreiro Nov 24, 2023
2b45dcc
Changelog
adambarreiro Nov 24, 2023
ab78049
Fix. Tests pass
adambarreiro Nov 24, 2023
124abc8
Merge
adambarreiro Nov 24, 2023
417c126
Merge
adambarreiro Nov 27, 2023
326dad3
Fix
adambarreiro Nov 27, 2023
7f29d98
Fix tests. One pass
adambarreiro Nov 27, 2023
029cff5
Update docs
adambarreiro Nov 27, 2023
8fe14ce
Changelog
adambarreiro Nov 27, 2023
ac96c3c
Merge
adambarreiro Nov 28, 2023
2594076
Fix bug
adambarreiro Nov 30, 2023
5aed2b3
Fix bug
adambarreiro Nov 30, 2023
769fa2b
Suggestions and fixes
adambarreiro Nov 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/v3.11.0/1018-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add `metadata_entry` attribute to `vcd_rde` resource and data source to manage metadata entries in Runtime Defined Entities [GH-1018]
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ require (
google.golang.org/grpc v1.57.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

replace github.com/vmware/go-vcloud-director/v2 => github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20231123112752-60218453cedb
Didainius marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 h1:KLq8BE0KwCL+mmXnjLWEAOYO+2l2AE4YMmqG1ZpZHBs=
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20231123112752-60218453cedb h1:jRIRygFh2Q/AKwR3gFkETfmz2p2J/wZszo+QcOHOav0=
github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20231123112752-60218453cedb/go.mod h1:QPxGFgrUcSyzy9IlpwDE4UNT3tsOy2047tJOPEJ4nlw=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
Expand Down Expand Up @@ -124,8 +126,6 @@ github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/vmware/go-vcloud-director/v2 v2.22.0-alpha.11 h1:Q71HXQSRSKKf5gcKGHQKvldFRyiOO7MpLJOaIIKCosg=
github.com/vmware/go-vcloud-director/v2 v2.22.0-alpha.11/go.mod h1:QPxGFgrUcSyzy9IlpwDE4UNT3tsOy2047tJOPEJ4nlw=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zclconf/go-cty v1.14.0 h1:/Xrd39K7DXbHzlisFP9c4pHao4yyf+/Ug9LEz+Y/yhc=
Expand Down
12 changes: 9 additions & 3 deletions vcd/datasource_vcd_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func getCatalogFromResource(catalogName string, d *schema.ResourceData, meta int

func datasourceVcdCatalogRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
diags diag.Diagnostics
vcdClient = meta.(*VCDClient)
catalog *govcd.AdminCatalog
err error
Expand Down Expand Up @@ -257,9 +258,14 @@ func datasourceVcdCatalogRead(_ context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

diagErr := updateMetadataInStateDeprecated(d, vcdClient, "vcd_catalog", catalog)
if diagErr != nil {
return diagErr
diags = append(diags, updateMetadataInStateDeprecated(d, vcdClient, "vcd_catalog", catalog)...)
if diags != nil && diags.HasError() {
return diags
}

// This must be checked at the end as updateMetadataInStateDeprecated can throw Warning diagnostics
if len(diags) > 0 {
return diags
}

return nil
Expand Down
11 changes: 8 additions & 3 deletions vcd/datasource_vcd_independent_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,16 @@ func dataSourceVcdIndependentDiskRead(_ context.Context, d *schema.ResourceData,
return diag.Errorf("unable to find queried disk with name %s: and href: %s, %s", identifier, disk.Disk.HREF, err)
}

diagErr := setMainData(d, vcdClient, disk, diskRecord)
if diagErr != nil {
return diagErr
diags := setMainData(d, vcdClient, disk, diskRecord)
if diags != nil && diags.HasError() {
return diags
}

log.Printf("[TRACE] Disk read completed.")

// This must be checked at the end as setMainData can throw Warning diagnostics
if len(diags) > 0 {
return diags
}
return nil
}
17 changes: 10 additions & 7 deletions vcd/datasource_vcd_network_isolated_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package vcd

import (
"context"
"github.com/vmware/go-vcloud-director/v2/govcd"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v2/govcd"
)

func datasourceVcdNetworkIsolatedV2() *schema.Resource {
Expand Down Expand Up @@ -130,6 +128,7 @@ func datasourceVcdNetworkIsolatedV2() *schema.Resource {
}

func datasourceVcdNetworkIsolatedV2Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
vcdClient := meta.(*VCDClient)

org, err := vcdClient.GetOrgFromResource(d)
Expand Down Expand Up @@ -203,12 +202,16 @@ func datasourceVcdNetworkIsolatedV2Read(_ context.Context, d *schema.ResourceDat

// Metadata is not supported when the network is in a VDC Group
if !govcd.OwnerIsVdcGroup(network.OpenApiOrgVdcNetwork.OwnerRef.ID) {
diagErr := updateMetadataInStateDeprecated(d, vcdClient, "vcd_network_isolated_v2", network)
if diagErr != nil {
log.Printf("[DEBUG] Unable to set isolated network v2 metadata: %s", err)
return diagErr
diags = append(diags, updateMetadataInStateDeprecated(d, vcdClient, "vcd_network_isolated_v2", network)...)
if diags != nil && diags.HasError() {
return diags
}
}

// This must be checked at the end as updateMetadataInStateDeprecated can throw Warning diagnostics
if len(diags) > 0 {
return diags
}

return nil
}
17 changes: 10 additions & 7 deletions vcd/datasource_vcd_network_routed_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package vcd

import (
"context"
"github.com/vmware/go-vcloud-director/v2/govcd"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v2/govcd"
)

func datasourceVcdNetworkRoutedV2() *schema.Resource {
Expand Down Expand Up @@ -150,6 +148,7 @@ var networkV2IpRangeComputed = &schema.Resource{
}

func datasourceVcdNetworkRoutedV2Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
vcdClient := meta.(*VCDClient)

org, err := vcdClient.GetOrgFromResource(d)
Expand Down Expand Up @@ -255,12 +254,16 @@ func datasourceVcdNetworkRoutedV2Read(_ context.Context, d *schema.ResourceData,

// Metadata is not supported when the network is in a VDC Group
if !govcd.OwnerIsVdcGroup(network.OpenApiOrgVdcNetwork.OwnerRef.ID) {
diagErr := updateMetadataInStateDeprecated(d, vcdClient, "vcd_network_routed_v2", network)
if diagErr != nil {
log.Printf("[DEBUG] Unable to set routed network v2 metadata: %s", err)
return diagErr
diags = append(diags, updateMetadataInStateDeprecated(d, vcdClient, "vcd_network_routed_v2", network)...)
if diags != nil && diags.HasError() {
return diags
}
}

// This must be checked at the end as updateMetadataInStateDeprecated can throw Warning diagnostics
if len(diags) > 0 {
return diags
}

return nil
}
13 changes: 8 additions & 5 deletions vcd/datasource_vcd_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ func datasourceVcdOrg() *schema.Resource {
}

func datasourceVcdOrgRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

vcdClient := meta.(*VCDClient)

identifier := d.Get("name").(string)
Expand All @@ -158,9 +156,14 @@ func datasourceVcdOrgRead(_ context.Context, d *schema.ResourceData, meta interf
log.Printf("Org with id %s found", identifier)
d.SetId(adminOrg.AdminOrg.ID)

diagErr := setOrgData(d, vcdClient, adminOrg)
if diagErr != nil {
return diagErr
diags := setOrgData(d, vcdClient, adminOrg)
if diags != nil && diags.HasError() {
return diags
}

// This must be checked at the end as setOrgData can throw Warning diagnostics
if len(diags) > 0 {
return diags
}
return diags
}
13 changes: 9 additions & 4 deletions vcd/datasource_vcd_org_vdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ func datasourceVcdOrgVdcRead(_ context.Context, d *schema.ResourceData, meta int

d.SetId(adminVdc.AdminVdc.ID)

diagErr := setOrgVdcData(d, vcdClient, adminVdc)
if diagErr != nil {
return diagErr
diags := setOrgVdcData(d, vcdClient, adminVdc)
if diags != nil && diags.HasError() {
return diags
}

err = setEdgeClusterData(d, adminVdc, "data.vcd_org_vdc")
Expand All @@ -266,9 +266,14 @@ func datasourceVcdOrgVdcRead(_ context.Context, d *schema.ResourceData, meta int
dfw := govcd.NewNsxvDistributedFirewall(&vcdClient.Client, adminVdc.AdminVdc.ID)
enabled, err := dfw.IsEnabled()
if err != nil {
return diag.Errorf("error retrieving NSX-V distributed firewall state for VDC '%s': %s", vdcName, err)
return append(diags, diag.Errorf("error retrieving NSX-V distributed firewall state for VDC '%s': %s", vdcName, err)...)
}
dSet(d, "enable_nsxv_distributed_firewall", enabled)
}

// This must be checked at the end as setOrgVdcData can throw Warning diagnostics
if len(diags) > 0 {
return diags
}
return nil
}
6 changes: 6 additions & 0 deletions vcd/datasource_vcd_rde.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func datasourceVcdRde() *schema.Resource {
Description: "Specifies whether the entity is correctly resolved or not. One of PRE_CREATED, RESOLVED or RESOLUTION_ERROR",
Computed: true,
},
"metadata_entry": openApiMetadataEntryDatasourceSchema("Runtime Defined Entity"),
},
}
}
Expand Down Expand Up @@ -83,6 +84,11 @@ func datasourceVcdRdeRead(_ context.Context, d *schema.ResourceData, meta interf
dSet(d, "owner_user_id", rde.DefinedEntity.Owner.ID)
}

diagErr := updateOpenApiMetadataInState(d, vcdClient, "vcd_rde", rde)
if diagErr != nil {
return diagErr
}

d.SetId(rde.DefinedEntity.ID)

return nil
Expand Down
30 changes: 20 additions & 10 deletions vcd/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ var resourceMetadataApiRelation = map[string]string{
"vcd_vapp": "vApp",
"vcd_vapp_vm": "vApp",
"vcd_vm": "vApp",
"vcd_rde": "entity",
}

// metadataEntryDatasourceSchema returns the schema associated to metadata_entry for a given data source.
Expand Down Expand Up @@ -358,7 +359,7 @@ func checkIgnoredMetadataConflicts(d *schema.ResourceData, vcdClient *VCDClient,
Severity: severity,
Summary: "Found a conflict between 'ignore_metadata_changes' and 'metadata_entry'",
Detail: fmt.Sprintf("There is an 'ignored_metadata' block: %s\n"+
"and there is a 'metadata_entry' with key '%s' and value '%s' in your Terraform configuration that matches the criteria, hence it will be ignored.\n"+
"and there is a 'metadata_entry' with key '%s' and value '%s' in your Terraform configuration or state that matches that criteria, hence it will be ignored.\n"+
dataclouder marked this conversation as resolved.
Show resolved Hide resolved
"This will cause the entry to be present in Terraform state but it won't have any effect in VCD, causing an inconsistency.\n"+
"Please use a more fine-grained 'ignore_metadata_changes' configuration or change your metadata entry.", ignoredMetadata, newEntry["key"].(string), newEntry["value"].(string)),
AttributePath: cty.Path{},
Expand All @@ -373,6 +374,7 @@ func checkIgnoredMetadataConflicts(d *schema.ResourceData, vcdClient *VCDClient,
// This can be done as both are Computed, for compatibility reasons.
// TODO: Remove this function once "metadata" attribute is deleted in a future major release.
func updateMetadataInStateDeprecated(d *schema.ResourceData, vcdClient *VCDClient, resourceType string, receiverObject metadataCompatible) diag.Diagnostics {
var diags diag.Diagnostics

// We temporarily remove the ignored metadata filter to retrieve the deprecated metadata contents,
// which should not be affected by it.
Expand Down Expand Up @@ -405,24 +407,29 @@ func updateMetadataInStateDeprecated(d *schema.ResourceData, vcdClient *VCDClien
}

// We get metadata again with the original metadata ignore filtering
diagErr := updateMetadataInState(d, vcdClient, resourceType, receiverObject)
if diagErr != nil {
return diagErr
diags = append(diags, updateMetadataInState(d, vcdClient, resourceType, receiverObject)...)
if diags != nil && diags.HasError() {
return diags
}

// This must be checked at the end as updateMetadataInState can throw Warning diagnostics
if len(diags) > 0 {
return diags
}

return nil
}

// updateMetadataInState updates ONLY metadata_entry in the Terraform state for the given receiver object.
func updateMetadataInState(d *schema.ResourceData, vcdClient *VCDClient, resourceType string, receiverObject metadataCompatible) diag.Diagnostics {
diagErr := checkIgnoredMetadataConflicts(d, vcdClient, resourceType)
if diagErr != nil {
return diagErr
diags := checkIgnoredMetadataConflicts(d, vcdClient, resourceType)
if diags != nil && diags.HasError() {
return diags
}

metadata, err := receiverObject.GetMetadata()
if err != nil {
return diag.Errorf("error getting metadata to save in state: %s", err)
return append(diags, diag.Errorf("error getting metadata to save in state: %s", err)...)
}

// VMs can have special metadata automatically set by VCD that require to be filtered out and
Expand All @@ -431,13 +438,16 @@ func updateMetadataInState(d *schema.ResourceData, vcdClient *VCDClient, resourc
inheritedMetadataBlock := filterAndGetVcdInheritedMetadata(metadata)
err = d.Set("inherited_metadata", inheritedMetadataBlock)
if err != nil {
return diag.Errorf("could not set 'inherited_metadata' attribute: %s", err)
return append(diags, diag.Errorf("could not set 'inherited_metadata' attribute: %s", err)...)
}
}

err = setMetadataEntryInState(d, metadata.MetadataEntry)
if err != nil {
return diag.Errorf("error setting metadata entry in state: %s", err)
return append(diags, diag.Errorf("error setting metadata entry in state: %s", err)...)
}
if len(diags) > 0 {
return diags
}
return nil
}
Expand Down
Loading