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

Refactor metadata handling #853

Merged
merged 26 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
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
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.13

require (
github.com/hashicorp/go-version v1.4.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
github.com/kr/pretty v0.2.1
github.com/vmware/go-vcloud-director/v2 v2.16.0-alpha.4
)

replace github.com/vmware/go-vcloud-director/v2 => github.com/adambarreiro/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220519104704-1e8dd9ca357d
26 changes: 14 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C6
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/adambarreiro/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220519104704-1e8dd9ca357d h1:HjCV1tkbxyrcMBMzbaWIw7eiww3sm3gfPqWVV1sLlWw=
github.com/adambarreiro/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220519104704-1e8dd9ca357d/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
Expand Down Expand Up @@ -92,8 +94,9 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
Expand All @@ -120,10 +123,11 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hc-install v0.3.1 h1:VIjllE6KyAI1A244G8kTaHXy+TL5/XYzvrtFi8po/Yk=
github.com/hashicorp/hc-install v0.3.1/go.mod h1:3LCdWcCDS1gaHC9mhHCGbkYfoY6vdsKohGjugbZdZak=
github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc=
github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hc-install v0.3.2 h1:oiQdJZvXmkNcRcEOOfM5n+VTsvNjWQeOjfAoO6dKSH8=
github.com/hashicorp/hc-install v0.3.2/go.mod h1:xMG6Tr8Fw1WFjlxH0A9v61cW15pFwgEGqEz0V4jisHs=
github.com/hashicorp/hcl/v2 v2.12.0 h1:PsYxySWpMD4KPaoJLnsHwtK5Qptvj/4Q6s0t4sUxZf4=
github.com/hashicorp/hcl/v2 v2.12.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.16.1 h1:NAwZFJW2L2SaCBVZoVaH8LPImLOGbPLkSHy0IYbs2uE=
Expand All @@ -132,10 +136,11 @@ github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniy
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
github.com/hashicorp/terraform-plugin-go v0.9.0 h1:FvLY/3z4SNVatPZdoFcyrlNbCar+WyyOTv5X4Tp+WZc=
github.com/hashicorp/terraform-plugin-go v0.9.0/go.mod h1:EawBkgjBWNf7jiKnVoyDyF39OSV+u6KUX+Y73EPj3oM=
github.com/hashicorp/terraform-plugin-log v0.3.0 h1:NPENNOjaJSVX0f7JJTl4f/2JKRPQ7S2ZN9B4NSqq5kA=
github.com/hashicorp/terraform-plugin-log v0.3.0/go.mod h1:EjueSP/HjlyFAsDqt+okpCPjkT4NDynAe32AeDC4vps=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0 h1:GZ8NY74rxObB7QHE/JiuBW0ZEr04rlplR/TVrkgw3rw=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0/go.mod h1:+m4FDQ8h1ulz7zpWtqmZn2JSZQDXUVibhUShbkQVId4=
github.com/hashicorp/terraform-plugin-log v0.4.0 h1:F3eVnm8r2EfQCe2k9blPIiF/r2TT01SHijXnS7bujvc=
github.com/hashicorp/terraform-plugin-log v0.4.0/go.mod h1:9KclxdunFownr4pIm1jdmwKRmE4d6HVG2c9XDq47rpg=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0 h1:9fjPgCenJqnbjo95SDcbJ+YdLyEC1N35cwKWcRWhJTQ=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0/go.mod h1:hLa0sTiySU/AWEgV2GxJh0/pQIqcCmm30IPja9N9lTg=
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 h1:1FGtlkJw87UsTMg5s8jrekrHmUPUJaMcu6ELiVhQrNw=
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
Expand Down Expand Up @@ -180,8 +185,8 @@ github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
Expand Down Expand Up @@ -220,8 +225,6 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmware/go-vcloud-director/v2 v2.16.0-alpha.4 h1:ssluMUa3mtr37JuO/ADbasUTQ7e00USf7DcIciq3LZ8=
github.com/vmware/go-vcloud-director/v2 v2.16.0-alpha.4/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68=
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -307,7 +310,6 @@ golang.org/x/tools v0.0.0-20200713011307-fd294ab11aed/go.mod h1:njjCfa9FT2d7l9Bc
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down
28 changes: 1 addition & 27 deletions vcd/resource_vcd_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,33 +418,7 @@ func createOrUpdateAdminCatalogMetadata(d *schema.ResourceData, meta interface{}
return fmt.Errorf("unable to find catalog: %s", err)
}

