This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from magneticio/feature/lam-244
Added merge command related to LAM-244
- Loading branch information
Showing
5 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright © 2018 Developer [email protected] | ||
// | ||
// 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 cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/magneticio/vampkubistcli/client" | ||
"github.com/magneticio/vampkubistcli/logging" | ||
"github.com/magneticio/vampkubistcli/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// mergeCmd represents the merge command | ||
var mergeCmd = &cobra.Command{ | ||
Use: "merge", | ||
Short: "Merges a resource", | ||
Long: AddAppName(`To merge a resource | ||
Run as $AppName merge resourceType ResourceName | ||
Example: | ||
$AppName merge project myproject -f project.yaml | ||
$AppName merge -p myproject cluster mycluster -f cluster.yaml`), | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 2 { | ||
return errors.New("Not Enough Arguments") | ||
} | ||
Type = args[0] | ||
Name = args[1] | ||
Source := SourceString | ||
if Source == "" { | ||
b, err := util.UseSourceUrl(SourceFile) // just pass the file name | ||
if err != nil { | ||
return err | ||
} | ||
Source = string(b) | ||
} | ||
|
||
restClient := client.NewRestClient(Config.Url, Config.Token, Config.APIVersion, logging.Verbose, Config.Cert) | ||
values := make(map[string]string) | ||
values["project"] = Config.Project | ||
values["cluster"] = Config.Cluster | ||
values["virtual_cluster"] = Config.VirtualCluster | ||
values["application"] = Application | ||
spec, getSpecError := restClient.GetSpec(Type, Name, SourceFileType, values) | ||
if getSpecError != nil { | ||
return getSpecError | ||
} | ||
|
||
updatedSource, mergeError := util.Merge(spec, Source, SourceFileType) | ||
if mergeError != nil { | ||
return mergeError | ||
} | ||
|
||
isUpdated, updateError := restClient.Update(Type, Name, updatedSource, SourceFileType, values) | ||
if !isUpdated { | ||
return updateError | ||
} | ||
fmt.Println(Type + " " + Name + " is merged") | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(mergeCmd) | ||
|
||
mergeCmd.Flags().StringVarP(&SourceString, "string", "s", "", "Source from string") | ||
mergeCmd.Flags().StringVarP(&SourceFile, "file", "f", "", "Source from file") | ||
mergeCmd.Flags().StringVarP(&SourceFileType, "input", "i", "yaml", "Resource file type yaml or json") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/magneticio/vampkubistcli/util" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMergeBasic(t *testing.T) { | ||
destination := | ||
`metadata: | ||
field1: value1 | ||
field2: value2 | ||
` | ||
source := | ||
`metadata: | ||
field1: value1updated | ||
field3: value3 | ||
` | ||
|
||
expectedMerged := | ||
`metadata: | ||
field1: value1updated | ||
field2: value2 | ||
field3: value3 | ||
` | ||
serializationType := "yaml" | ||
merged, err := util.Merge(destination, source, serializationType) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expectedMerged, merged) | ||
} | ||
|
||
func TestMergeMapInMap(t *testing.T) { | ||
destination := | ||
`metadata: | ||
field1: | ||
value1map: | ||
key1: innerValue1 | ||
key2: 1 | ||
key3: noUpdate | ||
field2: value2 | ||
` | ||
source := | ||
`metadata: | ||
field1: | ||
value1map: | ||
key1: innerValue1updated | ||
key2: 2 | ||
field3: value3 | ||
` | ||
|
||
expectedMerged := | ||
`metadata: | ||
field1: | ||
value1map: | ||
key1: innerValue1updated | ||
key2: 2 | ||
key3: noUpdate | ||
field2: value2 | ||
field3: value3 | ||
` | ||
serializationType := "yaml" | ||
merged, err := util.Merge(destination, source, serializationType) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expectedMerged, merged) | ||
} |