Skip to content

Commit

Permalink
feat(changetracking): Support custom attributes JSON (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
amolero-nr authored Oct 25, 2023
1 parent 33d79ab commit e767a55
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .tutone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ packages:
- name: EntityGuid
field_type_override: common.EntityGUID
skip_type_create: true
- name: ChangeTrackingRawCustomAttributesMap
create_as: "map[string]interface{}"
mutations:
- name: changeTrackingCreateDeployment
max_query_field_depth: 2
Expand Down
13 changes: 10 additions & 3 deletions pkg/changetracking/changetracking_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions pkg/changetracking/changetracking_api_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package changetracking

import (
"encoding/json"
"log"
"testing"
"time"

Expand All @@ -19,15 +21,17 @@ func TestChangeTrackingCreateDeployment_Basic(t *testing.T) {

a := newIntegrationTestClient(t)

var customAttributes = map[string]string{
"test": "123",
"test2": "456",
var customAttributes = `{"a":"1","b":"two","c":"1.5","d":"true"}`
attrs := make(map[string]interface{})
err := json.Unmarshal([]byte(customAttributes), &attrs)
if err != nil {
log.Fatal(err)
}

input := ChangeTrackingDeploymentInput{
Changelog: "test",
Commit: "12345a",
CustomAttributes: &customAttributes,
CustomAttributes: attrs,
DeepLink: "newrelic-client-go",
DeploymentType: ChangeTrackingDeploymentTypeTypes.BASIC,
Description: "This is a test description",
Expand All @@ -38,7 +42,10 @@ func TestChangeTrackingCreateDeployment_Basic(t *testing.T) {
Version: "0.0.1",
}

res, err := a.ChangeTrackingCreateDeployment(input)
res, err := a.ChangeTrackingCreateDeployment(
ChangeTrackingDataHandlingRules{ValidationFlags: []ChangeTrackingValidationFlag{ChangeTrackingValidationFlagTypes.FAIL_ON_FIELD_LENGTH}},
input,
)
require.NoError(t, err)

require.NotNil(t, res)
Expand All @@ -63,7 +70,10 @@ func TestChangeTrackingCreateDeployment_TimestampError(t *testing.T) {
Version: "0.0.1",
}

res, err := a.ChangeTrackingCreateDeployment(input)
res, err := a.ChangeTrackingCreateDeployment(
ChangeTrackingDataHandlingRules{ValidationFlags: []ChangeTrackingValidationFlag{ChangeTrackingValidationFlagTypes.FAIL_ON_FIELD_LENGTH}},
input,
)
require.Error(t, err)
require.Nil(t, res)
}
Expand Down
37 changes: 0 additions & 37 deletions pkg/changetracking/changetracking_types.go

This file was deleted.

77 changes: 70 additions & 7 deletions pkg/changetracking/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,91 @@ var ChangeTrackingDeploymentTypeTypes = struct {
SHADOW: "SHADOW",
}

// ChangeTrackingValidationFlag - Validation flags to determine how we handle input data.
type ChangeTrackingValidationFlag string

var ChangeTrackingValidationFlagTypes = struct {
// Will validate all string fields to be within max size limit. An error is returned and data is not saved if any of the fields exceeds max size limit.
FAIL_ON_FIELD_LENGTH ChangeTrackingValidationFlag
// For APM entities, a call is made to the legacy New Relic v2 REST API. When this flag is set, if the call fails for any reason, an error will be returned containing the failure message.
FAIL_ON_REST_API_FAILURES ChangeTrackingValidationFlag
}{
// Will validate all string fields to be within max size limit. An error is returned and data is not saved if any of the fields exceeds max size limit.
FAIL_ON_FIELD_LENGTH: "FAIL_ON_FIELD_LENGTH",
// For APM entities, a call is made to the legacy New Relic v2 REST API. When this flag is set, if the call fails for any reason, an error will be returned containing the failure message.
FAIL_ON_REST_API_FAILURES: "FAIL_ON_REST_API_FAILURES",
}

// ChangeTrackingDataHandlingRules - Validation and data handling rules to be applied to deployment input data.
type ChangeTrackingDataHandlingRules struct {
// Flags for validation, for example, ‘FAIL_ON_FIELD_LENGTH’.
ValidationFlags []ChangeTrackingValidationFlag `json:"validationFlags"`
}

// ChangeTrackingDeployment - A deployment.
type ChangeTrackingDeployment struct {
// A URL for the changelog or list of changes if not linkable.
// A URL to the changelog or, if not linkable, a list of changes.
Changelog string `json:"changelog,omitempty"`
// The commit identifier, for example, a Git commit SHA.
Commit string `json:"commit,omitempty"`
// A link back to the system generating the deployment.
// Represents key-value pairs of custom attributes in JSON format.
CustomAttributes ChangeTrackingRawCustomAttributesMap `json:"customAttributes,omitempty"`
// A link to the system that generated the deployment.
DeepLink string `json:"deepLink,omitempty"`
// Optional: deployment identifier.
// A unique deployment identifier.
DeploymentId string `json:"deploymentId,omitempty"`
// The type of deployment, for example, ‘Blue green’ or ‘Rolling’.
DeploymentType ChangeTrackingDeploymentType `json:"deploymentType,omitempty"`
// A description of the deployment.
Description string `json:"description,omitempty"`
// The NR1 entity that was deployed.
// The NR entity that was deployed.
EntityGUID common.EntityGUID `json:"entityGuid"`
// String that can be used to correlate two or more events.
// An identifier used to correlate two or more events.
GroupId string `json:"groupId,omitempty"`
// The start time of the deployment, the number of milliseconds since the Unix epoch.
// The start time of the deployment as the number of milliseconds since the Unix epoch.
Timestamp nrtime.EpochMilliseconds `json:"timestamp"`
// Username of the deployer or bot.
// The username of the deployer or bot.
User string `json:"user,omitempty"`
// The version of the deployed software, for example, something like v1.1.
Version string `json:"version"`
}

// ChangeTrackingDeploymentInput - A deployment.
type ChangeTrackingDeploymentInput struct {
// A URL for the changelog or, if not linkable, a list of changes.
Changelog string `json:"changelog,omitempty"`
// The commit identifier, for example, a Git commit SHA.
Commit string `json:"commit,omitempty"`
// Represents key-value pairs of custom attributes in JSON format. Attribute values can be of type string, boolean, or numeric.
//
// **Restricted attributes names:** accountId, appID, changelog, commit, customAttributes, deepLink, deploymentType, description, entity.guid, entity.name, entity.type, entityGuid, entityName, eventType, groupId, timestamp, user, version
//
// **Restricted attribute name prefixes:** 'nr.', 'newrelic.'
//
// For more information on limitations, see [our docs](https://docs.newrelic.com/docs/change-tracking/change-tracking-graphql/)
//
// **Examples:**
//
// • {cloudVendor : "vendorName", region : "us-east-1", environment : "staging"}
// • {isProd : true, region : "us-east-1", instances: 2, deployTime : 10.5}
CustomAttributes ChangeTrackingRawCustomAttributesMap `json:"customAttributes,omitempty"`
// A URL to the system that generated the deployment.
DeepLink string `json:"deepLink,omitempty"`
// The type of deployment, for example, ‘Blue green’ or ‘Rolling’.
DeploymentType ChangeTrackingDeploymentType `json:"deploymentType,omitempty"`
// A description of the deployment.
Description string `json:"description,omitempty"`
// The NR entity that was deployed.
EntityGUID common.EntityGUID `json:"entityGuid"`
// An identifier used to correlate two or more events.
GroupId string `json:"groupId,omitempty"`
// The start time of the deployment as the number of milliseconds since the Unix epoch. Should be within the boundary of the past or future 24 hours. Defaults to now.
Timestamp nrtime.EpochMilliseconds `json:"timestamp,omitempty"`
// The username of the deployer or bot.
User string `json:"user,omitempty"`
// The version of the deployed software, for example, something like v1.1
Version string `json:"version"`
}

// ChangeTrackingRawCustomAttributesMap - A JSON scalar
type ChangeTrackingRawCustomAttributesMap map[string]interface{}

0 comments on commit e767a55

Please sign in to comment.