-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathfleet_configuration_create.go
More file actions
95 lines (82 loc) · 3.26 KB
/
fleet_configuration_create.go
File metadata and controls
95 lines (82 loc) · 3.26 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
93
94
95
package fleetcontrol
import (
"fmt"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/client"
)
// handleFleetCreateConfiguration implements the 'create-configuration' command to create a fleet configuration.
//
// This command creates a new fleet configuration with custom attributes.
// Configurations allow you to define settings and attributes for fleet management.
//
// The configuration body must be provided via one of two mutually exclusive flags:
// - --configuration-file-path: Path to a file (recommended for production)
// - --configuration-content: Inline content (for testing/development/emergency only)
//
// The command:
// 1. Validates flag values (done automatically by framework via YAML rules)
// 2. Validates mutual exclusivity of configuration flags
// 3. Derives organization ID if not provided
// 4. Builds custom headers with entity metadata
// 5. Calls the New Relic API to create the configuration
//
// Parameters:
// - cmd: The cobra command being executed
// - args: Command arguments (not used)
// - flags: Validated flag values from YAML configuration
//
// Returns:
// - Error if configuration creation fails, nil on success
func handleFleetCreateConfiguration(cmd *cobra.Command, args []string, flags *FlagValues) error {
// Get typed flag values - no hardcoded strings!
// Note: CreateConfiguration() returns error because it handles file reading
f, err := flags.CreateConfiguration()
if err != nil {
return fmt.Errorf("failed to read flags: %w", err)
}
// Validate that exactly one of --configuration-file-path or --configuration-content is provided
hasFilePath := f.ConfigurationFilePath != ""
hasContent := f.ConfigurationContent != ""
if !hasFilePath && !hasContent {
return fmt.Errorf("one of --configuration-file-path or --configuration-content must be provided")
}
if hasFilePath && hasContent {
return fmt.Errorf("--configuration-file-path and --configuration-content are mutually exclusive, use only one")
}
// Determine which configuration content to use
configBody := f.ConfigurationFilePath
if hasContent {
configBody = f.ConfigurationContent
}
// Convert the configuration body to []byte to prevent JSON marshaling
// This preserves newlines and formatting in the configuration file
configBodyBytes := []byte(configBody)
// Get organization ID (provided or fetched from API)
orgID, err := GetOrganizationID(f.OrganizationID)
if err != nil {
return PrintError(fmt.Errorf("failed to determine organization ID: %w", err))
}
// Build custom headers required by the API
// These headers specify the entity name, agent type, and managed entity type
customHeaders := map[string]interface{}{
"x-newrelic-client-go-custom-headers": map[string]string{
"Newrelic-Entity": fmt.Sprintf(
`{"name": "%s", "agentType": "%s", "managedEntityType": "%s"}`,
f.Name,
f.AgentType,
f.ManagedEntityType,
),
},
}
// Call New Relic API to create the configuration
result, err := client.NRClient.FleetControl.FleetControlCreateConfiguration(
configBodyBytes,
customHeaders,
orgID,
)
if err != nil {
return PrintError(fmt.Errorf("failed to create fleet configuration: %w", err))
}
// Print the created configuration to stdout with status wrapper
return PrintConfigurationSuccess(result)
}