|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// +tool:mockgcp-support |
| 16 | +// proto.service: google.cloud.batch.v1alpha.BatchService |
| 17 | +// proto.message: google.cloud.batch.v1alpha.ResourceAllowance |
| 18 | + |
| 19 | +package mockbatch |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "regexp" |
| 25 | + "time" |
| 26 | + |
| 27 | + "cloud.google.com/go/longrunning/autogen/longrunningpb" |
| 28 | + "google.golang.org/grpc/codes" |
| 29 | + "google.golang.org/grpc/status" |
| 30 | + "google.golang.org/protobuf/proto" |
| 31 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 32 | + |
| 33 | + "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects" |
| 34 | + "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage" |
| 35 | + pb_v1alpha "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/batch/resourceallowance/pb" |
| 36 | +) |
| 37 | + |
| 38 | +type resourceAllowanceName struct { |
| 39 | + Project *projects.ProjectData |
| 40 | + Location string |
| 41 | + ResourceAllowance string |
| 42 | +} |
| 43 | + |
| 44 | +func (n *resourceAllowanceName) String() string { |
| 45 | + return fmt.Sprintf("projects/%s/locations/%s/resourceAllowances/%s", n.Project.ID, n.Location, n.ResourceAllowance) |
| 46 | +} |
| 47 | + |
| 48 | +func (s *MockService) parseResourceAllowanceName(name string) (*resourceAllowanceName, error) { |
| 49 | + r := regexp.MustCompile("^projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/resourceAllowances/(?P<resource_allowance>[^/]+)$") |
| 50 | + match := r.FindStringSubmatch(name) |
| 51 | + if len(match) == 0 { |
| 52 | + return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name) |
| 53 | + } |
| 54 | + m := make(map[string]string) |
| 55 | + for i, n := range r.SubexpNames() { |
| 56 | + if len(n) > 0 { |
| 57 | + m[n] = match[i] |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + project, err := s.Projects.GetProjectByID(m["project"]) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + return &resourceAllowanceName{ |
| 67 | + Project: project, |
| 68 | + Location: m["location"], |
| 69 | + ResourceAllowance: m["resource_allowance"], |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +func (s *BatchV1Alpha) CreateResourceAllowance(ctx context.Context, req *pb_v1alpha.CreateResourceAllowanceRequest) (*pb_v1alpha.ResourceAllowance, error) { |
| 74 | + reqName := req.Parent + "/resourceAllowances/" + req.ResourceAllowanceId |
| 75 | + name, err := s.parseResourceAllowanceName(reqName) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + fqn := name.String() |
| 81 | + |
| 82 | + obj := proto.Clone(req.ResourceAllowance).(*pb_v1alpha.ResourceAllowance) |
| 83 | + obj.Name = fqn |
| 84 | + obj.Uid = "b9a676df-c595-4c81-9963-f44b8e44e50c" |
| 85 | + obj.CreateTime = timestamppb.Now() |
| 86 | + |
| 87 | + if obj.GetUsageResourceAllowance() != nil { |
| 88 | + if obj.GetUsageResourceAllowance().Status == nil { |
| 89 | + obj.GetUsageResourceAllowance().Status = &pb_v1alpha.UsageResourceAllowanceStatus{} |
| 90 | + } |
| 91 | + obj.GetUsageResourceAllowance().Status.State = pb_v1alpha.ResourceAllowanceState_RESOURCE_ALLOWANCE_ACTIVE |
| 92 | + |
| 93 | + if obj.GetUsageResourceAllowance().Spec != nil && obj.GetUsageResourceAllowance().Spec.Limit != nil { |
| 94 | + limitVal := obj.GetUsageResourceAllowance().Spec.Limit.Limit |
| 95 | + obj.GetUsageResourceAllowance().Status.LimitStatus = &pb_v1alpha.UsageResourceAllowanceStatus_LimitStatus{ |
| 96 | + Limit: limitVal, |
| 97 | + Consumed: float64Ptr(0.0), |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if err := s.storage.Create(ctx, fqn, obj); err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + return obj, nil |
| 107 | +} |
| 108 | + |
| 109 | +func (s *BatchV1Alpha) GetResourceAllowance(ctx context.Context, req *pb_v1alpha.GetResourceAllowanceRequest) (*pb_v1alpha.ResourceAllowance, error) { |
| 110 | + name, err := s.parseResourceAllowanceName(req.Name) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + fqn := name.String() |
| 116 | + |
| 117 | + obj := &pb_v1alpha.ResourceAllowance{} |
| 118 | + if err := s.storage.Get(ctx, fqn, obj); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + return obj, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (s *BatchV1Alpha) UpdateResourceAllowance(ctx context.Context, req *pb_v1alpha.UpdateResourceAllowanceRequest) (*pb_v1alpha.ResourceAllowance, error) { |
| 126 | + name, err := s.parseResourceAllowanceName(req.ResourceAllowance.Name) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + fqn := name.String() |
| 131 | + |
| 132 | + obj := &pb_v1alpha.ResourceAllowance{} |
| 133 | + if err := s.storage.Get(ctx, fqn, obj); err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + updated := proto.Clone(req.ResourceAllowance).(*pb_v1alpha.ResourceAllowance) |
| 138 | + updated.Uid = obj.Uid |
| 139 | + updated.CreateTime = obj.CreateTime |
| 140 | + |
| 141 | + if updated.GetUsageResourceAllowance() != nil { |
| 142 | + if updated.GetUsageResourceAllowance().Status == nil { |
| 143 | + updated.GetUsageResourceAllowance().Status = &pb_v1alpha.UsageResourceAllowanceStatus{} |
| 144 | + } |
| 145 | + updated.GetUsageResourceAllowance().Status.State = pb_v1alpha.ResourceAllowanceState_RESOURCE_ALLOWANCE_ACTIVE |
| 146 | + |
| 147 | + if updated.GetUsageResourceAllowance().Spec != nil && updated.GetUsageResourceAllowance().Spec.Limit != nil { |
| 148 | + limitVal := updated.GetUsageResourceAllowance().Spec.Limit.Limit |
| 149 | + updated.GetUsageResourceAllowance().Status.LimitStatus = &pb_v1alpha.UsageResourceAllowanceStatus_LimitStatus{ |
| 150 | + Limit: limitVal, |
| 151 | + Consumed: float64Ptr(0.0), |
| 152 | + } |
| 153 | + if obj.GetUsageResourceAllowance() != nil && obj.GetUsageResourceAllowance().Status != nil && obj.GetUsageResourceAllowance().Status.LimitStatus != nil { |
| 154 | + updated.GetUsageResourceAllowance().Status.LimitStatus.Consumed = obj.GetUsageResourceAllowance().Status.LimitStatus.Consumed |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if err := s.storage.Update(ctx, fqn, updated); err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + |
| 163 | + return updated, nil |
| 164 | +} |
| 165 | + |
| 166 | +func (s *BatchV1Alpha) DeleteResourceAllowance(ctx context.Context, req *pb_v1alpha.DeleteResourceAllowanceRequest) (*longrunningpb.Operation, error) { |
| 167 | + name, err := s.parseResourceAllowanceName(req.Name) |
| 168 | + if err != nil { |
| 169 | + return nil, err |
| 170 | + } |
| 171 | + |
| 172 | + fqn := name.String() |
| 173 | + now := time.Now() |
| 174 | + |
| 175 | + deleted := &pb_v1alpha.ResourceAllowance{} |
| 176 | + if err := s.storage.Delete(ctx, fqn, deleted); err != nil { |
| 177 | + return nil, err |
| 178 | + } |
| 179 | + |
| 180 | + operationMetadata := &pb_v1alpha.OperationMetadata{ |
| 181 | + CreateTime: timestamppb.New(now), |
| 182 | + EndTime: timestamppb.New(now), |
| 183 | + ApiVersion: "v1alpha", |
| 184 | + RequestedCancellation: false, |
| 185 | + Verb: "delete", |
| 186 | + Target: fqn, |
| 187 | + } |
| 188 | + return s.operations.DoneLRO(ctx, fqn, operationMetadata, nil) |
| 189 | +} |
| 190 | + |
| 191 | +func (s *BatchV1Alpha) ListResourceAllowances(ctx context.Context, req *pb_v1alpha.ListResourceAllowancesRequest) (*pb_v1alpha.ListResourceAllowancesResponse, error) { |
| 192 | + parent := req.GetParent() |
| 193 | + if parent == "" { |
| 194 | + return nil, status.Errorf(codes.InvalidArgument, "parent must be specified") |
| 195 | + } |
| 196 | + |
| 197 | + var resourceAllowances []*pb_v1alpha.ResourceAllowance |
| 198 | + if err := s.storage.List(ctx, (&pb_v1alpha.ResourceAllowance{}).ProtoReflect().Descriptor(), storage.ListOptions{ |
| 199 | + Prefix: parent + "/resourceAllowances/", |
| 200 | + }, func(obj proto.Message) error { |
| 201 | + allowance := obj.(*pb_v1alpha.ResourceAllowance) |
| 202 | + resourceAllowances = append(resourceAllowances, allowance) |
| 203 | + return nil |
| 204 | + }); err != nil { |
| 205 | + return nil, err |
| 206 | + } |
| 207 | + |
| 208 | + return &pb_v1alpha.ListResourceAllowancesResponse{ |
| 209 | + ResourceAllowances: resourceAllowances, |
| 210 | + }, nil |
| 211 | +} |
| 212 | + |
| 213 | +func float64Ptr(v float64) *float64 { |
| 214 | + return &v |
| 215 | +} |
0 commit comments