-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathotelrelationmapping_edit.go
More file actions
92 lines (78 loc) · 3.16 KB
/
otelrelationmapping_edit.go
File metadata and controls
92 lines (78 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package otelrelationmapping
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
const LongDescription = `Edit an OTel Relation Mapping.
The edit command allows you to directly edit any OTel Relation Mapping. It will open
the editor defined by your EDITOR environment variables.
The OTel Relation Mapping will be presented as JSON format for editing.
`
type EditArgs struct {
Identifier string
}
func OtelRelationMappingEditCommand(deps *di.Deps) *cobra.Command {
args := &EditArgs{}
cmd := &cobra.Command{
Use: "edit",
Short: "Edit an OTel Relation Mapping using $EDITOR",
Long: LongDescription,
Example: `# edit a relation mapping using your editor
sts otel-relation-mapping edit --identifier urn:stackpack:stackpack-name:shared:otel-relation-mapping:database`,
RunE: deps.CmdRunEWithApi(RunEditRelationMappingCommand(args)),
}
common.AddRequiredIdentifierFlagVar(cmd, &args.Identifier, "Identifier (URN) of the Relation Mapping")
return cmd
}
func RunEditRelationMappingCommand(args *EditArgs) di.CmdWithApiFn {
return func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError {
mapping, resp, err := api.OtelMappingApi.GetOtelRelationMapping(cli.Context, args.Identifier).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
originalYAML, err := yaml.Marshal(mapping)
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to marshal mapping to YAML: %v", err))
}
editedContent, err := cli.Editor.Edit("otel-relation-mapping-", ".yaml", strings.NewReader(string(originalYAML)))
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to open editor: %v", err))
}
if string(originalYAML) == string(editedContent) {
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{"message": "No changes made"})
} else {
cli.Printer.PrintWarn("No changes made")
}
return nil
}
var editedMapping stackstate_api.OtelRelationMapping
if err := yaml.Unmarshal(editedContent, &editedMapping); err != nil {
return common.NewExecutionError(fmt.Errorf("failed to parse edited YAML: %v", err))
}
reqObj := stackstate_api.UpsertOtelRelationMappingsRequest{
Identifier: editedMapping.Identifier,
Name: editedMapping.Name,
Description: editedMapping.Description,
Input: editedMapping.Input,
Output: editedMapping.Output,
Vars: editedMapping.Vars,
ExpireAfter: editedMapping.ExpireAfter,
}
upserted, resp, err := api.OtelMappingApi.UpsertOtelRelationMappings(cli.Context).UpsertOtelRelationMappingsRequest(reqObj).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{"otel_relation_mapping": upserted})
} else {
cli.Printer.Success(fmt.Sprintf("OTel Relation Mapping updated successfully! Identifier: %s, Name: %s", upserted.GetIdentifier(), upserted.GetName()))
}
return nil
}
}