|
| 1 | +/* |
| 2 | +Objects Testing AutoTagActionsAPIService |
| 3 | +Note: This API uses name-based operations for delete (not ID-based) |
| 4 | +*/ |
| 5 | +package objects |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/paloaltonetworks/scm-go/common" |
| 15 | + "github.com/paloaltonetworks/scm-go/generated/objects" |
| 16 | +) |
| 17 | + |
| 18 | +// Test_objects_AutoTagActionsAPIService_Create tests the creation of an auto tag action |
| 19 | +func Test_objects_AutoTagActionsAPIService_Create(t *testing.T) { |
| 20 | + client := SetupObjectSvcTestClient(t) |
| 21 | + |
| 22 | + testName := "test-autotag-create-" + common.GenerateRandomString(10) |
| 23 | + autoTag := objects.AutoTagActions{ |
| 24 | + Name: testName, |
| 25 | + Folder: common.StringPtr("Prisma Access"), |
| 26 | + Filter: "addr.src in 10.0.0.0/8", |
| 27 | + LogType: "traffic", |
| 28 | + Actions: []objects.AutoTagActionsActionsInner{ |
| 29 | + { |
| 30 | + Name: "test-action", |
| 31 | + Type: objects.AutoTagActionsActionsInnerType{ |
| 32 | + Tagging: objects.AutoTagActionsActionsInnerTypeTagging{ |
| 33 | + Action: "add-tag", |
| 34 | + Tags: []string{"test-tag"}, |
| 35 | + Target: "source-address", |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + req := client.AutoTagActionsAPI.CreateAutoTagActions(context.Background()).AutoTagActions(autoTag) |
| 43 | + res, httpRes, err := req.Execute() |
| 44 | + if err != nil { |
| 45 | + handleAPIError(err) |
| 46 | + } |
| 47 | + |
| 48 | + require.NoError(t, err, "Failed to create auto tag action") |
| 49 | + assert.Equal(t, 201, httpRes.StatusCode, "Expected 201 Created status") |
| 50 | + require.NotNil(t, res, "Response should not be nil") |
| 51 | + assert.Equal(t, testName, res.Name, "Created auto tag action name should match") |
| 52 | + |
| 53 | + t.Logf("Successfully created auto tag action: %s", testName) |
| 54 | + |
| 55 | + // Cleanup - uses name-based delete |
| 56 | + reqDel := client.AutoTagActionsAPI.DeleteAutoTagActions(context.Background()).Name(testName) |
| 57 | + httpResDel, errDel := reqDel.Execute() |
| 58 | + if errDel != nil { |
| 59 | + handleAPIError(errDel) |
| 60 | + } |
| 61 | + require.NoError(t, errDel, "Failed to delete auto tag action during cleanup") |
| 62 | + assert.Equal(t, 200, httpResDel.StatusCode, "Expected 200 OK status for delete") |
| 63 | + t.Logf("Successfully cleaned up auto tag action: %s", testName) |
| 64 | +} |
| 65 | + |
| 66 | +// Test_objects_AutoTagActionsAPIService_List tests listing auto tag actions |
| 67 | +func Test_objects_AutoTagActionsAPIService_List(t *testing.T) { |
| 68 | + client := SetupObjectSvcTestClient(t) |
| 69 | + |
| 70 | + testName := "test-autotag-list-" + common.GenerateRandomString(10) |
| 71 | + autoTag := objects.AutoTagActions{ |
| 72 | + Name: testName, |
| 73 | + Folder: common.StringPtr("Prisma Access"), |
| 74 | + Filter: "addr.src in 172.16.0.0/12", |
| 75 | + LogType: "traffic", |
| 76 | + Actions: []objects.AutoTagActionsActionsInner{ |
| 77 | + { |
| 78 | + Name: "list-action", |
| 79 | + Type: objects.AutoTagActionsActionsInnerType{ |
| 80 | + Tagging: objects.AutoTagActionsActionsInnerTypeTagging{ |
| 81 | + Action: "add-tag", |
| 82 | + Tags: []string{"list-tag"}, |
| 83 | + Target: "source-address", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + req := client.AutoTagActionsAPI.CreateAutoTagActions(context.Background()).AutoTagActions(autoTag) |
| 91 | + createRes, _, err := req.Execute() |
| 92 | + if err != nil { |
| 93 | + handleAPIError(err) |
| 94 | + } |
| 95 | + require.NoError(t, err, "Failed to create auto tag action for list test") |
| 96 | + require.NotNil(t, createRes, "Create response should not be nil") |
| 97 | + |
| 98 | + // Test List |
| 99 | + reqList := client.AutoTagActionsAPI.ListAutoTagActions(context.Background()) |
| 100 | + listRes, httpResList, errList := reqList.Execute() |
| 101 | + if errList != nil { |
| 102 | + handleAPIError(errList) |
| 103 | + } |
| 104 | + |
| 105 | + require.NoError(t, errList, "Failed to list auto tag actions") |
| 106 | + assert.Equal(t, 200, httpResList.StatusCode, "Expected 200 OK status") |
| 107 | + require.NotNil(t, listRes, "List response should not be nil") |
| 108 | + assert.NotNil(t, listRes.Data, "List response data should not be nil") |
| 109 | + assert.Greater(t, len(listRes.Data), 0, "Should have at least one auto tag action in the list") |
| 110 | + |
| 111 | + t.Logf("Successfully listed auto tag actions, count: %d", len(listRes.Data)) |
| 112 | + |
| 113 | + // Cleanup |
| 114 | + reqDel := client.AutoTagActionsAPI.DeleteAutoTagActions(context.Background()).Name(testName) |
| 115 | + httpResDel, errDel := reqDel.Execute() |
| 116 | + if errDel != nil { |
| 117 | + handleAPIError(errDel) |
| 118 | + } |
| 119 | + require.NoError(t, errDel, "Failed to delete auto tag action during cleanup") |
| 120 | + assert.Equal(t, 200, httpResDel.StatusCode, "Expected 200 OK status for delete") |
| 121 | + t.Logf("Successfully cleaned up auto tag action: %s", testName) |
| 122 | +} |
| 123 | + |
| 124 | +// Test_objects_AutoTagActionsAPIService_Update tests updating an existing auto tag action |
| 125 | +func Test_objects_AutoTagActionsAPIService_Update(t *testing.T) { |
| 126 | + client := SetupObjectSvcTestClient(t) |
| 127 | + |
| 128 | + testName := "test-autotag-update-" + common.GenerateRandomString(10) |
| 129 | + autoTag := objects.AutoTagActions{ |
| 130 | + Name: testName, |
| 131 | + Folder: common.StringPtr("Prisma Access"), |
| 132 | + Filter: "addr.src in 192.168.0.0/16", |
| 133 | + LogType: "traffic", |
| 134 | + Actions: []objects.AutoTagActionsActionsInner{ |
| 135 | + { |
| 136 | + Name: "update-action", |
| 137 | + Type: objects.AutoTagActionsActionsInnerType{ |
| 138 | + Tagging: objects.AutoTagActionsActionsInnerTypeTagging{ |
| 139 | + Action: "add-tag", |
| 140 | + Tags: []string{"update-tag"}, |
| 141 | + Target: "source-address", |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + req := client.AutoTagActionsAPI.CreateAutoTagActions(context.Background()).AutoTagActions(autoTag) |
| 149 | + createRes, _, err := req.Execute() |
| 150 | + if err != nil { |
| 151 | + handleAPIError(err) |
| 152 | + } |
| 153 | + require.NoError(t, err, "Failed to create auto tag action for update test") |
| 154 | + require.NotNil(t, createRes, "Create response should not be nil") |
| 155 | + |
| 156 | + // Test Update |
| 157 | + updatedAutoTag := objects.AutoTagActions{ |
| 158 | + Name: testName, |
| 159 | + Folder: common.StringPtr("Prisma Access"), |
| 160 | + Filter: "addr.src in 192.168.0.0/16", |
| 161 | + LogType: "traffic", |
| 162 | + Description: common.StringPtr("Updated description"), |
| 163 | + Actions: []objects.AutoTagActionsActionsInner{ |
| 164 | + { |
| 165 | + Name: "updated-action", |
| 166 | + Type: objects.AutoTagActionsActionsInnerType{ |
| 167 | + Tagging: objects.AutoTagActionsActionsInnerTypeTagging{ |
| 168 | + Action: "add-tag", |
| 169 | + Tags: []string{"updated-tag"}, |
| 170 | + Target: "source-address", |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + } |
| 176 | + |
| 177 | + reqUpdate := client.AutoTagActionsAPI.UpdateAutoTagActions(context.Background()).AutoTagActions(updatedAutoTag) |
| 178 | + updateRes, httpResUpdate, errUpdate := reqUpdate.Execute() |
| 179 | + if errUpdate != nil { |
| 180 | + handleAPIError(errUpdate) |
| 181 | + } |
| 182 | + |
| 183 | + require.NoError(t, errUpdate, "Failed to update auto tag action") |
| 184 | + assert.Equal(t, 200, httpResUpdate.StatusCode, "Expected 200 OK status") |
| 185 | + require.NotNil(t, updateRes, "Update response should not be nil") |
| 186 | + assert.Equal(t, testName, updateRes.Name, "Auto tag action name should remain the same") |
| 187 | + assert.Equal(t, common.StringPtr("Updated description"), updateRes.Description, "Description should be updated") |
| 188 | + |
| 189 | + t.Logf("Successfully updated auto tag action: %s", testName) |
| 190 | + |
| 191 | + // Cleanup |
| 192 | + reqDel := client.AutoTagActionsAPI.DeleteAutoTagActions(context.Background()).Name(testName) |
| 193 | + httpResDel, errDel := reqDel.Execute() |
| 194 | + if errDel != nil { |
| 195 | + handleAPIError(errDel) |
| 196 | + } |
| 197 | + require.NoError(t, errDel, "Failed to delete auto tag action during cleanup") |
| 198 | + assert.Equal(t, 200, httpResDel.StatusCode, "Expected 200 OK status for delete") |
| 199 | + t.Logf("Successfully cleaned up auto tag action: %s", testName) |
| 200 | +} |
| 201 | + |
| 202 | +// Test_objects_AutoTagActionsAPIService_Delete tests deleting an auto tag action by name |
| 203 | +func Test_objects_AutoTagActionsAPIService_Delete(t *testing.T) { |
| 204 | + client := SetupObjectSvcTestClient(t) |
| 205 | + |
| 206 | + testName := "test-autotag-delete-" + common.GenerateRandomString(10) |
| 207 | + autoTag := objects.AutoTagActions{ |
| 208 | + Name: testName, |
| 209 | + Folder: common.StringPtr("Prisma Access"), |
| 210 | + Filter: "addr.src in 10.10.0.0/16", |
| 211 | + LogType: "traffic", |
| 212 | + Actions: []objects.AutoTagActionsActionsInner{ |
| 213 | + { |
| 214 | + Name: "delete-action", |
| 215 | + Type: objects.AutoTagActionsActionsInnerType{ |
| 216 | + Tagging: objects.AutoTagActionsActionsInnerTypeTagging{ |
| 217 | + Action: "add-tag", |
| 218 | + Tags: []string{"delete-tag"}, |
| 219 | + Target: "source-address", |
| 220 | + }, |
| 221 | + }, |
| 222 | + }, |
| 223 | + }, |
| 224 | + } |
| 225 | + |
| 226 | + req := client.AutoTagActionsAPI.CreateAutoTagActions(context.Background()).AutoTagActions(autoTag) |
| 227 | + createRes, _, err := req.Execute() |
| 228 | + if err != nil { |
| 229 | + handleAPIError(err) |
| 230 | + } |
| 231 | + require.NoError(t, err, "Failed to create auto tag action for delete test") |
| 232 | + require.NotNil(t, createRes, "Create response should not be nil") |
| 233 | + |
| 234 | + // Test Delete by name |
| 235 | + reqDel := client.AutoTagActionsAPI.DeleteAutoTagActions(context.Background()).Name(testName) |
| 236 | + httpResDel, errDel := reqDel.Execute() |
| 237 | + if errDel != nil { |
| 238 | + handleAPIError(errDel) |
| 239 | + } |
| 240 | + |
| 241 | + require.NoError(t, errDel, "Failed to delete auto tag action") |
| 242 | + assert.Equal(t, 200, httpResDel.StatusCode, "Expected 200 OK status") |
| 243 | + |
| 244 | + t.Logf("Successfully deleted auto tag action: %s", testName) |
| 245 | +} |
| 246 | + |
| 247 | +// Test_objects_AutoTagActionsAPIService_Fetch tests the Fetch convenience method |
| 248 | +func Test_objects_AutoTagActionsAPIService_Fetch(t *testing.T) { |
| 249 | + client := SetupObjectSvcTestClient(t) |
| 250 | + |
| 251 | + testName := "test-autotag-fetch-" + common.GenerateRandomString(10) |
| 252 | + autoTag := objects.AutoTagActions{ |
| 253 | + Name: testName, |
| 254 | + Folder: common.StringPtr("Prisma Access"), |
| 255 | + Filter: "addr.src in 10.20.0.0/16", |
| 256 | + LogType: "traffic", |
| 257 | + Actions: []objects.AutoTagActionsActionsInner{ |
| 258 | + { |
| 259 | + Name: "fetch-action", |
| 260 | + Type: objects.AutoTagActionsActionsInnerType{ |
| 261 | + Tagging: objects.AutoTagActionsActionsInnerTypeTagging{ |
| 262 | + Action: "add-tag", |
| 263 | + Tags: []string{"fetch-tag"}, |
| 264 | + Target: "source-address", |
| 265 | + }, |
| 266 | + }, |
| 267 | + }, |
| 268 | + }, |
| 269 | + } |
| 270 | + |
| 271 | + req := client.AutoTagActionsAPI.CreateAutoTagActions(context.Background()).AutoTagActions(autoTag) |
| 272 | + createRes, _, err := req.Execute() |
| 273 | + if err != nil { |
| 274 | + handleAPIError(err) |
| 275 | + } |
| 276 | + require.NoError(t, err, "Failed to create auto tag action for fetch test") |
| 277 | + require.NotNil(t, createRes, "Create response should not be nil") |
| 278 | + |
| 279 | + // Cleanup after test |
| 280 | + defer func() { |
| 281 | + deleteReq := client.AutoTagActionsAPI.DeleteAutoTagActions(context.Background()).Name(testName) |
| 282 | + _, _ = deleteReq.Execute() |
| 283 | + t.Logf("Cleaned up test object: %s", testName) |
| 284 | + }() |
| 285 | + |
| 286 | + // Test 1: Fetch existing object by name |
| 287 | + fetchedObj, err := client.AutoTagActionsAPI.FetchAutoTagActions( |
| 288 | + context.Background(), |
| 289 | + testName, |
| 290 | + common.StringPtr("Prisma Access"), |
| 291 | + nil, // snippet |
| 292 | + nil, // device |
| 293 | + ) |
| 294 | + |
| 295 | + require.NoError(t, err, "Failed to fetch auto tag action by name") |
| 296 | + require.NotNil(t, fetchedObj, "Fetched object should not be nil") |
| 297 | + assert.Equal(t, testName, fetchedObj.Name, "Fetched object name should match") |
| 298 | + t.Logf("[SUCCESS] FetchAutoTagActions found object: %s", fetchedObj.Name) |
| 299 | + |
| 300 | + // Test 2: Fetch non-existent object (should return nil, nil) |
| 301 | + notFound, err := client.AutoTagActionsAPI.FetchAutoTagActions( |
| 302 | + context.Background(), |
| 303 | + "non-existent-autotag-xyz-12345", |
| 304 | + common.StringPtr("Prisma Access"), |
| 305 | + nil, |
| 306 | + nil, |
| 307 | + ) |
| 308 | + require.NoError(t, err, "Fetch should not error for non-existent object") |
| 309 | + assert.Nil(t, notFound, "Should return nil for non-existent object") |
| 310 | + t.Logf("[SUCCESS] FetchAutoTagActions correctly returned nil for non-existent object") |
| 311 | +} |
0 commit comments