File: incus_network_address_sets.go

package info (click to toggle)
incus 6.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,788 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (136 lines) | stat: -rw-r--r-- 4,225 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package incus

import (
	"errors"
	"fmt"
	"net/url"

	"github.com/lxc/incus/v6/shared/api"
)

// GetNetworkAddressSetNames returns a list of network address set names.
func (r *ProtocolIncus) GetNetworkAddressSetNames() ([]string, error) {
	if !r.HasExtension("network_address_set") {
		return nil, errors.New(`The server is missing the required "network_address_set" API extension`)
	}

	// Fetch the raw URL values.
	urls := []string{}
	baseURL := "/network-address-sets"
	_, err := r.queryStruct("GET", baseURL, nil, "", &urls)
	if err != nil {
		return nil, err
	}

	// Parse it.
	return urlsToResourceNames(baseURL, urls...)
}

// GetNetworkAddressSets returns a list of network address set structs.
func (r *ProtocolIncus) GetNetworkAddressSets() ([]api.NetworkAddressSet, error) {
	if !r.HasExtension("network_address_set") {
		return nil, errors.New(`The server is missing the required "network_address_set" API extension`)
	}

	addressSets := []api.NetworkAddressSet{}

	// Fetch the raw value.
	_, err := r.queryStruct("GET", "/network-address-sets?recursion=1", nil, "", &addressSets)
	if err != nil {
		return nil, err
	}

	return addressSets, nil
}

// GetNetworkAddressSetsAllProjects returns a list of network address set structs across all projects.
func (r *ProtocolIncus) GetNetworkAddressSetsAllProjects() ([]api.NetworkAddressSet, error) {
	if !r.HasExtension("network_address_set") {
		return nil, errors.New(`The server is missing the required "network_address_set" API extension`)
	}

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

	return addressSets, nil
}

// GetNetworkAddressSet returns a network address set entry for the provided name.
func (r *ProtocolIncus) GetNetworkAddressSet(name string) (*api.NetworkAddressSet, string, error) {
	if !r.HasExtension("network_address_set") {
		return nil, "", errors.New(`The server is missing the required "network_address_set" API extension`)
	}

	addrSet := api.NetworkAddressSet{}

	// Fetch the raw value.
	etag, err := r.queryStruct("GET", fmt.Sprintf("/network-address-sets/%s", url.PathEscape(name)), nil, "", &addrSet)
	if err != nil {
		return nil, "", err
	}

	return &addrSet, etag, nil
}

// CreateNetworkAddressSet defines a new network address set using the provided struct.
func (r *ProtocolIncus) CreateNetworkAddressSet(as api.NetworkAddressSetsPost) error {
	if !r.HasExtension("network_address_set") {
		return errors.New(`The server is missing the required "network_address_set" API extension`)
	}

	// Send the request.
	_, _, err := r.query("POST", "/network-address-sets", as, "")
	if err != nil {
		return err
	}

	return nil
}

// UpdateNetworkAddressSet updates the network address set to match the provided struct.
func (r *ProtocolIncus) UpdateNetworkAddressSet(name string, as api.NetworkAddressSetPut, ETag string) error {
	if !r.HasExtension("network_address_set") {
		return errors.New(`The server is missing the required "network_address_set" API extension`)
	}

	// Send the request.
	_, _, err := r.query("PUT", fmt.Sprintf("/network-address-sets/%s", url.PathEscape(name)), as, ETag)
	if err != nil {
		return err
	}

	return nil
}

// RenameNetworkAddressSet renames an existing network address set entry.
func (r *ProtocolIncus) RenameNetworkAddressSet(name string, as api.NetworkAddressSetPost) error {
	if !r.HasExtension("network_address_set") {
		return errors.New(`The server is missing the required "network_address_set" API extension`)
	}

	// Send the request.
	_, _, err := r.query("POST", fmt.Sprintf("/network-address-sets/%s", url.PathEscape(name)), as, "")
	if err != nil {
		return err
	}

	return nil
}

// DeleteNetworkAddressSet deletes an existing network address set.
func (r *ProtocolIncus) DeleteNetworkAddressSet(name string) error {
	if !r.HasExtension("network_address_set") {
		return errors.New(`The server is missing the required "network_address_set" API extension`)
	}

	// Send the request.
	_, _, err := r.query("DELETE", fmt.Sprintf("/network-address-sets/%s", url.PathEscape(name)), nil, "")
	if err != nil {
		return err
	}

	return nil
}