Skip to content

Commit 8a3634e

Browse files
authored
feat!: Add support for network-configurations endpoints for organization (#3511)
BREAKING CHANGE: `EnterpriseNetwork*` structs have been replaced with `Network*` structs.
1 parent 259cd22 commit 8a3634e

6 files changed

+1063
-299
lines changed

github/enterprise_network_configurations.go

+18-49
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,12 @@ import (
1010
"fmt"
1111
)
1212

13-
// ComputeService represents a hosted compute service the network configuration supports.
14-
type ComputeService string
15-
16-
const (
17-
ComputeServiceNone ComputeService = "none"
18-
ComputeServiceActions ComputeService = "actions"
19-
)
20-
21-
// EnterpriseNetworkConfiguration represents a hosted compute network configuration.
22-
type EnterpriseNetworkConfiguration struct {
23-
ID *string `json:"id,omitempty"`
24-
Name *string `json:"name,omitempty"`
25-
ComputeService *ComputeService `json:"compute_service,omitempty"`
26-
NetworkSettingsIDs []string `json:"network_settings_ids,omitempty"`
27-
CreatedOn *Timestamp `json:"created_on,omitempty"`
28-
}
29-
30-
// EnterpriseNetworkConfigurations represents a hosted compute network configurations.
31-
type EnterpriseNetworkConfigurations struct {
32-
TotalCount *int64 `json:"total_count,omitempty"`
33-
NetworkConfigurations []*EnterpriseNetworkConfiguration `json:"network_configurations,omitempty"`
34-
}
35-
36-
// EnterpriseNetworkSettingsResource represents a hosted compute network settings resource.
37-
type EnterpriseNetworkSettingsResource struct {
38-
ID *string `json:"id,omitempty"`
39-
Name *string `json:"name,omitempty"`
40-
NetworkConfigurationID *string `json:"network_configuration_id,omitempty"`
41-
SubnetID *string `json:"subnet_id,omitempty"`
42-
Region *string `json:"region,omitempty"`
43-
}
44-
45-
// EnterpriseNetworkConfigurationRequest represents a request to create or update a network configuration for an enterprise.
46-
type EnterpriseNetworkConfigurationRequest struct {
47-
Name *string `json:"name,omitempty"`
48-
ComputeService *ComputeService `json:"compute_service,omitempty"`
49-
NetworkSettingsIDs []string `json:"network_settings_ids,omitempty"`
50-
}
51-
5213
// ListEnterpriseNetworkConfigurations lists all hosted compute network configurations configured in an enterprise.
5314
//
5415
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/network-configurations#list-hosted-compute-network-configurations-for-an-enterprise
5516
//
5617
//meta:operation GET /enterprises/{enterprise}/network-configurations
57-
func (s *EnterpriseService) ListEnterpriseNetworkConfigurations(ctx context.Context, enterprise string, opts *ListOptions) (*EnterpriseNetworkConfigurations, *Response, error) {
18+
func (s *EnterpriseService) ListEnterpriseNetworkConfigurations(ctx context.Context, enterprise string, opts *ListOptions) (*NetworkConfigurations, *Response, error) {
5819
u := fmt.Sprintf("enterprises/%v/network-configurations", enterprise)
5920
u, err := addOptions(u, opts)
6021
if err != nil {
@@ -66,7 +27,7 @@ func (s *EnterpriseService) ListEnterpriseNetworkConfigurations(ctx context.Cont
6627
return nil, nil, err
6728
}
6829

69-
networks := &EnterpriseNetworkConfigurations{}
30+
networks := &NetworkConfigurations{}
7031
resp, err := s.client.Do(ctx, req, networks)
7132
if err != nil {
7233
return nil, resp, err
@@ -79,14 +40,18 @@ func (s *EnterpriseService) ListEnterpriseNetworkConfigurations(ctx context.Cont
7940
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/network-configurations#create-a-hosted-compute-network-configuration-for-an-enterprise
8041
//
8142
//meta:operation POST /enterprises/{enterprise}/network-configurations
82-
func (s *EnterpriseService) CreateEnterpriseNetworkConfiguration(ctx context.Context, enterprise string, createReq EnterpriseNetworkConfigurationRequest) (*EnterpriseNetworkConfiguration, *Response, error) {
43+
func (s *EnterpriseService) CreateEnterpriseNetworkConfiguration(ctx context.Context, enterprise string, createReq NetworkConfigurationRequest) (*NetworkConfiguration, *Response, error) {
44+
if err := validateNetworkConfigurationRequest(createReq); err != nil {
45+
return nil, nil, fmt.Errorf("validation failed: %w", err)
46+
}
47+
8348
u := fmt.Sprintf("enterprises/%v/network-configurations", enterprise)
8449
req, err := s.client.NewRequest("POST", u, createReq)
8550
if err != nil {
8651
return nil, nil, err
8752
}
8853

89-
network := &EnterpriseNetworkConfiguration{}
54+
network := &NetworkConfiguration{}
9055
resp, err := s.client.Do(ctx, req, network)
9156
if err != nil {
9257
return nil, resp, err
@@ -100,14 +65,14 @@ func (s *EnterpriseService) CreateEnterpriseNetworkConfiguration(ctx context.Con
10065
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/network-configurations#get-a-hosted-compute-network-configuration-for-an-enterprise
10166
//
10267
//meta:operation GET /enterprises/{enterprise}/network-configurations/{network_configuration_id}
103-
func (s *EnterpriseService) GetEnterpriseNetworkConfiguration(ctx context.Context, enterprise, networkID string) (*EnterpriseNetworkConfiguration, *Response, error) {
68+
func (s *EnterpriseService) GetEnterpriseNetworkConfiguration(ctx context.Context, enterprise, networkID string) (*NetworkConfiguration, *Response, error) {
10469
u := fmt.Sprintf("enterprises/%v/network-configurations/%v", enterprise, networkID)
10570
req, err := s.client.NewRequest("GET", u, nil)
10671
if err != nil {
10772
return nil, nil, err
10873
}
10974

110-
network := &EnterpriseNetworkConfiguration{}
75+
network := &NetworkConfiguration{}
11176
resp, err := s.client.Do(ctx, req, network)
11277
if err != nil {
11378
return nil, resp, err
@@ -120,14 +85,18 @@ func (s *EnterpriseService) GetEnterpriseNetworkConfiguration(ctx context.Contex
12085
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/network-configurations#update-a-hosted-compute-network-configuration-for-an-enterprise
12186
//
12287
//meta:operation PATCH /enterprises/{enterprise}/network-configurations/{network_configuration_id}
123-
func (s *EnterpriseService) UpdateEnterpriseNetworkConfiguration(ctx context.Context, enterprise, networkID string, updateReq EnterpriseNetworkConfigurationRequest) (*EnterpriseNetworkConfiguration, *Response, error) {
88+
func (s *EnterpriseService) UpdateEnterpriseNetworkConfiguration(ctx context.Context, enterprise, networkID string, updateReq NetworkConfigurationRequest) (*NetworkConfiguration, *Response, error) {
89+
if err := validateNetworkConfigurationRequest(updateReq); err != nil {
90+
return nil, nil, fmt.Errorf("validation failed: %w", err)
91+
}
92+
12493
u := fmt.Sprintf("enterprises/%v/network-configurations/%v", enterprise, networkID)
12594
req, err := s.client.NewRequest("PATCH", u, updateReq)
12695
if err != nil {
12796
return nil, nil, err
12897
}
12998

130-
network := &EnterpriseNetworkConfiguration{}
99+
network := &NetworkConfiguration{}
131100
resp, err := s.client.Do(ctx, req, network)
132101
if err != nil {
133102
return nil, resp, err
@@ -154,14 +123,14 @@ func (s *EnterpriseService) DeleteEnterpriseNetworkConfiguration(ctx context.Con
154123
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/network-configurations#get-a-hosted-compute-network-settings-resource-for-an-enterprise
155124
//
156125
//meta:operation GET /enterprises/{enterprise}/network-settings/{network_settings_id}
157-
func (s *EnterpriseService) GetEnterpriseNetworkSettingsResource(ctx context.Context, enterprise, networkID string) (*EnterpriseNetworkSettingsResource, *Response, error) {
126+
func (s *EnterpriseService) GetEnterpriseNetworkSettingsResource(ctx context.Context, enterprise, networkID string) (*NetworkSettingsResource, *Response, error) {
158127
u := fmt.Sprintf("enterprises/%v/network-settings/%v", enterprise, networkID)
159128
req, err := s.client.NewRequest("GET", u, nil)
160129
if err != nil {
161130
return nil, nil, err
162131
}
163132

164-
resource := &EnterpriseNetworkSettingsResource{}
133+
resource := &NetworkSettingsResource{}
165134
resp, err := s.client.Do(ctx, req, resource)
166135
if err != nil {
167136
return nil, resp, err

github/enterprise_network_configurations_test.go

+98-28
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestEnterpriseService_ListEnterpriseNetworkConfigurations(t *testing.T) {
2727
"network_configurations": [
2828
{
2929
"id": "123456789ABCDEF",
30-
"name": "configuration_one",
30+
"name": "configuration one",
3131
"compute_service": "actions",
3232
"network_settings_ids": [
3333
"23456789ABDCEF1",
@@ -37,7 +37,7 @@ func TestEnterpriseService_ListEnterpriseNetworkConfigurations(t *testing.T) {
3737
},
3838
{
3939
"id": "456789ABDCEF123",
40-
"name": "configuration_two",
40+
"name": "configuration two",
4141
"compute_service": "none",
4242
"network_settings_ids": [
4343
"56789ABDCEF1234",
@@ -57,19 +57,19 @@ func TestEnterpriseService_ListEnterpriseNetworkConfigurations(t *testing.T) {
5757
t.Errorf("Enterprise.ListEnterpriseNetworkConfigurations returned error: %v", err)
5858
}
5959

60-
want := &EnterpriseNetworkConfigurations{
60+
want := &NetworkConfigurations{
6161
TotalCount: Ptr(int64(2)),
62-
NetworkConfigurations: []*EnterpriseNetworkConfiguration{
62+
NetworkConfigurations: []*NetworkConfiguration{
6363
{
6464
ID: Ptr("123456789ABCDEF"),
65-
Name: Ptr("configuration_one"),
65+
Name: Ptr("configuration one"),
6666
ComputeService: Ptr(ComputeService("actions")),
6767
NetworkSettingsIDs: []string{"23456789ABDCEF1", "3456789ABDCEF12"},
6868
CreatedOn: &Timestamp{time.Date(2024, 4, 9, 17, 30, 15, 0, time.UTC)},
6969
},
7070
{
7171
ID: Ptr("456789ABDCEF123"),
72-
Name: Ptr("configuration_two"),
72+
Name: Ptr("configuration two"),
7373
ComputeService: Ptr(ComputeService("none")),
7474
NetworkSettingsIDs: []string{"56789ABDCEF1234", "6789ABDCEF12345"},
7575
CreatedOn: &Timestamp{time.Date(2024, 11, 2, 12, 30, 30, 0, time.UTC)},
@@ -103,42 +103,77 @@ func TestEnterpriseService_CreateEnterpriseNetworkConfiguration(t *testing.T) {
103103
testMethod(t, r, "POST")
104104
fmt.Fprintf(w, `{
105105
"id": "123456789ABCDEF",
106-
"name": "configuration_one",
106+
"name": "configuration one",
107107
"compute_service": "actions",
108108
"network_settings_ids": [
109-
"23456789ABDCEF1",
110-
"3456789ABDCEF12"
109+
"23456789ABDCEF1"
111110
],
112111
"created_on": "2024-04-09T17:30:15Z"
113112
}`)
114113
})
115114

116115
ctx := context.Background()
117116

118-
req := EnterpriseNetworkConfigurationRequest{
119-
Name: Ptr("configuration_one"),
117+
req := NetworkConfigurationRequest{
118+
Name: Ptr("configuration-one"),
120119
ComputeService: Ptr(ComputeService("actions")),
121120
NetworkSettingsIDs: []string{
122121
"23456789ABDCEF1",
123-
"3456789ABDCEF12",
124122
},
125123
}
126124
configuration, _, err := client.Enterprise.CreateEnterpriseNetworkConfiguration(ctx, "e", req)
127125
if err != nil {
128126
t.Errorf("Enterprise.CreateEnterpriseNetworkConfiguration returned error: %v", err)
129127
}
130128

131-
want := &EnterpriseNetworkConfiguration{
129+
want := &NetworkConfiguration{
132130
ID: Ptr("123456789ABCDEF"),
133-
Name: Ptr("configuration_one"),
131+
Name: Ptr("configuration one"),
134132
ComputeService: Ptr(ComputeService("actions")),
135-
NetworkSettingsIDs: []string{"23456789ABDCEF1", "3456789ABDCEF12"},
133+
NetworkSettingsIDs: []string{"23456789ABDCEF1"},
136134
CreatedOn: &Timestamp{time.Date(2024, 4, 9, 17, 30, 15, 0, time.UTC)},
137135
}
138136
if !cmp.Equal(configuration, want) {
139137
t.Errorf("Enterprise.CreateEnterpriseNetworkConfiguration mismatch (-want +got):\n%s", cmp.Diff(want, configuration))
140138
}
141139

140+
validationTest := []struct {
141+
name string
142+
request NetworkConfigurationRequest
143+
want string
144+
}{
145+
{
146+
name: "invalid network settings id",
147+
request: NetworkConfigurationRequest{
148+
Name: Ptr(""),
149+
NetworkSettingsIDs: []string{"56789ABDCEF1234"},
150+
},
151+
want: "validation failed: must be between 1 and 100 characters",
152+
},
153+
{
154+
name: "invalid network settings id",
155+
request: NetworkConfigurationRequest{
156+
Name: Ptr("updated-configuration-one"),
157+
},
158+
want: "validation failed: exactly one network settings id must be specified",
159+
},
160+
{
161+
name: "invalid compute service",
162+
request: NetworkConfigurationRequest{
163+
Name: Ptr("updated-configuration-one"),
164+
ComputeService: Ptr(ComputeService("")),
165+
NetworkSettingsIDs: []string{"56789ABDCEF1234"},
166+
},
167+
want: "validation failed: compute service can only be one of: none, actions",
168+
},
169+
}
170+
for _, tc := range validationTest {
171+
_, _, err = client.Enterprise.CreateEnterpriseNetworkConfiguration(ctx, "e", tc.request)
172+
if err == nil || err.Error() != tc.want {
173+
t.Errorf("expected error to be %v, got %v", tc.want, err)
174+
}
175+
}
176+
142177
const methodName = "CreateEnterpriseNetworkConfiguration"
143178
testBadOptions(t, methodName, func() error {
144179
_, _, err = client.Enterprise.CreateEnterpriseNetworkConfiguration(ctx, "\ne", req)
@@ -162,7 +197,7 @@ func TestEnterpriseService_GetEnterpriseNetworkConfiguration(t *testing.T) {
162197
testMethod(t, r, "GET")
163198
fmt.Fprintf(w, `{
164199
"id": "123456789ABCDEF",
165-
"name": "configuration_one",
200+
"name": "configuration one",
166201
"compute_service": "actions",
167202
"network_settings_ids": [
168203
"23456789ABDCEF1",
@@ -178,9 +213,9 @@ func TestEnterpriseService_GetEnterpriseNetworkConfiguration(t *testing.T) {
178213
t.Errorf("Enterprise.GetEnterpriseNetworkConfiguration returned err: %v", err)
179214
}
180215

181-
want := &EnterpriseNetworkConfiguration{
216+
want := &NetworkConfiguration{
182217
ID: Ptr("123456789ABCDEF"),
183-
Name: Ptr("configuration_one"),
218+
Name: Ptr("configuration one"),
184219
ComputeService: Ptr(ComputeService("actions")),
185220
NetworkSettingsIDs: []string{"23456789ABDCEF1", "3456789ABDCEF12"},
186221
CreatedOn: &Timestamp{time.Date(2024, 12, 10, 19, 00, 15, 0, time.UTC)},
@@ -212,22 +247,20 @@ func TestEnterpriseService_UpdateEnterpriseNetworkConfiguration(t *testing.T) {
212247
testMethod(t, r, "PATCH")
213248
fmt.Fprintf(w, `{
214249
"id": "123456789ABCDEF",
215-
"name": "updated_configuration_one",
250+
"name": "updated configuration one",
216251
"compute_service": "none",
217252
"network_settings_ids": [
218-
"456789ABDCEF123",
219-
"56789ABDCEF1234"
253+
"456789ABDCEF123"
220254
],
221255
"created_on": "2024-12-10T19:00:15Z"
222256
}`)
223257
})
224258

225259
ctx := context.Background()
226-
req := EnterpriseNetworkConfigurationRequest{
227-
Name: Ptr("updated_configuration_one"),
260+
req := NetworkConfigurationRequest{
261+
Name: Ptr("updated-configuration-one"),
228262
NetworkSettingsIDs: []string{
229263
"456789ABDCEF123",
230-
"56789ABDCEF1234",
231264
},
232265
ComputeService: Ptr(ComputeService("none")),
233266
}
@@ -236,17 +269,54 @@ func TestEnterpriseService_UpdateEnterpriseNetworkConfiguration(t *testing.T) {
236269
t.Errorf("Enterprise.UpdateEnterpriseNetworkConfiguration returned error %v", err)
237270
}
238271

239-
want := &EnterpriseNetworkConfiguration{
272+
want := &NetworkConfiguration{
240273
ID: Ptr("123456789ABCDEF"),
241-
Name: Ptr("updated_configuration_one"),
274+
Name: Ptr("updated configuration one"),
242275
ComputeService: Ptr(ComputeService("none")),
243-
NetworkSettingsIDs: []string{"456789ABDCEF123", "56789ABDCEF1234"},
276+
NetworkSettingsIDs: []string{"456789ABDCEF123"},
244277
CreatedOn: &Timestamp{time.Date(2024, 12, 10, 19, 00, 15, 0, time.UTC)},
245278
}
246279
if !cmp.Equal(configuration, want) {
247280
t.Errorf("Enterprise.UpdateEnterpriseNetworkConfiguration mismatch (-want +get)\n%s", cmp.Diff(want, configuration))
248281
}
249282

283+
validationTest := []struct {
284+
name string
285+
request NetworkConfigurationRequest
286+
want string
287+
}{
288+
{
289+
name: "invalid network settings id",
290+
request: NetworkConfigurationRequest{
291+
Name: Ptr(""),
292+
NetworkSettingsIDs: []string{"56789ABDCEF1234"},
293+
},
294+
want: "validation failed: must be between 1 and 100 characters",
295+
},
296+
{
297+
name: "invalid network settings id",
298+
request: NetworkConfigurationRequest{
299+
Name: Ptr("updated-configuration-one"),
300+
},
301+
want: "validation failed: exactly one network settings id must be specified",
302+
},
303+
{
304+
name: "invalid compute service",
305+
request: NetworkConfigurationRequest{
306+
Name: Ptr("updated-configuration-one"),
307+
ComputeService: Ptr(ComputeService("something")),
308+
NetworkSettingsIDs: []string{"56789ABDCEF1234"},
309+
},
310+
want: "validation failed: compute service can only be one of: none, actions",
311+
},
312+
}
313+
for _, tc := range validationTest {
314+
_, _, err = client.Enterprise.UpdateEnterpriseNetworkConfiguration(ctx, "e", "123456789ABCDEF", tc.request)
315+
if err == nil || err.Error() != tc.want {
316+
t.Errorf("expected error to be %v, got %v", tc.want, err)
317+
}
318+
}
319+
250320
const methodName = "UpdateEnterpriseNetworkConfiguration"
251321
testBadOptions(t, methodName, func() error {
252322
_, _, err = client.Enterprise.UpdateEnterpriseNetworkConfiguration(ctx, "\ne", "123456789ABCDEF", req)
@@ -308,7 +378,7 @@ func TestEnterpriseService_GetEnterpriseNetworkSettingsResource(t *testing.T) {
308378
t.Errorf("Enterprise.GetEnterpriseNetworkSettingsResource returned error %v", err)
309379
}
310380

311-
want := &EnterpriseNetworkSettingsResource{
381+
want := &NetworkSettingsResource{
312382
ID: Ptr("220F78DACB92BBFBC5E6F22DE1CCF52309D"),
313383
NetworkConfigurationID: Ptr("934E208B3EE0BD60CF5F752C426BFB53562"),
314384
Name: Ptr("my_network_settings"),

0 commit comments

Comments
 (0)