This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a --set flag for generate (#2787)
- Loading branch information
1 parent
914172d
commit ca9b3f1
Showing
12 changed files
with
2,113 additions
and
6 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,119 @@ | ||
package transform | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/Jeffail/gabs" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// APIModelValue represents a value in the APIModel JSON file | ||
type APIModelValue struct { | ||
stringValue string | ||
intValue int64 | ||
arrayValue bool | ||
arrayIndex int | ||
arrayProperty string | ||
arrayName string | ||
} | ||
|
||
// MapValues converts an arraw of rwa ApiModel values (like ["masterProfile.count=4","linuxProfile.adminUsername=admin"]) to a map | ||
func MapValues(m map[string]APIModelValue, values []string) { | ||
if values == nil || len(values) == 0 { | ||
return | ||
} | ||
|
||
for _, value := range values { | ||
splittedValues := strings.Split(value, ",") | ||
if len(splittedValues) > 1 { | ||
MapValues(m, splittedValues) | ||
} else { | ||
keyValueSplitted := strings.Split(value, "=") | ||
key := keyValueSplitted[0] | ||
stringValue := keyValueSplitted[1] | ||
|
||
flagValue := APIModelValue{} | ||
|
||
if asInteger, err := strconv.ParseInt(stringValue, 10, 64); err == nil { | ||
flagValue.intValue = asInteger | ||
} else { | ||
flagValue.stringValue = stringValue | ||
} | ||
|
||
// use regex to find array[index].property pattern in the key | ||
re := regexp.MustCompile(`(.*?)\[(.*?)\]\.(.*?)$`) | ||
match := re.FindStringSubmatch(key) | ||
|
||
// it's an array | ||
if len(match) != 0 { | ||
i, err := strconv.ParseInt(match[2], 10, 32) | ||
if err != nil { | ||
log.Warnln(fmt.Sprintf("array index is not specified for property %s", key)) | ||
} else { | ||
arrayIndex := int(i) | ||
flagValue.arrayValue = true | ||
flagValue.arrayName = match[1] | ||
flagValue.arrayIndex = arrayIndex | ||
flagValue.arrayProperty = match[3] | ||
m[key] = flagValue | ||
} | ||
} else { | ||
m[key] = flagValue | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MergeValuesWithAPIModel takes the path to an ApiModel JSON file, loads it and merges it with the values in the map to another temp file | ||
func MergeValuesWithAPIModel(apiModelPath string, m map[string]APIModelValue) (string, error) { | ||
// load the apiModel file from path | ||
fileContent, err := ioutil.ReadFile(apiModelPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// parse the json from file content | ||
jsonObj, err := gabs.ParseJSON(fileContent) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// update api model definition with each value in the map | ||
for key, flagValue := range m { | ||
// working on an array | ||
if flagValue.arrayValue { | ||
log.Infoln(fmt.Sprintf("--set flag array value detected. Name: %s, Index: %b, PropertyName: %s", flagValue.arrayName, flagValue.arrayIndex, flagValue.arrayProperty)) | ||
arrayValue := jsonObj.Path(fmt.Sprint("properties.", flagValue.arrayName)) | ||
if flagValue.stringValue != "" { | ||
arrayValue.Index(flagValue.arrayIndex).SetP(flagValue.stringValue, flagValue.arrayProperty) | ||
} else { | ||
arrayValue.Index(flagValue.arrayIndex).SetP(flagValue.intValue, flagValue.arrayProperty) | ||
} | ||
} else { | ||
if flagValue.stringValue != "" { | ||
jsonObj.SetP(flagValue.stringValue, fmt.Sprint("properties.", key)) | ||
} else { | ||
jsonObj.SetP(flagValue.intValue, fmt.Sprint("properties.", key)) | ||
} | ||
} | ||
} | ||
|
||
// generate a new file | ||
tmpFile, err := ioutil.TempFile("", "mergedApiModel") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
tmpFileName := tmpFile.Name() | ||
err = ioutil.WriteFile(tmpFileName, []byte(jsonObj.String()), os.ModeAppend) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return tmpFileName, nil | ||
} |
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,50 @@ | ||
package transform | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/Jeffail/gabs" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestAPIModelMergerMapValues(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
m := make(map[string]APIModelValue) | ||
values := []string{"masterProfile.count=5", "agentPoolProfiles[0].name=agentpool1", "linuxProfile.adminUsername=admin"} | ||
|
||
MapValues(m, values) | ||
Expect(m["masterProfile.count"].intValue).To(BeIdenticalTo(int64(5))) | ||
Expect(m["agentPoolProfiles[0].name"].arrayValue).To(BeTrue()) | ||
Expect(m["agentPoolProfiles[0].name"].arrayIndex).To(BeIdenticalTo(0)) | ||
Expect(m["agentPoolProfiles[0].name"].arrayProperty).To(BeIdenticalTo("name")) | ||
Expect(m["agentPoolProfiles[0].name"].arrayName).To(BeIdenticalTo("agentPoolProfiles")) | ||
Expect(m["agentPoolProfiles[0].name"].stringValue).To(BeIdenticalTo("agentpool1")) | ||
Expect(m["linuxProfile.adminUsername"].stringValue).To(BeIdenticalTo("admin")) | ||
} | ||
|
||
func TestMergeValuesWithAPIModel(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
m := make(map[string]APIModelValue) | ||
values := []string{"masterProfile.count=5", "agentPoolProfiles[0].name=agentpool1", "linuxProfile.adminUsername=admin"} | ||
|
||
MapValues(m, values) | ||
tmpFile, _ := MergeValuesWithAPIModel("../testdata/simple/kubernetes.json", m) | ||
|
||
jsonFileContent, err := ioutil.ReadFile(tmpFile) | ||
Expect(err).To(BeNil()) | ||
|
||
jsonAPIModel, err := gabs.ParseJSON(jsonFileContent) | ||
Expect(err).To(BeNil()) | ||
|
||
masterProfileCount := jsonAPIModel.Path("properties.masterProfile.count").Data() | ||
Expect(masterProfileCount).To(BeIdenticalTo(float64(5))) | ||
|
||
adminUsername := jsonAPIModel.Path("properties.linuxProfile.adminUsername").Data() | ||
Expect(adminUsername).To(BeIdenticalTo("admin")) | ||
|
||
agentPoolProfileName := jsonAPIModel.Path("properties.agentPoolProfiles").Index(0).Path("name").Data().(string) | ||
Expect(agentPoolProfileName).To(BeIdenticalTo("agentpool1")) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.