|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mockcompute |
| 18 | + |
| 19 | +import ( |
| 20 | +"context" |
| 21 | +"fmt" |
| 22 | +"sync" |
| 23 | + |
| 24 | +compute "google.golang.org/api/compute/v1" |
| 25 | +"k8s.io/kops/upup/pkg/fi/cloudup/gce" |
| 26 | +) |
| 27 | + |
| 28 | +type regionInstanceGroupManagerClient struct { |
| 29 | +// regionInstanceGroupManagers keyed by project, region, and name. |
| 30 | +regionInstanceGroupManagers map[string]map[string]map[string]*compute.InstanceGroupManager |
| 31 | +sync.Mutex |
| 32 | +} |
| 33 | + |
| 34 | +var _ gce.RegionInstanceGroupManagerClient = ®ionInstanceGroupManagerClient{} |
| 35 | + |
| 36 | +func newRegionInstanceGroupManagerClient() *regionInstanceGroupManagerClient { |
| 37 | +return ®ionInstanceGroupManagerClient{ |
| 38 | +regionInstanceGroupManagers: map[string]map[string]map[string]*compute.InstanceGroupManager{}, |
| 39 | +} |
| 40 | +} |
| 41 | + |
| 42 | +func (c *regionInstanceGroupManagerClient) All() map[string]interface{} { |
| 43 | +c.Lock() |
| 44 | +defer c.Unlock() |
| 45 | +m := map[string]interface{}{} |
| 46 | +for _, regions := range c.regionInstanceGroupManagers { |
| 47 | +for _, igms := range regions { |
| 48 | +for n, igm := range igms { |
| 49 | +m[n] = igm |
| 50 | +} |
| 51 | +} |
| 52 | +} |
| 53 | +return m |
| 54 | +} |
| 55 | + |
| 56 | +func (c *regionInstanceGroupManagerClient) Insert(project, region string, igm *compute.InstanceGroupManager) (*compute.Operation, error) { |
| 57 | +c.Lock() |
| 58 | +defer c.Unlock() |
| 59 | +igmRegions, ok := c.regionInstanceGroupManagers[project] |
| 60 | +if !ok { |
| 61 | +igmRegions = map[string]map[string]*compute.InstanceGroupManager{} |
| 62 | +c.regionInstanceGroupManagers[project] = igmRegions |
| 63 | +} |
| 64 | +igms, ok := igmRegions[region] |
| 65 | +if !ok { |
| 66 | +igms = map[string]*compute.InstanceGroupManager{} |
| 67 | +igmRegions[region] = igms |
| 68 | +} |
| 69 | +igm.SelfLink = fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/instanceGroupManagers/%s", project, region, igm.Name) |
| 70 | +igms[igm.Name] = igm |
| 71 | +return doneOperation(), nil |
| 72 | +} |
| 73 | + |
| 74 | +func (c *regionInstanceGroupManagerClient) Delete(project, region, name string) (*compute.Operation, error) { |
| 75 | +c.Lock() |
| 76 | +defer c.Unlock() |
| 77 | +regions, ok := c.regionInstanceGroupManagers[project] |
| 78 | +if !ok { |
| 79 | +return nil, notFoundError() |
| 80 | +} |
| 81 | +igms, ok := regions[region] |
| 82 | +if !ok { |
| 83 | +return nil, notFoundError() |
| 84 | +} |
| 85 | +if _, ok := igms[name]; !ok { |
| 86 | +return nil, notFoundError() |
| 87 | +} |
| 88 | +delete(igms, name) |
| 89 | +return doneOperation(), nil |
| 90 | +} |
| 91 | + |
| 92 | +func (c *regionInstanceGroupManagerClient) Get(project, region, name string) (*compute.InstanceGroupManager, error) { |
| 93 | +c.Lock() |
| 94 | +defer c.Unlock() |
| 95 | +regions, ok := c.regionInstanceGroupManagers[project] |
| 96 | +if !ok { |
| 97 | +return nil, notFoundError() |
| 98 | +} |
| 99 | +igms, ok := regions[region] |
| 100 | +if !ok { |
| 101 | +return nil, notFoundError() |
| 102 | +} |
| 103 | +igm, ok := igms[name] |
| 104 | +if !ok { |
| 105 | +return nil, notFoundError() |
| 106 | +} |
| 107 | +return igm, nil |
| 108 | +} |
| 109 | + |
| 110 | +func (c *regionInstanceGroupManagerClient) List(ctx context.Context, project, region string) ([]*compute.InstanceGroupManager, error) { |
| 111 | +c.Lock() |
| 112 | +defer c.Unlock() |
| 113 | +regions, ok := c.regionInstanceGroupManagers[project] |
| 114 | +if !ok { |
| 115 | +return nil, nil |
| 116 | +} |
| 117 | +igms, ok := regions[region] |
| 118 | +if !ok { |
| 119 | +return nil, nil |
| 120 | +} |
| 121 | +var l []*compute.InstanceGroupManager |
| 122 | +for _, d := range igms { |
| 123 | +l = append(l, d) |
| 124 | +} |
| 125 | +return l, nil |
| 126 | +} |
| 127 | + |
| 128 | +func (c *regionInstanceGroupManagerClient) SetTargetPools(project, region, name string, targetPools []string) (*compute.Operation, error) { |
| 129 | +return doneOperation(), nil |
| 130 | +} |
| 131 | + |
| 132 | +func (c *regionInstanceGroupManagerClient) SetInstanceTemplate(project, region, name, instanceTemplateURL string) (*compute.Operation, error) { |
| 133 | +return doneOperation(), nil |
| 134 | +} |
| 135 | + |
| 136 | +func (c *regionInstanceGroupManagerClient) Resize(project, region, name string, newSize int64) (*compute.Operation, error) { |
| 137 | +return doneOperation(), nil |
| 138 | +} |
0 commit comments