This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
136 lines (112 loc) · 3.01 KB
/
scope.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package goh4
import (
"encoding/json"
"fmt"
)
// Scope Structure holding a scope
type Scope struct {
h4 *H4
ID string `json:"id,omitempty"`
Name string `json:"short_name,omitempty"`
Description string `json:"description,omitempty"`
Query *QueryFilter `json:"short_query,omitempty"`
ScopeQuery *QueryFilter `json:"query,omitempty"`
Parent string `json:"parent_app_scope_id,omitempty"`
VRF int `json:"vrf_id,omitempty"`
}
// func (s *Scope) String() string {
// return s.ID
// }
// GetParent Return the parent scope as a *Scope
func (s *Scope) GetParent() *Scope {
if s.h4 != nil {
p, err := s.h4.GetScope(s.Parent)
if err != nil {
return nil
}
return p
}
return nil
}
// AddScope Add a new scope
func (h *H4) AddScope(s *Scope) error {
jsonStr, err := json.Marshal(&s)
if err != nil {
return fmt.Errorf("Error Marshalling scope: %s", err.Error())
}
postResp, err := h.Post("/app_scopes", fmt.Sprintf("%s", jsonStr))
if err != nil {
return fmt.Errorf("POST error: %s / POST: %s", err.Error(), jsonStr)
}
err = json.Unmarshal(postResp, &s)
if err != nil {
return fmt.Errorf("Error unmarshalling JSON: %s / JSON: %s", err.Error(), postResp)
}
s.h4 = h
return nil
}
// GetScope Get a single scope by ID
func (h *H4) GetScope(id string) (*Scope, error) {
getResp, err := h.Get(fmt.Sprintf("/app_scopes/%s", id))
if err != nil {
return nil, fmt.Errorf("GET error: %s", err.Error())
}
var jsonResp *Scope
//fmt.Printf("%s\n", getResp)
//
err = json.Unmarshal(getResp, &jsonResp)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling JSON: %s / JSON: %s", err.Error(), getResp)
}
jsonResp.h4 = h
return jsonResp, nil
}
// GetAllScopes Get all scopes
func (h *H4) GetAllScopes() ([]*Scope, error) {
getResp, err := h.Get(fmt.Sprintf("/app_scopes"))
if err != nil {
return nil, fmt.Errorf("GET error: %s", err.Error())
}
//fmt.Printf("%s", getResp)
var jsonResp []*Scope
err = json.Unmarshal(getResp, &jsonResp)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling JSON: %s / JSON: %s", err.Error(), getResp)
}
return jsonResp, nil
}
// DeleteScope Delete a scope by ID
func (h *H4) DeleteScope(scopeID string) error {
err := h.Delete(fmt.Sprintf("/app_scopes/%s", scopeID), "")
if err != nil {
return fmt.Errorf("Error deleting scope %s: %s", scopeID, err)
}
return nil
}
// GetRootScope Returns the root scope for a given VRF
func (h *H4) GetRootScope(vrf int) (*Scope, error) {
// First get all scopes
allScopes, err := h.GetAllScopes()
if err != nil {
return nil, err
}
for v := range allScopes {
if allScopes[v].VRF == vrf && allScopes[v].Parent == "" {
return allScopes[v], nil
}
}
return nil, nil
}
// GetScopeByName Returns a scope based on it's name
func (h *H4) GetScopeByName(name string) (*Scope, error) {
scopes, err := h.GetAllScopes()
if err != nil {
return nil, err
}
for _, s := range scopes {
if s.Name == name {
return s, nil
}
}
return nil, nil
}