-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathotelcomponentmapping_apply.go
More file actions
80 lines (68 loc) · 2.65 KB
/
otelcomponentmapping_apply.go
File metadata and controls
80 lines (68 loc) · 2.65 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
package otelcomponentmapping
import (
"fmt"
"os"
"path/filepath"
"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"
)
type ApplyArgs struct {
File string
}
func OtelComponentMappingApplyCommand(deps *di.Deps) *cobra.Command {
args := &ApplyArgs{}
cmd := &cobra.Command{
Use: "apply",
Short: "Create or edit an OTel Component Mapping from YAML",
Long: "Create or edit a OTel Component Mapping from YAML file.",
Example: `# create a new OTel component mapping from a YAML file
sts otel-component-mapping apply --file new-component-mapping.yaml
# update an existing mapping
sts otel-component-mapping apply --file updated-component-mapping.yaml`,
RunE: deps.CmdRunEWithApi(RunApplyComponentMappingCommand(args)),
}
common.AddRequiredFileFlagVar(cmd, &args.File, "Path to a .yaml file with the mapping definition")
return cmd
}
func RunApplyComponentMappingCommand(args *ApplyArgs) di.CmdWithApiFn {
return func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError {
fileBytes, err := os.ReadFile(args.File)
if err != nil {
return common.NewReadFileError(err, args.File)
}
ext := strings.ToLower(filepath.Ext(args.File))
if ext != ".yaml" {
return common.NewCLIArgParseError(fmt.Errorf("unsupported file type: %s. Only .yaml files are supported", ext))
}
return applyYAMLOtelComponentMapping(cli, api, fileBytes)
}
}
func applyYAMLOtelComponentMapping(cli *di.Deps, api *stackstate_api.APIClient, fileBytes []byte) common.CLIError {
var mapping stackstate_api.OtelComponentMapping
if err := yaml.Unmarshal(fileBytes, &mapping); err != nil {
return common.NewCLIArgParseError(fmt.Errorf("failed to parse YAML: %v", err))
}
reqObj := stackstate_api.UpsertOtelComponentMappingsRequest{
Identifier: mapping.Identifier,
Name: mapping.Name,
Description: mapping.Description,
Input: mapping.Input,
Output: mapping.Output,
Vars: mapping.Vars,
ExpireAfter: mapping.ExpireAfter,
}
upserted, resp, err := api.OtelMappingApi.UpsertOtelComponentMappings(cli.Context).UpsertOtelComponentMappingsRequest(reqObj).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{"otel_component_mapping": upserted})
} else {
cli.Printer.Success(fmt.Sprintf("OTel component mapping upserted successfully! Identifier: %s, Name: %s", mapping.GetIdentifier(), mapping.GetName()))
}
return nil
}