Skip to content

Commit f74b535

Browse files
committed
feat(instance): get server renders both block and instance volumes details
1 parent 157a515 commit f74b535

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

internal/namespaces/instance/v1/custom_server.go

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,28 @@ func serversMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error)
120120
return human.Marshal(humanServers, opt)
121121
}
122122

123+
type customVolume struct {
124+
ID string `json:"id"`
125+
Name string `json:"name"`
126+
Size scw.Size `json:"size"`
127+
VolumeType string `json:"volume_type"`
128+
IOPS string `json:"iops"`
129+
State string `json:"state"`
130+
CreationDate *time.Time `json:"creation_date"`
131+
ModificationDate *time.Time `json:"modification_date"`
132+
Boot bool `json:"boot"`
133+
Zone string `json:"zone"`
134+
}
135+
123136
// orderVolumes return an ordered slice based on the volume map key "0", "1", "2",...
124-
func orderVolumes(v map[string]*instance.VolumeServer) []*instance.VolumeServer {
137+
func orderVolumes(v map[string]*customVolume) []*customVolume {
125138
indexes := []string(nil)
126139
for index := range v {
127140
indexes = append(indexes, index)
128141
}
129142
sort.Strings(indexes)
130143

131-
orderedVolumes := make([]*instance.VolumeServer, 0, len(indexes))
144+
orderedVolumes := make([]*customVolume, 0, len(indexes))
132145
for _, index := range indexes {
133146
orderedVolumes = append(orderedVolumes, v[index])
134147
}
@@ -407,13 +420,58 @@ func serverGetBuilder(c *core.Command) *core.Command {
407420
})
408421
}
409422

423+
volumes := map[string]*customVolume{}
424+
blockAPI := block.NewAPI(client)
425+
426+
for _, volume := range getServerResp.Server.Volumes {
427+
customVol := &customVolume{
428+
ID: volume.ID,
429+
Zone: volume.Zone.String(),
430+
Boot: volume.Boot,
431+
}
432+
433+
blockVol, _ := blockAPI.GetVolume(&block.GetVolumeRequest{
434+
VolumeID: volume.ID,
435+
Zone: volume.Zone,
436+
})
437+
if blockVol != nil {
438+
customVol.Name = blockVol.Name
439+
customVol.Size = blockVol.Size
440+
customVol.VolumeType = blockVol.Type
441+
customVol.State = blockVol.Status.String()
442+
customVol.CreationDate = blockVol.CreatedAt
443+
customVol.ModificationDate = blockVol.UpdatedAt
444+
if *blockVol.Specs.PerfIops == 5000 {
445+
customVol.IOPS = "5K"
446+
} else {
447+
customVol.IOPS = "15K"
448+
}
449+
} else {
450+
instanceVol, err := instance.NewAPI(client).GetVolume(&instance.GetVolumeRequest{
451+
VolumeID: volume.ID,
452+
Zone: volume.Zone,
453+
})
454+
if err != nil {
455+
return nil, err
456+
}
457+
customVol.Name = instanceVol.Volume.Name
458+
customVol.Size = instanceVol.Volume.Size
459+
customVol.VolumeType = instanceVol.Volume.VolumeType.String()
460+
customVol.State = instanceVol.Volume.State.String()
461+
customVol.CreationDate = instanceVol.Volume.CreationDate
462+
customVol.ModificationDate = instanceVol.Volume.ModificationDate
463+
}
464+
465+
volumes[volume.ID] = customVol
466+
}
467+
410468
return &struct {
411469
*instance.Server
412-
Volumes []*instance.VolumeServer
470+
Volumes []*customVolume
413471
PrivateNics []customNICs `json:"private_nics"`
414472
}{
415473
getServerResp.Server,
416-
orderVolumes(getServerResp.Server.Volumes),
474+
orderVolumes(volumes),
417475
nics,
418476
}, nil
419477
}

0 commit comments

Comments
 (0)