+
Skip to content

feat!: Add support for network-configurations endpoints for organization #3511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 18 additions & 49 deletions github/enterprise_network_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,12 @@ import (
"fmt"
)

// ComputeService represents a hosted compute service the network configuration supports.
type ComputeService string

const (
ComputeServiceNone ComputeService = "none"
ComputeServiceActions ComputeService = "actions"
)

// EnterpriseNetworkConfiguration represents a hosted compute network configuration.
type EnterpriseNetworkConfiguration struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
ComputeService *ComputeService `json:"compute_service,omitempty"`
NetworkSettingsIDs []string `json:"network_settings_ids,omitempty"`
CreatedOn *Timestamp `json:"created_on,omitempty"`
}

// EnterpriseNetworkConfigurations represents a hosted compute network configurations.
type EnterpriseNetworkConfigurations struct {
TotalCount *int64 `json:"total_count,omitempty"`
NetworkConfigurations []*EnterpriseNetworkConfiguration `json:"network_configurations,omitempty"`
}

// EnterpriseNetworkSettingsResource represents a hosted compute network settings resource.
type EnterpriseNetworkSettingsResource struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
NetworkConfigurationID *string `json:"network_configuration_id,omitempty"`
SubnetID *string `json:"subnet_id,omitempty"`
Region *string `json:"region,omitempty"`
}

// EnterpriseNetworkConfigurationRequest represents a request to create or update a network configuration for an enterprise.
type EnterpriseNetworkConfigurationRequest struct {
Name *string `json:"name,omitempty"`
ComputeService *ComputeService `json:"compute_service,omitempty"`
NetworkSettingsIDs []string `json:"network_settings_ids,omitempty"`
}

