|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + |
| 9 | + "github.com/threefoldtech/zosbase/pkg" |
| 10 | + "github.com/threefoldtech/zosbase/pkg/gridtypes" |
| 11 | +) |
| 12 | + |
| 13 | +func (a *API) NetworkWGPorts(ctx context.Context) ([]uint, error) { |
| 14 | + if a.isLightMode() { |
| 15 | + return nil, ErrNotSupportedInLightMode |
| 16 | + } |
| 17 | + return a.networkerStub.WireguardPorts(ctx) |
| 18 | +} |
| 19 | + |
| 20 | +func (a *API) NetworkPublicConfigGet(ctx context.Context, _ any) (pkg.PublicConfig, error) { |
| 21 | + if a.isLightMode() { |
| 22 | + return pkg.PublicConfig{}, ErrNotSupportedInLightMode |
| 23 | + } |
| 24 | + |
| 25 | + return a.networkerStub.GetPublicConfig(ctx) |
| 26 | +} |
| 27 | + |
| 28 | +func (a *API) NetworkHasIPv6(ctx context.Context) (bool, error) { |
| 29 | + if a.isLightMode() { |
| 30 | + return false, nil |
| 31 | + } |
| 32 | + |
| 33 | + ipData, err := a.networkerStub.GetPublicIPv6Subnet(ctx) |
| 34 | + hasIP := ipData.IP != nil && err == nil |
| 35 | + return hasIP, nil |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +func (a *API) NetworkListPublicIPs(ctx context.Context) ([]string, error) { |
| 40 | + if a.isLightMode() { |
| 41 | + return nil, ErrNotSupportedInLightMode |
| 42 | + } |
| 43 | + |
| 44 | + return a.provisionStub.ListPublicIPs(ctx) |
| 45 | +} |
| 46 | + |
| 47 | +func (a *API) NetworkListPrivateIPs(ctx context.Context, networkName string) ([]string, error) { |
| 48 | + name := gridtypes.Name(networkName) |
| 49 | + twinID, ok := ctx.Value("twin_id").(uint32) |
| 50 | + if !ok { |
| 51 | + return nil, errors.New("could not get twin_id from context") |
| 52 | + } |
| 53 | + return a.provisionStub.ListPrivateIPs(ctx, twinID, name) |
| 54 | +} |
| 55 | + |
| 56 | +func (a *API) NetworkInterfaces(ctx context.Context) (pkg.Interfaces, error) { |
| 57 | + if a.isLightMode() { |
| 58 | + return a.networkerLightStub.Interfaces(ctx, "zos", "") |
| 59 | + } |
| 60 | + |
| 61 | + type q struct { |
| 62 | + inf string |
| 63 | + ns string |
| 64 | + rename string |
| 65 | + } |
| 66 | + result := pkg.Interfaces{ |
| 67 | + Interfaces: make(map[string]pkg.Interface), |
| 68 | + } |
| 69 | + for _, i := range []q{{"zos", "", "zos"}, {"nygg6", "ndmz", "ygg"}} { |
| 70 | + ips, mac, err := a.networkerStub.Addrs(ctx, i.inf, i.ns) |
| 71 | + if err != nil { |
| 72 | + return pkg.Interfaces{Interfaces: make(map[string]pkg.Interface)}, fmt.Errorf("failed to get ips for '%s' interface: %w", i.inf, err) |
| 73 | + } |
| 74 | + |
| 75 | + iface := pkg.Interface{ |
| 76 | + Name: i.rename, |
| 77 | + Mac: mac, |
| 78 | + IPs: []net.IPNet{}, |
| 79 | + } |
| 80 | + |
| 81 | + for _, item := range ips { |
| 82 | + ipNet := net.IPNet{ |
| 83 | + IP: item, |
| 84 | + Mask: nil, |
| 85 | + } |
| 86 | + iface.IPs = append(iface.IPs, ipNet) |
| 87 | + } |
| 88 | + |
| 89 | + result.Interfaces[i.rename] = iface |
| 90 | + } |
| 91 | + |
| 92 | + return result, nil |
| 93 | +} |
| 94 | + |
| 95 | +// all interfaces on the node |
| 96 | +func (a *API) AdminInterfaces(ctx context.Context) (pkg.Interfaces, error) { |
| 97 | + if a.isLightMode() { |
| 98 | + return a.networkerLightStub.Interfaces(ctx, "", "") |
| 99 | + } |
| 100 | + |
| 101 | + return a.networkerStub.Interfaces(ctx, "", "") |
| 102 | +} |
| 103 | + |
| 104 | +func (a *API) AdminSetPublicNIC(ctx context.Context, device string) error { |
| 105 | + if a.isLightMode() { |
| 106 | + return ErrNotSupportedInLightMode |
| 107 | + } |
| 108 | + return a.networkerStub.SetPublicExitDevice(ctx, device) |
| 109 | +} |
| 110 | + |
| 111 | +func (a *API) AdminGetPublicNIC(ctx context.Context) (pkg.ExitDevice, error) { |
| 112 | + if a.isLightMode() { |
| 113 | + return pkg.ExitDevice{}, ErrNotSupportedInLightMode |
| 114 | + } |
| 115 | + |
| 116 | + return a.networkerStub.GetPublicExitDevice(ctx) |
| 117 | +} |
0 commit comments