Skip to content

Commit

Permalink
Merge branch 'main' into catalog-RO-access-control
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Maxia <[email protected]>
  • Loading branch information
Giuseppe Maxia committed Nov 13, 2023
2 parents 1e71603 + 521d330 commit 0178799
Show file tree
Hide file tree
Showing 40 changed files with 383 additions and 136 deletions.
1 change: 1 addition & 0 deletions .changes/v3.11.0/1126-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Resource `vcd_provider_vdc` supports metadata with `metadata_entry` blocks [GH-1126]
14 changes: 14 additions & 0 deletions scripts/make-changelog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ then
exit 1
fi

function check_eol {
file_name=$1
# Checks the last line of the file.
# Counts the number of lines (==EOL)
has_eol=$(tail -n 1 $file_name | wc -l | tr -d ' ' | tr -d '\t')

# If there isn't an EOL, we add one on the spot
if [ "$has_eol" == "0" ]
then
echo ""
fi
}

# We can indicate a version on the command line
version=$1

Expand Down Expand Up @@ -52,6 +65,7 @@ do
for f in $(ls *${section}.md | sort -n)
do
cat $f
check_eol $f
done
echo ""
done
Expand Down
2 changes: 1 addition & 1 deletion vcd/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build api || functional || catalog || vapp || network || extnetwork || org || query || vm || vdc || gateway || disk || binary || lb || lbServiceMonitor || lbServerPool || lbAppProfile || lbAppRule || lbVirtualServer || access_control || user || standaloneVm || search || auth || nsxt || role || alb || certificate || vdcGroup || ldap || rde || uiPlugin || ALL
//go:build api || functional || catalog || vapp || network || extnetwork || org || query || vm || vdc || gateway || disk || binary || lb || lbServiceMonitor || lbServerPool || lbAppProfile || lbAppRule || lbVirtualServer || access_control || user || standaloneVm || search || auth || nsxt || role || alb || certificate || vdcGroup || ldap || rde || uiPlugin || providerVdc || ALL

package vcd

Expand Down
2 changes: 1 addition & 1 deletion vcd/datasource_vcd_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func datasourceVcdCatalogRead(_ context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

diagErr := updateMetadataInState(d, vcdClient, "vcd_catalog", catalog)
diagErr := updateMetadataInStateDeprecated(d, vcdClient, "vcd_catalog", catalog)
if diagErr != nil {
return diagErr
}
Expand Down
2 changes: 1 addition & 1 deletion vcd/datasource_vcd_network_isolated_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ 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 := updateMetadataInState(d, vcdClient, "vcd_network_isolated_v2", network)
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
Expand Down
2 changes: 1 addition & 1 deletion vcd/datasource_vcd_network_routed_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ 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 := updateMetadataInState(d, vcdClient, "vcd_network_routed_v2", network)
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
Expand Down
2 changes: 1 addition & 1 deletion vcd/datasource_vcd_provider_vdc_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build ALL || functional
//go:build ALL || providerVdc || functional

package vcd

Expand Down
71 changes: 66 additions & 5 deletions vcd/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,64 @@ func metadataEntryDatasourceSchema(resourceType string) *schema.Schema {
"is_system": {
Type: schema.TypeBool,
Computed: true,
Description: "Domain for this metadata entry. true if it belongs to SYSTEM, false if it belongs to GENERAL",
Description: "Domain for this metadata entry. true, if it belongs to SYSTEM. false, if it belongs to GENERAL",
},
},
},
}
}

// metadataEntryResourceSchema returns the schema associated to metadata_entry for a given resource.
// The schema is for those resources which do NOT have old "metadata" attribute.
// The description will refer to the resource type given as input.
func metadataEntryResourceSchema(resourceType string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: fmt.Sprintf("Metadata entries for the given %s", resourceType),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
Description: "Key of this metadata entry",
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Value of this metadata entry",
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: types.MetadataStringValue,
Description: fmt.Sprintf("Type of this metadata entry. One of: '%s', '%s', '%s', '%s'. Defaults to '%s'", types.MetadataStringValue, types.MetadataNumberValue, types.MetadataBooleanValue, types.MetadataDateTimeValue, types.MetadataStringValue),
ValidateFunc: validation.StringInSlice([]string{types.MetadataStringValue, types.MetadataNumberValue, types.MetadataBooleanValue, types.MetadataDateTimeValue}, false),
},
"user_access": {
Type: schema.TypeString,
Optional: true,
Default: types.MetadataReadWriteVisibility,
Description: fmt.Sprintf("User access level for this metadata entry. One of: '%s', '%s', '%s'. Defaults to '%s'", types.MetadataReadWriteVisibility, types.MetadataReadOnlyVisibility, types.MetadataHiddenVisibility, types.MetadataReadWriteVisibility),
ValidateFunc: validation.StringInSlice([]string{types.MetadataReadWriteVisibility, types.MetadataReadOnlyVisibility, types.MetadataHiddenVisibility}, false),
},
"is_system": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Domain for this metadata entry. true, if it belongs to SYSTEM. false, if it belongs to GENERAL. Defaults to false",
},
},
},
}
}

