databend_common_ast/ast/statements/
network_policy.rs

1// Copyright 2021 Datafuse Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::Display;
16use std::fmt::Formatter;
17
18use derive_visitor::Drive;
19use derive_visitor::DriveMut;
20
21use crate::ast::quote::QuotedString;
22use crate::ast::CreateOption;
23
24#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
25pub struct CreateNetworkPolicyStmt {
26    pub create_option: CreateOption,
27    pub name: String,
28    pub allowed_ip_list: Vec<String>,
29    pub blocked_ip_list: Option<Vec<String>>,
30    pub comment: Option<String>,
31}
32
33impl Display for CreateNetworkPolicyStmt {
34    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
35        write!(f, "CREATE ")?;
36        if let CreateOption::CreateOrReplace = self.create_option {
37            write!(f, "OR REPLACE ")?;
38        }
39        write!(f, "NETWORK POLICY ")?;
40        if let CreateOption::CreateIfNotExists = self.create_option {
41            write!(f, "IF NOT EXISTS ")?;
42        }
43        write!(f, "{}", self.name)?;
44        write!(f, " ALLOWED_IP_LIST = (")?;
45        for (i, ip) in self.allowed_ip_list.iter().enumerate() {
46            if i > 0 {
47                write!(f, ",")?;
48            }
49            write!(f, "'{}'", ip)?;
50        }
51        write!(f, ")")?;
52        if let Some(blocked_ip_list) = &self.blocked_ip_list {
53            write!(f, " BLOCKED_IP_LIST = (")?;
54            for (i, ip) in blocked_ip_list.iter().enumerate() {
55                if i > 0 {
56                    write!(f, ",")?;
57                }
58                write!(f, "'{}'", ip)?;
59            }
60            write!(f, ")")?;
61        }
62        if let Some(comment) = &self.comment {
63            write!(f, " COMMENT = {}", QuotedString(comment, '\''))?;
64        }
65
66        Ok(())
67    }
68}
69
70#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
71pub struct AlterNetworkPolicyStmt {
72    pub if_exists: bool,
73    pub name: String,
74    pub allowed_ip_list: Option<Vec<String>>,
75    pub blocked_ip_list: Option<Vec<String>>,
76    pub comment: Option<String>,
77}
78
79impl Display for AlterNetworkPolicyStmt {
80    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
81        write!(f, "ALTER NETWORK POLICY ")?;
82        if self.if_exists {
83            write!(f, "IF EXISTS ")?;
84        }
85        write!(f, "{} SET ", self.name)?;
86
87        if let Some(allowed_ip_list) = &self.allowed_ip_list {
88            write!(f, " ALLOWED_IP_LIST = (")?;
89            for (i, ip) in allowed_ip_list.iter().enumerate() {
90                if i > 0 {
91                    write!(f, ",")?;
92                }
93                write!(f, "'{}'", ip)?;
94            }
95            write!(f, ")")?;
96        }
97        if let Some(blocked_ip_list) = &self.blocked_ip_list {
98            write!(f, " BLOCKED_IP_LIST = (")?;
99            for (i, ip) in blocked_ip_list.iter().enumerate() {
100                if i > 0 {
101                    write!(f, ",")?;
102                }
103                write!(f, "'{}'", ip)?;
104            }
105            write!(f, ")")?;
106        }
107        if let Some(comment) = &self.comment {
108            write!(f, " COMMENT = {}", QuotedString(comment, '\''))?;
109        }
110
111        Ok(())
112    }
113}
114
115#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
116pub struct DropNetworkPolicyStmt {
117    pub if_exists: bool,
118    pub name: String,
119}
120
121impl Display for DropNetworkPolicyStmt {
122    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
123        write!(f, "DROP NETWORK POLICY ")?;
124        if self.if_exists {
125            write!(f, "IF EXISTS ")?;
126        }
127        write!(f, "{}", self.name)?;
128
129        Ok(())
130    }
131}
132
133#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
134pub struct DescNetworkPolicyStmt {
135    pub name: String,
136}
137
138impl Display for DescNetworkPolicyStmt {
139    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
140        write!(f, "DESCRIBE NETWORK POLICY {}", self.name)?;
141
142        Ok(())
143    }
144}