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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
package incus
import (
"errors"
"fmt"
"net/url"
"github.com/lxc/incus/v6/shared/api"
)
// Project handling functions
// GetProjectNames returns a list of available project names.
func (r *ProtocolIncus) GetProjectNames() ([]string, error) {
if !r.HasExtension("projects") {
return nil, errors.New("The server is missing the required \"projects\" API extension")
}
// Fetch the raw URL values.
urls := []string{}
baseURL := "/projects"
_, err := r.queryStruct("GET", baseURL, nil, "", &urls)
if err != nil {
return nil, err
}
// Parse it.
return urlsToResourceNames(baseURL, urls...)
}
// GetProjects returns a list of available Project structs.
func (r *ProtocolIncus) GetProjects() ([]api.Project, error) {
if !r.HasExtension("projects") {
return nil, errors.New("The server is missing the required \"projects\" API extension")
}
projects := []api.Project{}
// Fetch the raw value
_, err := r.queryStruct("GET", "/projects?recursion=1", nil, "", &projects)
if err != nil {
return nil, err
}
return projects, nil
}
// GetProjectsWithFilter returns a filtered list of projects as Project structs.
func (r *ProtocolIncus) GetProjectsWithFilter(filters []string) ([]api.Project, error) {
if !r.HasExtension("projects") {
return nil, errors.New("The server is missing the required \"projects\" API extension")
}
projects := []api.Project{}
v := url.Values{}
v.Set("recursion", "1")
v.Set("filter", parseFilters(filters))
_, err := r.queryStruct("GET", fmt.Sprintf("/projects?%s", v.Encode()), nil, "", &projects)
if err != nil {
return nil, err
}
return projects, nil
}
// GetProject returns a Project entry for the provided name.
func (r *ProtocolIncus) GetProject(name string) (*api.Project, string, error) {
if !r.HasExtension("projects") {
return nil, "", errors.New("The server is missing the required \"projects\" API extension")
}
project := api.Project{}
// Fetch the raw value
etag, err := r.queryStruct("GET", fmt.Sprintf("/projects/%s", url.PathEscape(name)), nil, "", &project)
if err != nil {
return nil, "", err
}
return &project, etag, nil
}
// GetProjectState returns a Project state for the provided name.
func (r *ProtocolIncus) GetProjectState(name string) (*api.ProjectState, error) {
if !r.HasExtension("project_usage") {
return nil, errors.New("The server is missing the required \"project_usage\" API extension")
}
projectState := api.ProjectState{}
// Fetch the raw value
_, err := r.queryStruct("GET", fmt.Sprintf("/projects/%s/state", url.PathEscape(name)), nil, "", &projectState)
if err != nil {
return nil, err
}
return &projectState, nil
}
// GetProjectAccess returns an Access entry for the specified project.
func (r *ProtocolIncus) GetProjectAccess(name string) (api.Access, error) {
access := api.Access{}
if !r.HasExtension("project_access") {
return nil, errors.New("The server is missing the required \"project_access\" API extension")
}
// Fetch the raw value
_, err := r.queryStruct("GET", fmt.Sprintf("/projects/%s/access", url.PathEscape(name)), nil, "", &access)
if err != nil {
return nil, err
}
return access, nil
}
// CreateProject defines a new project.
func (r *ProtocolIncus) CreateProject(project api.ProjectsPost) error {
if !r.HasExtension("projects") {
return errors.New("The server is missing the required \"projects\" API extension")
}
// Send the request
_, _, err := r.query("POST", "/projects", project, "")
if err != nil {
return err
}
return nil
}
// UpdateProject updates the project to match the provided Project struct.
func (r *ProtocolIncus) UpdateProject(name string, project api.ProjectPut, ETag string) error {
if !r.HasExtension("projects") {
return errors.New("The server is missing the required \"projects\" API extension")
}
// Send the request
_, _, err := r.query("PUT", fmt.Sprintf("/projects/%s", url.PathEscape(name)), project, ETag)
if err != nil {
return err
}
return nil
}
// RenameProject renames an existing project entry.
func (r *ProtocolIncus) RenameProject(name string, project api.ProjectPost) (Operation, error) {
if !r.HasExtension("projects") {
return nil, errors.New("The server is missing the required \"projects\" API extension")
}
// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/projects/%s", url.PathEscape(name)), project, "")
if err != nil {
return nil, err
}
return op, nil
}
// DeleteProject deletes a project.
func (r *ProtocolIncus) DeleteProject(name string) error {
if !r.HasExtension("projects") {
return errors.New("The server is missing the required \"projects\" API extension")
}
// Send the request
_, _, err := r.query("DELETE", fmt.Sprintf("/projects/%s", url.PathEscape(name)), nil, "")
if err != nil {
return err
}
return nil
}
// DeleteProjectForce deletes a project and everything inside of it.
func (r *ProtocolIncus) DeleteProjectForce(name string) error {
if !r.HasExtension("projects_force_delete") {
return errors.New("The server is missing the required \"projects_force_delete\" API extension")
}
// Send the request
_, _, err := r.query("DELETE", fmt.Sprintf("/projects/%s?force=1", url.PathEscape(name)), nil, "")
if err != nil {
return err
}
return nil
}
|