Skip to content

Commit

Permalink
mockgcp: apigeeorganization
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pana <[email protected]>
  • Loading branch information
acpana committed Jan 22, 2025
1 parent 7f572ec commit bbfc9e2
Show file tree
Hide file tree
Showing 9 changed files with 1,404 additions and 15 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: "ApigeeOrganization"}:

case schema.GroupKind{Group: "apikeys.cnrm.cloud.google.com", Kind: "APIKeysKey"}:

Expand Down
216 changes: 216 additions & 0 deletions mockgcp/mockapigee/organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright 2024 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.

package mockapigee

import (
"context"
"fmt"
"sort"
"strings"
"time"

"cloud.google.com/go/longrunning/autogen/longrunningpb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"

pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/apigee/v1"
)

type organizationsServer struct {
*MockService
pb.UnimplementedOrganizationsServerServer
}

func (s *organizationsServer) CreateOrganization(ctx context.Context, req *pb.CreateOrganizationRequest) (*longrunningpb.Operation, error) {
var name *OrganizationName

projectID := ""

parent := req.GetParent()
tokens := strings.Split(parent, "/")
if len(tokens) == 2 && tokens[0] == "projects" {
project, err := s.Projects.GetProjectByID(tokens[1])
if err != nil {
return nil, err
}
projectID = project.ID

// Name is same as project ID
name = &OrganizationName{
ID: project.ID,
}
} else {
return nil, status.Errorf(codes.InvalidArgument, "parent %q is not valid", parent)
}

now := time.Now()

fqn := name.String()

obj := proto.Clone(req.GetOrganization()).(*pb.GoogleCloudApigeeV1Organization)
obj.Name = name.ID
obj.CreatedAt = now.UnixMilli()
obj.LastModifiedAt = now.UnixMilli()
obj.ProjectId = projectID
obj.State = "ACTIVE"

obj.BillingType = "EVALUATION"
obj.SubscriptionType = "TRIAL"

expiresAt := now.Add(60 * 24 * time.Hour)
obj.ExpiresAt = expiresAt.UnixMilli()

obj.CaCertificate = []byte("LS0t...")

if obj.AddonsConfig != nil {
if obj.AddonsConfig.MonetizationConfig != nil {
if !obj.AddonsConfig.MonetizationConfig.GetEnabled() {
obj.AddonsConfig.MonetizationConfig = nil
}
}
}
if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
}

prefix := "organizations/" + name.ID
opMetadata := &pb.GoogleCloudApigeeV1OperationMetadata{
OperationType: "INSERT",
State: "IN_PROGRESS",
TargetResourceName: fqn,
}
return s.operations.StartLRO(ctx, prefix, opMetadata, func() (proto.Message, error) {
opMetadata.State = "FINISHED"
return obj, nil
})
}

func (s *organizationsServer) SetAddonsOrganization(ctx context.Context, req *pb.SetAddonsOrganizationRequest) (*longrunningpb.Operation, error) {
name, err := s.parseOrganizationName(req.GetOrg())
if err != nil {
return nil, err
}
fqn := name.String()
existing := &pb.GoogleCloudApigeeV1Organization{}
if err := s.storage.Get(ctx, fqn, existing); err != nil {
return nil, err
}

updated := proto.Clone(existing).(*pb.GoogleCloudApigeeV1Organization)
updated.AddonsConfig = req.Organization.AddonsConfig

if err := s.storage.Update(ctx, fqn, updated); err != nil {
return nil, err
}

prefix := "organizations/" + name.ID
opMetadata := &pb.GoogleCloudApigeeV1OperationMetadata{
OperationType: "UPDATE",
State: "IN_PROGRESS",
TargetResourceName: fqn,
}
return s.operations.StartLRO(ctx, prefix, opMetadata, func() (proto.Message, error) {
opMetadata.State = "FINISHED"
return updated.AddonsConfig, nil
})
}

func (s *organizationsServer) UpdateOrganization(ctx context.Context, req *pb.UpdateOrganizationRequest) (*pb.GoogleCloudApigeeV1Organization, error) {
name, err := s.parseOrganizationName(req.GetName())
if err != nil {
return nil, err
}
fqn := name.String()
existing := &pb.GoogleCloudApigeeV1Organization{}
if err := s.storage.Get(ctx, fqn, existing); err != nil {
return nil, err
}

// Updates the properties for an Apigee organization. No other fields in the organization profile will be updated.

updated := proto.Clone(existing).(*pb.GoogleCloudApigeeV1Organization)
props := req.Organization.Properties
sort.Slice(props.Property, func(i, j int) bool {
return props.Property[i].Name < props.Property[j].GetName()
})
updated.Properties = props

updated.DisplayName = req.Organization.DisplayName
updated.Description = req.Organization.Description

if err := s.storage.Update(ctx, fqn, updated); err != nil {
return nil, err
}
return updated, nil
}

