|
| 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 | +} |
0 commit comments