Skip to content

Commit

Permalink
wip:tests: basic test
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pana <[email protected]>
  • Loading branch information
acpana committed Feb 4, 2025
1 parent 2456040 commit 04c17c3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 26 deletions.
1 change: 1 addition & 0 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
case schema.GroupKind{Group: "alloydb.cnrm.cloud.google.com", Kind: "AlloyDBInstance"}:

case schema.GroupKind{Group: "apigee.cnrm.cloud.google.com", Kind: "ApigeeEnvgroup"}:
case schema.GroupKind{Group: "apigee.cnrm.cloud.google.com", Kind: "ApigeeEnvgroupAttachment"}:
case schema.GroupKind{Group: "apigee.cnrm.cloud.google.com", Kind: "ApigeeEnvironment"}:
case schema.GroupKind{Group: "apigee.cnrm.cloud.google.com", Kind: "ApigeeOrganization"}:

Expand Down
41 changes: 15 additions & 26 deletions mockgcp/mockapigee/envgroupattachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"

"cloud.google.com/go/longrunning/autogen/longrunningpb"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/apigee/v1"

"google.golang.org/grpc/codes"
Expand All @@ -42,6 +41,9 @@ func (s *envgroupAttachmentServer) CreateOrganizationsEnvgroupsAttachment(ctx co

fqn := name.String()
obj := proto.Clone(req.GetOrganizationsEnvgroupsAttachment()).(*pb.GoogleCloudApigeeV1EnvironmentGroupAttachment)

// The name field in the request body is ignored by Apigee.
// Set it to the fully qualified name for consistency with GCP
obj.Name = fqn

if err := s.storage.Create(ctx, fqn, obj); err != nil {
Expand All @@ -54,14 +56,8 @@ func (s *envgroupAttachmentServer) CreateOrganizationsEnvgroupsAttachment(ctx co
TargetResourceName: fqn,
}

// TODO: StartLRO
return s.operations.DoneLRO(ctx, fqn, opMetadata, func() *pb.GoogleCloudApigeeV1EnvironmentGroupAttachment {
obj.Name = name.AttachmentID
obj.Environment = name.Environment
obj.EnvironmentGroupId = name.Envgroup

return obj
}())
opPrefix := fmt.Sprintf("organizations/%s/envgroups/%s", name.Organization, name.Envgroup)
return s.operations.DoneLRO(ctx, opPrefix, opMetadata, obj)
}

func (s *envgroupAttachmentServer) GetOrganizationsEnvgroupsAttachment(ctx context.Context, req *pb.GetOrganizationsEnvgroupsAttachmentRequest) (*pb.GoogleCloudApigeeV1EnvironmentGroupAttachment, error) {
Expand All @@ -79,7 +75,7 @@ func (s *envgroupAttachmentServer) GetOrganizationsEnvgroupsAttachment(ctx conte
return obj, nil
}

func (s *envgroupAttachmentServer) DeleteEnvgroupAttachment(ctx context.Context, req *pb.DeleteOrganizationsEnvgroupsAttachmentRequest) (*longrunningpb.Operation, error) {
func (s *envgroupAttachmentServer) DeleteOrganizationsEnvgroupsAttachment(ctx context.Context, req *pb.DeleteOrganizationsEnvgroupsAttachmentRequest) (*longrunningpb.Operation, error) {
name, err := s.parseEnvgroupAttachmentName(req.GetName())
if err != nil {
return nil, err
Expand All @@ -96,42 +92,35 @@ func (s *envgroupAttachmentServer) DeleteEnvgroupAttachment(ctx context.Context,
State: "FINISHED",
TargetResourceName: fqn,
}
opPrefix := fmt.Sprintf("projects/%d/environments/%s/envgroups/%s", name.Project.Number, name.Environment, name.Envgroup)
opPrefix := fmt.Sprintf("organizations/%s/envgroups/%s", name.Organization, name.Envgroup)
return s.operations.DoneLRO(ctx, opPrefix, opMetadata, &emptypb.Empty{})
}

// There is no UPDATE func for this API based on the gneerated proto
// There is no UPDATE func for this API based on the generated proto

// EnvgroupAttachmentName represents a "fully qualified name" for an EnvgroupAttachment resource.
type envgroupAttachmentName struct {
Project *projects.ProjectData
Environment string
Organization string
Envgroup string
AttachmentID string
}

func (n *envgroupAttachmentName) String() string {
return fmt.Sprintf("projects/%s/environments/%s/envgroups/%s/attachments/%s", n.Project.ID, n.Environment, n.Envgroup, n.AttachmentID)
return fmt.Sprintf("organizations/%s/envgroups/%s/attachments/%s", n.Organization, n.Envgroup, n.AttachmentID)
}

// parseEnvgroupAttachmentName parses the given name string into a envgroupAttachmentName struct.
// The expected format is: projects/<projectID>/environments/<environment>/envgroups/<envgroup>/attachments/<attachmentID>.
// The expected format is: organizations/<organization>/envgroups/<envgroup>/attachments/<attachmentID>.
func (s *MockService) parseEnvgroupAttachmentName(name string) (*envgroupAttachmentName, error) {
split := strings.Split(name, "/")
if len(split) != 8 || split[0] != "projects" || split[2] != "environments" || split[4] != "envgroups" || split[6] != "attachments" {
if len(split) != 6 || split[0] != "organizations" || split[2] != "envgroups" || split[4] != "attachments" {
return nil, status.Errorf(codes.InvalidArgument, "invalid envgroup attachment name: %q", name)
}

project, err := s.Projects.GetProjectByID(split[1])
if err != nil {
return nil, err
}

result := &envgroupAttachmentName{
Project: project,
Environment: split[3],
Envgroup: split[5],
AttachmentID: split[7],
Organization: split[1],
Envgroup: split[3],
AttachmentID: split[5],
}
return result, nil
}
2 changes: 2 additions & 0 deletions mockgcp/mockapigee/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterOrganizationsEnvironmentsServerServer(grpcServer, &environmentsServer{MockService: s})
pb.RegisterOrganizationsEnvgroupsServerServer(grpcServer, &EnvgroupV1{MockService: s})
pb.RegisterOrganizationsServerServer(grpcServer, &organizationsServer{MockService: s})
pb.RegisterOrganizationsEnvgroupsAttachmentsServerServer(grpcServer, &envgroupAttachmentServer{MockService: s})
}

func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (http.Handler, error) {
mux, err := httpmux.NewServeMux(ctx, conn, httpmux.Options{},
pb.RegisterOrganizationsEnvironmentsServerHandler,
pb.RegisterOrganizationsEnvgroupsServerHandler,
pb.RegisterOrganizationsServerHandler,
pb.RegisterOrganizationsEnvgroupsAttachmentsServerHandler,
s.operations.RegisterOperationsPath("/v1/{prefix=**}/operations/{name}"))
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2025 Google LLC
#
# 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.

apiVersion: apigee.cnrm.cloud.google.com/v1alpha1
kind: ApigeeEnvgroupAttachment
metadata:
name: my-env-group-attachment
spec:
environment: apigeeenvgroup-${uniqueId}
envgroupId: apigeeenvgroup-${uniqueId}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2025 Google LLC
#
# 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.

apiVersion: apigee.cnrm.cloud.google.com/v1beta1
kind: ApigeeEnvgroup
metadata:
name: apigeeenvgroup-${uniqueId}
spec:
hostnames:
- ${uniqueId}.mytesthost.net
organizationRef:
external: organizations/${projectId}
---
apiVersion: apigee.cnrm.cloud.google.com/v1beta1
kind: ApigeeEnvironment
metadata:
name: apigeeenvgroup-${uniqueId}
spec:
apigeeOrganizationRef:
external: organizations/${projectId}

0 comments on commit 04c17c3

Please sign in to comment.