GCE: Add a fake forwarding rule service
Also add more methods to the address service.
This commit is contained in:
		| @@ -22,6 +22,7 @@ go_library( | ||||
|         "gce_disks.go", | ||||
|         "gce_firewall.go", | ||||
|         "gce_forwardingrule.go", | ||||
|         "gce_forwardingrule_fakes.go", | ||||
|         "gce_healthchecks.go", | ||||
|         "gce_instancegroup.go", | ||||
|         "gce_instances.go", | ||||
|   | ||||
| @@ -106,6 +106,18 @@ func (cas *FakeCloudAddressService) GetRegionAddress(name, region string) (*comp | ||||
| 	return nil, err | ||||
| } | ||||
|  | ||||
| func (cas *FakeCloudAddressService) DeleteRegionAddress(name, region string) error { | ||||
| 	if _, exists := cas.addrsByRegionAndName[region]; !exists { | ||||
| 		return makeGoogleAPINotFoundError("") | ||||
| 	} | ||||
|  | ||||
| 	if _, exists := cas.addrsByRegionAndName[region][name]; !exists { | ||||
| 		return makeGoogleAPINotFoundError("") | ||||
| 	} | ||||
| 	delete(cas.addrsByRegionAndName[region], name) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (cas *FakeCloudAddressService) GetRegionAddressByIP(region, ipAddress string) (*compute.Address, error) { | ||||
| 	if _, exists := cas.addrsByRegionAndName[region]; !exists { | ||||
| 		return nil, makeGoogleAPINotFoundError("") | ||||
|   | ||||
							
								
								
									
										127
									
								
								pkg/cloudprovider/providers/gce/gce_forwardingrule_fakes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										127
									
								
								pkg/cloudprovider/providers/gce/gce_forwardingrule_fakes.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,127 @@ | ||||
| /* | ||||
| Copyright 2017 The Kubernetes Authors. | ||||
|  | ||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| you may not use this file except in compliance with the License. | ||||
| You may obtain a copy of the License at | ||||
|  | ||||
|     http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  | ||||
| Unless required by applicable law or agreed to in writing, software | ||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| See the License for the specific language governing permissions and | ||||
| limitations under the License. | ||||
| */ | ||||
|  | ||||
| package gce | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
|  | ||||
| 	computealpha "google.golang.org/api/compute/v0.alpha" | ||||
| 	compute "google.golang.org/api/compute/v1" | ||||
| 	"google.golang.org/api/googleapi" | ||||
| ) | ||||
|  | ||||
| type FakeCloudForwardingRuleService struct { | ||||
| 	// fwdRulesByRegionAndName | ||||
| 	// Outer key is for region string; inner key is for fwdRuleess name. | ||||
| 	fwdRulesByRegionAndName map[string]map[string]*computealpha.ForwardingRule | ||||
| } | ||||
|  | ||||
| // FakeCloudForwardingRuleService Implements CloudForwardingRuleService | ||||
| var _ CloudForwardingRuleService = &FakeCloudForwardingRuleService{} | ||||
|  | ||||
| func NewFakeCloudForwardingRuleService() *FakeCloudForwardingRuleService { | ||||
| 	return &FakeCloudForwardingRuleService{ | ||||
| 		fwdRulesByRegionAndName: make(map[string]map[string]*computealpha.ForwardingRule), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SetRegionalForwardingRulees sets the fwdRuleesses of ther region. This is used for | ||||
| // setting the test environment. | ||||
| func (f *FakeCloudForwardingRuleService) SetRegionalForwardingRulees(region string, fwdRules []*computealpha.ForwardingRule) { | ||||
| 	// Reset fwdRuleesses in the region. | ||||
| 	f.fwdRulesByRegionAndName[region] = make(map[string]*computealpha.ForwardingRule) | ||||
|  | ||||
| 	for _, fwdRule := range fwdRules { | ||||
| 		f.fwdRulesByRegionAndName[region][fwdRule.Name] = fwdRule | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (f *FakeCloudForwardingRuleService) CreateAlphaRegionForwardingRule(fwdRule *computealpha.ForwardingRule, region string) error { | ||||
| 	if _, exists := f.fwdRulesByRegionAndName[region]; !exists { | ||||
| 		f.fwdRulesByRegionAndName[region] = make(map[string]*computealpha.ForwardingRule) | ||||
| 	} | ||||
|  | ||||
| 	if _, exists := f.fwdRulesByRegionAndName[region][fwdRule.Name]; exists { | ||||
| 		return &googleapi.Error{Code: http.StatusConflict} | ||||
| 	} | ||||
|  | ||||
| 	f.fwdRulesByRegionAndName[region][fwdRule.Name] = fwdRule | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (f *FakeCloudForwardingRuleService) CreateRegionForwardingRule(fwdRule *compute.ForwardingRule, region string) error { | ||||
| 	alphafwdRule := convertToAlphaForwardingRule(fwdRule) | ||||
| 	return f.CreateAlphaRegionForwardingRule(alphafwdRule, region) | ||||
| } | ||||
|  | ||||
| func (f *FakeCloudForwardingRuleService) DeleteRegionForwardingRule(name, region string) error { | ||||
| 	if _, exists := f.fwdRulesByRegionAndName[region]; !exists { | ||||
| 		return makeGoogleAPINotFoundError("") | ||||
| 	} | ||||
|  | ||||
| 	if _, exists := f.fwdRulesByRegionAndName[region][name]; !exists { | ||||
| 		return makeGoogleAPINotFoundError("") | ||||
| 	} | ||||
| 	delete(f.fwdRulesByRegionAndName[region], name) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (f *FakeCloudForwardingRuleService) GetAlphaRegionForwardingRule(name, region string) (*computealpha.ForwardingRule, error) { | ||||
| 	if _, exists := f.fwdRulesByRegionAndName[region]; !exists { | ||||
| 		return nil, makeGoogleAPINotFoundError("") | ||||
| 	} | ||||
|  | ||||
| 	if fwdRule, exists := f.fwdRulesByRegionAndName[region][name]; !exists { | ||||
| 		return nil, makeGoogleAPINotFoundError("") | ||||
| 	} else { | ||||
| 		return fwdRule, nil | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (f *FakeCloudForwardingRuleService) GetRegionForwardingRule(name, region string) (*compute.ForwardingRule, error) { | ||||
| 	fwdRule, err := f.GetAlphaRegionForwardingRule(name, region) | ||||
| 	if fwdRule != nil { | ||||
| 		return convertToV1ForwardingRule(fwdRule), err | ||||
| 	} | ||||
| 	return nil, err | ||||
| } | ||||
|  | ||||
| func convertToV1ForwardingRule(object gceObject) *compute.ForwardingRule { | ||||
| 	enc, err := object.MarshalJSON() | ||||
| 	if err != nil { | ||||
| 		panic(fmt.Sprintf("Failed to encode to json: %v", err)) | ||||
| 	} | ||||
| 	var fwdRule compute.ForwardingRule | ||||
| 	if err := json.Unmarshal(enc, &fwdRule); err != nil { | ||||
| 		panic(fmt.Sprintf("Failed to convert GCE apiObject %v to v1 fwdRuleess: %v", object, err)) | ||||
| 	} | ||||
| 	return &fwdRule | ||||
| } | ||||
|  | ||||
| func convertToAlphaForwardingRule(object gceObject) *computealpha.ForwardingRule { | ||||
| 	enc, err := object.MarshalJSON() | ||||
| 	if err != nil { | ||||
| 		panic(fmt.Sprintf("Failed to encode to json: %v", err)) | ||||
| 	} | ||||
| 	var fwdRule computealpha.ForwardingRule | ||||
| 	if err := json.Unmarshal(enc, &fwdRule); err != nil { | ||||
| 		panic(fmt.Sprintf("Failed to convert GCE apiObject %v to alpha fwdRuleess: %v", object, err)) | ||||
| 	} | ||||
| 	return &fwdRule | ||||
| } | ||||
| @@ -26,10 +26,22 @@ type CloudAddressService interface { | ||||
| 	ReserveRegionAddress(*compute.Address, string) error | ||||
| 	GetRegionAddress(string, string) (*compute.Address, error) | ||||
| 	GetRegionAddressByIP(region, ipAddress string) (*compute.Address, error) | ||||
| 	// TODO: Mock `DeleteRegionAddress(name, region string) endpoint | ||||
| 	DeleteRegionAddress(name, region string) error | ||||
| 	// TODO: Mock Global endpoints | ||||
|  | ||||
| 	// Alpha API. | ||||
| 	GetAlphaRegionAddress(name, region string) (*computealpha.Address, error) | ||||
| 	ReserveAlphaRegionAddress(addr *computealpha.Address, region string) error | ||||
| } | ||||
|  | ||||
| // CloudForwardingRuleService is an interface for managing forwarding rules. | ||||
| // TODO: Expand the interface to include more methods. | ||||
| type CloudForwardingRuleService interface { | ||||
| 	GetRegionForwardingRule(name, region string) (*compute.ForwardingRule, error) | ||||
| 	CreateRegionForwardingRule(rule *compute.ForwardingRule, region string) error | ||||
| 	DeleteRegionForwardingRule(name, region string) error | ||||
|  | ||||
| 	// Alpha API. | ||||
| 	GetAlphaRegionForwardingRule(name, region string) (*computealpha.ForwardingRule, error) | ||||
| 	CreateAlphaRegionForwardingRule(rule *computealpha.ForwardingRule, region string) error | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Yu-Ju Hong
					Yu-Ju Hong