@@ -224,3 +224,52 @@ func (c *Client) DeleteFirewallRule(id string, ruleID string) (*SimpleResponse,
224
224
225
225
return c .DecodeSimpleResponse (resp )
226
226
}
227
+
228
+
229
+
230
+ // Check if the firewall is using the default rules
231
+ func (c * Client ) IsUsingDefaultRules (firewallID string ) (bool , error ) {
232
+ // Define default firewall rules
233
+ var defaultRules = []FirewallRule {
234
+ {Protocol : "tcp" , Ports : "22" , Cidr : []string {"0.0.0.0/0" }, Direction : "ingress" , Action : "allow" },
235
+ {Protocol : "tcp" , Ports : "80" , Cidr : []string {"0.0.0.0/0" }, Direction : "ingress" , Action : "allow" },
236
+ {Protocol : "tcp" , Ports : "443" , Cidr : []string {"0.0.0.0/0" }, Direction : "ingress" , Action : "allow" },
237
+ }
238
+
239
+ // Retrieve actual firewall rules
240
+ rules , err := c .ListFirewallRules (firewallID )
241
+ if err != nil {
242
+ return false , fmt .Errorf ("error retrieving firewall rules: %s" , err )
243
+ }
244
+
245
+ // Compare the actual rules with the default rules
246
+ return areDefaultRules (rules , defaultRules ), nil
247
+ }
248
+
249
+ // Helper function to check if the firewall rules match the default rules
250
+ func areDefaultRules (rules []FirewallRule , defaultRules []FirewallRule ) bool {
251
+ if len (rules ) != len (defaultRules ) {
252
+ return false
253
+ }
254
+
255
+ for _ , defaultRule := range defaultRules {
256
+ match := false
257
+ for _ , rule := range rules {
258
+ if rule .Protocol == defaultRule .Protocol &&
259
+ rule .Ports == defaultRule .Ports &&
260
+ rule .Direction == defaultRule .Direction &&
261
+ rule .Action == defaultRule .Action &&
262
+ len (rule .Cidr ) == len (defaultRule .Cidr ) &&
263
+ rule .Cidr [0 ] == defaultRule .Cidr [0 ] {
264
+ match = true
265
+ break
266
+ }
267
+ }
268
+ if ! match {
269
+ return false
270
+ }
271
+ }
272
+
273
+ return true
274
+ }
275
+
0 commit comments