From 65f7d8525cdc9339220b6a56b5818cec242c9cdc Mon Sep 17 00:00:00 2001 From: Srevin Saju Date: Tue, 23 Nov 2021 20:51:28 +0300 Subject: [PATCH] feat: add patch command wip implementation --- cmd/go-cat/cli.go | 14 +++++++++ cmd/go-cat/main.go | 12 ++++++-- infrastructure/operations.go | 40 ++++++++++++++++++++++++++ ops/patch.go | 56 ++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 ops/patch.go diff --git a/cmd/go-cat/cli.go b/cmd/go-cat/cli.go index d43675e..09eca84 100644 --- a/cmd/go-cat/cli.go +++ b/cmd/go-cat/cli.go @@ -93,6 +93,20 @@ func upsertInfrastructureCliContext(context *cli.Context) error { return nil } + +func patchInfrastructureCliContext(context *cli.Context) error { + // + // cloud -> cloud-project-id -> subsystem -> name + i := newInfrastructureFromCliContext(context) + i.GetId() + + err := ops.Patch(config.NewGlobalConfigFromCliContext(context), i, context.Args().First()) + if err != nil { + return err + } + return nil +} + func removeInfrastructureCliContext(context *cli.Context) error { id := context.String("id") return ops.Remove(config.NewGlobalConfigFromCliContext(context), id) diff --git a/cmd/go-cat/main.go b/cmd/go-cat/main.go index 70a4526..23e779d 100644 --- a/cmd/go-cat/main.go +++ b/cmd/go-cat/main.go @@ -22,11 +22,12 @@ func main() { } infraFlags := []cli.Flag{ &cli.StringFlag{Name: "name", Usage: "Name of the service or endpoint"}, - &cli.StringFlag{Name: "type", Usage: "Type of infrastructure on which the service is deployed to"}, - &cli.StringFlag{Name: "commit-sha", Usage: "Deployed Commit SHA"}, &cli.StringFlag{Name: "cloud", Usage: "Name of the cloud, example: gcp, aws"}, &cli.StringFlag{Name: "cloud-project-id", Usage: "A unique identifier of the project / environment in which the service is deployed"}, &cli.StringFlag{Name: "subsystem", Usage: "Name of the parent project, to which the child service belongs to"}, + + &cli.StringFlag{Name: "type", Usage: "Type of infrastructure on which the service is deployed to"}, + &cli.StringFlag{Name: "commit-sha", Usage: "Deployed Commit SHA"}, &cli.StringFlag{Name: "deployment-link", Usage: "The HTTP URL or access endpoint of the API or service"}, &cli.StringFlag{Name: "deployment-links", Usage: "Multiple HTTP URLs or access endpoint of the API or service, separated by comma"}, &cli.StringFlag{Name: "parameters", Usage: "Additional parameters"}, @@ -57,6 +58,13 @@ func main() { Flags: gitFlags, }, + { + Name: "patch", + Usage: "Patch changes to an individual unit of infrastructure", + Action: patchInfrastructureCliContext, + + Flags: append(gitFlags, infraFlags[5:]...), + }, { Name: "cat", Usage: "Read the infra.json file to stdout", diff --git a/infrastructure/operations.go b/infrastructure/operations.go index 8e6821b..12f1b20 100644 --- a/infrastructure/operations.go +++ b/infrastructure/operations.go @@ -47,3 +47,43 @@ func (infraMeta *MetadataGroup) Add(infra *Metadata) (*MetadataGroup, error) { infras = append(infras, infra) return &MetadataGroup{Infra: infras, Version: "1", UpdatedAt: time.Now()}, nil } + + +// Patch updates InfrastructureMetadata with Infrastructure, duplicate infrastructure +// is merged with each other, and the final InfrastructureMetadata is returned +func (infraMeta *MetadataGroup) Patch(infra *Metadata) (*MetadataGroup, error) { + infra.DeployedOn = time.Now() + infras := infraMeta.Infra + for i := range infras { + newInfra := infras[i] + logger.Info(infra.GetId(), newInfra.GetId()) + if newInfra.GetId() == infra.GetId() { + // we found a match of the same infra, but probably older + if infra.Parameters != nil { + newInfra.Parameters = infra.Parameters + } + if infra.MonitoringLink != "" { + newInfra.MonitoringLink = infra.MonitoringLink + } + if infra.DeploymentLinks != nil { + newInfra.DeploymentLinks = infra.DeploymentLinks + } + if infra.DeploymentLink != "" { + newInfra.DeploymentLink = infra.DeploymentLink + } + if infra.CommitSha != "" { + newInfra.CommitSha = infra.CommitSha + } + if infra.Type != "" { + newInfra.Type = infra.Type + } + infras[i] = newInfra + logger.Info(newInfra) + + return &MetadataGroup{Infra: infras, Version: "1", UpdatedAt: time.Now()}, nil + } + } + infras = append(infras, infra) + return &MetadataGroup{Infra: infras, Version: "1", UpdatedAt: time.Now()}, nil +} + diff --git a/ops/patch.go b/ops/patch.go new file mode 100644 index 0000000..c303aab --- /dev/null +++ b/ops/patch.go @@ -0,0 +1,56 @@ +package ops + +import ( + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-git/v5" + "gitlab.com/sorcero/community/go-cat/config" + "gitlab.com/sorcero/community/go-cat/infrastructure" + "gitlab.com/sorcero/community/go-cat/parser" + "gitlab.com/sorcero/community/go-cat/storage" +) + +// Patch pulls the git repository, edits/updates the infrastructure metadata +// and pushes the repository back. All processes happen within an im-memory +// git storage system to minimize moving parts +func Patch(cfg config.GlobalConfig, infra *infrastructure.Metadata, args string) error { + repo, fs, err := storage.Clone(cfg) + if err != nil { + return err + } + logger.Info("AAA", args) + infra.Id = args + logger.Info("infra.id", infra.Id) + return PatchFromStorage(cfg, repo, fs, infra) +} + +// PatchFromStorage parses the provided argument storage for infrastructure +// updates the infrastructure metadata +// and pushes the repository back. All processes happen within an im-memory +// git storage system to minimize moving parts +func PatchFromStorage(cfg config.GlobalConfig, repo *git.Repository, fs billy.Filesystem, infra *infrastructure.Metadata) error { + infraJson, err := storage.ReadInfraDb(fs) + if err != nil { + return err + } + + logger.Info("AAAA", infra) + + logger.Info("Adding infrastructure") + infraMeta, err := parser.ReadInfrastructureFromJson(infraJson) + if err != nil { + return err + } + infraMeta, _ = infraMeta.Patch(infra) + + readmeString, infraJson, err := parser.InfrastructureMetaToString(infraMeta) + if err != nil { + panic(err) + } + + err = updateRepository(cfg, repo, fs, readmeString, infraJson) + if err != nil { + return err + } + return nil +} + -- GitLab