Skip to content

Commit 0bcf1e4

Browse files
Merge pull request GoogleCloudPlatform#1226 from gemmahou/323011402
mockgcp support for ComputeNodeGroup and ComputeNodeTemplate
2 parents 1e3e36a + 9e6f735 commit 0bcf1e4

File tree

7 files changed

+647
-4
lines changed

7 files changed

+647
-4
lines changed

config/tests/samples/create/harness.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
412412

413413
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeDisk"}:
414414
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeNetwork"}:
415+
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeNodeGroup"}:
416+
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeNodeTemplate"}:
415417
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSubnetwork"}:
416418
// ok
417419

mockgcp/mockcompute/nodegroupsv1.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2024 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+
package mockcompute
16+
17+
import (
18+
"context"
19+
20+
"google.golang.org/protobuf/proto"
21+
22+
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
23+
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/compute/v1"
24+
)
25+
26+
type NodeGroupsV1 struct {
27+
*MockService
28+
pb.UnimplementedNodeGroupsServer
29+
}
30+
31+
func (s *NodeGroupsV1) Get(ctx context.Context, req *pb.GetNodeGroupRequest) (*pb.NodeGroup, error) {
32+
name, err := s.newNodeGroupName(req.GetProject(), req.GetZone(), req.GetNodeGroup())
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
fqn := name.String()
38+
39+
obj := &pb.NodeGroup{}
40+
if err := s.storage.Get(ctx, fqn, obj); err != nil {
41+
return nil, err
42+
}
43+
44+
return obj, nil
45+
}
46+
47+
func (s *NodeGroupsV1) Insert(ctx context.Context, req *pb.InsertNodeGroupRequest) (*pb.Operation, error) {
48+
name, err := s.newNodeGroupName(req.GetProject(), req.GetZone(), req.GetNodeGroupResource().GetName())
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
fqn := name.String()
54+
55+
id := s.generateID()
56+
57+
obj := proto.Clone(req.GetNodeGroupResource()).(*pb.NodeGroup)
58+
obj.SelfLink = PtrTo("https://compute.googleapis.com/compute/v1/" + name.String())
59+
obj.CreationTimestamp = PtrTo(s.nowString())
60+
obj.Id = &id
61+
obj.Kind = PtrTo("compute#nodegroup")
62+
63+
if err := s.storage.Create(ctx, fqn, obj); err != nil {
64+
return nil, err
65+
}
66+
67+
return s.newLRO(ctx, name.Project.ID)
68+
}
69+
70+
func (s *NodeGroupsV1) Patch(ctx context.Context, req *pb.PatchNodeGroupRequest) (*pb.Operation, error) {
71+
name, err := s.newNodeGroupName(req.GetProject(), req.GetZone(), req.GetNodeGroup())
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
fqn := name.String()
77+
obj := &pb.NodeGroup{}
78+
79+
if err := s.storage.Update(ctx, fqn, obj); err != nil {
80+
return nil, err
81+
}
82+
83+
return s.newLRO(ctx, name.Project.ID)
84+
}
85+
86+
func (s *NodeGroupsV1) Delete(ctx context.Context, req *pb.DeleteNodeGroupRequest) (*pb.Operation, error) {
87+
name, err := s.newNodeGroupName(req.GetProject(), req.GetZone(), req.GetNodeGroup())
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
fqn := name.String()
93+
94+
obj := &pb.NodeGroup{}
95+
if err := s.storage.Delete(ctx, fqn, obj); err != nil {
96+
return nil, err
97+
}
98+
99+
return s.newLRO(ctx, name.Project.ID)
100+
}
101+
102+
type nodeGroupName struct {
103+
Project *projects.ProjectData
104+
Zone string
105+
Name string
106+
}
107+
108+
func (n *nodeGroupName) String() string {
109+
return "projects/" + n.Project.ID + "/zones/" + n.Zone + "/nodeGroups/" + n.Name
110+
}
111+
112+
// newNodeGroupName builds a normalized nodeGroupName from the constituent parts.
113+
// The expected form is `projects/{project}/zones/{zone}/nodeGroups/{nodeGroup}`.
114+
func (s *MockService) newNodeGroupName(project string, zone string, name string) (*nodeGroupName, error) {
115+
projectObj, err := s.projects.GetProjectByID(project)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
return &nodeGroupName{
121+
Project: projectObj,
122+
Zone: zone,
123+
Name: name,
124+
}, nil
125+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2024 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+
package mockcompute
16+
17+
import (
18+
"context"
19+
20+
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
21+
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/compute/v1"
22+
"google.golang.org/protobuf/proto"
23+
)
24+
25+
type NodeTemplatesV1 struct {
26+
*MockService
27+
pb.UnimplementedNodeTemplatesServer
28+
}
29+
30+
func (s *NodeTemplatesV1) Get(ctx context.Context, req *pb.GetNodeTemplateRequest) (*pb.NodeTemplate, error) {
31+
name, err := s.newNodeTemplateName(req.GetProject(), req.GetRegion(), req.GetNodeTemplate())
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
fqn := name.String()
37+
38+
obj := &pb.NodeTemplate{}
39+
if err := s.storage.Get(ctx, fqn, obj); err != nil {
40+
return nil, err
41+
}
42+
43+
return obj, nil
44+
}
45+
46+
func (s *NodeTemplatesV1) Insert(ctx context.Context, req *pb.InsertNodeTemplateRequest) (*pb.Operation, error) {
47+
name, err := s.newNodeTemplateName(req.GetProject(), req.GetRegion(), req.GetNodeTemplateResource().GetName())
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
fqn := name.String()
53+
54+
id := s.generateID()
55+
56+
obj := proto.Clone(req.GetNodeTemplateResource()).(*pb.NodeTemplate)
57+
obj.SelfLink = PtrTo("https://compute.googleapis.com/compute/v1/" + name.String())
58+
obj.CreationTimestamp = PtrTo(s.nowString())
59+
obj.Id = &id
60+
obj.Kind = PtrTo("compute#nodetemplate")
61+
62+
if err := s.storage.Create(ctx, fqn, obj); err != nil {
63+
return nil, err
64+
}
65+
66+
return s.newLRO(ctx, name.Project.ID)
67+
}
68+
69+
func (s *NodeTemplatesV1) Delete(ctx context.Context, req *pb.DeleteNodeTemplateRequest) (*pb.Operation, error) {
70+
name, err := s.newNodeTemplateName(req.GetProject(), req.GetRegion(), req.GetNodeTemplate())
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
fqn := name.String()
76+
77+
obj := &pb.NodeTemplate{}
78+
if err := s.storage.Delete(ctx, fqn, obj); err != nil {
79+
return nil, err
80+
}
81+
82+
return s.newLRO(ctx, name.Project.ID)
83+
}
84+
85+
type nodeTemplateName struct {
86+
Project *projects.ProjectData
87+
Region string
88+
Name string
89+
}
90+
91+
func (n *nodeTemplateName) String() string {
92+
return "projects/" + n.Project.ID + "/regions/" + n.Region + "/nodeTemplates/" + n.Name
93+
}
94+
95+
// newNodeTemplateName builds a normalized nodeTemplateName from the constituent parts.
96+
// The expected form is `projects/{project}/regions/{region}/nodeTemplates/{nodeTemplate}`.
97+
func (s *MockService) newNodeTemplateName(project string, region string, name string) (*nodeTemplateName, error) {
98+
projectObj, err := s.projects.GetProjectByID(project)
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
return &nodeTemplateName{
104+
Project: projectObj,
105+
Region: region,
106+
Name: name,
107+
}, nil
108+
}

mockgcp/mockcompute/service.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ type MockService struct {
3838
projects projects.ProjectStore
3939
operations *operations.Operations
4040

41-
networksv1 *NetworksV1
42-
subnetsv1 *SubnetsV1
41+
networksv1 *NetworksV1
42+
nodegroupsv1 *NodeGroupsV1
43+
nodetemplatesv1 *NodeTemplatesV1
44+
subnetsv1 *SubnetsV1
4345
}
4446

4547
// New creates a MockService.
@@ -51,6 +53,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
5153
operations: operations.NewOperationsService(storage),
5254
}
5355
s.networksv1 = &NetworksV1{MockService: s}
56+
s.nodegroupsv1 = &NodeGroupsV1{MockService: s}
57+
s.nodetemplatesv1 = &NodeTemplatesV1{MockService: s}
5458
s.subnetsv1 = &SubnetsV1{MockService: s}
5559
return s
5660
}
@@ -61,6 +65,8 @@ func (s *MockService) ExpectedHost() string {
6165

6266
func (s *MockService) Register(grpcServer *grpc.Server) {
6367
pb.RegisterNetworksServer(grpcServer, s.networksv1)
68+
pb.RegisterNodeGroupsServer(grpcServer, s.nodegroupsv1)
69+
pb.RegisterNodeTemplatesServer(grpcServer, s.nodetemplatesv1)
6470
pb.RegisterSubnetworksServer(grpcServer, s.subnetsv1)
6571

6672
pb.RegisterDisksServer(grpcServer, &DisksV1{MockService: s})
@@ -99,6 +105,12 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
99105
if err := pb.RegisterNetworksHandler(ctx, mux, conn); err != nil {
100106
return nil, err
101107
}
108+
if err := pb.RegisterNodeGroupsHandler(ctx, mux, conn); err != nil {
109+
return nil, err
110+
}
111+
if err := pb.RegisterNodeTemplatesHandler(ctx, mux, conn); err != nil {
112+
return nil, err
113+
}
102114
if err := pb.RegisterSubnetworksHandler(ctx, mux, conn); err != nil {
103115
return nil, err
104116
}

0 commit comments

Comments
 (0)