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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
//go:build linux && cgo && !agent
// Code generated by generate-database from the incus project - DO NOT EDIT.
package cluster
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
)
var networkACLObjects = RegisterStmt(`
SELECT networks_acls.id, networks_acls.project_id, projects.name AS project, networks_acls.name, networks_acls.description, networks_acls.ingress, networks_acls.egress
FROM networks_acls
JOIN projects ON networks_acls.project_id = projects.id
ORDER BY networks_acls.id
`)
var networkACLObjectsByID = RegisterStmt(`
SELECT networks_acls.id, networks_acls.project_id, projects.name AS project, networks_acls.name, networks_acls.description, networks_acls.ingress, networks_acls.egress
FROM networks_acls
JOIN projects ON networks_acls.project_id = projects.id
WHERE ( networks_acls.id = ? )
ORDER BY networks_acls.id
`)
var networkACLObjectsByName = RegisterStmt(`
SELECT networks_acls.id, networks_acls.project_id, projects.name AS project, networks_acls.name, networks_acls.description, networks_acls.ingress, networks_acls.egress
FROM networks_acls
JOIN projects ON networks_acls.project_id = projects.id
WHERE ( networks_acls.name = ? )
ORDER BY networks_acls.id
`)
var networkACLObjectsByProject = RegisterStmt(`
SELECT networks_acls.id, networks_acls.project_id, projects.name AS project, networks_acls.name, networks_acls.description, networks_acls.ingress, networks_acls.egress
FROM networks_acls
JOIN projects ON networks_acls.project_id = projects.id
WHERE ( project = ? )
ORDER BY networks_acls.id
`)
var networkACLObjectsByProjectAndName = RegisterStmt(`
SELECT networks_acls.id, networks_acls.project_id, projects.name AS project, networks_acls.name, networks_acls.description, networks_acls.ingress, networks_acls.egress
FROM networks_acls
JOIN projects ON networks_acls.project_id = projects.id
WHERE ( project = ? AND networks_acls.name = ? )
ORDER BY networks_acls.id
`)
var networkACLID = RegisterStmt(`
SELECT networks_acls.id FROM networks_acls
JOIN projects ON networks_acls.project_id = projects.id
WHERE projects.name = ? AND networks_acls.name = ?
`)
var networkACLCreate = RegisterStmt(`
INSERT INTO networks_acls (project_id, name, description, ingress, egress)
VALUES ((SELECT projects.id FROM projects WHERE projects.name = ?), ?, ?, ?, ?)
`)
var networkACLRename = RegisterStmt(`
UPDATE networks_acls SET name = ? WHERE project_id = (SELECT projects.id FROM projects WHERE projects.name = ?) AND name = ?
`)
var networkACLUpdate = RegisterStmt(`
UPDATE networks_acls
SET project_id = (SELECT projects.id FROM projects WHERE projects.name = ?), name = ?, description = ?, ingress = ?, egress = ?
WHERE id = ?
`)
var networkACLDeleteByID = RegisterStmt(`
DELETE FROM networks_acls WHERE id = ?
`)
// networkACLColumns returns a string of column names to be used with a SELECT statement for the entity.
// Use this function when building statements to retrieve database entries matching the NetworkACL entity.
func networkACLColumns() string {
return "networks_acls.id, networks_acls.project_id, projects.name AS project, networks_acls.name, networks_acls.description, networks_acls.ingress, networks_acls.egress"
}
// getNetworkACLs can be used to run handwritten sql.Stmts to return a slice of objects.
func getNetworkACLs(ctx context.Context, stmt *sql.Stmt, args ...any) ([]NetworkACL, error) {
objects := make([]NetworkACL, 0)
dest := func(scan func(dest ...any) error) error {
n := NetworkACL{}
var ingressStr string
var egressStr string
err := scan(&n.ID, &n.ProjectID, &n.Project, &n.Name, &n.Description, &ingressStr, &egressStr)
if err != nil {
return err
}
err = unmarshalJSON(ingressStr, &n.Ingress)
if err != nil {
return err
}
err = unmarshalJSON(egressStr, &n.Egress)
if err != nil {
return err
}
objects = append(objects, n)
return nil
}
err := selectObjects(ctx, stmt, dest, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"networks_acls\" table: %w", err)
}
return objects, nil
}
// getNetworkACLsRaw can be used to run handwritten query strings to return a slice of objects.
func getNetworkACLsRaw(ctx context.Context, db dbtx, sql string, args ...any) ([]NetworkACL, error) {
objects := make([]NetworkACL, 0)
dest := func(scan func(dest ...any) error) error {
n := NetworkACL{}
var ingressStr string
var egressStr string
err := scan(&n.ID, &n.ProjectID, &n.Project, &n.Name, &n.Description, &ingressStr, &egressStr)
if err != nil {
return err
}
err = unmarshalJSON(ingressStr, &n.Ingress)
if err != nil {
return err
}
err = unmarshalJSON(egressStr, &n.Egress)
if err != nil {
return err
}
objects = append(objects, n)
return nil
}
err := scan(ctx, db, sql, dest, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"networks_acls\" table: %w", err)
}
return objects, nil
}
// GetNetworkACLs returns all available NetworkACLs.
// generator: NetworkACL GetMany
func GetNetworkACLs(ctx context.Context, db dbtx, filters ...NetworkACLFilter) (_ []NetworkACL, _err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
var err error
// Result slice.
objects := make([]NetworkACL, 0)
// Pick the prepared statement and arguments to use based on active criteria.
var sqlStmt *sql.Stmt
args := []any{}
queryParts := [2]string{}
if len(filters) == 0 {
sqlStmt, err = Stmt(db, networkACLObjects)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjects\" prepared statement: %w", err)
}
}
for i, filter := range filters {
if filter.Project != nil && filter.Name != nil && filter.ID == nil {
args = append(args, []any{filter.Project, filter.Name}...)
if len(filters) == 1 {
sqlStmt, err = Stmt(db, networkACLObjectsByProjectAndName)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjectsByProjectAndName\" prepared statement: %w", err)
}
break
}
query, err := StmtString(networkACLObjectsByProjectAndName)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjects\" prepared statement: %w", err)
}
parts := strings.SplitN(query, "ORDER BY", 2)
if i == 0 {
copy(queryParts[:], parts)
continue
}
_, where, _ := strings.Cut(parts[0], "WHERE")
queryParts[0] += "OR" + where
} else if filter.Project != nil && filter.ID == nil && filter.Name == nil {
args = append(args, []any{filter.Project}...)
if len(filters) == 1 {
sqlStmt, err = Stmt(db, networkACLObjectsByProject)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjectsByProject\" prepared statement: %w", err)
}
break
}
query, err := StmtString(networkACLObjectsByProject)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjects\" prepared statement: %w", err)
}
parts := strings.SplitN(query, "ORDER BY", 2)
if i == 0 {
copy(queryParts[:], parts)
continue
}
_, where, _ := strings.Cut(parts[0], "WHERE")
queryParts[0] += "OR" + where
} else if filter.Name != nil && filter.ID == nil && filter.Project == nil {
args = append(args, []any{filter.Name}...)
if len(filters) == 1 {
sqlStmt, err = Stmt(db, networkACLObjectsByName)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjectsByName\" prepared statement: %w", err)
}
break
}
query, err := StmtString(networkACLObjectsByName)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjects\" prepared statement: %w", err)
}
parts := strings.SplitN(query, "ORDER BY", 2)
if i == 0 {
copy(queryParts[:], parts)
continue
}
_, where, _ := strings.Cut(parts[0], "WHERE")
queryParts[0] += "OR" + where
} else if filter.ID != nil && filter.Name == nil && filter.Project == nil {
args = append(args, []any{filter.ID}...)
if len(filters) == 1 {
sqlStmt, err = Stmt(db, networkACLObjectsByID)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjectsByID\" prepared statement: %w", err)
}
break
}
query, err := StmtString(networkACLObjectsByID)
if err != nil {
return nil, fmt.Errorf("Failed to get \"networkACLObjects\" prepared statement: %w", err)
}
parts := strings.SplitN(query, "ORDER BY", 2)
if i == 0 {
copy(queryParts[:], parts)
continue
}
_, where, _ := strings.Cut(parts[0], "WHERE")
queryParts[0] += "OR" + where
} else if filter.ID == nil && filter.Name == nil && filter.Project == nil {
return nil, fmt.Errorf("Cannot filter on empty NetworkACLFilter")
} else {
return nil, errors.New("No statement exists for the given Filter")
}
}
// Select.
if sqlStmt != nil {
objects, err = getNetworkACLs(ctx, sqlStmt, args...)
} else {
queryStr := strings.Join(queryParts[:], "ORDER BY")
objects, err = getNetworkACLsRaw(ctx, db, queryStr, args...)
}
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"networks_acls\" table: %w", err)
}
return objects, nil
}
// GetNetworkACLConfig returns all available NetworkACL Config
// generator: NetworkACL GetMany
func GetNetworkACLConfig(ctx context.Context, db tx, networkACLID int, filters ...ConfigFilter) (_ map[string]string, _err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
networkACLConfig, err := GetConfig(ctx, db, "networks_acls", "network_acl", filters...)
if err != nil {
return nil, err
}
config, ok := networkACLConfig[networkACLID]
if !ok {
config = map[string]string{}
}
return config, nil
}
// GetNetworkACL returns the NetworkACL with the given key.
// generator: NetworkACL GetOne
func GetNetworkACL(ctx context.Context, db dbtx, project string, name string) (_ *NetworkACL, _err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
filter := NetworkACLFilter{}
filter.Project = &project
filter.Name = &name
objects, err := GetNetworkACLs(ctx, db, filter)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"networks_acls\" table: %w", err)
}
switch len(objects) {
case 0:
return nil, ErrNotFound
case 1:
return &objects[0], nil
default:
return nil, fmt.Errorf("More than one \"networks_acls\" entry matches")
}
}
// NetworkACLExists checks if a NetworkACL with the given key exists.
// generator: NetworkACL Exists
func NetworkACLExists(ctx context.Context, db dbtx, project string, name string) (_ bool, _err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
stmt, err := Stmt(db, networkACLID)
if err != nil {
return false, fmt.Errorf("Failed to get \"networkACLID\" prepared statement: %w", err)
}
row := stmt.QueryRowContext(ctx, project, name)
var id int64
err = row.Scan(&id)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("Failed to get \"networks_acls\" ID: %w", err)
}
return true, nil
}
// CreateNetworkACL adds a new NetworkACL to the database.
// generator: NetworkACL Create
func CreateNetworkACL(ctx context.Context, db dbtx, object NetworkACL) (_ int64, _err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
args := make([]any, 5)
// Populate the statement arguments.
args[0] = object.Project
args[1] = object.Name
args[2] = object.Description
marshaledIngress, err := marshalJSON(object.Ingress)
if err != nil {
return -1, err
}
args[3] = marshaledIngress
marshaledEgress, err := marshalJSON(object.Egress)
if err != nil {
return -1, err
}
args[4] = marshaledEgress
// Prepared statement to use.
stmt, err := Stmt(db, networkACLCreate)
if err != nil {
return -1, fmt.Errorf("Failed to get \"networkACLCreate\" prepared statement: %w", err)
}
// Execute the statement.
result, err := stmt.Exec(args...)
if err != nil && strings.HasPrefix(err.Error(), "UNIQUE constraint failed:") {
return -1, ErrConflict
}
if err != nil {
return -1, fmt.Errorf("Failed to create \"networks_acls\" entry: %w", err)
}
id, err := result.LastInsertId()
if err != nil {
return -1, fmt.Errorf("Failed to fetch \"networks_acls\" entry ID: %w", err)
}
return id, nil
}
// CreateNetworkACLConfig adds new NetworkACL Config to the database.
// generator: NetworkACL Create
func CreateNetworkACLConfig(ctx context.Context, db dbtx, networkACLID int64, config map[string]string) (_err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
referenceID := int(networkACLID)
for key, value := range config {
insert := Config{
ReferenceID: referenceID,
Key: key,
Value: value,
}
err := CreateConfig(ctx, db, "networks_acls", "network_acl", insert)
if err != nil {
return fmt.Errorf("Insert Config failed for NetworkACL: %w", err)
}
}
return nil
}
// GetNetworkACLID return the ID of the NetworkACL with the given key.
// generator: NetworkACL ID
func GetNetworkACLID(ctx context.Context, db tx, project string, name string) (_ int64, _err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
stmt, err := Stmt(db, networkACLID)
if err != nil {
return -1, fmt.Errorf("Failed to get \"networkACLID\" prepared statement: %w", err)
}
row := stmt.QueryRowContext(ctx, project, name)
var id int64
err = row.Scan(&id)
if errors.Is(err, sql.ErrNoRows) {
return -1, ErrNotFound
}
if err != nil {
return -1, fmt.Errorf("Failed to get \"networks_acls\" ID: %w", err)
}
return id, nil
}
// RenameNetworkACL renames the NetworkACL matching the given key parameters.
// generator: NetworkACL Rename
func RenameNetworkACL(ctx context.Context, db dbtx, project string, name string, to string) (_err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
stmt, err := Stmt(db, networkACLRename)
if err != nil {
return fmt.Errorf("Failed to get \"networkACLRename\" prepared statement: %w", err)
}
result, err := stmt.Exec(to, project, name)
if err != nil {
return fmt.Errorf("Rename NetworkACL failed: %w", err)
}
n, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("Fetch affected rows failed: %w", err)
}
if n != 1 {
return fmt.Errorf("Query affected %d rows instead of 1", n)
}
return nil
}
// UpdateNetworkACL updates the NetworkACL matching the given key parameters.
// generator: NetworkACL Update
func UpdateNetworkACL(ctx context.Context, db tx, project string, name string, object NetworkACL) (_err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
id, err := GetNetworkACLID(ctx, db, project, name)
if err != nil {
return err
}
stmt, err := Stmt(db, networkACLUpdate)
if err != nil {
return fmt.Errorf("Failed to get \"networkACLUpdate\" prepared statement: %w", err)
}
marshaledIngress, err := marshalJSON(object.Ingress)
if err != nil {
return err
}
marshaledEgress, err := marshalJSON(object.Egress)
if err != nil {
return err
}
result, err := stmt.Exec(object.Project, object.Name, object.Description, marshaledIngress, marshaledEgress, id)
if err != nil {
return fmt.Errorf("Update \"networks_acls\" entry failed: %w", err)
}
n, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("Fetch affected rows: %w", err)
}
if n != 1 {
return fmt.Errorf("Query updated %d rows instead of 1", n)
}
return nil
}
// UpdateNetworkACLConfig updates the NetworkACL Config matching the given key parameters.
// generator: NetworkACL Update
func UpdateNetworkACLConfig(ctx context.Context, db tx, networkACLID int64, config map[string]string) (_err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
err := UpdateConfig(ctx, db, "networks_acls", "network_acl", int(networkACLID), config)
if err != nil {
return fmt.Errorf("Replace Config for NetworkACL failed: %w", err)
}
return nil
}
// DeleteNetworkACL deletes the NetworkACL matching the given key parameters.
// generator: NetworkACL DeleteOne-by-ID
func DeleteNetworkACL(ctx context.Context, db dbtx, id int) (_err error) {
defer func() {
_err = mapErr(_err, "NetworkACL")
}()
stmt, err := Stmt(db, networkACLDeleteByID)
if err != nil {
return fmt.Errorf("Failed to get \"networkACLDeleteByID\" prepared statement: %w", err)
}
result, err := stmt.Exec(id)
if err != nil {
return fmt.Errorf("Delete \"networks_acls\": %w", err)
}
n, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("Fetch affected rows: %w", err)
}
if n == 0 {
return ErrNotFound
} else if n > 1 {
return fmt.Errorf("Query deleted %d NetworkACL rows instead of 1", n)
}
return nil
}
|