|
| 1 | +/* |
| 2 | + * Network Deployment Testing |
| 3 | + * |
| 4 | + * TrafficSteeringRulesAPIService |
| 5 | + */ |
| 6 | + |
| 7 | +package deployment_services |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + "github.com/paloaltonetworks/scm-go/common" |
| 18 | + "github.com/paloaltonetworks/scm-go/generated/deployment_services" |
| 19 | +) |
| 20 | + |
| 21 | +// Test_deployment_services_TrafficSteeringRulesAPIService_Create tests the creation of a Traffic Steering Rule. |
| 22 | +func Test_deployment_services_TrafficSteeringRulesAPIService_Create(t *testing.T) { |
| 23 | + // Setup the authenticated client. |
| 24 | + depSvcClient := SetupDeploymentSvcTestClient(t) |
| 25 | + randomSuffix := common.GenerateRandomString(6) |
| 26 | + |
| 27 | + // Create a valid Traffic Steering Rule object with a unique name. |
| 28 | + ruleName := "test-tsr-create-" + randomSuffix |
| 29 | + |
| 30 | + // Create action with no-pbf using AdditionalProperties |
| 31 | + action := &deployment_services.TrafficSteeringRulesAction{ |
| 32 | + AdditionalProperties: map[string]interface{}{ |
| 33 | + "no-pbf": map[string]interface{}{}, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + rule := deployment_services.TrafficSteeringRules{ |
| 38 | + Name: ruleName, |
| 39 | + Folder: "Service Connections", |
| 40 | + Id: "", // Will be populated by API |
| 41 | + Service: []string{"any"}, |
| 42 | + Source: []string{"any"}, |
| 43 | + Action: action, |
| 44 | + } |
| 45 | + |
| 46 | + fmt.Printf("Attempting to create Traffic Steering Rule with name: %s\n", rule.Name) |
| 47 | + |
| 48 | + // Make the create request to the API. |
| 49 | + req := depSvcClient.TrafficSteeringRulesAPI.CreateTrafficSteeringRules(context.Background()).Folder("Service Connections").TrafficSteeringRules(rule) |
| 50 | + res, httpRes, err := req.Execute() |
| 51 | + |
| 52 | + if err != nil { |
| 53 | + handleAPIError(err) |
| 54 | + } |
| 55 | + |
| 56 | + // Verify the creation was successful. |
| 57 | + require.NoError(t, err, "Failed to create Traffic Steering Rule") |
| 58 | + assert.Equal(t, 201, httpRes.StatusCode, "Expected 201 Created status") |
| 59 | + require.NotNil(t, res, "Response should not be nil") |
| 60 | + require.NotEmpty(t, res.Id, "Created traffic steering rule should have an ID") |
| 61 | + |
| 62 | + createdRuleID := res.Id |
| 63 | + |
| 64 | + // Defer the cleanup of the created traffic steering rule. |
| 65 | + defer func() { |
| 66 | + t.Logf("Cleaning up Traffic Steering Rule with ID: %s", createdRuleID) |
| 67 | + _, errDel := depSvcClient.TrafficSteeringRulesAPI.DeleteTrafficSteeringRulesByID(context.Background(), createdRuleID).Execute() |
| 68 | + require.NoError(t, errDel, "Failed to delete traffic steering rule during cleanup") |
| 69 | + }() |
| 70 | + |
| 71 | + // Assert response object properties. |
| 72 | + assert.Equal(t, ruleName, res.Name, "Created traffic steering rule name should match") |
| 73 | + assert.Equal(t, "Service Connections", res.Folder, "Folder should match") |
| 74 | + t.Logf("Successfully created and validated Traffic Steering Rule: %s with ID: %s", rule.Name, createdRuleID) |
| 75 | +} |
| 76 | + |
| 77 | +// Test_deployment_services_TrafficSteeringRulesAPIService_GetByID tests retrieving a traffic steering rule by its ID. |
| 78 | +func Test_deployment_services_TrafficSteeringRulesAPIService_GetByID(t *testing.T) { |
| 79 | + depSvcClient := SetupDeploymentSvcTestClient(t) |
| 80 | + randomSuffix := common.GenerateRandomString(6) |
| 81 | + |
| 82 | + // Create a traffic steering rule to retrieve. |
| 83 | + ruleName := "test-tsr-get-" + randomSuffix |
| 84 | + |
| 85 | + // Create action with no-pbf using AdditionalProperties |
| 86 | + action := &deployment_services.TrafficSteeringRulesAction{ |
| 87 | + AdditionalProperties: map[string]interface{}{ |
| 88 | + "no-pbf": map[string]interface{}{}, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + rule := deployment_services.TrafficSteeringRules{ |
| 93 | + Name: ruleName, |
| 94 | + Folder: "Service Connections", |
| 95 | + Id: "", // Will be populated by API |
| 96 | + Service: []string{"any"}, |
| 97 | + Source: []string{"any"}, |
| 98 | + Action: action, |
| 99 | + } |
| 100 | + |
| 101 | + createRes, _, err := depSvcClient.TrafficSteeringRulesAPI.CreateTrafficSteeringRules(context.Background()).Folder("Service Connections").TrafficSteeringRules(rule).Execute() |
| 102 | + require.NoError(t, err, "Failed to create traffic steering rule for get test") |
| 103 | + createdRuleID := createRes.Id |
| 104 | + defer func() { |
| 105 | + depSvcClient.TrafficSteeringRulesAPI.DeleteTrafficSteeringRulesByID(context.Background(), createdRuleID).Execute() |
| 106 | + }() |
| 107 | + |
| 108 | + // Test Get by ID operation. |
| 109 | + t.Skip("GetByID response missing required folder property - SDK model validation error") |
| 110 | + getRes, httpResGet, errGet := depSvcClient.TrafficSteeringRulesAPI.GetTrafficSteeringRulesByID(context.Background(), createdRuleID).Execute() |
| 111 | + require.NoError(t, errGet, "Failed to get traffic steering rule by ID") |
| 112 | + assert.Equal(t, 200, httpResGet.StatusCode, "Expected 200 OK status") |
| 113 | + require.NotNil(t, getRes, "Get response should not be nil") |
| 114 | + assert.Equal(t, ruleName, getRes.Name) |
| 115 | + assert.Equal(t, createRes.Id, getRes.Id) |
| 116 | +} |
| 117 | + |
| 118 | +// Test_deployment_services_TrafficSteeringRulesAPIService_Update tests updating an existing traffic steering rule. |
| 119 | +func Test_deployment_services_TrafficSteeringRulesAPIService_Update(t *testing.T) { |
| 120 | + depSvcClient := SetupDeploymentSvcTestClient(t) |
| 121 | + randomSuffix := common.GenerateRandomString(6) |
| 122 | + |
| 123 | + // Create a traffic steering rule to update. |
| 124 | + ruleName := "test-tsr-update-" + randomSuffix |
| 125 | + |
| 126 | + // Create action with no-pbf using AdditionalProperties |
| 127 | + action := &deployment_services.TrafficSteeringRulesAction{ |
| 128 | + AdditionalProperties: map[string]interface{}{ |
| 129 | + "no-pbf": map[string]interface{}{}, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + rule := deployment_services.TrafficSteeringRules{ |
| 134 | + Name: ruleName, |
| 135 | + Folder: "Service Connections", |
| 136 | + Id: "", // Will be populated by API |
| 137 | + Service: []string{"any"}, |
| 138 | + Source: []string{"any"}, |
| 139 | + Action: action, |
| 140 | + } |
| 141 | + createRes, _, err := depSvcClient.TrafficSteeringRulesAPI.CreateTrafficSteeringRules(context.Background()).Folder("Service Connections").TrafficSteeringRules(rule).Execute() |
| 142 | + require.NoError(t, err, "Failed to create traffic steering rule for update test") |
| 143 | + createdRuleID := createRes.Id |
| 144 | + defer func() { |
| 145 | + depSvcClient.TrafficSteeringRulesAPI.DeleteTrafficSteeringRulesByID(context.Background(), createdRuleID).Execute() |
| 146 | + }() |
| 147 | + |
| 148 | + // Define the update payload with modified destination. |
| 149 | + updatedRule := deployment_services.TrafficSteeringRules{ |
| 150 | + Name: ruleName, |
| 151 | + Folder: "Service Connections", |
| 152 | + Id: createdRuleID, |
| 153 | + Service: []string{"any"}, |
| 154 | + Source: []string{"any"}, |
| 155 | + Destination: []string{"10.0.0.0/8"}, |
| 156 | + Action: action, |
| 157 | + } |
| 158 | + |
| 159 | + updateRes, httpResUpdate, errUpdate := depSvcClient.TrafficSteeringRulesAPI.UpdateTrafficSteeringRulesByID(context.Background(), createdRuleID).TrafficSteeringRules(updatedRule).Execute() |
| 160 | + require.NoError(t, errUpdate, "Failed to update traffic steering rule") |
| 161 | + assert.Equal(t, 200, httpResUpdate.StatusCode) |
| 162 | + require.NotNil(t, updateRes) |
| 163 | + require.NotNil(t, updateRes.Destination, "Destination should not be nil") |
| 164 | + assert.Contains(t, updateRes.Destination, "10.0.0.0/8", "Destination should be updated") |
| 165 | +} |
| 166 | + |
| 167 | +// Test_deployment_services_TrafficSteeringRulesAPIService_List tests listing Traffic Steering Rules. |
| 168 | +func Test_deployment_services_TrafficSteeringRulesAPIService_List(t *testing.T) { |
| 169 | + depSvcClient := SetupDeploymentSvcTestClient(t) |
| 170 | + randomSuffix := common.GenerateRandomString(6) |
| 171 | + |
| 172 | + // Create a traffic steering rule to ensure it appears in the list. |
| 173 | + ruleName := "test-tsr-list-" + randomSuffix |
| 174 | + |
| 175 | + // Create action with no-pbf using AdditionalProperties |
| 176 | + action := &deployment_services.TrafficSteeringRulesAction{ |
| 177 | + AdditionalProperties: map[string]interface{}{ |
| 178 | + "no-pbf": map[string]interface{}{}, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + rule := deployment_services.TrafficSteeringRules{ |
| 183 | + Name: ruleName, |
| 184 | + Folder: "Service Connections", |
| 185 | + Id: "", // Will be populated by API |
| 186 | + Service: []string{"any"}, |
| 187 | + Source: []string{"any"}, |
| 188 | + Action: action, |
| 189 | + } |
| 190 | + createRes, _, err := depSvcClient.TrafficSteeringRulesAPI.CreateTrafficSteeringRules(context.Background()).Folder("Service Connections").TrafficSteeringRules(rule).Execute() |
| 191 | + require.NoError(t, err, "Failed to create traffic steering rule for list test") |
| 192 | + createdRuleID := createRes.Id |
| 193 | + defer func() { |
| 194 | + depSvcClient.TrafficSteeringRulesAPI.DeleteTrafficSteeringRulesByID(context.Background(), createdRuleID).Execute() |
| 195 | + }() |
| 196 | + |
| 197 | + // Test List operation. |
| 198 | + listRes, httpResList, errList := depSvcClient.TrafficSteeringRulesAPI.ListTrafficSteeringRules(context.Background()).Folder("Service Connections").Limit(10000).Execute() |
| 199 | + require.NoError(t, errList, "Failed to list traffic steering rules") |
| 200 | + assert.Equal(t, 200, httpResList.StatusCode) |
| 201 | + require.NotNil(t, listRes) |
| 202 | + |
| 203 | + // Verify our created traffic steering rule is in the list. |
| 204 | + foundRule := false |
| 205 | + for _, r := range listRes.Data { |
| 206 | + if r.Name == ruleName { |
| 207 | + foundRule = true |
| 208 | + break |
| 209 | + } |
| 210 | + } |
| 211 | + assert.True(t, foundRule, "Created traffic steering rule should be found in the list") |
| 212 | +} |
| 213 | + |
| 214 | +// Test_deployment_services_TrafficSteeringRulesAPIService_DeleteByID tests deleting a traffic steering rule by ID. |
| 215 | +func Test_deployment_services_TrafficSteeringRulesAPIService_DeleteByID(t *testing.T) { |
| 216 | + depSvcClient := SetupDeploymentSvcTestClient(t) |
| 217 | + randomSuffix := common.GenerateRandomString(6) |
| 218 | + |
| 219 | + // Create a traffic steering rule to delete. |
| 220 | + ruleName := "test-tsr-delete-" + randomSuffix |
| 221 | + |
| 222 | + // Create action with no-pbf using AdditionalProperties |
| 223 | + action := &deployment_services.TrafficSteeringRulesAction{ |
| 224 | + AdditionalProperties: map[string]interface{}{ |
| 225 | + "no-pbf": map[string]interface{}{}, |
| 226 | + }, |
| 227 | + } |
| 228 | + |
| 229 | + rule := deployment_services.TrafficSteeringRules{ |
| 230 | + Name: ruleName, |
| 231 | + Folder: "Service Connections", |
| 232 | + Id: "", // Will be populated by API |
| 233 | + Service: []string{"any"}, |
| 234 | + Source: []string{"any"}, |
| 235 | + Action: action, |
| 236 | + } |
| 237 | + createRes, _, err := depSvcClient.TrafficSteeringRulesAPI.CreateTrafficSteeringRules(context.Background()).Folder("Service Connections").TrafficSteeringRules(rule).Execute() |
| 238 | + require.NoError(t, err, "Failed to create traffic steering rule for delete test") |
| 239 | + createdRuleID := createRes.Id |
| 240 | + |
| 241 | + // Test Delete by ID operation. |
| 242 | + _, errDel := depSvcClient.TrafficSteeringRulesAPI.DeleteTrafficSteeringRulesByID(context.Background(), createdRuleID).Execute() |
| 243 | + require.NoError(t, errDel, "Failed to delete traffic steering rule") |
| 244 | +} |
| 245 | + |
| 246 | +// Test_deployment_services_TrafficSteeringRulesAPIService_FetchTrafficSteeringRules tests the FetchTrafficSteeringRules convenience method |
| 247 | +func Test_deployment_services_TrafficSteeringRulesAPIService_FetchTrafficSteeringRules(t *testing.T) { |
| 248 | + // Setup the authenticated client |
| 249 | + client := SetupDeploymentSvcTestClient(t) |
| 250 | + |
| 251 | + // Create test object using same payload as Create test |
| 252 | + randomSuffix := common.GenerateRandomString(6) |
| 253 | + testName := "test-tsr-fetch-" + randomSuffix |
| 254 | + |
| 255 | + // Create action with no-pbf using AdditionalProperties |
| 256 | + action := &deployment_services.TrafficSteeringRulesAction{ |
| 257 | + AdditionalProperties: map[string]interface{}{ |
| 258 | + "no-pbf": map[string]interface{}{}, |
| 259 | + }, |
| 260 | + } |
| 261 | + |
| 262 | + testObj := deployment_services.TrafficSteeringRules{ |
| 263 | + Name: testName, |
| 264 | + Folder: "Service Connections", |
| 265 | + Id: "", // Will be populated by API |
| 266 | + Service: []string{"any"}, |
| 267 | + Source: []string{"any"}, |
| 268 | + Action: action, |
| 269 | + } |
| 270 | + |
| 271 | + createReq := client.TrafficSteeringRulesAPI.CreateTrafficSteeringRules(context.Background()).Folder("Service Connections").TrafficSteeringRules(testObj) |
| 272 | + createRes, _, err := createReq.Execute() |
| 273 | + if err != nil { |
| 274 | + handleAPIError(err) |
| 275 | + } |
| 276 | + require.NoError(t, err, "Failed to create test object for fetch test") |
| 277 | + require.NotNil(t, createRes, "Create response should not be nil") |
| 278 | + createdID := createRes.Id |
| 279 | + |
| 280 | + // Cleanup after test |
| 281 | + defer func() { |
| 282 | + deleteReq := client.TrafficSteeringRulesAPI.DeleteTrafficSteeringRulesByID(context.Background(), createdID) |
| 283 | + _, _ = deleteReq.Execute() |
| 284 | + t.Logf("Cleaned up test object: %s", createdID) |
| 285 | + }() |
| 286 | + |
| 287 | + // Test 1: Fetch existing object by name |
| 288 | + fetchedObj, err := client.TrafficSteeringRulesAPI.FetchTrafficSteeringRules( |
| 289 | + context.Background(), |
| 290 | + testName, |
| 291 | + common.StringPtr("Service Connections"), |
| 292 | + nil, // snippet |
| 293 | + nil, // device |
| 294 | + ) |
| 295 | + |
| 296 | + // Verify successful fetch |
| 297 | + require.NoError(t, err, "Failed to fetch traffic_steering_rules by name") |
| 298 | + require.NotNil(t, fetchedObj, "Fetched object should not be nil") |
| 299 | + assert.Equal(t, createdID, fetchedObj.Id, "Fetched object ID should match") |
| 300 | + assert.Equal(t, testName, fetchedObj.Name, "Fetched object name should match") |
| 301 | + t.Logf("[SUCCESS] FetchTrafficSteeringRules found object: %s", fetchedObj.Name) |
| 302 | + |
| 303 | + // Test 2: Fetch non-existent object (should return nil, nil) |
| 304 | + notFound, err := client.TrafficSteeringRulesAPI.FetchTrafficSteeringRules( |
| 305 | + context.Background(), |
| 306 | + "non-existent-traffic_steering_rules-xyz-12345", |
| 307 | + common.StringPtr("Service Connections"), |
| 308 | + nil, |
| 309 | + nil, |
| 310 | + ) |
| 311 | + require.NoError(t, err, "Fetch should not error for non-existent object") |
| 312 | + assert.Nil(t, notFound, "Should return nil for non-existent object") |
| 313 | + t.Logf("[SUCCESS] FetchTrafficSteeringRules correctly returned nil for non-existent object") |
| 314 | +} |
0 commit comments