// metadataEntryResourceSchemaDeprecated returns the schema associated to metadata_entry for a given resource.
// The schema is for those resources which have deprecated "metadata" attribute, as it contains several constraints and optional-compute
// combinations for that matter.
// The description will refer to the resource type given as input.
// TODO: Remove this function once "metadata" attribute is deleted in a future major release.
func metadataEntryResourceSchemaDeprecated(resourceType string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -213,7 +261,7 @@ func metadataEntryResourceSchema(resourceType string) *schema.Schema {
Type: schema.TypeBool,
Optional: true,
// Default: false, // Can't be set like this as we must allow empty `metadata_entry`, to be able to delete metadata (see above comment)
Description: "Domain for this metadata entry. true if it belongs to SYSTEM, false if it belongs to GENERAL",
Description: "Domain for this metadata entry. true, if it belongs to SYSTEM. false, if it belongs to GENERAL",
},
},
},
Expand Down Expand Up @@ -321,9 +369,10 @@ func checkIgnoredMetadataConflicts(d *schema.ResourceData, vcdClient *VCDClient,
return nil
}

// updateMetadataInState updates metadata and metadata_entry in the Terraform state for the given receiver object.
// updateMetadataInStateDeprecated updates deprecated metadata and the new metadata_entry in the Terraform state for the given receiver object.
// This can be done as both are Computed, for compatibility reasons.
func updateMetadataInState(d *schema.ResourceData, vcdClient *VCDClient, resourceType string, receiverObject metadataCompatible) diag.Diagnostics {
// 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 {

// We temporarily remove the ignored metadata filter to retrieve the deprecated metadata contents,
// which should not be affected by it.
Expand Down Expand Up @@ -351,6 +400,16 @@ func updateMetadataInState(d *schema.ResourceData, vcdClient *VCDClient, resourc
}

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

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
Expand All @@ -365,11 +424,11 @@ func updateMetadataInState(d *schema.ResourceData, vcdClient *VCDClient, resourc
if err != nil {
return diag.Errorf("error setting metadata entry in state: %s", err)
}

return nil
}

// setMetadataEntryInState sets the given metadata entries retrieved from VCD in the Terraform state.
// TODO: Refactor this function once "metadata" attribute is deleted in a future major release.
func setMetadataEntryInState(d *schema.ResourceData, metadataFromVcd []*types.MetadataEntry) error {
// A consequence of having metadata_entry computed is that to remove the entries one needs to write `metadata_entry {}`.
// This snippet guarantees that if we try to delete metadata with `metadata_entry {}`, we don't
Expand Down Expand Up @@ -402,6 +461,7 @@ func setMetadataEntryInState(d *schema.ResourceData, metadataFromVcd []*types.Me

// convertFromStateToMetadataValues converts the structure retrieved from Terraform state to a structure compatible
// with the Go SDK.
// TODO: Refactor this function once "metadata" attribute is deleted in a future major release.
func convertFromStateToMetadataValues(metadataAttribute []interface{}) (map[string]types.MetadataValue, error) {
metadataValues := map[string]types.MetadataValue{}
for _, rawItem := range metadataAttribute {
Expand Down Expand Up @@ -446,6 +506,7 @@ func convertFromStateToMetadataValues(metadataAttribute []interface{}) (map[stri

// getMetadataKeyWithDomainMap converts the input metadata attribute from Terraform state to a map that associates keys
// with their domain (true if is from SYSTEM domain, false if GENERAL).
// TODO: Refactor this function once "metadata" attribute is deleted in a future major release.
func getMetadataKeyWithDomainMap(metadataAttribute []interface{}) map[string]bool {
metadataKeys := map[string]bool{}
for _, rawItem := range metadataAttribute {
Expand Down
Loading

0 comments on commit 0178799

Please sign in to comment.