Skip to content

Commit 5db77de

Browse files
feat(fleetcontrol): add new package with fleet, fleet members CRUD ops (#1360)
feat(fleetcontrol): add new package with fleet, fleet members CRUD ops (#1360)
1 parent 25308d5 commit 5db77de

File tree

12 files changed

+12474
-0
lines changed

12 files changed

+12474
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ schema.json
99
.vscode/
1010
.DS_Store
1111
.idea
12+
tutone

.tutone.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,4 +1838,73 @@ packages:
18381838
skip_type_create: true
18391839
- name: Seconds
18401840
field_type_override: nrtime.Seconds
1841+
skip_type_create: true
1842+
1843+
- name: fleetcontrol
1844+
path: pkg/fleetcontrol
1845+
import_path: github.com/newrelic/newrelic-client-go/v2/pkg/fleetcontrol
1846+
generators:
1847+
- typegen
1848+
- nerdgraphclient
1849+
imports:
1850+
- github.com/newrelic/newrelic-client-go/v2/pkg/accounts
1851+
- github.com/newrelic/newrelic-client-go/v2/pkg/common
1852+
- github.com/newrelic/newrelic-client-go/v2/pkg/nrtime
1853+
- github.com/newrelic/newrelic-client-go/v2/pkg/users
1854+
mutations:
1855+
- name: fleetControlCreateFleet
1856+
max_query_field_depth: 4
1857+
- name: fleetControlUpdateFleet
1858+
max_query_field_depth: 4
1859+
- name: fleetControlDeleteFleet
1860+
max_query_field_depth: 4
1861+
- name: fleetControlAddFleetMembers
1862+
max_query_field_depth: 3
1863+
- name: fleetControlRemoveFleetMembers
1864+
max_query_field_depth: 3
1865+
- name: fleetControlCreateFleetDeployment
1866+
max_query_field_depth: 3
1867+
- name: fleetControlUpdateFleetDeployment
1868+
max_query_field_depth: 3
1869+
- name: fleetControlDeleteFleetDeployment
1870+
max_query_field_depth: 3
1871+
- name: fleetControlDeploy
1872+
max_query_field_depth: 2
1873+
queries:
1874+
- path: [ "actor", "fleetControl" ]
1875+
endpoints:
1876+
- name: fleetMembers
1877+
max_query_field_depth: 2
1878+
- path: [ "actor", "entityManagement" ]
1879+
endpoints:
1880+
- name: entity
1881+
max_query_field_depth: 4
1882+
exclude_fields:
1883+
- agents
1884+
- tools
1885+
- name: entitySearch
1886+
max_query_field_depth: 4
1887+
exclude_fields:
1888+
- agents
1889+
- tools
1890+
1891+
types:
1892+
- name: FleetControlFleetMembersFilterInput
1893+
field_type_override: "*FleetControlFleetMembersFilterInput"
1894+
- name: FleetControlOperatingSystemCreateInput
1895+
field_type_override: "*FleetControlOperatingSystemCreateInput"
1896+
- name: EntityManagementEntity
1897+
include_implementations:
1898+
- EntityManagementFleetEntity
1899+
- EntityManagementAgentConfigurationEntity
1900+
- EntityManagementAgentConfigurationVersionEntity
1901+
- EntityManagementFleetDeploymentEntity
1902+
- name: ID
1903+
field_type_override: string
1904+
skip_type_create: true
1905+
- name: DateTime
1906+
field_type_override: nrtime.DateTime
1907+
skip_type_create: true
1908+
- name: EpochMilliseconds
1909+
field_type_override: nrtime.EpochMilliseconds
18411910
skip_type_create: true

internal/http/client.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ func (c *Client) Post(
185185
return c.PostWithContext(context.Background(), url, queryParams, reqBody, respBody)
186186
}
187187

188+
func getRequestCustomHeadersFromQueryParams(queryParams interface{}) (map[string]string, interface{}) {
189+
if queryParams == nil {
190+
return nil, nil
191+
}
192+
193+
customHeaders, ok := queryParams.(map[string]interface{})
194+
if !ok {
195+
return nil, queryParams
196+
}
197+
if ch, exists := customHeaders["x-newrelic-client-go-custom-headers"]; exists {
198+
if headersMap, ok := ch.(map[string]string); ok {
199+
return headersMap, nil
200+
}
201+
}
202+
203+
return nil, queryParams
204+
}
205+
206+
func setRequestCustomHeaders(req *Request, customHeaders map[string]string) {
207+
for key, value := range customHeaders {
208+
req.SetHeader(key, value)
209+
}
210+
}
211+
188212
// PostWithContext represents an HTTP POST request to a New Relic API.
189213
// The queryParams argument can be used to add query string parameters to the requested URL.
190214
// The reqBody argument will be marshaled to JSON from the type provided and included in the request body.
@@ -197,11 +221,17 @@ func (c *Client) PostWithContext(
197221
reqBody interface{},
198222
respBody interface{},
199223
) (*http.Response, error) {
224+
customHeaders, queryParams := getRequestCustomHeadersFromQueryParams(queryParams)
225+
200226
req, err := c.NewRequest(http.MethodPost, url, queryParams, reqBody, respBody)
201227
if err != nil {
202228
return nil, err
203229
}
204230

231+
if customHeaders != nil {
232+
setRequestCustomHeaders(req, customHeaders)
233+
}
234+
205235
req.WithContext(ctx)
206236

207237
return c.Do(req)

newrelic/newrelic.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/newrelic/newrelic-client-go/v2/pkg/entities"
2525
"github.com/newrelic/newrelic-client-go/v2/pkg/events"
2626
"github.com/newrelic/newrelic-client-go/v2/pkg/eventstometrics"
27+
"github.com/newrelic/newrelic-client-go/v2/pkg/fleetcontrol"
2728
"github.com/newrelic/newrelic-client-go/v2/pkg/installevents"
2829
"github.com/newrelic/newrelic-client-go/v2/pkg/keytransaction"
2930
"github.com/newrelic/newrelic-client-go/v2/pkg/logconfigurations"
@@ -62,6 +63,7 @@ type NewRelic struct {
6263
Entities entities.Entities
6364
Events events.Events
6465
EventsToMetrics eventstometrics.EventsToMetrics
66+
FleetControl fleetcontrol.Fleetcontrol
6567
InstallEvents installevents.Installevents
6668
Logs logs.Logs
6769
Logconfigurations logconfigurations.Logconfigurations
@@ -113,6 +115,7 @@ func New(opts ...ConfigOption) (*NewRelic, error) {
113115
Entities: entities.New(cfg),
114116
Events: events.New(cfg),
115117
EventsToMetrics: eventstometrics.New(cfg),
118+
FleetControl: fleetcontrol.New(cfg),
116119
InstallEvents: installevents.New(cfg),
117120
Logs: logs.New(cfg),
118121
Logconfigurations: logconfigurations.New(cfg),

pkg/fleetcontrol/fleetcontrol.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package fleetcontrol
2+
3+
import (
4+
"github.com/newrelic/newrelic-client-go/v2/internal/http"
5+
"github.com/newrelic/newrelic-client-go/v2/pkg/config"
6+
"github.com/newrelic/newrelic-client-go/v2/pkg/infrastructure"
7+
"github.com/newrelic/newrelic-client-go/v2/pkg/logging"
8+
)
9+
10+
// Alerts is used to communicate with New Relic Alerts.
11+
type Fleetcontrol struct {
12+
client http.Client
13+
logger logging.Logger
14+
config config.Config
15+
infraClient http.Client
16+
pager http.Pager
17+
}
18+
19+
// New is used to create a new Alerts client instance.
20+
func New(config config.Config) Fleetcontrol {
21+
infraConfig := config
22+
23+
infraClient := http.NewClient(infraConfig)
24+
infraClient.SetAuthStrategy(&http.PersonalAPIKeyCapableV2Authorizer{})
25+
infraClient.SetErrorValue(&infrastructure.ErrorResponse{})
26+
27+
client := http.NewClient(config)
28+
client.SetAuthStrategy(&http.PersonalAPIKeyCapableV2Authorizer{})
29+
30+
pkg := Fleetcontrol{
31+
client: client,
32+
config: config,
33+
infraClient: infraClient,
34+
logger: config.GetLogger(),
35+
pager: &http.LinkHeaderPager{},
36+
}
37+
38+
return pkg
39+
}

0 commit comments

Comments
 (0)