Skip to content

Commit

Permalink
CIEDEV-2144: Expand Version Manager service to enable publishing of r…
Browse files Browse the repository at this point in the history
…eleases (#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 <[email protected]>

* Update internal/module/services/version_manager/version_manager.go

Co-authored-by: Adam Charrett <[email protected]>

* Update internal/module/services/version_manager/version_manager.go

Co-authored-by: Adam Charrett <[email protected]>

* Fix Test_BeginVersion - revert changes

* Remove unused variable

---------

Co-authored-by: vmilovanovicc <[email protected]>
Co-authored-by: aahamed <[email protected]>
Co-authored-by: Vesna Milovanovic <[email protected]>
Co-authored-by: Adam Charrett <[email protected]>
  • Loading branch information
5 people authored Nov 28, 2023
1 parent 633f710 commit dcc19b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
13 changes: 8 additions & 5 deletions cmd/version_manager.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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)
Expand Down
49 changes: 43 additions & 6 deletions internal/module/services/version_manager/version_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -64,7 +69,6 @@ func (s *VersionManagerService) RegisterWithServer(grpcServer grpc.ServiceRegist
log.Println(err)
return ModuleVersionsTableInitializationError
}

services.RegisterVersionManagerServer(grpcServer, s)

return nil
Expand Down Expand Up @@ -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.")

Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -285,3 +318,7 @@ func GetModuleVersionsSchema(table string) *dynamodb.CreateTableInput {
BillingMode: types.BillingModePayPerRequest,
}
}

func closeClient(conn *grpc.ClientConn) {
_ = conn.Close()
}

0 comments on commit dcc19b4

Please sign in to comment.