|
| 1 | +package vpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/scaleway/scaleway-cli/internal/core" |
| 7 | + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/api/vpc/v1" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | +) |
| 11 | + |
| 12 | +func privateNetworkGetBuilder(c *core.Command) *core.Command { |
| 13 | + type customServer struct { |
| 14 | + ID string `json:"id"` |
| 15 | + Name string `json:"name"` |
| 16 | + State instance.ServerState `json:"state"` |
| 17 | + NicID string `json:"nic_id"` |
| 18 | + MacAddress string `json:"mac"` |
| 19 | + } |
| 20 | + |
| 21 | + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { |
| 22 | + getPNResp, err := runner(ctx, argsI) |
| 23 | + if err != nil { |
| 24 | + return getPNResp, err |
| 25 | + } |
| 26 | + pn := getPNResp.(*vpc.PrivateNetwork) |
| 27 | + |
| 28 | + client := core.ExtractClient(ctx) |
| 29 | + instanceAPI := instance.NewAPI(client) |
| 30 | + listServers, err := instanceAPI.ListServers(&instance.ListServersRequest{ |
| 31 | + PrivateNetwork: &pn.ID, |
| 32 | + }, scw.WithAllPages()) |
| 33 | + if err != nil { |
| 34 | + return getPNResp, err |
| 35 | + } |
| 36 | + |
| 37 | + customServers := []customServer{} |
| 38 | + for _, server := range listServers.Servers { |
| 39 | + for _, nic := range server.PrivateNics { |
| 40 | + if nic.PrivateNetworkID == pn.ID { |
| 41 | + customServers = append(customServers, customServer{ |
| 42 | + NicID: nic.ID, |
| 43 | + ID: nic.ServerID, |
| 44 | + MacAddress: nic.MacAddress, |
| 45 | + Name: server.Name, |
| 46 | + State: server.State, |
| 47 | + }) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return &struct { |
| 53 | + *vpc.PrivateNetwork |
| 54 | + Servers []customServer `json:"servers"` |
| 55 | + }{ |
| 56 | + pn, |
| 57 | + customServers, |
| 58 | + }, nil |
| 59 | + } |
| 60 | + |
| 61 | + c.View = &core.View{ |
| 62 | + Sections: []*core.ViewSection{ |
| 63 | + {FieldName: "Servers", Title: "Servers"}, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + return c |
| 68 | +} |
0 commit comments