// ListEnterpriseNetworkConfigurations lists all hosted compute network configurations configured in an enterprise.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/network-configurations#list-hosted-compute-network-configurations-for-an-enterprise
//
//meta:operation GET /enterprises/{enterprise}/network-configurations
func (s *EnterpriseService) ListEnterpriseNetworkConfigurations(ctx context.Context, enterprise string, opts *ListOptions) (*EnterpriseNetworkConfigurations, *Response, error) {
func (s *EnterpriseService) ListEnterpriseNetworkConfigurations(ctx context.Context, enterprise string, opts *ListOptions) (*NetworkConfigurations, *Response, error) {
u := fmt.Sprintf("enterprises/%v/network-configurations", enterprise)
u, err := addOptions(u, opts)
if err != nil {
Expand All @@ -66,7 +27,7 @@ func (s *EnterpriseService) ListEnterpriseNetworkConfigurations(ctx context.Cont
return nil, nil, err
}

networks := &EnterpriseNetworkConfigurations{}
networks := &NetworkConfigurations{}
resp, err := s.client.Do(ctx, req, networks)
if err != nil {
return nil, resp, err
Expand All @@ -79,14 +40,18 @@ func (s *EnterpriseService) ListEnterpriseNetworkConfigurations(ctx context.Cont
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/network-configurations#create-a-hosted-compute-network-configuration-for-an-enterprise
//
//meta:operation POST /enterprises/{enterprise}/network-configurations
func (s *EnterpriseService) CreateEnterpriseNetworkConfiguration(ctx context.Context, enterprise string, createReq EnterpriseNetworkConfigurationRequest) (*EnterpriseNetworkConfiguration, *Response, error) {
func (s *EnterpriseService) CreateEnterpriseNetworkConfiguration(ctx context.Context, enterprise string, createReq NetworkConfigurationRequest) (*NetworkConfiguration, *Response, error) {
if err := validateNetworkConfigurationRequest(createReq); err != nil {
return nil, nil, fmt.Errorf("validation failed: %w", err)
}

u := fmt.Sprintf("enterprises/%v/network-configurations", enterprise)
req, err := s.client.NewRequest("POST", u, createReq)
if err != nil {
return nil, nil, err
}

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

network := &EnterpriseNetworkConfiguration{}
network := &NetworkConfiguration{}
resp, err := s.client.Do(ctx, req, network)
if err != nil {
return nil, resp, err
Expand All @@ -120,14 +85,18 @@ func (s *EnterpriseService) GetEnterpriseNetworkConfiguration(ctx context.Contex
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/enterprise-admin/network-configurations#update-a-hosted-compute-network-configuration-for-an-enterprise
//
//meta:operation PATCH /enterprises/{enterprise}/network-configurations/{network_configuration_id}
func (s *EnterpriseService) UpdateEnterpriseNetworkConfiguration(ctx context.Context, enterprise, networkID string, updateReq EnterpriseNetworkConfigurationRequest) (*EnterpriseNetworkConfiguration, *Response, error) {
func (s *EnterpriseService) UpdateEnterpriseNetworkConfiguration(ctx context.Context, enterprise, networkID string, updateReq NetworkConfigurationRequest) (*NetworkConfiguration, *Response, error) {
if err := validateNetworkConfigurationRequest(updateReq); err != nil {
return nil, nil, fmt.Errorf("validation failed: %w", err)
}

u := fmt.Sprintf("enterprises/%v/network-configurations/%v", enterprise, networkID)
req, err := s.client.NewRequest("PATCH", u, updateReq)
if err != nil {
return nil, nil, err
}

network := &EnterpriseNetworkConfiguration{}
network := &NetworkConfiguration{}
resp, err := s.client.Do(ctx, req, network)
if err != nil {
return nil, resp, err
Expand All @@ -154,14 +123,14 @@ func (s *EnterpriseService) DeleteEnterpriseNetworkConfiguration(ctx context.Con
// 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
//
//meta:operation GET /enterprises/{enterprise}/network-settings/{network_settings_id}
func (s *EnterpriseService) GetEnterpriseNetworkSettingsResource(ctx context.Context, enterprise, networkID string) (*EnterpriseNetworkSettingsResource, *Response, error) {
func (s *EnterpriseService) GetEnterpriseNetworkSettingsResource(ctx context.Context, enterprise, networkID string) (*NetworkSettingsResource, *Response, error) {
u := fmt.Sprintf("enterprises/%v/network-settings/%v", enterprise, networkID)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

resource := &EnterpriseNetworkSettingsResource{}
resource := &NetworkSettingsResource{}
resp, err := s.client.Do(ctx, req, resource)
if err != nil {
return nil, resp, err
Expand Down
126 changes: 98 additions & 28 deletions github/enterprise_network_configurations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestEnterpriseService_ListEnterpriseNetworkConfigurations(t *testing.T) {
"network_configurations": [
{
"id": "123456789ABCDEF",
"name": "configuration_one",
"name": "configuration one",
"compute_service": "actions",
"network_settings_ids": [
"23456789ABDCEF1",
Expand All @@ -37,7 +37,7 @@ func TestEnterpriseService_ListEnterpriseNetworkConfigurations(t *testing.T) {
},
{
"id": "456789ABDCEF123",
"name": "configuration_two",
"name": "configuration two",
"compute_service": "none",
"network_settings_ids": [
"56789ABDCEF1234",
Expand All @@ -57,19 +57,19 @@ func TestEnterpriseService_ListEnterpriseNetworkConfigurations(t *testing.T) {
t.Errorf("Enterprise.ListEnterpriseNetworkConfigurations returned error: %v", err)
}

want := &EnterpriseNetworkConfigurations{
want := &NetworkConfigurations{
TotalCount: Ptr(int64(2)),
NetworkConfigurations: []*EnterpriseNetworkConfiguration{
NetworkConfigurations: []*NetworkConfiguration{
{
ID: Ptr("123456789ABCDEF"),
Name: Ptr("configuration_one"),
Name: Ptr("configuration one"),
ComputeService: Ptr(ComputeService("actions")),
NetworkSettingsIDs: []string{"23456789ABDCEF1", "3456789ABDCEF12"},
CreatedOn: &Timestamp{time.Date(2024, 4, 9, 17, 30, 15, 0, time.UTC)},
},
{
ID: Ptr("456789ABDCEF123"),
Name: Ptr("configuration_two"),
Name: Ptr("configuration two"),
ComputeService: Ptr(ComputeService("none")),
NetworkSettingsIDs: []string{"56789ABDCEF1234", "6789ABDCEF12345"},
CreatedOn: &Timestamp{time.Date(2024, 11, 2, 12, 30, 30, 0, time.UTC)},
Expand Down Expand Up @@ -103,42 +103,77 @@ func TestEnterpriseService_CreateEnterpriseNetworkConfiguration(t *testing.T) {
testMethod(t, r, "POST")
fmt.Fprintf(w, `{
"id": "123456789ABCDEF",
"name": "configuration_one",
"name": "configuration one",
"compute_service": "actions",
"network_settings_ids": [
"23456789ABDCEF1",
"3456789ABDCEF12"
"23456789ABDCEF1"
],
"created_on": "2024-04-09T17:30:15Z"
}`)
})

ctx := context.Background()

req := EnterpriseNetworkConfigurationRequest{
Name: Ptr("configuration_one"),
req := NetworkConfigurationRequest{
Name: Ptr("configuration-one"),
ComputeService: Ptr(ComputeService("actions")),
NetworkSettingsIDs: []string{
"23456789ABDCEF1",
"3456789ABDCEF12",
},
}
configuration, _, err := client.Enterprise.CreateEnterpriseNetworkConfiguration(ctx, "e", req)
if err != nil {
t.Errorf("Enterprise.CreateEnterpriseNetworkConfiguration returned error: %v", err)
}

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

validationTest := []struct {
name string
request NetworkConfigurationRequest
want string
}{
{
name: "invalid network settings id",
request: NetworkConfigurationRequest{
Name: Ptr(""),
NetworkSettingsIDs: []string{"56789ABDCEF1234"},
},
want: "validation failed: must be between 1 and 100 characters",
},
{
name: "invalid network settings id",
request: NetworkConfigurationRequest{
Name: Ptr("updated-configuration-one"),
},
want: "validation failed: exactly one network settings id must be specified",
},
{
name: "invalid compute service",
request: NetworkConfigurationRequest{
Name: Ptr("updated-configuration-one"),
ComputeService: Ptr(ComputeService("")),
NetworkSettingsIDs: []string{"56789ABDCEF1234"},
},
want: "validation failed: compute service can only be one of: none, actions",
},
}
for _, tc := range validationTest {
_, _, err = client.Enterprise.CreateEnterpriseNetworkConfiguration(ctx, "e", tc.request)
if err == nil || err.Error() != tc.want {
t.Errorf("expected error to be %v, got %v", tc.want, err)
}
}

const methodName = "CreateEnterpriseNetworkConfiguration"
testBadOptions(t, methodName, func() error {
_, _, err = client.Enterprise.CreateEnterpriseNetworkConfiguration(ctx, "\ne", req)
Expand All @@ -162,7 +197,7 @@ func TestEnterpriseService_GetEnterpriseNetworkConfiguration(t *testing.T) {
testMethod(t, r, "GET")
fmt.Fprintf(w, `{
"id": "123456789ABCDEF",
"name": "configuration_one",
"name": "configuration one",
"compute_service": "actions",
"network_settings_ids": [
"23456789ABDCEF1",
Expand All @@ -178,9 +213,9 @@ func TestEnterpriseService_GetEnterpriseNetworkConfiguration(t *testing.T) {
t.Errorf("Enterprise.GetEnterpriseNetworkConfiguration returned err: %v", err)
}

want := &EnterpriseNetworkConfiguration{
want := &NetworkConfiguration{
ID: Ptr("123456789ABCDEF"),
Name: Ptr("configuration_one"),
Name: Ptr("configuration one"),
ComputeService: Ptr(ComputeService("actions")),
NetworkSettingsIDs: []string{"23456789ABDCEF1", "3456789ABDCEF12"},
CreatedOn: &Timestamp{time.Date(2024, 12, 10, 19, 00, 15, 0, time.UTC)},
Expand Down Expand Up @@ -212,22 +247,20 @@ func TestEnterpriseService_UpdateEnterpriseNetworkConfiguration(t *testing.T) {
testMethod(t, r, "PATCH")
fmt.Fprintf(w, `{
"id": "123456789ABCDEF",
"name": "updated_configuration_one",
"name": "updated configuration one",
"compute_service": "none",
"network_settings_ids": [
"456789ABDCEF123",
"56789ABDCEF1234"
"456789ABDCEF123"
],
"created_on": "2024-12-10T19:00:15Z"
}`)
})

ctx := context.Background()
req := EnterpriseNetworkConfigurationRequest{
Name: Ptr("updated_configuration_one"),
req := NetworkConfigurationRequest{
Name: Ptr("updated-configuration-one"),
NetworkSettingsIDs: []string{
"456789ABDCEF123",
"56789ABDCEF1234",
},
ComputeService: Ptr(ComputeService("none")),
}
Expand All @@ -236,17 +269,54 @@ func TestEnterpriseService_UpdateEnterpriseNetworkConfiguration(t *testing.T) {
t.Errorf("Enterprise.UpdateEnterpriseNetworkConfiguration returned error %v", err)
}

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

validationTest := []struct {
name string
request NetworkConfigurationRequest
want string
}{
{
name: "invalid network settings id",
request: NetworkConfigurationRequest{
Name: Ptr(""),
NetworkSettingsIDs: []string{"56789ABDCEF1234"},
},
want: "validation failed: must be between 1 and 100 characters",
},
{
name: "invalid network settings id",
request: NetworkConfigurationRequest{
Name: Ptr("updated-configuration-one"),
},
want: "validation failed: exactly one network settings id must be specified",
},
{
name: "invalid compute service",
request: NetworkConfigurationRequest{
Name: Ptr("updated-configuration-one"),
ComputeService: Ptr(ComputeService("something")),
NetworkSettingsIDs: []string{"56789ABDCEF1234"},
},
want: "validation failed: compute service can only be one of: none, actions",
},
}
for _, tc := range validationTest {
_, _, err = client.Enterprise.UpdateEnterpriseNetworkConfiguration(ctx, "e", "123456789ABCDEF", tc.request)
if err == nil || err.Error() != tc.want {
t.Errorf("expected error to be %v, got %v", tc.want, err)
}
}

const methodName = "UpdateEnterpriseNetworkConfiguration"
testBadOptions(t, methodName, func() error {
_, _, err = client.Enterprise.UpdateEnterpriseNetworkConfiguration(ctx, "\ne", "123456789ABCDEF", req)
Expand Down Expand Up @@ -308,7 +378,7 @@ func TestEnterpriseService_GetEnterpriseNetworkSettingsResource(t *testing.T) {
t.Errorf("Enterprise.GetEnterpriseNetworkSettingsResource returned error %v", err)
}

want := &EnterpriseNetworkSettingsResource{
want := &NetworkSettingsResource{
ID: Ptr("220F78DACB92BBFBC5E6F22DE1CCF52309D"),
NetworkConfigurationID: Ptr("934E208B3EE0BD60CF5F752C426BFB53562"),
Name: Ptr("my_network_settings"),
Expand Down
Loading
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载