/
security.go
49 lines (42 loc) · 1.29 KB
/
security.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
package client
import (
"encoding/json"
"fmt"
"log"
"net/url"
"github.com/hichtakk/nsxctl/structs"
)
func (c *NsxtClient) GetDfwPolicies(domain string, name string) ([]structs.DfwPolicy, error) {
path := "/policy/api/v1/infra/domains/" + domain + "/security-policies?include_rule_count=true"
res := c.Request("GET", path, nil, nil)
body, err := res.BodyBytes()
if err != nil {
return nil, err
}
var results structs.DfwPolicies
json.Unmarshal(body, &results)
if name != "" {
for _, p := range results.Policies {
if name == p.Name {
return []structs.DfwPolicy{p}, nil
}
}
return nil, fmt.Errorf("Dfw Policy '%s' is not found", name)
}
return results.Policies, nil
}
func (c *NsxtClient) GetDfwRules(policy structs.DfwPolicy) []structs.DfwRule {
// in case of using multi-byte characters that the following API doen't work
// GET /policy/api/v1/infra/domains/<domain-id>/security-policies/<security-policy-id>
parent_path := "*security-policies*" + policy.Id
path := "/policy/api/v1/search/query?query=resource_type:Rule%20AND%20parent_path:" + url.PathEscape(parent_path)
res := c.Request("GET", path, nil, nil)
body, err := res.BodyBytes()
if err != nil {
log.Fatal(err)
return nil
}
var results structs.DfwRules
json.Unmarshal(body, &results)
return results.Rules
}