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

fix(CLOUD-1044): Fix update metadata for security group and snapshot #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
}