if d.HasChange("metadata") {
oldRaw, newRaw := d.GetChange("metadata")
oldMetadata := oldRaw.(map[string]interface{})
newMetadata := newRaw.(map[string]interface{})
var toBeRemovedMetadata []string
// Check if any key in old metadata was removed in new metadata.
// Creates a list of keys to be removed.
for k := range oldMetadata {
if _, ok := newMetadata[k]; !ok {
toBeRemovedMetadata = append(toBeRemovedMetadata, k)
}
}
for _, k := range toBeRemovedMetadata {
err := catalog.DeleteMetadataEntry(k)
if err != nil {
return fmt.Errorf("error deleting metadata: %s", err)
}
}
// Add new metadata
for k, v := range newMetadata {
err = catalog.AddMetadataEntry(types.MetadataStringValue, k, v.(string))
if err != nil {
return fmt.Errorf("error adding metadata: %s", err)
}
}
}
return nil
return createOrUpdateMetadata(d, catalog, "metadata")
}

func setCatalogData(d *schema.ResourceData, adminOrg *govcd.AdminOrg, catalogName string) error {
Expand Down
68 changes: 13 additions & 55 deletions vcd/resource_vcd_catalog_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

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

func resourceVcdCatalogItem() *schema.Resource {
Expand Down Expand Up @@ -193,7 +192,7 @@ func finishHandlingTask(d *schema.ResourceData, task govcd.Task, itemName string
return nil
}

func resourceVcdCatalogItemRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceVcdCatalogItemRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return genericVcdCatalogItemRead(d, meta, "resource")
}

Expand All @@ -217,6 +216,7 @@ func genericVcdCatalogItemRead(d *schema.ResourceData, meta interface{}, origin
if err != nil {
return diag.Errorf("Unable to find catalog item's associated vApp template metadata: %s", err)
}

catalogItemMetadata, err := catalogItem.GetMetadata()
if err != nil {
return diag.Errorf("Unable to find metadata for the catalog item: %s", err)
Expand All @@ -225,10 +225,12 @@ func genericVcdCatalogItemRead(d *schema.ResourceData, meta interface{}, origin
dSet(d, "name", catalogItem.CatalogItem.Name)
dSet(d, "created", vAppTemplate.VAppTemplate.DateCreated)
dSet(d, "description", catalogItem.CatalogItem.Description)

err = d.Set("metadata", getMetadataStruct(vAppTemplateMetadata.MetadataEntry))
if err != nil {
return diag.Errorf("Unable to set metadata for the catalog item's associated vApp template: %s", err)
}

err = d.Set("catalog_item_metadata", getMetadataStruct(catalogItemMetadata.MetadataEntry))
if err != nil {
return diag.Errorf("Unable to set metadata for the catalog item: %s", err)
Expand All @@ -237,11 +239,11 @@ func genericVcdCatalogItemRead(d *schema.ResourceData, meta interface{}, origin
return nil
}

func resourceVcdCatalogItemDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceVcdCatalogItemDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return deleteCatalogItem(d, meta.(*VCDClient))
}

func resourceVcdCatalogItemUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceVcdCatalogItemUpdate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChange("description") || d.HasChange("name") {
catalogItem, err := findCatalogItem(d, meta.(*VCDClient), "resource")
if err != nil {
Expand Down Expand Up @@ -290,57 +292,13 @@ func createOrUpdateCatalogItemMetadata(d *schema.ResourceData, meta interface{})
return err
}

if d.HasChange("metadata") {
oldRaw, newRaw := d.GetChange("metadata")
oldMetadata := oldRaw.(map[string]interface{})
newMetadata := newRaw.(map[string]interface{})
var toBeRemovedMetadata []string
// Check if any key in old metadata was removed in new metadata.
// Creates a list of keys to be removed.
for k := range oldMetadata {
if _, ok := newMetadata[k]; !ok {
toBeRemovedMetadata = append(toBeRemovedMetadata, k)
}
}
for _, k := range toBeRemovedMetadata {
err := vAppTemplate.DeleteMetadataEntry(k)
if err != nil {
return fmt.Errorf("error deleting metadata from catalog item's associated vApp template: %s", err)
}
}
// Add new metadata
for k, v := range newMetadata {
err := vAppTemplate.AddMetadataEntry(types.MetadataStringValue, k, v.(string))
if err != nil {
return fmt.Errorf("error adding metadata to catalog item's associated vApp template: %s", err)
}
}
}
// TODO: Move this code snippet to a function with generics
if d.HasChange("catalog_item_metadata") {
oldRaw, newRaw := d.GetChange("catalog_item_metadata")
oldMetadata := oldRaw.(map[string]interface{})
newMetadata := newRaw.(map[string]interface{})
var toBeRemovedMetadata []string
for k := range oldMetadata {
if _, ok := newMetadata[k]; !ok {
toBeRemovedMetadata = append(toBeRemovedMetadata, k)
}
}
for _, k := range toBeRemovedMetadata {
err := catalogItem.DeleteMetadataEntry(k)
if err != nil {
return fmt.Errorf("error deleting metadata from catalog item: %s", err)
}
}
for k, v := range newMetadata {
err := catalogItem.AddMetadataEntry(types.MetadataStringValue, k, v.(string))
if err != nil {
return fmt.Errorf("error adding metadata to catalog item: %s", err)
}
}
err = createOrUpdateMetadata(d, &vAppTemplate, "metadata")
if err != nil {
return err

}
return nil

return createOrUpdateMetadata(d, catalogItem, "catalog_item_metadata")
}

// Imports a CatalogItem into Terraform state
Expand All @@ -349,7 +307,7 @@ func createOrUpdateCatalogItemMetadata(d *schema.ResourceData, meta interface{})
//
// Example import path (id): org_name.catalog_name.catalog_item_name
// Note: the separator can be changed using Provider.import_separator or variable VCD_IMPORT_SEPARATOR
func resourceVcdCatalogItemImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
func resourceVcdCatalogItemImport(_ context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
resourceURI := strings.Split(d.Id(), ImportSeparator)
if len(resourceURI) != 3 {
return nil, fmt.Errorf("resource name must be specified as org.catalog.catalog_item")
Expand Down
29 changes: 1 addition & 28 deletions vcd/resource_vcd_catalog_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

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

func resourceVcdCatalogMedia() *schema.Resource {
Expand Down Expand Up @@ -304,33 +303,7 @@ func createOrUpdateMediaItemMetadata(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("unable to find media item: %s", err)
}

if d.HasChange("metadata") {
oldRaw, newRaw := d.GetChange("metadata")
oldMetadata := oldRaw.(map[string]interface{})
newMetadata := newRaw.(map[string]interface{})
var toBeRemovedMetadata []string
// Check if any key in old metadata was removed in new metadata.
// Creates a list of keys to be removed.
for k := range oldMetadata {
if _, ok := newMetadata[k]; !ok {
toBeRemovedMetadata = append(toBeRemovedMetadata, k)
}
}
for _, k := range toBeRemovedMetadata {
err := media.DeleteMetadataEntry(k)
if err != nil {
return fmt.Errorf("error deleting metadata: %s", err)
}
}
// Add new metadata
for k, v := range newMetadata {
err = media.AddMetadataEntry(types.MetadataStringValue, k, v.(string))
if err != nil {
return fmt.Errorf("error adding metadata: %s", err)
}
}
}
return nil
return createOrUpdateMetadata(d, media, "metadata")
}

// resourceVcdCatalogMediaImport is responsible for importing the resource.
Expand Down
36 changes: 2 additions & 34 deletions vcd/resource_vcd_independent_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func resourceVcdIndependentDiskCreate(ctx context.Context, d *schema.ResourceDat

d.SetId(disk.Disk.Id)

err = createOrUpdateDiskMetadata(d, disk)
err = createOrUpdateMetadata(d, disk, "metadata")
if err != nil {
return diag.Errorf("error adding metadata to independent disk: %s", err)
}
Expand Down Expand Up @@ -333,7 +333,7 @@ func resourceVcdIndependentDiskUpdate(ctx context.Context, d *schema.ResourceDat

}

err = createOrUpdateDiskMetadata(d, disk)
err = createOrUpdateMetadata(d, disk, "metadata")
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -708,35 +708,3 @@ func listDisksForImport(meta interface{}, orgName, vdcName, diskName string) ([]
}
return nil, fmt.Errorf("resource was not imported! %s\n%s", errHelpDiskImport, buf.String())
}

func createOrUpdateDiskMetadata(d *schema.ResourceData, disk *govcd.Disk) error {
log.Printf("[TRACE] adding/updating metadata to Disk")

if d.HasChange("metadata") {
oldRaw, newRaw := d.GetChange("metadata")
oldMetadata := oldRaw.(map[string]interface{})
newMetadata := newRaw.(map[string]interface{})
var toBeRemovedMetadata []string
// Check if any key in old metadata was removed in new metadata.
// Creates a list of keys to be removed.
for k := range oldMetadata {
if _, ok := newMetadata[k]; !ok {
toBeRemovedMetadata = append(toBeRemovedMetadata, k)
}
}
for _, k := range toBeRemovedMetadata {
err := disk.DeleteMetadataEntry(k)
if err != nil {
return fmt.Errorf("error deleting metadata: %s", err)
}
}
// Add new metadata
for k, v := range newMetadata {
err := disk.AddMetadataEntry(types.MetadataStringValue, k, v.(string))
if err != nil {
return fmt.Errorf("error adding metadata: %s", err)
}
}
}
return nil
}
Loading