Debian Codegraph @

nft/config/config.go

raw symbols SCIP
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 "encoding/json"
24
25 "github.com/networkplumbing/go-nft/nft/schema"
26 )
27
28 type Config struct {
29 schema.Root
30 }
31
32 // New returns a new nftables config structure.
33 func New() *Config {
34 c := &Config{}
35 c.Nftables = []schema.Nftable{}
36 return c
37 }
38
39 // ToJSON returns the JSON encoding of the nftables config.
40 func (c *Config) ToJSON() ([]byte, error) {
41 return json.Marshal(*c)
42 }
43
44 // FromJSON decodes the provided JSON-encoded data and populates the nftables config.
45 func (c *Config) FromJSON(data []byte) error {
46 if err := json.Unmarshal(data, c); err != nil {
47 return err
48 }
49 return nil
50 }
51
52 // FlushRuleset adds a command to the nftables config that erases all the configuration when applied.
53 // It is commonly used as the first config instruction, followed by a declarative configuration.
54 // When used, any previous configuration is flushed away before adding the new one.
55 // Calling FlushRuleset updates the configuration and will take effect only
56 // when applied on the system.
57 func (c *Config) FlushRuleset() {
58 c.Nftables = append(c.Nftables, schema.Nftable{Flush: &schema.Objects{Ruleset: true}})
59 }