| 1 | /* |
| 2 | * This file is part of the go-nft project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * Copyright 2021 Red Hat, Inc. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | package config |
| 21 | |
| 22 | import ( |
| 23 | "github.com/networkplumbing/go-nft/nft/schema" |
| 24 | ) |
| 25 | |
| 26 | // AddTable appends the given table to the nftable config. |
| 27 | // The table is added without an explicit action (`add`). |
| 28 | // Adding multiple times the same table has no effect when the config is applied. |
| 29 | func (c *Config) AddTable(table *schema.Table) { |
| 30 | nftable := schema.Nftable{Table: table} |
| 31 | c.Nftables = append(c.Nftables, nftable) |
| 32 | } |
| 33 | |
| 34 | // DeleteTable appends a given table to the nftable config |
| 35 | // with the `delete` action. |
| 36 | // Attempting to delete a non-existing table, results with a failure when the config is applied. |
| 37 | // All chains and rules under the table are removed as well (when applied). |
| 38 | func (c *Config) DeleteTable(table *schema.Table) { |
| 39 | nftable := schema.Nftable{Delete: &schema.Objects{Table: table}} |
| 40 | c.Nftables = append(c.Nftables, nftable) |
| 41 | } |
| 42 | |
| 43 | // FlushTable appends a given table to the nftable config |
| 44 | // with the `flush` action. |
| 45 | // All chains and rules under the table are removed (when applied). |
| 46 | // Attempting to flush a non-existing table, results with a failure when the config is applied. |
| 47 | func (c *Config) FlushTable(table *schema.Table) { |
| 48 | nftable := schema.Nftable{Flush: &schema.Objects{Table: table}} |
| 49 | c.Nftables = append(c.Nftables, nftable) |
| 50 | } |
| 51 | |
| 52 | // LookupTable searches the configuration for a matching table and returns it. |
| 53 | // Mutating the returned table will result in mutating the configuration. |
| 54 | func (c *Config) LookupTable(toFind *schema.Table) *schema.Table { |
| 55 | for _, nftable := range c.Nftables { |
| 56 | if t := nftable.Table; t != nil { |
| 57 | if t.Name == toFind.Name && t.Family == toFind.Family { |
| 58 | return t |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return nil |
| 63 | } |