func (s *organizationsServer) GetOrganization(ctx context.Context, req *pb.GetOrganizationRequest) (*pb.GoogleCloudApigeeV1Organization, error) {
name, err := s.parseOrganizationName(req.GetName())
if err != nil {
return nil, err
}
fqn := name.String()
obj := &pb.GoogleCloudApigeeV1Organization{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
}
return obj, nil
}

func (s *organizationsServer) DeleteOrganization(ctx context.Context, req *pb.DeleteOrganizationRequest) (*longrunningpb.Operation, error) {
name, err := s.parseOrganizationName(req.GetName())
if err != nil {
return nil, err
}
fqn := name.String()
deletedObj := &pb.GoogleCloudApigeeV1Organization{}
if err := s.storage.Delete(ctx, fqn, deletedObj); err != nil {
return nil, err
}
prefix := "organizations/" + name.ID
opMetadata := &pb.GoogleCloudApigeeV1OperationMetadata{
OperationType: "DELETE",
State: "IN_PROGRESS",
TargetResourceName: fqn,
}
return s.operations.StartLRO(ctx, prefix, opMetadata, func() (proto.Message, error) {
opMetadata.State = "FINISHED"
return &emptypb.Empty{}, nil
})
}

type OrganizationName struct {
ID string
}

func (n *OrganizationName) String() string {
return fmt.Sprintf("organizations/%s", n.ID)
}

// parseOrganizationName parses a string into a OrganizationName.
// The expected form is `organizations/*`.
func (s *MockService) parseOrganizationName(name string) (*OrganizationName, error) {
tokens := strings.Split(name, "/")
if len(tokens) == 2 && tokens[0] == "organizations" {
name := &OrganizationName{
ID: tokens[1],
}
return name, nil
} else {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}
2 changes: 2 additions & 0 deletions mockgcp/mockapigee/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ func (s *MockService) ExpectedHosts() []string {
}

func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterOrganizationsServerServer(grpcServer, &organizationsServer{MockService: s})
pb.RegisterOrganizationsEnvgroupsServerServer(grpcServer, &EnvgroupV1{MockService: s})
}

func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (http.Handler, error) {
mux, err := httpmux.NewServeMux(ctx, conn, httpmux.Options{},
pb.RegisterOrganizationsEnvgroupsServerHandler,
pb.RegisterOrganizationsServerHandler,
s.operations.RegisterOperationsPath("/v1/{prefix=**}/operations/{name}"))
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions mockgcp/mockserviceusage/knownservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package mockserviceusage

var allServices = []string{
"apigee.googleapis.com",
"bigquery.googleapis.com",
"compute.googleapis.com",
"pubsub.googleapis.com",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: apigee.cnrm.cloud.google.com/v1beta1
kind: ApigeeOrganization
metadata:
annotations:
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/state-into-spec: absent
finalizers:
- cnrm.cloud.google.com/finalizer
- cnrm.cloud.google.com/deletion-defender
generation: 3
labels:
cnrm-test: "true"
name: apigeeorganization-${uniqueId}
namespace: ${uniqueId}
spec:
analyticsRegion: us-west1
authorizedNetworkRef:
name: default
description: An updated organization
displayName: updated-organization
projectRef:
name: project-${uniqueId}
properties:
features.hybrid.enabled: "true"
features.mart.connect.enabled: "false"
features.mart.server.endpoint: https://127.0.0.1
resourceID: example-project-01
runtimeType: CLOUD
status:
billingType: EVALUATION
caCertificate: TFMwdC4uLg==
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
message: The resource is up to date
reason: UpToDate
status: "True"
type: Ready
createdAt: "2024-04-01T12:34:56.123456Z"
expiresAt: "2024-04-01T12:34:56.123456Z"
lastModifiedAt: "2024-04-01T12:34:56.123456Z"
observedGeneration: 3
projectId: example-project-01
state: ACTIVE
subscriptionType: TRIAL
Loading

0 comments on commit bbfc9e2

Please sign in to comment.