github.com/google/go-github/v70@v70.0.0/github/orgs_network_configurations.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 "errors" 11 "fmt" 12 "regexp" 13 ) 14 15 // ComputeService represents a hosted compute service the network configuration supports. 16 type ComputeService string 17 18 const ( 19 ComputeServiceNone ComputeService = "none" 20 ComputeServiceActions ComputeService = "actions" 21 ComputeServiceCodespaces ComputeService = "codespaces" 22 ) 23 24 // NetworkConfigurations represents a hosted compute network configuration. This type is identical 25 // for enterprise and organization endpoints. 26 type NetworkConfigurations struct { 27 TotalCount *int64 `json:"total_count,omitempty"` 28 NetworkConfigurations []*NetworkConfiguration `json:"network_configurations,omitempty"` 29 } 30 31 // NetworkConfiguration represents a hosted compute network configurations. This type is identical 32 // for enterprise and organization endpoints. 33 type NetworkConfiguration struct { 34 ID *string `json:"id,omitempty"` 35 Name *string `json:"name,omitempty"` 36 ComputeService *ComputeService `json:"compute_service,omitempty"` 37 NetworkSettingsIDs []string `json:"network_settings_ids,omitempty"` 38 CreatedOn *Timestamp `json:"created_on"` 39 } 40 41 // NetworkSettingsResource represents a hosted compute network settings resource. This type is identical 42 // for enterprise and organization endpoints. 43 type NetworkSettingsResource struct { 44 ID *string `json:"id,omitempty"` 45 NetworkConfigurationID *string `json:"network_configuration_id,omitempty"` 46 Name *string `json:"name,omitempty"` 47 SubnetID *string `json:"subnet_id,omitempty"` 48 Region *string `json:"region,omitempty"` 49 } 50 51 func validateComputeService(compute *ComputeService) error { 52 if compute == nil { 53 return nil 54 } 55 if *compute != ComputeServiceNone && *compute != ComputeServiceActions { 56 return errors.New("compute service can only be one of: none, actions") 57 } 58 return nil 59 } 60 61 var validNetworkNameRE = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`) 62 63 func validateNetworkName(name string) error { 64 if len(name) < 1 || len(name) > 100 { 65 return errors.New("must be between 1 and 100 characters") 66 } 67 if !validNetworkNameRE.MatchString(name) { 68 return errors.New("may only contain upper and lowercase letters a-z, numbers 0-9, '.', '-', and '_'") 69 } 70 return nil 71 } 72 73 func validateNetworkSettingsID(settingsID []string) error { 74 if len(settingsID) != 1 { 75 return errors.New("exactly one network settings id must be specified") 76 } 77 return nil 78 } 79 80 func validateNetworkConfigurationRequest(req NetworkConfigurationRequest) error { 81 networkName := req.GetName() 82 if err := validateNetworkName(networkName); err != nil { 83 return err 84 } 85 86 computeService := req.GetComputeService() 87 if err := validateComputeService(computeService); err != nil { 88 return err 89 } 90 91 networkIDs := req.NetworkSettingsIDs 92 if err := validateNetworkSettingsID(networkIDs); err != nil { 93 return err 94 } 95 return nil 96 } 97 98 // NetworkConfigurationRequest represents a request to create or update a network configuration for an organization. 99 type NetworkConfigurationRequest struct { 100 Name *string `json:"name,omitempty"` 101 ComputeService *ComputeService `json:"compute_service,omitempty"` 102 NetworkSettingsIDs []string `json:"network_settings_ids,omitempty"` 103 } 104 105 // ListNetworkConfigurations lists all hosted compute network configurations configured in an organization. 106 // 107 // GitHub API docs: https://docs.github.com/rest/orgs/network-configurations#list-hosted-compute-network-configurations-for-an-organization 108 // 109 //meta:operation GET /orgs/{org}/settings/network-configurations 110 func (s *OrganizationsService) ListNetworkConfigurations(ctx context.Context, org string, opts *ListOptions) (*NetworkConfigurations, *Response, error) { 111 u := fmt.Sprintf("orgs/%v/settings/network-configurations", org) 112 u, err := addOptions(u, opts) 113 if err != nil { 114 return nil, nil, err 115 } 116 117 req, err := s.client.NewRequest("GET", u, nil) 118 if err != nil { 119 return nil, nil, err 120 } 121 122 configurations := &NetworkConfigurations{} 123 resp, err := s.client.Do(ctx, req, configurations) 124 if err != nil { 125 return nil, resp, err 126 } 127 return configurations, resp, nil 128 } 129 130 // CreateNetworkConfiguration creates a hosted compute network configuration for an organization. 131 // 132 // GitHub API docs: https://docs.github.com/rest/orgs/network-configurations#create-a-hosted-compute-network-configuration-for-an-organization 133 // 134 //meta:operation POST /orgs/{org}/settings/network-configurations 135 func (s *OrganizationsService) CreateNetworkConfiguration(ctx context.Context, org string, createReq NetworkConfigurationRequest) (*NetworkConfiguration, *Response, error) { 136 if err := validateNetworkConfigurationRequest(createReq); err != nil { 137 return nil, nil, fmt.Errorf("validation failed: %w", err) 138 } 139 140 u := fmt.Sprintf("orgs/%v/settings/network-configurations", org) 141 req, err := s.client.NewRequest("POST", u, createReq) 142 if err != nil { 143 return nil, nil, err 144 } 145 146 configuration := &NetworkConfiguration{} 147 resp, err := s.client.Do(ctx, req, configuration) 148 if err != nil { 149 return nil, resp, err 150 } 151 return configuration, resp, nil 152 } 153 154 // GetNetworkConfiguration gets a hosted compute network configuration configured in an organization. 155 // 156 // GitHub API docs: https://docs.github.com/rest/orgs/network-configurations#get-a-hosted-compute-network-configuration-for-an-organization 157 // 158 //meta:operation GET /orgs/{org}/settings/network-configurations/{network_configuration_id} 159 func (s *OrganizationsService) GetNetworkConfiguration(ctx context.Context, org, networkID string) (*NetworkConfiguration, *Response, error) { 160 u := fmt.Sprintf("orgs/%v/settings/network-configurations/%v", org, networkID) 161 req, err := s.client.NewRequest("GET", u, nil) 162 if err != nil { 163 return nil, nil, err 164 } 165 166 configuration := &NetworkConfiguration{} 167 resp, err := s.client.Do(ctx, req, configuration) 168 if err != nil { 169 return nil, resp, err 170 } 171 return configuration, resp, nil 172 } 173 174 // UpdateNetworkConfiguration updates a hosted compute network configuration for an organization. 175 // 176 // GitHub API docs: https://docs.github.com/rest/orgs/network-configurations#update-a-hosted-compute-network-configuration-for-an-organization 177 // 178 //meta:operation PATCH /orgs/{org}/settings/network-configurations/{network_configuration_id} 179 func (s *OrganizationsService) UpdateNetworkConfiguration(ctx context.Context, org, networkID string, updateReq NetworkConfigurationRequest) (*NetworkConfiguration, *Response, error) { 180 if err := validateNetworkConfigurationRequest(updateReq); err != nil { 181 return nil, nil, fmt.Errorf("validation failed: %w", err) 182 } 183 184 u := fmt.Sprintf("orgs/%v/settings/network-configurations/%v", org, networkID) 185 req, err := s.client.NewRequest("PATCH", u, updateReq) 186 if err != nil { 187 return nil, nil, err 188 } 189 190 configuration := &NetworkConfiguration{} 191 resp, err := s.client.Do(ctx, req, configuration) 192 if err != nil { 193 return nil, resp, err 194 } 195 return configuration, resp, nil 196 } 197 198 // DeleteNetworkConfigurations deletes a hosted compute network configuration from an organization. 199 // 200 // GitHub API docs: https://docs.github.com/rest/orgs/network-configurations#delete-a-hosted-compute-network-configuration-from-an-organization 201 // 202 //meta:operation DELETE /orgs/{org}/settings/network-configurations/{network_configuration_id} 203 func (s *OrganizationsService) DeleteNetworkConfigurations(ctx context.Context, org, networkID string) (*Response, error) { 204 u := fmt.Sprintf("orgs/%v/settings/network-configurations/%v", org, networkID) 205 req, err := s.client.NewRequest("DELETE", u, nil) 206 if err != nil { 207 return nil, err 208 } 209 210 configuration := &NetworkConfiguration{} 211 resp, err := s.client.Do(ctx, req, configuration) 212 if err != nil { 213 return resp, err 214 } 215 return resp, nil 216 } 217 218 // GetNetworkConfigurationResource gets a hosted compute network settings resource configured for an organization. 219 // 220 // GitHub API docs: https://docs.github.com/rest/orgs/network-configurations#get-a-hosted-compute-network-settings-resource-for-an-organization 221 // 222 //meta:operation GET /orgs/{org}/settings/network-settings/{network_settings_id} 223 func (s *OrganizationsService) GetNetworkConfigurationResource(ctx context.Context, org, networkID string) (*NetworkSettingsResource, *Response, error) { 224 u := fmt.Sprintf("orgs/%v/settings/network-settings/%v", org, networkID) 225 req, err := s.client.NewRequest("GET", u, nil) 226 if err != nil { 227 return nil, nil, err 228 } 229 230 resource := &NetworkSettingsResource{} 231 resp, err := s.client.Do(ctx, req, resource) 232 if err != nil { 233 return nil, resp, err 234 } 235 return resource, resp, nil 236 }