Skip to content

Commit a13b439

Browse files
authored
Merge pull request #1728 from irhndt/main
Address sets for nftables and OVN
2 parents dd211db + af2a1e3 commit a13b439

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+12823
-4662
lines changed

client/incus_network_address_sets.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package incus
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
7+
"github.com/lxc/incus/v6/shared/api"
8+
)
9+
10+
// GetNetworkAddressSetNames returns a list of network address set names.
11+
func (r *ProtocolIncus) GetNetworkAddressSetNames() ([]string, error) {
12+
if !r.HasExtension("network_address_set") {
13+
return nil, fmt.Errorf(`The server is missing the required "network_address_set" API extension`)
14+
}
15+
16+
// Fetch the raw URL values.
17+
urls := []string{}
18+
baseURL := "/network-address-sets"
19+
_, err := r.queryStruct("GET", baseURL, nil, "", &urls)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
// Parse it.
25+
return urlsToResourceNames(baseURL, urls...)
26+
}
27+
28+
// GetNetworkAddressSets returns a list of network address set structs.
29+
func (r *ProtocolIncus) GetNetworkAddressSets() ([]api.NetworkAddressSet, error) {
30+
if !r.HasExtension("network_address_set") {
31+
return nil, fmt.Errorf(`The server is missing the required "network_address_set" API extension`)
32+
}
33+
34+
addressSets := []api.NetworkAddressSet{}
35+
36+
// Fetch the raw value.
37+
_, err := r.queryStruct("GET", "/network-address-sets?recursion=1", nil, "", &addressSets)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
return addressSets, nil
43+
}
44+
45+
// GetNetworkAddressSetsAllProjects returns a list of network address set structs across all projects.
46+
func (r *ProtocolIncus) GetNetworkAddressSetsAllProjects() ([]api.NetworkAddressSet, error) {
47+
if !r.HasExtension("network_address_sets_all_projects") {
48+
return nil, fmt.Errorf(`The server is missing the required "network_address_sets_all_projects" API extension`)
49+
}
50+
51+
addressSets := []api.NetworkAddressSet{}
52+
_, err := r.queryStruct("GET", "/network-address-sets?recursion=1&all-projects=true", nil, "", &addressSets)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
return addressSets, nil
58+
}
59+
60+
// GetNetworkAddressSet returns a network address set entry for the provided name.
61+
func (r *ProtocolIncus) GetNetworkAddressSet(name string) (*api.NetworkAddressSet, string, error) {
62+
if !r.HasExtension("network_address_set") {
63+
return nil, "", fmt.Errorf(`The server is missing the required "network_address_set" API extension`)
64+
}
65+
66+
addrSet := api.NetworkAddressSet{}
67+
68+
// Fetch the raw value.
69+
etag, err := r.queryStruct("GET", fmt.Sprintf("/network-address-sets/%s", url.PathEscape(name)), nil, "", &addrSet)
70+
if err != nil {
71+
return nil, "", err
72+
}
73+
74+
return &addrSet, etag, nil
75+
}
76+
77+
// CreateNetworkAddressSet defines a new network address set using the provided struct.
78+
func (r *ProtocolIncus) CreateNetworkAddressSet(as api.NetworkAddressSetsPost) error {
79+
if !r.HasExtension("network_address_set") {
80+
return fmt.Errorf(`The server is missing the required "network_address_set" API extension`)
81+
}
82+
83+
// Send the request.
84+
_, _, err := r.query("POST", "/network-address-sets", as, "")
85+
if err != nil {
86+
return err
87+
}
88+
89+
return nil
90+
}
91+
92+
// UpdateNetworkAddressSet updates the network address set to match the provided struct.
93+
func (r *ProtocolIncus) UpdateNetworkAddressSet(name string, as api.NetworkAddressSetPut, ETag string) error {
94+
if !r.HasExtension("network_address_set") {
95+
return fmt.Errorf(`The server is missing the required "network_address_set" API extension`)
96+
}
97+
98+
// Send the request.
99+
_, _, err := r.query("PUT", fmt.Sprintf("/network-address-sets/%s", url.PathEscape(name)), as, ETag)
100+
if err != nil {
101+
return err
102+
}
103+
104+
return nil
105+
}
106+
107+
// RenameNetworkAddressSet renames an existing network address set entry.
108+
func (r *ProtocolIncus) RenameNetworkAddressSet(name string, as api.NetworkAddressSetPost) error {
109+
if !r.HasExtension("network_address_set") {
110+
return fmt.Errorf(`The server is missing the required "network_address_set" API extension`)
111+
}
112+
113+
// Send the request.
114+
_, _, err := r.query("POST", fmt.Sprintf("/network-address-sets/%s", url.PathEscape(name)), as, "")
115+
if err != nil {
116+
return err
117+
}
118+
119+
return nil
120+
}
121+
122+
// DeleteNetworkAddressSet deletes an existing network address set.
123+
func (r *ProtocolIncus) DeleteNetworkAddressSet(name string) error {
124+
if !r.HasExtension("network_address_set") {
125+
return fmt.Errorf(`The server is missing the required "network_address_set" API extension`)
126+
}
127+
128+
// Send the request.
129+
_, _, err := r.query("DELETE", fmt.Sprintf("/network-address-sets/%s", url.PathEscape(name)), nil, "")
130+
if err != nil {
131+
return err
132+
}
133+
134+
return nil
135+
}

