Skip to content

Commit

Permalink
fix: rollback all output changes (#1219)
Browse files Browse the repository at this point in the history
* fix: rollback all output changes
  • Loading branch information
geffersonFerraz authored Oct 30, 2024
1 parent 0178829 commit feb54cd
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 236 deletions.
170 changes: 3 additions & 167 deletions mgc/cli/cmd/cmd_result_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -56,178 +55,15 @@ func handleResultWithReader(reader io.Reader, outFile string, cmd *cobra.Command
return nil
}

func removeProperty(data any, path string) any {
parts := strings.Split(strings.TrimPrefix(path, "$."), ".")
return removePropertyRecursive(data, parts)
}

func removePropertyRecursive(data any, parts []string) interface{} {
if len(parts) == 0 {
return data
}

currentPart := parts[0]
remainingParts := parts[1:]

// check if part is array
if currentPart, ok := strings.CutSuffix(currentPart, "[*]"); ok {
if obj, ok := data.(map[string]interface{}); ok {
obj[currentPart] = removePropertyRecursive(obj[currentPart], parts[1:])
}
} else {
if len(remainingParts) == 0 {
if obj, ok := data.([]interface{}); ok {
for _, item := range obj {
if it, ok := item.(map[string]interface{}); ok {
delete(it, currentPart)
}

}
}

}

}

return data
}

func keepProperties(data any, properties []string) map[string]interface{} {
result := make(map[string]interface{})
for _, prop := range properties {
parts := strings.Split(strings.TrimPrefix(prop, "$."), ".")
keepPropertyRecursive(data, parts, result)
}
return result
}

func keepPropertyRecursive(data interface{}, parts []string, result map[string]interface{}) {
if len(parts) == 0 || data == nil {
return
}

currentPart := parts[0]
remainingParts := parts[1:]

if arrayPart, isArray := strings.CutSuffix(currentPart, "[*]"); isArray {
if obj, ok := data.(map[string]interface{}); ok {
if arr, exists := obj[arrayPart]; exists {
if arrayData, ok := arr.([]interface{}); ok {
newArray := make([]interface{}, len(arrayData))
for i, item := range arrayData {
newItem := make(map[string]interface{})
if mapItem, ok := item.(map[string]interface{}); ok {
keepPropertyRecursive(mapItem, remainingParts, newItem)
}
newArray[i] = newItem
}
setOrMergeValue(result, arrayPart, newArray)
}
}
}
} else {
if obj, ok := data.(map[string]interface{}); ok {
if value, exists := obj[currentPart]; exists {
if len(remainingParts) == 0 {
setOrMergeValue(result, currentPart, value)
} else {
subResult := make(map[string]interface{})
keepPropertyRecursive(value, remainingParts, subResult)
setOrMergeValue(result, currentPart, subResult)
}
}
}
}
}

func setOrMergeValue(result map[string]interface{}, key string, value interface{}) {
if existing, exists := result[key]; exists {
if existingMap, ok := existing.(map[string]interface{}); ok {
if newMap, ok := value.(map[string]interface{}); ok {
for k, v := range newMap {
existingMap[k] = v
}
return
}
}
if existingArray, ok := existing.([]interface{}); ok {
if newArray, ok := value.([]interface{}); ok {
if len(existingArray) == len(newArray) {
for i, newItem := range newArray {
if newMap, ok := newItem.(map[string]interface{}); ok {
if existingMap, ok := existingArray[i].(map[string]interface{}); ok {
for k, v := range newMap {
existingMap[k] = v
}
} else {
existingArray[i] = newMap
}
}
}
} else {
result[key] = newArray
}
return
}
}
}
result[key] = value
}

func handleResultWithValue(result core.ResultWithValue, output string, cmd *cobra.Command) (err error) {
err = result.ValidateSchema()
if err != nil {
logValidationErr(err)
}

outputs := strings.Split(output, ";")
output = ""
var remove string
for _, ot := range outputs {
if strings.HasPrefix(ot, "remove=") {
remove = strings.Split(ot, "=")[1]
} else {
output = ot
}
}
var fieldsToRemove []string
if remove != "" {
fieldsToRemove = strings.Split(remove, ",")
}

outputs = strings.Split(output, ";")
var allowed string
for _, ot := range outputs {
if strings.HasPrefix(ot, "allowfields=") {
allowed = strings.Split(ot, "=")[1]
} else {
output = ot
}
}
var allowedFields []string
if allowed != "" {
allowedFields = strings.Split(allowed, ",")
for i, x := range allowedFields {
allowedFields[i] = strings.Split(x, ":")[1]
}
}

for _, ot := range outputs {
if strings.HasPrefix(ot, "default=") {
output = strings.Split(ot, "=")[1]
}
}

value := result.Value()
if value == nil {
return nil
}

for _, path := range fieldsToRemove {
value = removeProperty(value, path)
}
if len(allowedFields) > 0 {
value = keepProperties(value, allowedFields)
err = result.ValidateSchema()
if err != nil {
logValidationErr(err)
}

name, options := parseOutputFormatter(output)
Expand Down
44 changes: 6 additions & 38 deletions mgc/cli/cmd/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,47 +77,15 @@ func getOutputFormatter(name, options string) (formatter OutputFormatter, err er
}

func getOutputFor(sdk *mgcSdk.Sdk, cmd *cobra.Command, result core.Result) string {
var output string
var defaultConfigOutput string
var configFlag string
var addAfterWhenEmpty string

if defaultConfigOutput = getOutputConfig(sdk); defaultConfigOutput != "" {
output = defaultConfigOutput
}

if configFlag = getOutputFlag(cmd); configFlag != "" {
output = configFlag
}

if outputOptions, ok := core.ResultAs[core.ResultWithDefaultOutputOptions](result); ok {
outputFromSpec := outputOptions.DefaultOutputOptions()
if strings.Contains(outputFromSpec, "default=") {
outs := strings.Split(outputFromSpec, ";")
for i, ot := range outs {
if strings.HasPrefix(ot, "default=") {
if defaultConfigOutput != "" {
outs[i] = "default=" + defaultConfigOutput
} else if configFlag != "" {
outs[i] = "default=" + configFlag
} else {
outs[i] = ot
}
break
}
}
output = strings.Join(outs, ";")
} else {
if output != "" {
output = output + ";" + outputFromSpec
} else {
addAfterWhenEmpty = outputFromSpec
}
}
output := getOutputFlag(cmd)
if output == "" {
output = getOutputConfig(sdk)
}

if output == "" {
return defaultFormatter + ";" + addAfterWhenEmpty
if outputOptions, ok := core.ResultAs[core.ResultWithDefaultOutputOptions](result); ok {
return outputOptions.DefaultOutputOptions()
}
}

return output
Expand Down
4 changes: 2 additions & 2 deletions mgc/cli/openapis/audit.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ paths:
security:
- OAuth2:
- evt:event-tr
x-mgc-output-flag: default=table;allowfields=ID:$.results[*].id,SOURCE:$.results[*].source,TYPE:$.results[*].type,SPECVERSION:$.results[*].specversion,SUBJECT:$.results[*].subject,TIME:$.results[*].time,AUTHID:$.results[*].authid,AUTHTYPE:$.results[*].authtype,PRODUCT:$.results[*].product,REGION:$.results[*].region
x-mgc-output-flag: table
/v0/event-types:
get:
tags:
Expand Down Expand Up @@ -211,7 +211,7 @@ paths:
security:
- OAuth2:
- evt:event-tr
x-mgc-output-flag: default=table;allowfields=TYPE:$.results[*].type
x-mgc-output-flag: table
components:
schemas:
Event:
Expand Down
1 change: 0 additions & 1 deletion mgc/cli/openapis/profile.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ paths:
security:
- OAuth2:
- gdb:ssh-pkey-r
x-mgc-output-flag: remove=$.results[*].key
post:
tags:
- ssh_keys
Expand Down
22 changes: 13 additions & 9 deletions mgc/cli/openapis/virtual-machine.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ paths:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-output-flag: ''
x-mgc-output-flag: table=ID:$.images[*].id,NAME:$.images[*].name,VERSION:$.images[*].version,PLATFORM:$.images[*].platform,END_STANDARD_SUPPORT_AT:$.images[*].end_standard_support_at,STATUS:$.images[*].status
security:
- OAuth2:
- virtual-machine.read
Expand Down Expand Up @@ -625,7 +625,8 @@ paths:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-output-flag: ''
x-mgc-output-flag: table=ID:$.instances[*].id,NAME:$.instances[*].name,STATE:$.instances[*].state,STATUS:$.instances[*].status,MACHINE_TYPE:$.instances[*].machine_type,IMAGE:$.instances[*].image,
CREATED_AT:$.instances[*].created_at, SSH_KEY_NAME:$.instances[*].ssh_key_name
security:
- OAuth2:
- virtual-machine.read
Expand Down Expand Up @@ -742,7 +743,7 @@ paths:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-output-flag: ''
x-mgc-output-flag: table=ID:$.id
security:
- OAuth2:
- virtual-machine.write
Expand Down Expand Up @@ -1079,7 +1080,8 @@ paths:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-output-flag: ''
x-mgc-output-flag: table=ID:$.id,NAME:$.name,STATE:$.state,STATUS:$..status,MACHINE_TYPE:$.machine_type,IMAGE:$.image,
CREATED_AT:$.created_at, SSH_KEY_NAME:$.ssh_key_name
security:
- OAuth2:
- virtual-machine.read
Expand Down Expand Up @@ -1825,7 +1827,7 @@ paths:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-output-flag: remove=$.machine_types[*].sku,$.machine_types[*].status
x-mgc-output-flag: table=ID:$.instance_types[*].id,NAME:$.instance_types[*].name,STATUS:$.instance_types[*].status,VCPUS:$.instance_types[*].vcpus,RAM:$.instance_types[*].ram,DISK:$.instance_types[*].disk,GPU:$.instance_types[*].gpu
security:
- OAuth2:
- virtual-machine.read
Expand Down Expand Up @@ -1927,7 +1929,8 @@ paths:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-output-flag: ''
x-mgc-output-flag: table=ID:$.snapshots[*].id,NAME:$.snapshots[*].name,STATE:$.snapshots[*].state,STATUS:$.snapshots[*].status,SIZE:$.snapshots[*].size,INSTANCE:$.snapshots[*].instance,
CREATED_AT:$.snapshots[*].created_at
security:
- OAuth2:
- virtual-machine.read
Expand Down Expand Up @@ -2028,7 +2031,7 @@ paths:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-output-flag: ''
x-mgc-output-flag: table=ID:$.id
security:
- OAuth2:
- virtual-machine.write
Expand Down Expand Up @@ -2138,7 +2141,8 @@ paths:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-output-flag: ''
x-mgc-output-flag: table=ID:$.id,NAME:$.name,STATE:$.state,STATUS:$.status,SIZE:$.size,INSTANCE:$.instance,
CREATED_AT:$.created_at
security:
- OAuth2:
- virtual-machine.read
Expand Down Expand Up @@ -2241,7 +2245,7 @@ paths:
$ref: '#/components/schemas/HTTPValidationError'
x-viveiro: true
x-mgc-name: restore
x-mgc-output-flag: ''
x-mgc-output-flag: table=ID:$.id
security:
- OAuth2:
- virtual-machine.write
Expand Down
6 changes: 3 additions & 3 deletions mgc/sdk/openapi/embed_loader.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions openapi-customizations/audit.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ servers:
to: api.magalu.cloud
- from: pre-prod
to: api.pre-prod.jaxyendy.com
paths:
/v0/events:
get:
x-mgc-output-flag: table
/v0/event-types:
get:
x-mgc-output-flag: table
3 changes: 1 addition & 2 deletions openapi-customizations/profile.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ paths:
- name: _sort
in: query
schema:
default: name:asc
x-mgc-output-flag: remove=$.results[*].key
default: name:asc
Loading

0 comments on commit feb54cd

Please sign in to comment.