Skip to content

Commit

Permalink
fix(CLOUD-1044): Fix update metadata for security group and snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Semenov committed Feb 3, 2025
1 parent 3a3c23a commit 19eb9ea
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
1 change: 0 additions & 1 deletion edgecenter/resource_edgecenter_securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func resourceSecurityGroup() *schema.Resource {
"metadata_map": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Description: "A map containing metadata, for example tags.",
Elem: &schema.Schema{
Type: schema.TypeString,
Expand Down
30 changes: 28 additions & 2 deletions edgecenter/resource_edgecenter_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,31 @@ func resourceSnapshot() *schema.Resource {
"metadata": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"metadata_read_only": {
Type: schema.TypeList,
Computed: true,
Description: `A list of read-only metadata items, e.g. tags.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
"read_only": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"last_updated": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -164,7 +184,13 @@ func resourceSnapshotRead(ctx context.Context, d *schema.ResourceData, m interfa
d.Set("volume_id", snapshot.VolumeID)
d.Set("region_id", snapshot.RegionID)
d.Set("project_id", snapshot.ProjectID)
if err := d.Set("metadata", snapshot.Metadata); err != nil {

metadata, metadataReadOnly := separateMetadata(snapshot.Metadata)

if err := d.Set("metadata", metadata); err != nil {
return diag.FromErr(err)
}
if err := d.Set("metadata_read_only", metadataReadOnly); err != nil {
return diag.FromErr(err)
}

Expand Down
41 changes: 41 additions & 0 deletions edgecenter/utils_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edgecenter

type SnapshotMetadata = map[string]interface{}

type SnapshotReadOnlyMetadata = []map[string]interface{}

var snapshotReadOnlyTags = map[string]struct{}{
"bootable": {},
"task_id": {},
"volume_name": {},
"volume_type": {},
}

// separateMetadata takes a map of string key-value pairs (md) and separates it into two distinct collections
// Parameters:
// - md: A map of string key-value pairs representing metadata to be separated.
//
// Returns:
// - SnapshotMetadata: A map containing metadata that is mutable.
// - SnapshotReadOnlyMetadata: A slice containing metadata that is read-only.
func separateMetadata(md map[string]string) (SnapshotMetadata, SnapshotReadOnlyMetadata) {
metadata := make(SnapshotMetadata)
metadataReadOnly := make(SnapshotReadOnlyMetadata, 0, len(md))

for k, v := range md {
if _, ok := snapshotReadOnlyTags[k]; ok {
metadataReadOnly = append(
metadataReadOnly,
SnapshotMetadata{
"key": k,
"value": v,
"read_only": true,
})
continue
}

metadata[k] = v
}

return metadata, metadataReadOnly
}

0 comments on commit 19eb9ea

Please sign in to comment.