client/interfaces.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ type InstanceServer interface {
239239
RenameNetworkACL(name string, acl api.NetworkACLPost) (err error)
240240
DeleteNetworkACL(name string) (err error)
241241

242+
// Network address set functions ("network_address_set" API extension)
243+
GetNetworkAddressSetNames() (names []string, err error)
244+
GetNetworkAddressSets() (AddressSets []api.NetworkAddressSet, err error)
245+
GetNetworkAddressSetsAllProjects() (AddressSets []api.NetworkAddressSet, err error)
246+
GetNetworkAddressSet(name string) (AddressSet *api.NetworkAddressSet, ETag string, err error)
247+
CreateNetworkAddressSet(AddressSet api.NetworkAddressSetsPost) (err error)
248+
UpdateNetworkAddressSet(name string, AddressSet api.NetworkAddressSetPut, ETag string) (err error)
249+
RenameNetworkAddressSet(name string, AddressSet api.NetworkAddressSetPost) (err error)
250+
DeleteNetworkAddressSet(name string) (err error)
251+
242252
// Network allocations functions ("network_allocations" API extension)
243253
GetNetworkAllocations() (allocations []api.NetworkAllocations, err error)
244254
GetNetworkAllocationsAllProjects() (allocations []api.NetworkAllocations, err error)

cmd/incus/completion.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,69 @@ func (g *cmdGlobal) cmpNetworkACLRuleProperties() ([]string, cobra.ShellCompDire
501501
return results, cobra.ShellCompDirectiveNoSpace
502502
}
503503

504+
func (g *cmdGlobal) cmpNetworkAddressSets(toComplete string) ([]string, cobra.ShellCompDirective) {
505+
results := []string{}
506+
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
507+
508+
resources, _ := g.parseServers(toComplete)
509+
510+
if len(resources) <= 0 {
511+
return nil, cobra.ShellCompDirectiveError
512+
}
513+
514+
resource := resources[0]
515+
516+
// Get the network address set names from the server.
517+
addrSets, err := resource.server.GetNetworkAddressSetNames()
518+
if err != nil {
519+
return nil, cobra.ShellCompDirectiveError
520+
}
521+
522+
for _, addrSet := range addrSets {
523+
var name string
524+
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
525+
name = addrSet
526+
} else {
527+
name = fmt.Sprintf("%s:%s", resource.remote, addrSet)
528+
}
529+
530+
results = append(results, name)
531+
}
532+
533+
// Also suggest remotes if no ":" in toComplete.
534+
if !strings.Contains(toComplete, ":") {
535+
remotes, directives := g.cmpRemotes(toComplete, false)
536+
results = append(results, remotes...)
537+
cmpDirectives |= directives
538+
}
539+
540+
return results, cmpDirectives
541+
}
542+
543+
func (g *cmdGlobal) cmpNetworkAddressSetConfigs(addressSetName string) ([]string, cobra.ShellCompDirective) {
544+
// Parse remote
545+
resources, err := g.parseServers(addressSetName)
546+
if err != nil || len(resources) == 0 {
547+
return nil, cobra.ShellCompDirectiveError
548+
}
549+
550+
resource := resources[0]
551+
client := resource.server
552+
553+
// Get the network address set.
554+
addrSet, _, err := client.GetNetworkAddressSet(resource.name)
555+
if err != nil {
556+
return nil, cobra.ShellCompDirectiveError
557+
}
558+
559+
var results []string
560+
for k := range addrSet.Config {
561+
results = append(results, k)
562+
}
563+
564+
return results, cobra.ShellCompDirectiveNoFileComp
565+
}
566+
504567
func (g *cmdGlobal) cmpNetworkForwardConfigs(networkName string, listenAddress string) ([]string, cobra.ShellCompDirective) {
505568
// Parse remote
506569
resources, err := g.parseServers(networkName)

cmd/incus/network.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ func (c *cmdNetwork) Command() *cobra.Command {
106106
networkACLCmd := cmdNetworkACL{global: c.global}
107107
cmd.AddCommand(networkACLCmd.Command())
108108

109+
// Address set
110+
networkAddressSetCmd := cmdNetworkAddressSet{global: c.global}
111+
cmd.AddCommand(networkAddressSetCmd.Command())
112+
109113
// Forward
110114
networkForwardCmd := cmdNetworkForward{global: c.global}
111115
cmd.AddCommand(networkForwardCmd.Command())

0 commit comments

Comments
 (0)