diff --git a/momentum-core/momentum-core b/momentum-core/momentum-core index 24b4887..07cbdbf 100755 Binary files a/momentum-core/momentum-core and b/momentum-core/momentum-core differ diff --git a/momentum-core/services/application-service.go b/momentum-core/services/application-service.go index 2726694..8fcb737 100644 --- a/momentum-core/services/application-service.go +++ b/momentum-core/services/application-service.go @@ -100,7 +100,7 @@ func (as *ApplicationService) AddApplication(request *models.ApplicationCreateRe return nil, errors.New("unable to find kustomization resources for repository of new application") } - err = repositoryKustomizationResources.AddValue(request.Name, 0) + err = repositoryKustomizationResources.AddYamlValue(request.Name, 0) if err != nil { config.LOGGER.LogWarning("failed adding application to resources", err, traceId) return nil, err diff --git a/momentum-core/services/deployment-service.go b/momentum-core/services/deployment-service.go index 81149a8..a1e6852 100644 --- a/momentum-core/services/deployment-service.go +++ b/momentum-core/services/deployment-service.go @@ -4,7 +4,6 @@ import ( "errors" "momentum-core/config" "momentum-core/models" - "momentum-core/tree" "momentum-core/utils" ) @@ -108,14 +107,12 @@ func (ds *DeploymentService) AddDeployment(request *models.DeploymentCreateReque } if parentStageKustomizationResources != nil { - err = parentStageKustomizationResources.AddValue(deploymentYamlDestinationName, 0) + err = parentStageKustomizationResources.AddYamlValue(deploymentYamlDestinationName, 0) if err != nil { config.LOGGER.LogWarning("failed adding deployment to resources", err, traceId) return nil, err } - tree.Print(parentStageKustomizationResources) - err = parentStageKustomizationResources.Write(true) if err != nil { config.LOGGER.LogError("failed writing deployment to resources", err, traceId) @@ -128,14 +125,12 @@ func (ds *DeploymentService) AddDeployment(request *models.DeploymentCreateReque return nil, err } - err = parentStageKustomization.AddSequence("resources", []string{deploymentYamlDestinationName}, 0) + err = parentStageKustomization.AddYamlSequence("resources", []string{deploymentYamlDestinationName}, 0) if err != nil { config.LOGGER.LogError("unable to add resources sequence to parents stage kustomization", err, traceId) return nil, err } - tree.Print(parentStageKustomization) - err = parentStageKustomization.Write(true) if err != nil { config.LOGGER.LogError("failed writing deployment to resources", err, traceId) diff --git a/momentum-core/services/stage-service.go b/momentum-core/services/stage-service.go index d2d13c8..7c35d23 100644 --- a/momentum-core/services/stage-service.go +++ b/momentum-core/services/stage-service.go @@ -125,7 +125,7 @@ func (s *StageService) AddStage(request *models.StageCreateRequest, traceId stri return nil, err } - err = kustomizationFileNode.AddSequence("resources", []string{request.Name}, 0) + err = kustomizationFileNode.AddYamlSequence("resources", []string{request.Name}, 0) if err != nil { config.LOGGER.LogError(err.Error(), err, traceId) return nil, err @@ -137,7 +137,7 @@ func (s *StageService) AddStage(request *models.StageCreateRequest, traceId stri return nil, err } } else { - err = parentKustomizationResources.AddValue(request.Name, 0) + err = parentKustomizationResources.AddYamlValue(request.Name, 0) if err != nil { config.LOGGER.LogWarning("failed adding stage to resources", err, traceId) return nil, err diff --git a/momentum-core/tree/momentum-tree.go b/momentum-core/tree/momentum-tree.go index d44b5a6..b5bbba1 100644 --- a/momentum-core/tree/momentum-tree.go +++ b/momentum-core/tree/momentum-tree.go @@ -1,7 +1,6 @@ package tree import ( - "fmt" "momentum-core/config" "strings" ) @@ -44,7 +43,6 @@ func (n *Node) stages() []*Node { nodesStages := make([]*Node, 0) for _, possibleStage := range n.Directories() { - fmt.Println(possibleStage.Id, ":", possibleStage.FullPath()) if possibleStage.Kind == Directory && !strings.HasPrefix(possibleStage.Path, META_PREFIX) { childStages := possibleStage.stages() nodesStages = append([]*Node{possibleStage}, childStages...) diff --git a/momentum-core/tree/tree.go b/momentum-core/tree/tree.go index 09ad40f..5b16b90 100644 --- a/momentum-core/tree/tree.go +++ b/momentum-core/tree/tree.go @@ -214,74 +214,6 @@ func (n *Node) FileMapping() (*Node, error) { return nil, errors.New("file has no mapping node") } -func (n *Node) AddSequence(key string, values []string, style yaml.Style) error { - - if len(values) < 1 { - return errors.New("sequence must have at least one value") - } - - yamlSequenceNode := CreateSequenceNode(key, style) - sequenceNode := NewNode(Sequence, key, "", nil, nil, yamlSequenceNode) - - for _, val := range values { - err := sequenceNode.AddValue(val, 0) - if err != nil { - return err - } - } - - return AddContent(n, sequenceNode) -} - -func (n *Node) AddMapping(key string, style yaml.Style) (*Node, error) { - - return nil, errors.New("AddMapping is not yet ready!! see FILESYSTEMTEST_TestAddMapping") - - if n.Kind != File && n.Kind != Mapping && n.Kind != Sequence { - return nil, errors.New("can only add mapping to type of kind file mapping or sequence") - } - - yamlMappingNode := CreateMappingNode(key, style) - mappingNode := NewNode(Mapping, key, "", nil, nil, yamlMappingNode) - - err := AddContent(n, mappingNode) - if err != nil { - return nil, err - } - - return mappingNode, nil -} - -func (n *Node) AddProperty(key string, value string, valueTag string, style yaml.Style) error { - - if n.Kind != Mapping || n.YamlNode.Kind != yaml.MappingNode { - return errors.New("properties can only be added to mapping nodes") - } - - yamlKeyNode, yamlValueNode := CreatePropertyNodes(key, value, valueTag, style) - - mappingNode := NewNode(Property, key, value, n, nil, yamlValueNode) - - n.YamlNode.Content = append(n.YamlNode.Content, yamlKeyNode, yamlValueNode) - n.AddChild(mappingNode) - - return nil -} - -func (n *Node) AddValue(value string, style yaml.Style) error { - - if n.Kind != Sequence || n.YamlNode.Kind != yaml.SequenceNode { - return errors.New("can only add sequence value to node of type sequence") - } - - sequenceValue := CreateScalarNode(value, StrTag, style) - n.YamlNode.Content = append(n.YamlNode.Content, sequenceValue) - momentumNode := NewNode(Value, "", value, n, nil, sequenceValue) - n.AddChild(momentumNode) - - return nil -} - func idMatch(expect string, n *Node) bool { id, err := utils.GenerateId(n.FullPath()) diff --git a/momentum-core/tree/yaml-node-factory.go b/momentum-core/tree/yaml-node-factory.go index 073a5f9..cd9ed12 100644 --- a/momentum-core/tree/yaml-node-factory.go +++ b/momentum-core/tree/yaml-node-factory.go @@ -19,35 +19,86 @@ const ( mergeTag = "!!merge" ) -func AddContent(parent *Node, content *Node) error { +func (n *Node) AddYamlSequence(key string, values []string, style yaml.Style) error { - if parent == nil || content == nil { - return errors.New("parent or content is nil") + if len(values) < 1 { + return errors.New("sequence must have at least one value") } - if parent.YamlNode.Kind != yaml.DocumentNode && - parent.YamlNode.Kind != yaml.MappingNode && - parent.YamlNode.Kind != yaml.SequenceNode { - return errors.New("can only add content to document mapping and sequence yaml nodes") + var err error = nil + var anchor *Node = n + if n.Kind == File { + anchor, err = n.FileMapping() + if err != nil { + return errors.New("failed retrieving file mapping of file node") + } } - if parent.Kind != File && - parent.Kind != Mapping && - parent.Kind != Sequence { - return errors.New("can only add content to file mapping and sequence nodes") + yamlSequenceName := CreateScalarNode(key, StrTag, 0) + yamlSequenceNode := CreateSequenceNode(style) + anchor.YamlNode.Content = append(anchor.YamlNode.Content, yamlSequenceName, yamlSequenceNode) + + sequenceNode := NewNode(Sequence, key, "", nil, nil, yamlSequenceNode) + anchor.AddChild(sequenceNode) + + for _, val := range values { + err := sequenceNode.AddYamlValue(val, 0) + if err != nil { + return err + } } + return nil +} + +func (n *Node) AddYamlMapping(key string, style yaml.Style) (*Node, error) { + var err error = nil - p := parent - if parent.Kind == File { - p, err = parent.FileMapping() + var anchor *Node = n + if n.Kind == File { + anchor, err = n.FileMapping() if err != nil { - return errors.New("unable to retrieve parent files mapping") + return nil, err } } - p.YamlNode.Content = append(p.YamlNode.Content, content.YamlNode) - p.AddChild(content) + yamlMappingName := CreateScalarNode(key, StrTag, 0) + yamlMappingNode := CreateMappingNode(key, style) + + anchor.YamlNode.Content = append(anchor.YamlNode.Content, yamlMappingName, yamlMappingNode) + + mappingNode := NewNode(Mapping, key, "", nil, nil, yamlMappingNode) + anchor.AddChild(mappingNode) + + return mappingNode, nil +} + +func (n *Node) AddYamlProperty(key string, value string, valueTag string, style yaml.Style) error { + + if n.Kind != Mapping || n.YamlNode.Kind != yaml.MappingNode { + return errors.New("properties can only be added to mapping nodes") + } + + yamlKeyNode, yamlValueNode := CreatePropertyNodes(key, value, valueTag, style) + + mappingNode := NewNode(Property, key, value, n, nil, yamlValueNode) + + n.YamlNode.Content = append(n.YamlNode.Content, yamlKeyNode, yamlValueNode) + n.AddChild(mappingNode) + + return nil +} + +func (n *Node) AddYamlValue(value string, style yaml.Style) error { + + if n.Kind != Sequence || n.YamlNode.Kind != yaml.SequenceNode { + return errors.New("can only add sequence value to node of type sequence") + } + + sequenceValue := CreateScalarNode(value, StrTag, style) + n.YamlNode.Content = append(n.YamlNode.Content, sequenceValue) + momentumNode := NewNode(Value, "", value, n, nil, sequenceValue) + n.AddChild(momentumNode) return nil } @@ -60,13 +111,13 @@ func CreatePropertyNodes(key string, value string, valueTag string, style yaml.S return keyNode, valueNode } -func CreateSequenceNode(key string, style yaml.Style) *yaml.Node { +func CreateSequenceNode(style yaml.Style) *yaml.Node { n := new(yaml.Node) n.Kind = yaml.SequenceNode n.Tag = seqTag - n.Value = key + n.Value = "" n.Style = style return n @@ -78,7 +129,7 @@ func CreateMappingNode(key string, style yaml.Style) *yaml.Node { n.Kind = yaml.MappingNode n.Tag = mapTag - n.Value = key + n.Value = "" n.Style = style return n diff --git a/momentum-core/tree/yaml-node-factory_test.go b/momentum-core/tree/yaml-node-factory_test.go index 10ba0aa..3fd4ced 100644 --- a/momentum-core/tree/yaml-node-factory_test.go +++ b/momentum-core/tree/yaml-node-factory_test.go @@ -39,8 +39,10 @@ func FILESYSTEMTEST_TestAddSequenceValue(t *testing.T) { t.FailNow() } + tree.Print(parsed) + resourcesSequence.Children[0].SetValue("Ciao") - err = resourcesSequence.AddValue("World", 0) + err = resourcesSequence.AddYamlValue("World", 0) if err != nil { fmt.Println(err.Error()) t.FailNow() @@ -66,7 +68,7 @@ func FILESYSTEMTEST_TestAddSequenceValue(t *testing.T) { t.FailNow() } - tree.Print(resourcesSequence) + tree.Print(afterWriteTree) if len(resourcesSequence.Children) != 2 { fmt.Println("expected two children in resources") @@ -111,7 +113,7 @@ func FILESYSTEMTEST_TestAddSequence(t *testing.T) { utils.FileDelete(TEST_FILE_PATH) - err = parsed.AddSequence("sequence", []string{"value"}, 0) + err = parsed.AddYamlSequence("sequence", []string{"value"}, 0) if err != nil { fmt.Println("failed to write sequence:", err.Error()) t.FailNow() @@ -131,9 +133,13 @@ func FILESYSTEMTEST_TestAddSequence(t *testing.T) { t.FailNow() } - tree.Print(parsed) + _, found := parsed.FindFirst("sequence") + if !found { + fmt.Println("unable to find new sequence in testfile after update") + t.FailNow() + } - utils.FileDelete(TEST_FILE_PATH) + tree.Print(parsed) } func FILESYSTEMTEST_TestAddMapping(t *testing.T) { @@ -159,8 +165,6 @@ func FILESYSTEMTEST_TestAddMapping(t *testing.T) { t.FailNow() } - utils.FileDelete(TEST_FILE_PATH) - fileMapping, err := parsed.FileMapping() if err != nil { fmt.Println("unable to find file mapping", err.Error()) @@ -169,13 +173,13 @@ func FILESYSTEMTEST_TestAddMapping(t *testing.T) { fmt.Println("FILEMAP:", fileMapping) - mappingNode, err := fileMapping.AddMapping("mapping-key", 0) + mappingNode, err := fileMapping.AddYamlMapping("mapping-key", 0) if err != nil { fmt.Println("creating mapping node failed:", err.Error()) t.FailNow() } - err = mappingNode.AddProperty("subprob", "subpropvalue", tree.StrTag, 0) + err = mappingNode.AddYamlProperty("subprob", "subpropvalue", tree.StrTag, 0) if err != nil { fmt.Println("creating mappings property node failed:", err.Error()) t.FailNow() @@ -202,8 +206,6 @@ func FILESYSTEMTEST_TestAddMapping(t *testing.T) { fmt.Println("mapping was not added as expected") t.FailNow() } - - utils.FileDelete(TEST_FILE_PATH) } func FILESYSTEMTEST_TestAddProperty(t *testing.T) { @@ -221,6 +223,7 @@ func FILESYSTEMTEST_TestAddProperty(t *testing.T) { fmt.Println("unable to instantiate testfile") t.FailNow() } + defer utils.FileDelete(TEST_FILE_PATH) parsed, err := tree.Parse(TEST_FILE_PATH) if err != nil { @@ -228,15 +231,13 @@ func FILESYSTEMTEST_TestAddProperty(t *testing.T) { t.FailNow() } - utils.FileDelete(TEST_FILE_PATH) - fileMapping, err := parsed.FileMapping() if err != nil { fmt.Println("unable to find file mapping", err.Error()) t.FailNow() } - fileMapping.AddProperty("property-key", "property-value", tree.StrTag, 0) + fileMapping.AddYamlProperty("property-key", "property-value", tree.StrTag, 0) err = parsed.Write(true) if err != nil { @@ -252,11 +253,15 @@ func FILESYSTEMTEST_TestAddProperty(t *testing.T) { tree.Print(parsed) - utils.FileDelete(TEST_FILE_PATH) + _, found := parsed.FindFirst("property-key") + if !found { + fmt.Println("mapping was not added as expected") + t.FailNow() + } } func writeTestFile(p string) bool { - return utils.FileWriteLines(p, strings.Split("kind: Test\nresources:\n- \"Hello\"", "\n")) + return utils.FileWriteLines(p, strings.Split("kind: Test\nresources:\n- \"Hello\"\nprop: \"value\"", "\n")) } func cleanup(t *testing.T, p string) {