1/*
2Copyright (c) 2017 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package simulator
18
19import (
20	"github.com/vmware/govmomi/simulator/esx"
21	"github.com/vmware/govmomi/vim25/methods"
22	"github.com/vmware/govmomi/vim25/mo"
23	"github.com/vmware/govmomi/vim25/soap"
24	"github.com/vmware/govmomi/vim25/types"
25)
26
27type HostFirewallSystem struct {
28	mo.HostFirewallSystem
29}
30
31func NewHostFirewallSystem(_ *mo.HostSystem) *HostFirewallSystem {
32	info := esx.HostFirewallInfo
33
34	return &HostFirewallSystem{
35		HostFirewallSystem: mo.HostFirewallSystem{
36			FirewallInfo: &info,
37		},
38	}
39}
40
41func DisableRuleset(info *types.HostFirewallInfo, id string) bool {
42	for i := range info.Ruleset {
43		if info.Ruleset[i].Key == id {
44			info.Ruleset[i].Enabled = false
45			return true
46		}
47	}
48
49	return false
50}
51
52func (s *HostFirewallSystem) DisableRuleset(req *types.DisableRuleset) soap.HasFault {
53	body := &methods.DisableRulesetBody{}
54
55	if DisableRuleset(s.HostFirewallSystem.FirewallInfo, req.Id) {
56		body.Res = new(types.DisableRulesetResponse)
57		return body
58	}
59
60	body.Fault_ = Fault("", &types.NotFound{})
61
62	return body
63}
64
65func EnableRuleset(info *types.HostFirewallInfo, id string) bool {
66	for i := range info.Ruleset {
67		if info.Ruleset[i].Key == id {
68			info.Ruleset[i].Enabled = true
69			return true
70		}
71	}
72
73	return false
74}
75
76func (s *HostFirewallSystem) EnableRuleset(req *types.EnableRuleset) soap.HasFault {
77	body := &methods.EnableRulesetBody{}
78
79	if EnableRuleset(s.HostFirewallSystem.FirewallInfo, req.Id) {
80		body.Res = new(types.EnableRulesetResponse)
81		return body
82	}
83
84	body.Fault_ = Fault("", &types.NotFound{})
85
86	return body
87}
88