Skip to content

Commit b2ef7b1

Browse files
authored
feat(instance): render block volumes details when getting a server (#4633)
1 parent 808fb4d commit b2ef7b1

File tree

3 files changed

+1407
-1205
lines changed

3 files changed

+1407
-1205
lines changed

internal/namespaces/instance/v1/custom_server.go

Lines changed: 65 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,61 @@ 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 != nil && blockVol.Specs.PerfIops != nil {
445+
switch *blockVol.Specs.PerfIops {
446+
case 5000:
447+
customVol.IOPS = "5K"
448+
case 15000:
449+
customVol.IOPS = "15K"
450+
}
451+
}
452+
} else {
453+
instanceVol, err := instance.NewAPI(client).GetVolume(&instance.GetVolumeRequest{
454+
VolumeID: volume.ID,
455+
Zone: volume.Zone,
456+
})
457+
if err != nil {
458+
return nil, err
459+
}
460+
customVol.Name = instanceVol.Volume.Name
461+
customVol.Size = instanceVol.Volume.Size
462+
customVol.VolumeType = instanceVol.Volume.VolumeType.String()
463+
customVol.State = instanceVol.Volume.State.String()
464+
customVol.CreationDate = instanceVol.Volume.CreationDate
465+
customVol.ModificationDate = instanceVol.Volume.ModificationDate
466+
}
467+
468+
volumes[volume.ID] = customVol
469+
}
470+
410471
return &struct {
411472
*instance.Server
412-
Volumes []*instance.VolumeServer
473+
Volumes []*customVolume
413474
PrivateNics []customNICs `json:"private_nics"`
414475
}{
415476
getServerResp.Server,
416-
orderVolumes(getServerResp.Server.Volumes),
477+
orderVolumes(volumes),
417478
nics,
418479
}, nil
419480
}

0 commit comments

Comments
 (0)