Skip to content

Commit

Permalink
Merge pull request #838 from sona78/main
Browse files Browse the repository at this point in the history
Add support for all-projects for networks, network-acls, and storage buckets
  • Loading branch information
stgraber committed May 6, 2024
2 parents 0c8a646 + 4725f77 commit 1733379
Show file tree
Hide file tree
Showing 35 changed files with 2,555 additions and 2,044 deletions.
15 changes: 15 additions & 0 deletions client/incus_network_acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ func (r *ProtocolIncus) GetNetworkACLs() ([]api.NetworkACL, error) {
return acls, nil
}

// GetNetworkACLsAllProjects returns all list of Network ACL structs across all projects.
func (r *ProtocolIncus) GetNetworkACLsAllProjects() ([]api.NetworkACL, error) {
if !r.HasExtension("network_acls_all_projects") {
return nil, fmt.Errorf(`The server is missing the required "network_acls_all_projects" API extension`)
}

acls := []api.NetworkACL{}
_, err := r.queryStruct("GET", "/network-acls?recursion=1&all-projects=true", nil, "", &acls)
if err != nil {
return nil, err
}

return acls, nil
}

// GetNetworkACL returns a Network ACL entry for the provided name.
func (r *ProtocolIncus) GetNetworkACL(name string) (*api.NetworkACL, string, error) {
if !r.HasExtension("network_acl") {
Expand Down
24 changes: 18 additions & 6 deletions client/incus_network_allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@ import (
"github.com/lxc/incus/v6/shared/api"
)

// GetNetworkAllocations returns a list of Network allocations tied to one or several projects (e.g, for IPAM information for example).
func (r *ProtocolIncus) GetNetworkAllocations(allProjects bool) ([]api.NetworkAllocations, error) {
// GetNetworkAllocations returns a list of Network allocations for a specific project.
func (r *ProtocolIncus) GetNetworkAllocations() ([]api.NetworkAllocations, error) {
err := r.CheckExtension("network_allocations")
if err != nil {
return nil, err
}

// Fetch the raw value.
netAllocations := []api.NetworkAllocations{}
uri := "/network-allocations"
if allProjects {
uri += "?all-projects=true"
_, err = r.queryStruct("GET", "/network-allocations", nil, "", &netAllocations)
if err != nil {
return nil, err
}

return netAllocations, nil
}

// GetNetworkAllocationsAllProjects returns a list of Network allocations across all projects.
func (r *ProtocolIncus) GetNetworkAllocationsAllProjects() ([]api.NetworkAllocations, error) {
err := r.CheckExtension("network_allocations")
if err != nil {
return nil, err
}

// Fetch the raw value.
_, err = r.queryStruct("GET", uri, nil, "", &netAllocations)
netAllocations := []api.NetworkAllocations{}
_, err = r.queryStruct("GET", "/network-allocations?all-projects=true", nil, "", &netAllocations)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions client/incus_network_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func (r *ProtocolIncus) GetNetworkZonesAllProjects() ([]api.NetworkZone, error)
}

zones := []api.NetworkZone{}
uri := "/network-zones?all-projects=true&recursion=1"
_, err = r.queryStruct("GET", uri, nil, "", &zones)
_, err = r.queryStruct("GET", "/network-zones?recursion=1&all-projects=true", nil, "", &zones)
if err != nil {
return nil, err
}
Expand Down
15 changes: 15 additions & 0 deletions client/incus_networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ func (r *ProtocolIncus) GetNetworks() ([]api.Network, error) {
return networks, nil
}

// GetNetworksAllProjects gets all networks across all projects.
func (r *ProtocolIncus) GetNetworksAllProjects() ([]api.Network, error) {
if !r.HasExtension("networks_all_projects") {
return nil, fmt.Errorf(`The server is missing the required "networks_all_projects" API extension`)
}

networks := []api.Network{}
_, err := r.queryStruct("GET", "/networks?recursion=1&all-projects=true", nil, "", &networks)
if err != nil {
return nil, err
}

return networks, nil
}

// GetNetwork returns a Network entry for the provided name.
func (r *ProtocolIncus) GetNetwork(name string) (*api.Network, string, error) {
if !r.HasExtension("network") {
Expand Down
3 changes: 1 addition & 2 deletions client/incus_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func (r *ProtocolIncus) GetProfilesAllProjects() ([]api.Profile, error) {
}

profiles := []api.Profile{}
uri := "/profiles?all-projects=true&recursion=1"
_, err = r.queryStruct("GET", uri, nil, "", &profiles)
_, err = r.queryStruct("GET", "/profiles?recursion=1&all-projects=true", nil, "", &profiles)
if err != nil {
return nil, err
}
Expand Down
18 changes: 18 additions & 0 deletions client/incus_storage_buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ func (r *ProtocolIncus) GetStoragePoolBuckets(poolName string) ([]api.StorageBuc
return buckets, nil
}

// GetStoragePoolBucketsAllProjects gets all storage pool buckets across all projects.
func (r *ProtocolIncus) GetStoragePoolBucketsAllProjects(poolName string) ([]api.StorageBucket, error) {
err := r.CheckExtension("storage_buckets_all_projects")
if err != nil {
return nil, fmt.Errorf(`The server is missing the required "storage_buckets_all_projects" API extension`)
}

buckets := []api.StorageBucket{}

u := api.NewURL().Path("storage-pools", poolName, "buckets").WithQuery("recursion", "1").WithQuery("all-projects", "true")
_, err = r.queryStruct("GET", u.String(), nil, "", &buckets)
if err != nil {
return nil, err
}

return buckets, nil
}

// GetStoragePoolBucket returns a storage bucket entry for the provided pool and bucket name.
func (r *ProtocolIncus) GetStoragePoolBucket(poolName string, bucketName string) (*api.StorageBucket, string, error) {
err := r.CheckExtension("storage_buckets")
Expand Down
6 changes: 5 additions & 1 deletion client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ type InstanceServer interface {
// Network functions ("network" API extension)
GetNetworkNames() (names []string, err error)
GetNetworks() (networks []api.Network, err error)
GetNetworksAllProjects() (networks []api.Network, err error)
GetNetwork(name string) (network *api.Network, ETag string, err error)
GetNetworkLeases(name string) (leases []api.NetworkLease, err error)
GetNetworkState(name string) (state *api.NetworkState, err error)
Expand Down Expand Up @@ -224,6 +225,7 @@ type InstanceServer interface {
// Network ACL functions ("network_acl" API extension)
GetNetworkACLNames() (names []string, err error)
GetNetworkACLs() (acls []api.NetworkACL, err error)
GetNetworkACLsAllProjects() (acls []api.NetworkACL, err error)
GetNetworkACL(name string) (acl *api.NetworkACL, ETag string, err error)
GetNetworkACLLogfile(name string) (log io.ReadCloser, err error)
CreateNetworkACL(acl api.NetworkACLsPost) (err error)
Expand All @@ -232,7 +234,8 @@ type InstanceServer interface {
DeleteNetworkACL(name string) (err error)

// Network allocations functions ("network_allocations" API extension)
GetNetworkAllocations(allProjects bool) (allocations []api.NetworkAllocations, err error)
GetNetworkAllocations() (allocations []api.NetworkAllocations, err error)
GetNetworkAllocationsAllProjects() (allocations []api.NetworkAllocations, err error)

// Network zone functions ("network_dns" API extension)
GetNetworkZonesAllProjects() (zones []api.NetworkZone, err error)
Expand Down Expand Up @@ -300,6 +303,7 @@ type InstanceServer interface {

// Storage bucket functions ("storage_buckets" API extension)
GetStoragePoolBucketNames(poolName string) ([]string, error)
GetStoragePoolBucketsAllProjects(poolName string) ([]api.StorageBucket, error)
GetStoragePoolBuckets(poolName string) ([]api.StorageBucket, error)
GetStoragePoolBucket(poolName string, bucketName string) (bucket *api.StorageBucket, ETag string, err error)
CreateStoragePoolBucket(poolName string, bucket api.StorageBucketsPost) (*api.StorageBucketKey, error)
Expand Down
26 changes: 22 additions & 4 deletions cmd/incus/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,8 @@ type cmdNetworkList struct {
global *cmdGlobal
network *cmdNetwork

flagFormat string
flagFormat string
flagAllProjects bool
}

func (c *cmdNetworkList) Command() *cobra.Command {
Expand All @@ -1013,6 +1014,7 @@ func (c *cmdNetworkList) Command() *cobra.Command {

cmd.RunE = c.Run
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().BoolVar(&c.flagAllProjects, "all-projects", false, i18n.G("List networks in all projects"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
Expand Down Expand Up @@ -1050,9 +1052,17 @@ func (c *cmdNetworkList) Run(cmd *cobra.Command, args []string) error {
return fmt.Errorf(i18n.G("Filtering isn't supported yet"))
}

networks, err := resource.server.GetNetworks()
if err != nil {
return err
var networks []api.Network
if c.flagAllProjects {
networks, err = resource.server.GetNetworksAllProjects()
if err != nil {
return err
}
} else {
networks, err = resource.server.GetNetworks()
if err != nil {
return err
}
}

data := [][]string{}
Expand All @@ -1078,6 +1088,10 @@ func (c *cmdNetworkList) Run(cmd *cobra.Command, args []string) error {
strings.ToUpper(network.Status),
}

if c.flagAllProjects {
details = append([]string{network.Project}, details...)
}

data = append(data, details)
}

Expand All @@ -1094,6 +1108,10 @@ func (c *cmdNetworkList) Run(cmd *cobra.Command, args []string) error {
i18n.G("STATE"),
}

if c.flagAllProjects {
header = append([]string{i18n.G("PROJECT")}, header...)
}

return cli.RenderTable(c.flagFormat, header, data, networks)
}

Expand Down
26 changes: 22 additions & 4 deletions cmd/incus/network_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ type cmdNetworkACLList struct {
global *cmdGlobal
networkACL *cmdNetworkACL

flagFormat string
flagFormat string
flagAllProjects bool
}

func (c *cmdNetworkACLList) Command() *cobra.Command {
Expand All @@ -94,6 +95,7 @@ func (c *cmdNetworkACLList) Command() *cobra.Command {

cmd.RunE = c.Run
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().BoolVar(&c.flagAllProjects, "all-projects", false, i18n.G("List network ACLs across all projects"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
Expand Down Expand Up @@ -131,9 +133,17 @@ func (c *cmdNetworkACLList) Run(cmd *cobra.Command, args []string) error {
return fmt.Errorf(i18n.G("Filtering isn't supported yet"))
}

acls, err := resource.server.GetNetworkACLs()
if err != nil {
return err
var acls []api.NetworkACL
if c.flagAllProjects {
acls, err = resource.server.GetNetworkACLsAllProjects()
if err != nil {
return err
}
} else {
acls, err = resource.server.GetNetworkACLs()
if err != nil {
return err
}
}

data := [][]string{}
Expand All @@ -145,6 +155,10 @@ func (c *cmdNetworkACLList) Run(cmd *cobra.Command, args []string) error {
strUsedBy,
}

if c.flagAllProjects {
details = append([]string{acl.Project}, details...)
}

data = append(data, details)
}

Expand All @@ -156,6 +170,10 @@ func (c *cmdNetworkACLList) Run(cmd *cobra.Command, args []string) error {
i18n.G("USED BY"),
}

if c.flagAllProjects {
header = append([]string{i18n.G("PROJECT")}, header...)
}

return cli.RenderTable(c.flagFormat, header, data, acls)
}

Expand Down
12 changes: 11 additions & 1 deletion cmd/incus/network_allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ func (c *cmdNetworkListAllocations) Run(cmd *cobra.Command, args []string) error

resource := resources[0]
server := resource.server.UseProject(c.flagProject)
addresses, err := server.GetNetworkAllocations(c.flagAllProjects)

if c.flagAllProjects {
addresses, err := server.GetNetworkAllocationsAllProjects()
if err != nil {
return err
}

return c.pretty(addresses)
}

addresses, err := server.GetNetworkAllocations()
if err != nil {
return err
}
Expand Down
27 changes: 23 additions & 4 deletions cmd/incus/storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,9 @@ func (c *cmdStorageBucketGet) Run(cmd *cobra.Command, args []string) error {
type cmdStorageBucketList struct {
global *cmdGlobal
storageBucket *cmdStorageBucket
flagFormat string

flagFormat string
flagAllProjects bool
}

func (c *cmdStorageBucketList) Command() *cobra.Command {
Expand All @@ -470,6 +472,7 @@ func (c *cmdStorageBucketList) Command() *cobra.Command {

cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(`List storage buckets`))
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().BoolVar(&c.flagAllProjects, "all-projects", false, i18n.G("Display storage pool buckets from all projects"))

cmd.RunE = c.Run

Expand Down Expand Up @@ -497,9 +500,17 @@ func (c *cmdStorageBucketList) Run(cmd *cobra.Command, args []string) error {

client := resource.server

buckets, err := client.GetStoragePoolBuckets(resource.name)
if err != nil {
return err
var buckets []api.StorageBucket
if c.flagAllProjects {
buckets, err = client.GetStoragePoolBucketsAllProjects(resource.name)
if err != nil {
return err
}
} else {
buckets, err = client.GetStoragePoolBuckets(resource.name)
if err != nil {
return err
}
}

clustered := resource.server.IsClustered()
Expand All @@ -515,6 +526,10 @@ func (c *cmdStorageBucketList) Run(cmd *cobra.Command, args []string) error {
details = append(details, bucket.Location)
}

if c.flagAllProjects {
details = append([]string{bucket.Project}, details...)
}

data = append(data, details)
}

Expand All @@ -529,6 +544,10 @@ func (c *cmdStorageBucketList) Run(cmd *cobra.Command, args []string) error {
header = append(header, i18n.G("LOCATION"))
}

if c.flagAllProjects {
header = append([]string{i18n.G("PROJECT")}, header...)
}

return cli.RenderTable(c.flagFormat, header, data, buckets)
}

Expand Down
Loading

0 comments on commit 1733379

Please sign in to comment.