host_firewall_system.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. Copyright (c) 2015 VMware, Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package object
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "strings"
  19. "github.com/vmware/govmomi/vim25"
  20. "github.com/vmware/govmomi/vim25/methods"
  21. "github.com/vmware/govmomi/vim25/mo"
  22. "github.com/vmware/govmomi/vim25/types"
  23. )
  24. type HostFirewallSystem struct {
  25. Common
  26. }
  27. func NewHostFirewallSystem(c *vim25.Client, ref types.ManagedObjectReference) *HostFirewallSystem {
  28. return &HostFirewallSystem{
  29. Common: NewCommon(c, ref),
  30. }
  31. }
  32. func (s HostFirewallSystem) DisableRuleset(ctx context.Context, id string) error {
  33. req := types.DisableRuleset{
  34. This: s.Reference(),
  35. Id: id,
  36. }
  37. _, err := methods.DisableRuleset(ctx, s.c, &req)
  38. return err
  39. }
  40. func (s HostFirewallSystem) EnableRuleset(ctx context.Context, id string) error {
  41. req := types.EnableRuleset{
  42. This: s.Reference(),
  43. Id: id,
  44. }
  45. _, err := methods.EnableRuleset(ctx, s.c, &req)
  46. return err
  47. }
  48. func (s HostFirewallSystem) Refresh(ctx context.Context) error {
  49. req := types.RefreshFirewall{
  50. This: s.Reference(),
  51. }
  52. _, err := methods.RefreshFirewall(ctx, s.c, &req)
  53. return err
  54. }
  55. func (s HostFirewallSystem) Info(ctx context.Context) (*types.HostFirewallInfo, error) {
  56. var fs mo.HostFirewallSystem
  57. err := s.Properties(ctx, s.Reference(), []string{"firewallInfo"}, &fs)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return fs.FirewallInfo, nil
  62. }
  63. // HostFirewallRulesetList provides helpers for a slice of types.HostFirewallRuleset
  64. type HostFirewallRulesetList []types.HostFirewallRuleset
  65. // ByRule returns a HostFirewallRulesetList where Direction, PortType and Protocol are equal and Port is within range
  66. func (l HostFirewallRulesetList) ByRule(rule types.HostFirewallRule) HostFirewallRulesetList {
  67. var matches HostFirewallRulesetList
  68. for _, rs := range l {
  69. for _, r := range rs.Rule {
  70. if r.PortType != rule.PortType ||
  71. r.Protocol != rule.Protocol ||
  72. r.Direction != rule.Direction {
  73. continue
  74. }
  75. if r.EndPort == 0 && rule.Port == r.Port ||
  76. rule.Port >= r.Port && rule.Port <= r.EndPort {
  77. matches = append(matches, rs)
  78. break
  79. }
  80. }
  81. }
  82. return matches
  83. }
  84. // EnabledByRule returns a HostFirewallRulesetList with Match(rule) applied and filtered via Enabled()
  85. // if enabled param is true, otherwise filtered via Disabled().
  86. // An error is returned if the resulting list is empty.
  87. func (l HostFirewallRulesetList) EnabledByRule(rule types.HostFirewallRule, enabled bool) (HostFirewallRulesetList, error) {
  88. var matched, skipped HostFirewallRulesetList
  89. var matchedKind, skippedKind string
  90. l = l.ByRule(rule)
  91. if enabled {
  92. matched = l.Enabled()
  93. matchedKind = "enabled"
  94. skipped = l.Disabled()
  95. skippedKind = "disabled"
  96. } else {
  97. matched = l.Disabled()
  98. matchedKind = "disabled"
  99. skipped = l.Enabled()
  100. skippedKind = "enabled"
  101. }
  102. if len(matched) == 0 {
  103. msg := fmt.Sprintf("%d %s firewall rulesets match %s %s %s %d, %d %s rulesets match",
  104. len(matched), matchedKind,
  105. rule.Direction, rule.Protocol, rule.PortType, rule.Port,
  106. len(skipped), skippedKind)
  107. if len(skipped) != 0 {
  108. msg += fmt.Sprintf(": %s", strings.Join(skipped.Keys(), ", "))
  109. }
  110. return nil, errors.New(msg)
  111. }
  112. return matched, nil
  113. }
  114. // Enabled returns a HostFirewallRulesetList with enabled rules
  115. func (l HostFirewallRulesetList) Enabled() HostFirewallRulesetList {
  116. var matches HostFirewallRulesetList
  117. for _, rs := range l {
  118. if rs.Enabled {
  119. matches = append(matches, rs)
  120. }
  121. }
  122. return matches
  123. }
  124. // Disabled returns a HostFirewallRulesetList with disabled rules
  125. func (l HostFirewallRulesetList) Disabled() HostFirewallRulesetList {
  126. var matches HostFirewallRulesetList
  127. for _, rs := range l {
  128. if !rs.Enabled {
  129. matches = append(matches, rs)
  130. }
  131. }
  132. return matches
  133. }
  134. // Keys returns the HostFirewallRuleset.Key for each ruleset in the list
  135. func (l HostFirewallRulesetList) Keys() []string {
  136. var keys []string
  137. for _, rs := range l {
  138. keys = append(keys, rs.Key)
  139. }
  140. return keys
  141. }