Skip to content

Commit

Permalink
[knf/validators/network] Added 'HasIP' validator
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 16, 2023
1 parent 2eb430a commit f93a10f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 12.81.0

* `[knf/validators/network]` Added `HasIP` validator
* `[strutil]` Added method `ReplaceIgnoreCase`
* `[uuid]` `GenUUID4` renamed to `UUID4`
* `[uuid]` `GenUUID5` renamed to `UUID5`
Expand Down
39 changes: 36 additions & 3 deletions knf/validators/network/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var (

// URL returns error if config property isn't a valid URL
URL = validateURL

// HasIP returns error if system doesn't have interface with IP from config property
HasIP = validateHasIP
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -82,7 +85,7 @@ func validateMAC(config *knf.Config, prop string, value any) error {
return fmt.Errorf("%s is not a valid MAC address: %v", macStr, err)
}

return err
return nil
}

func validateCIDR(config *knf.Config, prop string, value any) error {
Expand All @@ -98,7 +101,7 @@ func validateCIDR(config *knf.Config, prop string, value any) error {
return fmt.Errorf("%s is not a valid CIDR address: %v", cidrStr, err)
}

return err
return nil
}

func validateURL(config *knf.Config, prop string, value any) error {
Expand All @@ -114,5 +117,35 @@ func validateURL(config *knf.Config, prop string, value any) error {
return fmt.Errorf("%s is not a valid URL address: %v", urlStr, err)
}

return err
return nil
}

func validateHasIP(config *knf.Config, prop string, value any) error {
ipStr := config.GetS(prop)

if ipStr == "" {
return nil
}

interfaces, err := net.Interfaces()

if err != nil {
return fmt.Errorf("Can't get interfaces info for check: %v", err)
}

for _, i := range interfaces {
addr, err := i.Addrs()

if err != nil {
continue
}

for _, a := range addr {
if ipStr == a.(*net.IPNet).IP.String() {
return nil
}
}
}

return fmt.Errorf("The system does not have an interface with the address %s", ipStr)
}
22 changes: 22 additions & 0 deletions knf/validators/network/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const _CONFIG_DATA = `
test0:
test1: 127.0.0.1
test2: 300.0.400.5
test3: 192.168.1.254
[port]
test0:
Expand Down Expand Up @@ -83,6 +84,27 @@ func (s *ValidatorSuite) TestIPValidator(c *C) {
c.Assert(errs[0].Error(), Equals, "300.0.400.5 is not a valid IP address")
}

func (s *ValidatorSuite) TestHasIPValidator(c *C) {
configFile := createConfig(c, _CONFIG_DATA)

err := knf.Global(configFile)
c.Assert(err, IsNil)

errs := knf.Validate([]*knf.Validator{
{"ip:test0", HasIP, nil},
{"ip:test1", HasIP, nil},
})

c.Assert(errs, HasLen, 0)

errs = knf.Validate([]*knf.Validator{
{"ip:test3", HasIP, nil},
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "The system does not have an interface with the address 192.168.1.254")
}

func (s *ValidatorSuite) TestPortValidator(c *C) {
configFile := createConfig(c, _CONFIG_DATA)

Expand Down

0 comments on commit f93a10f

Please sign in to comment.