This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
249 lines (214 loc) · 7.12 KB
/
api_test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package skyinfoblox
import (
"errors"
"github.com/sky-uk/skyinfoblox/api/common/v261/model"
"github.com/stretchr/testify/assert"
"math/rand"
"os"
"strconv"
"testing"
"time"
)
func getClient() (*Client, error) {
server, ok := os.LookupEnv("INFOBLOX_SERVER")
if ok == false || server == "" {
return nil, errors.New("[ERROR] INFOBLOX_SERVER env var not set")
}
username, ok := os.LookupEnv("INFOBLOX_USERNAME")
if ok == false {
return nil, errors.New("[ERROR] INFOBLOX_USERNAME env var not set")
}
password, ok := os.LookupEnv("INFOBLOX_PASSWORD")
if ok == false {
return nil, errors.New("[ERROR] INFOBLOX_PASSWORD env var not set")
}
params := Params{
WapiVersion: "v2.6.1", // this is anyhow the default...
URL: server,
User: username,
Password: password,
IgnoreSSL: true,
Debug: true,
}
client := Connect(params)
return client, nil
}
func TestFilterProfileKeys(t *testing.T) {
adminuser := map[string]interface{}{
"name": "user1",
"comment": "this is a comment",
"email": "exampleuser@domain.internal.com",
"admin_groups": []string{"APP-OVP-INFOBLOX-READONLY"},
"password": "c0a6264f0f128d94cd8ef26652e7d9fd",
}
validProfile := FilterProfileKeys(
adminuser,
[]string{"name", "comment"},
)
keys := make([]string, 0)
for k := range validProfile {
keys = append(keys, k)
}
assert.Equal(t, 2, len(keys))
assert.Equal(t, "user1", validProfile["name"])
assert.Equal(t, "this is a comment", validProfile["comment"])
}
func TestGetValidKeys(t *testing.T) {
client, err := getClient()
if err != nil {
t.Fatal(err)
}
validKeysWhileReading := client.GetValidKeys("adminuser", []string{"r"})
assert.Equal(t, 9, len(validKeysWhileReading))
validKeysWhileWriting := client.GetValidKeys("adminuser", []string{"w"})
assert.Equal(t, 10, len(validKeysWhileWriting))
validKeysWhileUpdating := client.GetValidKeys("adminuser", []string{"u"})
assert.Equal(t, 10, len(validKeysWhileUpdating))
}
func TestGetObjectTypeFromRef(t *testing.T) {
ref := "adminrole/b25lLnJvbGUkdGVzdDQ2Mw:test463"
objType := GetObjectTypeFromRef(ref)
assert.Equal(t, "adminrole", objType)
}
func TestFilterReturnFields(t *testing.T) {
required := []string{"one", "two", "three"}
allowed := []string{"one", "three"}
filtered := FilterReturnFields(required, allowed)
assert.Equal(t, 2, len(filtered))
assert.Equal(t, "one", filtered[0])
assert.Equal(t, "three", filtered[1])
}
func TestAllAPI(t *testing.T) {
rand.Seed(time.Now().UnixNano())
client, err := getClient()
if err != nil {
t.Fatal(err)
}
// With a generic map (that matches a given schema...)
adminRole := make(map[string]interface{})
adminRole["name"] = "test" + strconv.Itoa(rand.Intn(1000))
adminRole["comment"] = "An initial comment"
// creating an object...
adminRoleObjRef, err := client.Create("adminrole", adminRole)
if err != nil {
t.Fatal("[ERROR] Error creating an adminrole object")
}
assert.NotEmpty(t, adminRoleObjRef)
t.Log("[DEBUG] Object created, REFOBJ: ", adminRoleObjRef)
// ...or with a defined struct...
adminGroup := model.AdminGroup{
AccessMethod: []string{"API"},
Comment: "API Access only",
Disable: true,
EmailAddresses: []string{"test@example-test.com"},
Name: "test" + strconv.Itoa(rand.Intn(1000)),
Roles: []string{adminRole["name"].(string)},
SuperUser: false,
}
adminGroupObjRef, err := client.Create("admingroup", adminGroup)
if err != nil {
t.Fatal("[ERROR] Error creating an admingroup object")
}
//reading the object...
role := make(map[string]interface{})
err = client.Read(adminRoleObjRef, []string{"comment"}, &role)
if err != nil {
t.Fatal("[ERROR] Error reading object with ref: ", adminRoleObjRef)
}
t.Logf("[DEBUG] Object (as map):\n%+v\n", role)
//reading the object as struct...
var roleObj model.AdminRole
err = client.Read(adminRoleObjRef, []string{"comment"}, &roleObj)
if err != nil {
t.Fatal("[ERROR] Error reading object with ref: ", adminRoleObjRef)
}
t.Logf("[DEBUG] Object (as struct):\n%+v\n", roleObj)
//getting all roles...
roles, err := client.ReadAll("adminrole")
if err != nil {
t.Fatal("[ERROR] Error reading all roles")
}
t.Logf("[DEBUG] Objects:\n%+v\n", roles)
//updating the object...
adminRole["comment"] = "Object updated"
updatedRefObj, err := client.Update(adminRoleObjRef, adminRole)
if err != nil {
t.Fatal("[ERROR] Error updating the object")
}
t.Logf("[DEBUG] Object %s updated\n", updatedRefObj)
// getting the updated object and chedking for the comment...
err = client.Read(updatedRefObj, []string{"comment"}, &role)
if err != nil {
t.Fatal("[ERROR] Error reading object with ref: ", updatedRefObj)
}
t.Log("[DEBUG] Updated object comment: ", role["comment"])
assert.Equal(t, "Object updated", role["comment"])
//deleting all objects
refObj, err := client.Delete(adminGroupObjRef)
if err != nil {
t.Fatal("[ERROR] Error deleting object")
}
assert.NotEmpty(t, refObj)
t.Log("[DEBUG] Object deleted, REFOBJ: ", refObj)
refObj, err = client.Delete(adminRoleObjRef)
if err != nil {
t.Fatal("[ERROR] Error deleting object")
}
assert.NotEmpty(t, refObj)
t.Log("[DEBUG] Object deleted, REFOBJ: ", refObj)
// now creating and reading a user to check
// control on attributes
newUser := make(map[string]interface{})
newUser["name"] = "user_" + strconv.Itoa(rand.Intn(1000))
newUser["password"] = "foooooo" // at least 4 chars...
newUser["comment"] = "test user for attributes check"
newUser["admin_groups"] = []string{"APP-OVP-INFOBLOX-READONLY"}
newUserRef, err := client.Create("adminuser", newUser)
if err != nil {
t.Fatal("[ERROR] Error creating an adminuser object")
}
// now we try to read the password (which is forbidden)
user := make(map[string]interface{})
err = client.Read(newUserRef, []string{"name", "password"}, &user)
if err != nil {
t.Fatal("[ERROR] Error reading an adminuser object")
}
assert.Equal(t, 2, len(user))
assert.Equal(t, newUserRef, user["_ref"])
assert.Equal(t, newUser["name"], user["name"])
refObj, err = client.Delete(newUserRef)
if err != nil {
t.Fatal("[ERROR] Error deleting an adminuser object")
}
}
func TestNestedStructures(t *testing.T) {
rand.Seed(time.Now().UnixNano())
client, err := getClient()
if err != nil {
t.Fatal(err)
}
delegateToField := map[string]interface{}{
"stealth": false,
"tsig_key_alg": "HMAC-MD5",
"use_tsig_key_name": false,
"address": "192.168.100.1",
"name": "ns1.example.com",
"shared_with_ms_parent_delegation": false,
}
nsGroupDelegation := map[string]interface{}{
"comment": "Infoblox Terraform Acceptance test",
"name": "acctest-infoblox-ns-group-delegation-" + strconv.Itoa(rand.Intn(1000000)),
"delegate_to": []interface{}{delegateToField},
}
// attribute 'shared_with_ms_parent_delegation' should be
// filtered while creating this object...
refObj, err := client.Create("nsgroup:delegation", nsGroupDelegation)
if err != nil {
t.Fatal("[ERROR] Error creating a nsgroup:delegation object")
}
assert.NotEmpty(t, refObj)
refObj, err = client.Delete(refObj)
if err != nil {
t.Fatal("[ERROR] Error deleting object")
}
}