-
Notifications
You must be signed in to change notification settings - Fork 10
/
objects.go
105 lines (87 loc) · 2.7 KB
/
objects.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Copyright 2017, Rutger te Nijenhuis & Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package ciscoasa
import (
"errors"
"fmt"
"regexp"
"strings"
)
type objectsService struct {
*Client
}
func (s *objectsService) objectFromAddress(address string) (*AddressObject, error) {
if address == "" {
return nil, errors.New("an address cannot be an empty string")
}
o := &AddressObject{}
// Test if the address is referencing a network object.
if n, err := s.GetNetworkObject(address); err == nil {
o.Kind = strings.Replace(n.Kind, "object#", "objectRef#", 1)
o.ObjectID = address
return o, nil
}
// Test if the address is referencing a network object group.
if n, err := s.GetNetworkObjectGroup(address); err == nil {
o.Kind = strings.Replace(n.Kind, "object#", "objectRef#", 1)
o.ObjectID = address
return o, nil
}
// Test if the address kind can be inferred from it's value.
if k, err := kindFromValue(address); err == nil {
if strings.HasSuffix(o.Kind, "Range") {
return nil, errors.New("an address cannot be an IP range")
}
o.Kind = k
o.Value = address
return o, nil
}
return nil, fmt.Errorf("failed to create object from address %q", address)
}
var regexpPorts = regexp.MustCompile("^[0-9]+(-[0-9]+)?")
func (s *objectsService) objectFromService(service string) (*ServiceObject, error) {
if service == "" {
return nil, nil
}
o := &ServiceObject{}
parts := strings.SplitN(service, "/", 2)
if len(parts) == 1 {
// Test if the service is referencing a network service.
if n, err := s.GetNetworkService(parts[0]); err == nil {
o.Kind = strings.Replace(n.Kind, "object#", "objectRef#", 1)
o.ObjectID = n.ObjectID
return o, nil
}
// Test is the service is referencing a network service group.
if n, err := s.GetNetworkServiceGroup(parts[0]); err == nil {
o.Kind = strings.Replace(n.Kind, "object#", "objectRef#", 1)
o.ObjectID = n.ObjectID
return o, nil
}
}
p, err := protocolFromValue(parts[0])
if err != nil {
return nil, err
}
if len(parts) == 1 || parts[1] == "" {
o.Kind = "NetworkProtocol"
o.Value = p.Prefix
return o, nil
}
o.Kind = p.Kind
o.Value = fmt.Sprintf("%s/%s", p.Prefix, parts[1])
return o, nil
}