From dcc19b4bb6e2e5179cf45232f596a27c9328f203 Mon Sep 17 00:00:00 2001 From: Arunav Patgiri <46104369+jeetpatgiri@users.noreply.github.com> Date: Tue, 28 Nov 2023 18:50:35 +0530 Subject: [PATCH] CIEDEV-2144: Expand Version Manager service to enable publishing of releases (#54) * CIEDEV-2144: Verison manager calls releaase Publish API * Version service call release publish service * Initial refactoring * Using release service endpoint * Added helper function * Update internal/module/services/version_manager/version_manager.go Co-authored-by: Adam Charrett <73886859+adcharre@users.noreply.github.com> * Update internal/module/services/version_manager/version_manager.go Co-authored-by: Adam Charrett <73886859+adcharre@users.noreply.github.com> * Update internal/module/services/version_manager/version_manager.go Co-authored-by: Adam Charrett <73886859+adcharre@users.noreply.github.com> * Fix Test_BeginVersion - revert changes * Remove unused variable --------- Co-authored-by: vmilovanovicc Co-authored-by: aahamed Co-authored-by: Vesna Milovanovic <75800171+vmilovanovicc@users.noreply.github.com> Co-authored-by: Adam Charrett <73886859+adcharre@users.noreply.github.com> --- cmd/version_manager.go | 13 +++-- .../version_manager/version_manager.go | 49 ++++++++++++++++--- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/cmd/version_manager.go b/cmd/version_manager.go index 106791a..2fe6457 100644 --- a/cmd/version_manager.go +++ b/cmd/version_manager.go @@ -1,10 +1,10 @@ package cmd import ( + "github.com/spf13/cobra" "github.com/terrariumcloud/terrarium/internal/module/services/version_manager" + "github.com/terrariumcloud/terrarium/internal/release/services/release" "github.com/terrariumcloud/terrarium/internal/storage" - - "github.com/spf13/cobra" ) var versionManagerCmd = &cobra.Command{ @@ -16,15 +16,18 @@ var versionManagerCmd = &cobra.Command{ func init() { rootCmd.AddCommand(versionManagerCmd) + rootCmd.AddCommand(releaseServiceCmd) versionManagerCmd.Flags().StringVarP(&version_manager.VersionsTableName, "table", "t", version_manager.DefaultVersionsTableName, "Module versions table name") + releaseServiceCmd.Flags().StringVarP(&release.ReleaseServiceEndpoint, "release", "", release.DefaultReleaseServiceEndpoint, "GRPC Endpoint for Release Service") } func runVersionManager(cmd *cobra.Command, args []string) { versionManagerServer := &version_manager.VersionManagerService{ - Db: storage.NewDynamoDbClient(awsAccessKey, awsSecretKey, awsRegion), - Table: version_manager.VersionsTableName, - Schema: version_manager.GetModuleVersionsSchema(version_manager.VersionsTableName), + Db: storage.NewDynamoDbClient(awsAccessKey, awsSecretKey, awsRegion), + Table: version_manager.VersionsTableName, + Schema: version_manager.GetModuleVersionsSchema(version_manager.VersionsTableName), + ReleaseServiceEndpoint: release.ReleaseServiceEndpoint, } startGRPCService("version-manager", versionManagerServer) diff --git a/internal/module/services/version_manager/version_manager.go b/internal/module/services/version_manager/version_manager.go index 3d37988..96e07b1 100644 --- a/internal/module/services/version_manager/version_manager.go +++ b/internal/module/services/version_manager/version_manager.go @@ -3,12 +3,15 @@ package version_manager import ( "context" "log" + "strings" "time" + releasePkg "github.com/terrariumcloud/terrarium/pkg/terrarium/release" + "github.com/terrariumcloud/terrarium/internal/module/services" + releaseSvc "github.com/terrariumcloud/terrarium/internal/release/services" "github.com/terrariumcloud/terrarium/internal/storage" terrarium "github.com/terrariumcloud/terrarium/pkg/terrarium/module" - "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -42,13 +45,15 @@ var ( CreateModuleVersionError = status.Error(codes.Unknown, "Failed to create module version.") AbortModuleVersionError = status.Error(codes.Unknown, "Failed to abort module version.") PublishModuleVersionError = status.Error(codes.Unknown, "Failed to publish module version.") + DevelopmentVersion = versions.MustParseVersion("0.0.0") ) type VersionManagerService struct { services.UnimplementedVersionManagerServer - Db storage.DynamoDBTableCreator - Table string - Schema *dynamodb.CreateTableInput + Db storage.DynamoDBTableCreator + Table string + Schema *dynamodb.CreateTableInput + ReleaseServiceEndpoint string } type ModuleVersion struct { @@ -64,7 +69,6 @@ func (s *VersionManagerService) RegisterWithServer(grpcServer grpc.ServiceRegist log.Println(err) return ModuleVersionsTableInitializationError } - services.RegisterVersionManagerServer(grpcServer, s) return nil @@ -156,6 +160,7 @@ func (s *VersionManagerService) AbortVersion(ctx context.Context, request *servi } // PublishVersion Updates Module Version to published with Version Manager service +// And publishes a release. func (s *VersionManagerService) PublishVersion(ctx context.Context, request *services.TerminateVersionRequest) (*terrarium.Response, error) { log.Println("Publishing module version.") @@ -164,7 +169,6 @@ func (s *VersionManagerService) PublishVersion(ctx context.Context, request *ser attribute.String("module.name", request.Module.GetName()), attribute.String("module.version", request.Module.GetVersion()), ) - moduleKey, err := s.GetModuleKey(request.Module) if err != nil { span.RecordError(err) @@ -194,6 +198,35 @@ func (s *VersionManagerService) PublishVersion(ctx context.Context, request *ser return nil, PublishModuleVersionError } + // PUBLISH RELEASE + parsedVersion, err := versions.ParseVersion(strings.ReplaceAll(request.Module.GetVersion(), "v", "")) + if err != nil { + span.RecordError(err) + return nil, err + } + + if parsedVersion.GreaterThan(DevelopmentVersion) && s.ReleaseServiceEndpoint != "" { + moduleAddress := strings.Split(request.Module.GetName(), "/") + orgName := moduleAddress[0] + + if connVersion, err := services.CreateGRPCConnection(s.ReleaseServiceEndpoint); err != nil { + span.RecordError(err) + log.Printf("Failed to connect to '%s': %v", s.ReleaseServiceEndpoint, err) + } else { + defer closeClient(connVersion) + + client := releaseSvc.NewPublisherClient(connVersion) + if _, err := client.Publish(ctx, &releasePkg.PublishRequest{ + Name: request.Module.GetName(), + Version: request.Module.GetVersion(), + Type: "module", + Organization: orgName, + }); err != nil { + span.RecordError(err) + } + } + } + log.Println("Module version published.") return VersionPublished, nil } @@ -285,3 +318,7 @@ func GetModuleVersionsSchema(table string) *dynamodb.CreateTableInput { BillingMode: types.BillingModePayPerRequest, } } + +func closeClient(conn *grpc.ClientConn) { + _ = conn.Close() +}