github.com/google/go-github/v71@v71.0.0/github/orgs_network_configurations_test.go (about) 1 // Copyright 2025 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "net/http" 12 "testing" 13 "time" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestOrganizationsService_ListOrgsNetworkConfigurations(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 mux.HandleFunc("/orgs/o/settings/network-configurations", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{"page": "1", "per_page": "3"}) 25 fmt.Fprintf(w, `{ 26 "total_count": 3, 27 "network_configurations": [ 28 { 29 "id": "123456789ABCDEF", 30 "name": "Network Configuration One", 31 "compute_service": "actions", 32 "network_settings_ids": [ 33 "23456789ABDCEF1", 34 "3456789ABDCEF12" 35 ], 36 "created_on": "2024-04-09T17:30:15Z" 37 }, 38 { 39 "id": "456789ABDCEF123", 40 "name": "Network Configuration Two", 41 "compute_service": "none", 42 "network_settings_ids": [ 43 "56789ABDCEF1234", 44 "6789ABDCEF12345" 45 ], 46 "created_on": "2024-11-02T4:30:30Z" 47 }, 48 { 49 "id": "789ABDCEF123456", 50 "name": "Network Configuration Three", 51 "compute_service": "codespaces", 52 "network_settings_ids": [ 53 "56789ABDCEF1234", 54 "6789ABDCEF12345" 55 ], 56 "created_on": "2024-12-10T19:30:45Z" 57 } 58 ] 59 }`) 60 }) 61 62 ctx := context.Background() 63 64 opts := &ListOptions{Page: 1, PerPage: 3} 65 configurations, _, err := client.Organizations.ListNetworkConfigurations(ctx, "o", opts) 66 if err != nil { 67 t.Errorf("Organizations.ListNetworkConfigurations returned error %v", err) 68 } 69 want := &NetworkConfigurations{ 70 TotalCount: Ptr(int64(3)), 71 NetworkConfigurations: []*NetworkConfiguration{ 72 { 73 ID: Ptr("123456789ABCDEF"), 74 Name: Ptr("Network Configuration One"), 75 ComputeService: Ptr(ComputeService("actions")), 76 NetworkSettingsIDs: []string{ 77 "23456789ABDCEF1", 78 "3456789ABDCEF12", 79 }, 80 CreatedOn: &Timestamp{time.Date(2024, 4, 9, 17, 30, 15, 0, time.UTC)}, 81 }, 82 { 83 ID: Ptr("456789ABDCEF123"), 84 Name: Ptr("Network Configuration Two"), 85 ComputeService: Ptr(ComputeService("none")), 86 NetworkSettingsIDs: []string{ 87 "56789ABDCEF1234", 88 "6789ABDCEF12345", 89 }, 90 CreatedOn: &Timestamp{time.Date(2024, 11, 2, 4, 30, 30, 0, time.UTC)}, 91 }, 92 { 93 ID: Ptr("789ABDCEF123456"), 94 Name: Ptr("Network Configuration Three"), 95 ComputeService: Ptr(ComputeService("codespaces")), 96 NetworkSettingsIDs: []string{ 97 "56789ABDCEF1234", 98 "6789ABDCEF12345", 99 }, 100 CreatedOn: &Timestamp{time.Date(2024, 12, 10, 19, 30, 45, 0, time.UTC)}, 101 }, 102 }, 103 } 104 if !cmp.Equal(want, configurations) { 105 t.Errorf("Organizations.ListNetworkConfigurations mismatch (-want +got):\n%s", cmp.Diff(want, configurations)) 106 } 107 108 const methodName = "ListNetworkConfigurations" 109 testBadOptions(t, methodName, func() (err error) { 110 _, _, err = client.Organizations.ListNetworkConfigurations(ctx, "\no", opts) 111 return err 112 }) 113 114 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 115 got, resp, err := client.Organizations.ListNetworkConfigurations(ctx, "o", opts) 116 if got != nil { 117 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 118 } 119 return resp, err 120 }) 121 } 122 123 func TestOrganizationsService_CreateOrgsNetworkConfiguration(t *testing.T) { 124 t.Parallel() 125 client, mux, _ := setup(t) 126 127 mux.HandleFunc("/orgs/o/settings/network-configurations", func(w http.ResponseWriter, r *http.Request) { 128 testMethod(t, r, "POST") 129 fmt.Fprintf(w, `{ 130 "id": "456789ABDCEF123", 131 "name": "network-configuration-two", 132 "compute_service": "none", 133 "network_settings_ids": [ 134 "56789ABDCEF1234" 135 ], 136 "created_on": "2024-11-02T4:30:30Z" 137 }`) 138 }) 139 140 ctx := context.Background() 141 142 req := NetworkConfigurationRequest{ 143 Name: Ptr("network-configuration-two"), 144 ComputeService: Ptr(ComputeService("none")), 145 NetworkSettingsIDs: []string{ 146 "56789ABDCEF1234", 147 }, 148 } 149 150 configuration, _, err := client.Organizations.CreateNetworkConfiguration(ctx, "o", req) 151 if err != nil { 152 t.Errorf("Organizations.CreateNetworkConfiguration returned error %v", err) 153 } 154 155 want := &NetworkConfiguration{ 156 ID: Ptr("456789ABDCEF123"), 157 Name: Ptr("network-configuration-two"), 158 ComputeService: Ptr(ComputeService("none")), 159 NetworkSettingsIDs: []string{ 160 "56789ABDCEF1234", 161 }, 162 CreatedOn: &Timestamp{time.Date(2024, 11, 2, 4, 30, 30, 0, time.UTC)}, 163 } 164 165 if !cmp.Equal(want, configuration) { 166 t.Errorf("Organizations.CreateNetworkConfiguration mismatch (-want +got):\n%s", cmp.Diff(want, configuration)) 167 } 168 169 validationTests := []struct { 170 name string 171 request NetworkConfigurationRequest 172 want string 173 }{ 174 { 175 name: "invalid network configuration name length", 176 request: NetworkConfigurationRequest{ 177 Name: Ptr(""), 178 ComputeService: Ptr(ComputeService("none")), 179 NetworkSettingsIDs: []string{ 180 "56789ABDCEF1234", 181 }, 182 }, 183 want: "validation failed: must be between 1 and 100 characters", 184 }, 185 { 186 name: "invalid network configuration name", 187 // may only contain upper and lowercase letters a-z, numbers 0-9, '.', '-', and '_'. 188 request: NetworkConfigurationRequest{ 189 Name: Ptr("network configuration two"), 190 NetworkSettingsIDs: []string{ 191 "56789ABDCEF1234", 192 }, 193 }, 194 want: "validation failed: may only contain upper and lowercase letters a-z, numbers 0-9, '.', '-', and '_'", 195 }, 196 { 197 name: "invalid network settings ids", 198 request: NetworkConfigurationRequest{ 199 Name: Ptr("network-configuration-two"), 200 ComputeService: Ptr(ComputeService("none")), 201 NetworkSettingsIDs: []string{ 202 "56789ABDCEF1234", 203 "3456789ABDCEF12", 204 }, 205 }, 206 want: "validation failed: exactly one network settings id must be specified", 207 }, 208 { 209 name: "invalid compute service", 210 request: NetworkConfigurationRequest{ 211 Name: Ptr("network-configuration-two"), 212 ComputeService: Ptr(ComputeService("codespaces")), 213 NetworkSettingsIDs: []string{ 214 "56789ABDCEF1234", 215 }, 216 }, 217 want: "validation failed: compute service can only be one of: none, actions", 218 }, 219 } 220 221 for _, tc := range validationTests { 222 _, _, err := client.Organizations.CreateNetworkConfiguration(ctx, "o", tc.request) 223 if err == nil || err.Error() != tc.want { 224 t.Errorf("expected error to be %v, got %v", tc.want, err) 225 } 226 } 227 228 const methodName = "CreateNetworkConfiguration" 229 testBadOptions(t, methodName, func() (err error) { 230 _, _, err = client.Organizations.CreateNetworkConfiguration(ctx, "\no", req) 231 return err 232 }) 233 234 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 235 got, resp, err := client.Organizations.CreateNetworkConfiguration(ctx, "o", req) 236 if got != nil { 237 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 238 } 239 return resp, err 240 }) 241 } 242 243 func TestOrganizationsService_GetOrgsNetworkConfiguration(t *testing.T) { 244 t.Parallel() 245 client, mux, _ := setup(t) 246 247 mux.HandleFunc("/orgs/o/settings/network-configurations/789ABDCEF123456", func(w http.ResponseWriter, r *http.Request) { 248 testMethod(t, r, "GET") 249 fmt.Fprintf(w, `{ 250 "id": "789ABDCEF123456", 251 "name": "Network Configuration Three", 252 "compute_service": "codespaces", 253 "network_settings_ids": [ 254 "56789ABDCEF1234", 255 "6789ABDCEF12345" 256 ], 257 "created_on": "2024-12-10T19:30:45Z" 258 }`) 259 }) 260 261 ctx := context.Background() 262 263 configuration, _, err := client.Organizations.GetNetworkConfiguration(ctx, "o", "789ABDCEF123456") 264 if err != nil { 265 t.Errorf("Organizations.GetNetworkConfiguration returned error: %v", err) 266 } 267 want := &NetworkConfiguration{ 268 ID: Ptr("789ABDCEF123456"), 269 Name: Ptr("Network Configuration Three"), 270 ComputeService: Ptr(ComputeService("codespaces")), 271 NetworkSettingsIDs: []string{ 272 "56789ABDCEF1234", 273 "6789ABDCEF12345", 274 }, 275 CreatedOn: &Timestamp{time.Date(2024, 12, 10, 19, 30, 45, 0, time.UTC)}, 276 } 277 if !cmp.Equal(want, configuration) { 278 t.Errorf("Organizations.GetNetworkConfiguration mismatch (-want +got):\n%s", cmp.Diff(want, configuration)) 279 } 280 281 const methodName = "GetNetworkConfiguration" 282 testBadOptions(t, methodName, func() (err error) { 283 _, _, err = client.Organizations.GetNetworkConfiguration(ctx, "\no", "789ABDCEF123456") 284 return err 285 }) 286 287 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 288 got, resp, err := client.Organizations.GetNetworkConfiguration(ctx, "o", "789ABDCEF123456") 289 if got != nil { 290 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 291 } 292 return resp, err 293 }) 294 } 295 296 func TestOrganizationsService_UpdateOrgsNetworkConfiguration(t *testing.T) { 297 t.Parallel() 298 client, mux, _ := setup(t) 299 300 mux.HandleFunc("/orgs/o/settings/network-configurations/789ABDCEF123456", func(w http.ResponseWriter, r *http.Request) { 301 testMethod(t, r, "PATCH") 302 fmt.Fprintf(w, `{ 303 "id": "789ABDCEF123456", 304 "name": "Network Configuration Three Update", 305 "compute_service": "actions", 306 "network_settings_ids": [ 307 "56789ABDCEF1234", 308 "6789ABDCEF12345" 309 ], 310 "created_on": "2024-12-10T19:30:45Z" 311 }`) 312 }) 313 314 ctx := context.Background() 315 316 req := NetworkConfigurationRequest{ 317 Name: Ptr("network-configuration-three-update"), 318 ComputeService: Ptr(ComputeService("actions")), 319 NetworkSettingsIDs: []string{ 320 "56789ABDCEF1234", 321 }, 322 } 323 configuration, _, err := client.Organizations.UpdateNetworkConfiguration(ctx, "o", "789ABDCEF123456", req) 324 if err != nil { 325 t.Errorf("Organizations.UpdateNetworkConfiguration returned error: %v", err) 326 } 327 328 want := &NetworkConfiguration{ 329 ID: Ptr("789ABDCEF123456"), 330 Name: Ptr("Network Configuration Three Update"), 331 ComputeService: Ptr(ComputeService("actions")), 332 NetworkSettingsIDs: []string{ 333 "56789ABDCEF1234", 334 "6789ABDCEF12345", 335 }, 336 CreatedOn: &Timestamp{time.Date(2024, 12, 10, 19, 30, 45, 0, time.UTC)}, 337 } 338 if !cmp.Equal(want, configuration) { 339 t.Errorf("Organizations.UpdateNetworkConfiguration mismatch (-want +got):\n%s", cmp.Diff(want, configuration)) 340 } 341 342 validationTests := []struct { 343 name string 344 request NetworkConfigurationRequest 345 want string 346 }{ 347 { 348 name: "invalid network configuration name length", 349 request: NetworkConfigurationRequest{ 350 Name: Ptr(""), 351 ComputeService: Ptr(ComputeService("none")), 352 NetworkSettingsIDs: []string{ 353 "56789ABDCEF1234", 354 }, 355 }, 356 want: "validation failed: must be between 1 and 100 characters", 357 }, 358 { 359 name: "invalid network configuration name", 360 // may only contain upper and lowercase letters a-z, numbers 0-9, '.', '-', and '_'. 361 request: NetworkConfigurationRequest{ 362 Name: Ptr("network configuration three update"), 363 NetworkSettingsIDs: []string{ 364 "56789ABDCEF1234", 365 }, 366 }, 367 want: "validation failed: may only contain upper and lowercase letters a-z, numbers 0-9, '.', '-', and '_'", 368 }, 369 { 370 name: "invalid network settings ids", 371 request: NetworkConfigurationRequest{ 372 Name: Ptr("network-configuration-three-update"), 373 ComputeService: Ptr(ComputeService("none")), 374 NetworkSettingsIDs: []string{ 375 "56789ABDCEF1234", 376 "3456789ABDCEF12", 377 }, 378 }, 379 want: "validation failed: exactly one network settings id must be specified", 380 }, 381 { 382 name: "invalid compute service", 383 request: NetworkConfigurationRequest{ 384 Name: Ptr("network-configuration-three-update"), 385 ComputeService: Ptr(ComputeService("codespaces")), 386 NetworkSettingsIDs: []string{ 387 "56789ABDCEF1234", 388 }, 389 }, 390 want: "validation failed: compute service can only be one of: none, actions", 391 }, 392 } 393 394 for _, tc := range validationTests { 395 _, _, err := client.Organizations.UpdateNetworkConfiguration(ctx, "o", "789ABDCEF123456", tc.request) 396 if err == nil || err.Error() != tc.want { 397 t.Errorf("expected error to be %v, got %v", tc.want, err) 398 } 399 } 400 401 const methodName = "UpdateNetworkConfiguration" 402 testBadOptions(t, methodName, func() (err error) { 403 _, _, err = client.Organizations.UpdateNetworkConfiguration(ctx, "\no", "789ABDCEF123456", req) 404 return err 405 }) 406 407 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 408 got, resp, err := client.Organizations.UpdateNetworkConfiguration(ctx, "o", "789ABDCEF123456", req) 409 if got != nil { 410 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 411 } 412 return resp, err 413 }) 414 } 415 416 func TestOrganizationsService_DeleteOrgsNetworkConfiguration(t *testing.T) { 417 t.Parallel() 418 client, mux, _ := setup(t) 419 420 mux.HandleFunc("/orgs/o/settings/network-configurations/789ABDCEF123456", func(w http.ResponseWriter, r *http.Request) { 421 testMethod(t, r, "DELETE") 422 }) 423 424 ctx := context.Background() 425 _, err := client.Organizations.DeleteNetworkConfigurations(ctx, "o", "789ABDCEF123456") 426 if err != nil { 427 t.Errorf("Organizations.DeleteNetworkConfigurations returned error %v", err) 428 } 429 430 const methodName = "DeleteNetworkConfigurations" 431 testBadOptions(t, methodName, func() error { 432 _, err = client.Organizations.DeleteNetworkConfigurations(ctx, "\ne", "123456789ABCDEF") 433 return err 434 }) 435 436 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 437 return client.Organizations.DeleteNetworkConfigurations(ctx, "e", "123456789ABCDEF") 438 }) 439 } 440 441 func TestOrganizationsService_GetOrgsNetworkConfigurationResource(t *testing.T) { 442 t.Parallel() 443 client, mux, _ := setup(t) 444 445 mux.HandleFunc("/orgs/o/settings/network-settings/789ABDCEF123456", func(w http.ResponseWriter, r *http.Request) { 446 testMethod(t, r, "GET") 447 fmt.Fprintf(w, `{ 448 "id": "220F78DACB92BBFBC5E6F22DE1CCF52309D", 449 "network_configuration_id": "934E208B3EE0BD60CF5F752C426BFB53562", 450 "name": "my_network_settings", 451 "subnet_id": "/subscriptions/14839728-3ad9-43ab-bd2b-fa6ad0f75e2a/resourceGroups/my-rg/providers/Microsoft.Network/virtualNetworks/my-vnet/subnets/my-subnet", 452 "region": "germanywestcentral" 453 } 454 `) 455 }) 456 457 ctx := context.Background() 458 459 resource, _, err := client.Organizations.GetNetworkConfigurationResource(ctx, "o", "789ABDCEF123456") 460 if err != nil { 461 t.Errorf("Organizations.GetNetworkConfigurationResource returned error %v", err) 462 } 463 464 want := &NetworkSettingsResource{ 465 ID: Ptr("220F78DACB92BBFBC5E6F22DE1CCF52309D"), 466 Name: Ptr("my_network_settings"), 467 NetworkConfigurationID: Ptr("934E208B3EE0BD60CF5F752C426BFB53562"), 468 SubnetID: Ptr("/subscriptions/14839728-3ad9-43ab-bd2b-fa6ad0f75e2a/resourceGroups/my-rg/providers/Microsoft.Network/virtualNetworks/my-vnet/subnets/my-subnet"), 469 Region: Ptr("germanywestcentral"), 470 } 471 472 if !cmp.Equal(want, resource) { 473 t.Errorf("Organizations.GetNetworkConfigurationResource mismatch (-want +got):\n%s", cmp.Diff(want, resource)) 474 } 475 476 const methodName = "GetNetworkConfiguration" 477 testBadOptions(t, methodName, func() (err error) { 478 _, _, err = client.Organizations.GetNetworkConfigurationResource(ctx, "\no", "789ABDCEF123456") 479 return err 480 }) 481 482 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 483 got, resp, err := client.Organizations.GetNetworkConfigurationResource(ctx, "o", "789ABDCEF123456") 484 if got != nil { 485 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 486 } 487 return resp, err 488 }) 489 }