Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apptrust/commands/application/application_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func populateApplicationFromFlags(ctx *components.Context, descriptor *model.App
}

if ctx.IsFlagSet(commands.LabelsFlag) {
labelsMap, err := utils.ParseMapFlag(ctx.GetStringFlagValue(commands.LabelsFlag))
labels, err := utils.ParseLabelKeyValuePairs(ctx.GetStringFlagValue(commands.LabelsFlag))
if err != nil {
return fmt.Errorf("failed to parse --%s: %w", commands.LabelsFlag, err)
}
descriptor.Labels = &labelsMap
descriptor.Labels = &labels
}

// Only set LabelUpdates if at least one of add-labels or remove-labels flags is set
Expand Down
21 changes: 11 additions & 10 deletions apptrust/commands/application/create_app_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func TestCreateAppCommand_Run_Flags(t *testing.T) {
Description: &description,
BusinessCriticality: &businessCriticality,
MaturityLevel: &maturityLevel,
Labels: &map[string]string{
"env": "prod",
"region": "us-east",
Labels: &[]model.LabelEntry{
{Key: "env", Value: "prod"},
{Key: "region", Value: "us-east"},
},
UserOwners: &[]string{"john.doe", "jane.smith"},
GroupOwners: &[]string{"devops", "security"},
Expand Down Expand Up @@ -149,10 +149,11 @@ func TestCreateAppCommand_Run_FullSpecFile(t *testing.T) {
Description: &expectedDescription,
MaturityLevel: &expectedMaturityLevel,
BusinessCriticality: &expectedBusinessCriticality,
Labels: &map[string]string{
"environment": "production",
"region": "us-east-1",
"team": "devops",
Labels: &[]model.LabelEntry{
{Key: "environment", Value: "production"},
{Key: "environment", Value: "staging"},
{Key: "region", Value: "us-east-1"},
{Key: "team", Value: "devops"},
},
UserOwners: &[]string{"john.doe", "jane.smith"},
GroupOwners: &[]string{"devops-team", "security-team"},
Expand Down Expand Up @@ -281,9 +282,9 @@ func TestCreateAppCommand_Run_SpecVars(t *testing.T) {
Description: &expectedDescription,
MaturityLevel: &expectedMaturityLevel,
BusinessCriticality: &expectedBusinessCriticality,
Labels: &map[string]string{
"environment": "production",
"region": "us-east-1",
Labels: &[]model.LabelEntry{
{Key: "environment", Value: "production"},
{Key: "region", Value: "us-east-1"},
},
}

Expand Down
23 changes: 18 additions & 5 deletions apptrust/commands/application/testfiles/full-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@
"description": "A comprehensive test application",
"maturity_level": "production",
"criticality": "high",
"labels": {
"environment": "production",
"region": "us-east-1",
"team": "devops"
},
"labels": [
{
"key": "environment",
"value": "production"
},
{
"key": "environment",
"value": "staging"
},
{
"key": "region",
"value": "us-east-1"
},
{
"key": "team",
"value": "devops"
}
],
"user_owners": [
"john.doe",
"jane.smith"
Expand Down
14 changes: 10 additions & 4 deletions apptrust/commands/application/testfiles/with-vars-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"description": "A test application for ${ENVIRONMENT}",
"maturity_level": "${MATURITY_LEVEL}",
"criticality": "${CRITICALITY}",
"labels": {
"environment": "${ENVIRONMENT}",
"region": "${REGION}"
}
"labels": [
{
"key": "environment",
"value": "${ENVIRONMENT}"
},
{
"key": "region",
"value": "${REGION}"
}
]
}
40 changes: 20 additions & 20 deletions apptrust/commands/application/update_app_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestUpdateAppCommand_Run(t *testing.T) {
Description: &description,
MaturityLevel: &maturityLevel,
BusinessCriticality: &businessCriticality,
Labels: &map[string]string{
"environment": "production",
"region": "us-east",
Labels: &[]model.LabelEntry{
{Key: "environment", Value: "production"},
{Key: "region", Value: "us-east"},
},
UserOwners: &[]string{"JohnD", "Dave Rice"},
GroupOwners: &[]string{"DevOps"},
Expand Down Expand Up @@ -67,9 +67,9 @@ func TestUpdateAppCommand_Run_Error(t *testing.T) {
Description: &description,
MaturityLevel: &maturityLevel,
BusinessCriticality: &businessCriticality,
Labels: &map[string]string{
"environment": "production",
"region": "us-east",
Labels: &[]model.LabelEntry{
{Key: "environment", Value: "production"},
{Key: "region", Value: "us-east"},
},
UserOwners: &[]string{"JohnD", "Dave Rice"},
GroupOwners: &[]string{"DevOps"},
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Add: []model.LabelKeyValue{
Add: []model.LabelEntry{
{Key: "environment", Value: "production"},
},
},
Expand All @@ -142,7 +142,7 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Add: []model.LabelKeyValue{
Add: []model.LabelEntry{
{Key: "environment", Value: "production"},
{Key: "region", Value: "us-east"},
},
Expand All @@ -158,7 +158,7 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Add: []model.LabelKeyValue{
Add: []model.LabelEntry{
{Key: "environment", Value: "production"},
{Key: "environment", Value: "staging"},
{Key: "region", Value: "us-east"},
Expand All @@ -175,7 +175,7 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Remove: []model.LabelKeyValue{
Remove: []model.LabelEntry{
{Key: "infra-version", Value: "v1.0"},
},
},
Expand All @@ -190,7 +190,7 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Remove: []model.LabelKeyValue{
Remove: []model.LabelEntry{
{Key: "infra-version", Value: "v1.0"},
{Key: "region", Value: "us-west"},
},
Expand All @@ -207,12 +207,12 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Add: []model.LabelKeyValue{
Add: []model.LabelEntry{
{Key: "environment", Value: "production"},
{Key: "environment", Value: "staging"},
{Key: "region", Value: "us-east"},
},
Remove: []model.LabelKeyValue{
Remove: []model.LabelEntry{
{Key: "infra-version", Value: "v1.0"},
{Key: "region", Value: "us-west"},
},
Expand Down Expand Up @@ -246,10 +246,10 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
Description: stringPtr("Updated description"),
MaturityLevel: stringPtr("production"),
LabelUpdates: &model.LabelUpdates{
Add: []model.LabelKeyValue{
Add: []model.LabelEntry{
{Key: "environment", Value: "production"},
},
Remove: []model.LabelKeyValue{
Remove: []model.LabelEntry{
{Key: "old-label", Value: "old-value"},
},
},
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Add: []model.LabelKeyValue{
Add: []model.LabelEntry{
{Key: "key1", Value: "value1"},
{Key: "key2", Value: "value2"},
},
Expand All @@ -299,8 +299,8 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Add: []model.LabelKeyValue{},
Remove: []model.LabelKeyValue{
Add: []model.LabelEntry{},
Remove: []model.LabelEntry{
{Key: "key", Value: "value"},
},
},
Expand All @@ -316,10 +316,10 @@ func TestUpdateAppCommand_FlagsSuite(t *testing.T) {
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-key",
LabelUpdates: &model.LabelUpdates{
Add: []model.LabelKeyValue{
Add: []model.LabelEntry{
{Key: "key", Value: "value"},
},
Remove: []model.LabelKeyValue{},
Remove: []model.LabelEntry{},
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions apptrust/commands/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ func ParseListPropertiesFlag(propertiesStr string) (map[string][]string, error)
return result, nil
}

func ParseLabelKeyValuePairs(flagValue string) ([]model.LabelKeyValue, error) {
func ParseLabelKeyValuePairs(flagValue string) ([]model.LabelEntry, error) {
if flagValue == "" {
return []model.LabelKeyValue{}, nil
return []model.LabelEntry{}, nil
}

var result []model.LabelKeyValue
var result []model.LabelEntry
pairs := strings.Split(flagValue, ";")
for _, pair := range pairs {
trimmedPair := strings.TrimSpace(pair)
Expand All @@ -184,7 +184,7 @@ func ParseLabelKeyValuePairs(flagValue string) ([]model.LabelKeyValue, error) {
if len(keyValue) != 2 {
return nil, errorutils.CheckErrorf("invalid key-value pair: '%s' (expected format key=value)", pair)
}
result = append(result, model.LabelKeyValue{
result = append(result, model.LabelEntry{
Key: strings.TrimSpace(keyValue[0]),
Value: strings.TrimSpace(keyValue[1]),
})
Expand Down
26 changes: 13 additions & 13 deletions apptrust/commands/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,62 +169,62 @@ func TestParseLabelKeyValuePairs(t *testing.T) {
tests := []struct {
name string
input string
expected []model.LabelKeyValue
expected []model.LabelEntry
expectErr bool
errorMsg string
}{
{
name: "empty string",
input: "",
expected: []model.LabelKeyValue{},
expected: []model.LabelEntry{},
expectErr: false,
},
{
name: "single pair",
input: "key1=value1",
expected: []model.LabelKeyValue{{Key: "key1", Value: "value1"}},
expected: []model.LabelEntry{{Key: "key1", Value: "value1"}},
expectErr: false,
},
{
name: "multiple pairs",
input: "key1=value1;key2=value2;key3=value3",
expected: []model.LabelKeyValue{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}, {Key: "key3", Value: "value3"}},
expected: []model.LabelEntry{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}, {Key: "key3", Value: "value3"}},
expectErr: false,
},
{
name: "same key multiple values",
input: "environment=production;environment=staging;region=us-east",
expected: []model.LabelKeyValue{{Key: "environment", Value: "production"}, {Key: "environment", Value: "staging"}, {Key: "region", Value: "us-east"}},
expected: []model.LabelEntry{{Key: "environment", Value: "production"}, {Key: "environment", Value: "staging"}, {Key: "region", Value: "us-east"}},
expectErr: false,
},
{
name: "whitespace handling",
input: " key1 = value1 ; key2 = value2 ",
expected: []model.LabelKeyValue{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}},
expected: []model.LabelEntry{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}},
expectErr: false,
},
{
name: "empty pairs skipped",
input: "key1=value1;;key2=value2",
expected: []model.LabelKeyValue{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}},
expected: []model.LabelEntry{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}},
expectErr: false,
},
{
name: "leading and trailing separators",
input: ";key1=value1;key2=value2;",
expected: []model.LabelKeyValue{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}},
expected: []model.LabelEntry{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}},
expectErr: false,
},
{
name: "empty value",
input: "key1=;key2=value2",
expected: []model.LabelKeyValue{{Key: "key1", Value: ""}, {Key: "key2", Value: "value2"}},
expected: []model.LabelEntry{{Key: "key1", Value: ""}, {Key: "key2", Value: "value2"}},
expectErr: false,
},
{
name: "value with equals sign",
input: "key1=value=with=equals;key2=normal",
expected: []model.LabelKeyValue{{Key: "key1", Value: "value=with=equals"}, {Key: "key2", Value: "normal"}},
expected: []model.LabelEntry{{Key: "key1", Value: "value=with=equals"}, {Key: "key2", Value: "normal"}},
expectErr: false,
},
{
Expand All @@ -242,19 +242,19 @@ func TestParseLabelKeyValuePairs(t *testing.T) {
{
name: "empty key",
input: "=value1;key2=value2",
expected: []model.LabelKeyValue{{Key: "", Value: "value1"}, {Key: "key2", Value: "value2"}},
expected: []model.LabelEntry{{Key: "", Value: "value1"}, {Key: "key2", Value: "value2"}},
expectErr: false,
},
{
name: "whitespace only pairs skipped",
input: "key1=value1; ;key2=value2",
expected: []model.LabelKeyValue{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}},
expected: []model.LabelEntry{{Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}},
expectErr: false,
},
{
name: "special characters in key and value",
input: "env-name=prod-env;region=us-east-1",
expected: []model.LabelKeyValue{{Key: "env-name", Value: "prod-env"}, {Key: "region", Value: "us-east-1"}},
expected: []model.LabelEntry{{Key: "env-name", Value: "prod-env"}, {Key: "region", Value: "us-east-1"}},
expectErr: false,
},
}
Expand Down
26 changes: 13 additions & 13 deletions apptrust/model/app_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ var (
}
)

type LabelKeyValue struct {
type LabelEntry struct {
Key string `json:"key"`
Value string `json:"value"`
}

type LabelUpdates struct {
Remove []LabelKeyValue `json:"remove,omitempty"`
Add []LabelKeyValue `json:"add,omitempty"`
Remove []LabelEntry `json:"remove,omitempty"`
Add []LabelEntry `json:"add,omitempty"`
}

type AppDescriptor struct {
ApplicationKey string `json:"application_key"`
ApplicationName string `json:"application_name,omitempty"`
ProjectKey string `json:"project_key,omitempty"`
Description *string `json:"description,omitempty"`
MaturityLevel *string `json:"maturity_level,omitempty"`
BusinessCriticality *string `json:"criticality,omitempty"`
Labels *map[string]string `json:"labels,omitempty"`
LabelUpdates *LabelUpdates `json:"label_updates,omitempty"`
UserOwners *[]string `json:"user_owners,omitempty"`
GroupOwners *[]string `json:"group_owners,omitempty"`
ApplicationKey string `json:"application_key"`
ApplicationName string `json:"application_name,omitempty"`
ProjectKey string `json:"project_key,omitempty"`
Description *string `json:"description,omitempty"`
MaturityLevel *string `json:"maturity_level,omitempty"`
BusinessCriticality *string `json:"criticality,omitempty"`
Labels *[]LabelEntry `json:"labels,omitempty"`
LabelUpdates *LabelUpdates `json:"label_updates,omitempty"`
UserOwners *[]string `json:"user_owners,omitempty"`
GroupOwners *[]string `json:"group_owners,omitempty"`
}
Loading
Loading