Skip to content

Commit

Permalink
Set RawJson format for volume meta
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Zarechenskyi authored and DevMike committed Mar 21, 2024
1 parent 8cfb356 commit bab1de6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
27 changes: 14 additions & 13 deletions ah/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ah

import (
"context"
"encoding/json"
"fmt"
"net/http"
)
Expand All @@ -33,19 +34,19 @@ type Volume struct {
DatacenterIDs []string `json:"datacenter_ids,omitempty"`
ReplicationLevel int `json:"replication_level,omitempty"`
} `json:"volume_pool,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
FileSystem string `json:"file_system,omitempty"`
State string `json:"state,omitempty"`
Number string `json:"number,omitempty"`
OriginalID string `json:"original_id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
AttachedAt string `json:"attached_at,omitempty"`
ProductID string `json:"product_id,omitempty"`
Size int `json:"size,omitempty"`
Port int `json:"port,omitempty"`
PlanID int `json:"plan_id,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
FileSystem string `json:"file_system,omitempty"`
State string `json:"state,omitempty"`
Number string `json:"number,omitempty"`
OriginalID string `json:"original_id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
AttachedAt string `json:"attached_at,omitempty"`
ProductID string `json:"product_id,omitempty"`
Meta json.RawMessage `json:"meta,omitempty"`
Size int `json:"size,omitempty"`
Port int `json:"port,omitempty"`
PlanID int `json:"plan_id,omitempty"`
}

// VolumeAction object
Expand Down
37 changes: 28 additions & 9 deletions ah/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,7 @@ const volumeResponse = `{
],
"replication_level": 2
},
"meta": {
"kubernetes": {
"cluster": {
"id": "193e10b3-25ce-4488-9c5a-840b6a22abd6",
"number": "KUB1000001"
}
}
}
"meta": "{\"kubernetes\":{\"cluster\":{\"id\":\"193e10b3-25ce-4488-9c5a-840b6a22abd6\",\"number\":\"KUB1000001\"}}}"
}`

var (
Expand Down Expand Up @@ -175,7 +168,33 @@ func TestVolumes_Update(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if volume == nil || volume.ID != "e88cb60e-828f-416f-8ab0-e05ab4493b1a" || volume.Meta["kubernetes"].(map[string]interface{})["cluster"].(map[string]interface{})["id"] != "193e10b3-25ce-4488-9c5a-840b6a22abd6" {

var metaString string
if err := json.Unmarshal(volume.Meta, &metaString); err != nil {
t.Errorf("Unexpected error: %v", err)
}

var metaMap map[string]interface{}
if err := json.Unmarshal([]byte(metaString), &metaMap); err != nil {
t.Errorf("Unexpected error: %v", err)
}
kubernetes, ok := metaMap["kubernetes"].(map[string]interface{})
if !ok {
t.Errorf("Unexpected error: %v", err)
return
}
cluster, ok := kubernetes["cluster"].(map[string]interface{})
if !ok {
t.Errorf("Unexpected error: %v", err)
return
}
id, ok := cluster["id"].(string)
if !ok {
t.Errorf("Unexpected error: %v", err)
return
}

if volume == nil || volume.ID != "e88cb60e-828f-416f-8ab0-e05ab4493b1a" || id != "193e10b3-25ce-4488-9c5a-840b6a22abd6" {
t.Errorf("Invalid response: %v", volume)
}

Expand Down

0 comments on commit bab1de6

Please sign in to comment.