diff --git a/.tutone.yml b/.tutone.yml index d7748d1d..5fb18c8f 100644 --- a/.tutone.yml +++ b/.tutone.yml @@ -1390,6 +1390,24 @@ packages: - name: DateTime field_type_override: nrtime.DateTime skip_type_create: true + + - name: authorizationmanagement + path: pkg/authorizationmanagement + import_path: github.com/newrelic/newrelic-client-go/v2/pkg/authorizationmanagement + generators: + - typegen + - nerdgraphclient + imports: + - github.com/newrelic/newrelic-client-go/v2/pkg/common + - github.com/newrelic/newrelic-client-go/v2/pkg/nrtime + queries: [] + mutations: + - name: authorizationManagementGrantAccess + max_query_field_depth: 2 + types: + - name: ID + field_type_override: string + skip_type_create: true generators: - name: typegen fileName: "types.go" diff --git a/newrelic/newrelic.go b/newrelic/newrelic.go index 3dc6b265..2cac32a5 100644 --- a/newrelic/newrelic.go +++ b/newrelic/newrelic.go @@ -10,6 +10,7 @@ import ( "github.com/newrelic/newrelic-client-go/v2/pkg/alerts" "github.com/newrelic/newrelic-client-go/v2/pkg/apiaccess" "github.com/newrelic/newrelic-client-go/v2/pkg/apm" + "github.com/newrelic/newrelic-client-go/v2/pkg/authorizationmanagement" "github.com/newrelic/newrelic-client-go/v2/pkg/changetracking" "github.com/newrelic/newrelic-client-go/v2/pkg/cloud" "github.com/newrelic/newrelic-client-go/v2/pkg/config" @@ -37,33 +38,34 @@ import ( // NewRelic is a collection of New Relic APIs. type NewRelic struct { - AccountManagement accountmanagement.Accountmanagement - AgentApplications agentapplications.AgentApplications - Accounts accounts.Accounts - Alerts alerts.Alerts - APIAccess apiaccess.APIAccess - APM apm.APM - ChangeTracking changetracking.Changetracking - Cloud cloud.Cloud - Dashboards dashboards.Dashboards - Edge edge.Edge - Entities entities.Entities - Events events.Events - EventsToMetrics eventstometrics.EventsToMetrics - InstallEvents installevents.Installevents - Logs logs.Logs - Logconfigurations logconfigurations.Logconfigurations - NerdGraph nerdgraph.NerdGraph - NerdStorage nerdstorage.NerdStorage - Notifications notifications.Notifications - Nrdb nrdb.Nrdb - Nrqldroprules nrqldroprules.Nrqldroprules - Plugins plugins.Plugins - ServiceLevel servicelevel.Servicelevel - Synthetics synthetics.Synthetics - UserManagement usermanagement.Usermanagement - Workflows workflows.Workflows - Workloads workloads.Workloads + AccountManagement accountmanagement.Accountmanagement + AgentApplications agentapplications.AgentApplications + AuthorizationManagement authorizationmanagement.Authorizationmanagement + Accounts accounts.Accounts + Alerts alerts.Alerts + APIAccess apiaccess.APIAccess + APM apm.APM + ChangeTracking changetracking.Changetracking + Cloud cloud.Cloud + Dashboards dashboards.Dashboards + Edge edge.Edge + Entities entities.Entities + Events events.Events + EventsToMetrics eventstometrics.EventsToMetrics + InstallEvents installevents.Installevents + Logs logs.Logs + Logconfigurations logconfigurations.Logconfigurations + NerdGraph nerdgraph.NerdGraph + NerdStorage nerdstorage.NerdStorage + Notifications notifications.Notifications + Nrdb nrdb.Nrdb + Nrqldroprules nrqldroprules.Nrqldroprules + Plugins plugins.Plugins + ServiceLevel servicelevel.Servicelevel + Synthetics synthetics.Synthetics + UserManagement usermanagement.Usermanagement + Workflows workflows.Workflows + Workloads workloads.Workloads config config.Config } @@ -80,33 +82,34 @@ func New(opts ...ConfigOption) (*NewRelic, error) { nr := &NewRelic{ config: cfg, - AccountManagement: accountmanagement.New(cfg), - AgentApplications: agentapplications.New(cfg), - Accounts: accounts.New(cfg), - Alerts: alerts.New(cfg), - APIAccess: apiaccess.New(cfg), - APM: apm.New(cfg), - ChangeTracking: changetracking.New(cfg), - Cloud: cloud.New(cfg), - Dashboards: dashboards.New(cfg), - Edge: edge.New(cfg), - Entities: entities.New(cfg), - Events: events.New(cfg), - EventsToMetrics: eventstometrics.New(cfg), - InstallEvents: installevents.New(cfg), - Logs: logs.New(cfg), - Logconfigurations: logconfigurations.New(cfg), - NerdGraph: nerdgraph.New(cfg), - NerdStorage: nerdstorage.New(cfg), - Notifications: notifications.New(cfg), - Nrdb: nrdb.New(cfg), - Nrqldroprules: nrqldroprules.New(cfg), - Plugins: plugins.New(cfg), - ServiceLevel: servicelevel.New(cfg), - Synthetics: synthetics.New(cfg), - UserManagement: usermanagement.New(cfg), - Workflows: workflows.New(cfg), - Workloads: workloads.New(cfg), + AccountManagement: accountmanagement.New(cfg), + AgentApplications: agentapplications.New(cfg), + AuthorizationManagement: authorizationmanagement.New(cfg), + Accounts: accounts.New(cfg), + Alerts: alerts.New(cfg), + APIAccess: apiaccess.New(cfg), + APM: apm.New(cfg), + ChangeTracking: changetracking.New(cfg), + Cloud: cloud.New(cfg), + Dashboards: dashboards.New(cfg), + Edge: edge.New(cfg), + Entities: entities.New(cfg), + Events: events.New(cfg), + EventsToMetrics: eventstometrics.New(cfg), + InstallEvents: installevents.New(cfg), + Logs: logs.New(cfg), + Logconfigurations: logconfigurations.New(cfg), + NerdGraph: nerdgraph.New(cfg), + NerdStorage: nerdstorage.New(cfg), + Notifications: notifications.New(cfg), + Nrdb: nrdb.New(cfg), + Nrqldroprules: nrqldroprules.New(cfg), + Plugins: plugins.New(cfg), + ServiceLevel: servicelevel.New(cfg), + Synthetics: synthetics.New(cfg), + UserManagement: usermanagement.New(cfg), + Workflows: workflows.New(cfg), + Workloads: workloads.New(cfg), } return nr, nil diff --git a/pkg/authorizationmanagement/authorizationmanagement.go b/pkg/authorizationmanagement/authorizationmanagement.go new file mode 100644 index 00000000..ae74aa7e --- /dev/null +++ b/pkg/authorizationmanagement/authorizationmanagement.go @@ -0,0 +1,26 @@ +// Code generated by tutone: DO NOT EDIT +package authorizationmanagement + +// Package authorizationmanagement provides a programmatic API for interacting with the New Relic authorization management +import ( + "github.com/newrelic/newrelic-client-go/v2/internal/http" + "github.com/newrelic/newrelic-client-go/v2/pkg/config" + "github.com/newrelic/newrelic-client-go/v2/pkg/logging" +) + +type Authorizationmanagement struct { + client http.Client + logger logging.Logger +} + +// New is used to create a new Account Management. +func New(config config.Config) Authorizationmanagement { + client := http.NewClient(config) + + pkg := Authorizationmanagement{ + client: client, + logger: config.GetLogger(), + } + + return pkg +} diff --git a/pkg/authorizationmanagement/authorizationmanagement_api.go b/pkg/authorizationmanagement/authorizationmanagement_api.go new file mode 100644 index 00000000..d4f859da --- /dev/null +++ b/pkg/authorizationmanagement/authorizationmanagement_api.go @@ -0,0 +1,52 @@ +// Code generated by tutone: DO NOT EDIT +package authorizationmanagement + +import "context" + +// Grant access for a group +func (a *Authorizationmanagement) AuthorizationManagementGrantAccess( + grantAccessOptions AuthorizationManagementGrantAccess, +) (*AuthorizationManagementGrantAccessPayload, error) { + return a.AuthorizationManagementGrantAccessWithContext(context.Background(), + grantAccessOptions, + ) +} + +// Grant access for a group +func (a *Authorizationmanagement) AuthorizationManagementGrantAccessWithContext( + ctx context.Context, + grantAccessOptions AuthorizationManagementGrantAccess, +) (*AuthorizationManagementGrantAccessPayload, error) { + + resp := AuthorizationManagementGrantAccessQueryResponse{} + vars := map[string]interface{}{ + "grantAccessOptions": grantAccessOptions, + } + + if err := a.client.NerdGraphQueryWithContext(ctx, AuthorizationManagementGrantAccessMutation, vars, &resp); err != nil { + return nil, err + } + + return &resp.AuthorizationManagementGrantAccessPayload, nil +} + +type AuthorizationManagementGrantAccessQueryResponse struct { + AuthorizationManagementGrantAccessPayload AuthorizationManagementGrantAccessPayload `json:"AuthorizationManagementGrantAccess"` +} + +const AuthorizationManagementGrantAccessMutation = `mutation( + $grantAccessOptions: AuthorizationManagementGrantAccess, +) { authorizationManagementGrantAccess( + grantAccessOptions: $grantAccessOptions, +) { + roles { + accountId + displayName + groupId + id + name + organizationId + roleId + type + } +} }` diff --git a/pkg/authorizationmanagement/authorizationmanagement_test.go b/pkg/authorizationmanagement/authorizationmanagement_test.go new file mode 100644 index 00000000..0ff74b63 --- /dev/null +++ b/pkg/authorizationmanagement/authorizationmanagement_test.go @@ -0,0 +1,21 @@ +package authorizationmanagement + +import ( + "testing" + + mock "github.com/newrelic/newrelic-client-go/v2/pkg/testhelpers" +) + +func newAuthorizationManagementTestClient(t *testing.T) Authorizationmanagement { + cfg := mock.NewIntegrationTestConfig(t) + client := New(cfg) + + return client +} + +func newMockResponse(t *testing.T, mockJSONResponse string, statusCode int) Authorizationmanagement { + ts := mock.NewMockServer(t, mockJSONResponse, statusCode) + tc := mock.NewTestConfig(t, ts) + + return New(tc) +} diff --git a/pkg/authorizationmanagement/authorizationmanagement_unit_test.go b/pkg/authorizationmanagement/authorizationmanagement_unit_test.go new file mode 100644 index 00000000..85ff9f41 --- /dev/null +++ b/pkg/authorizationmanagement/authorizationmanagement_unit_test.go @@ -0,0 +1,75 @@ +package authorizationmanagement + +import ( + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + testAuthorizationManagementCreateResponse = `{ + "authorizationManagementGrantAccess": { + "roles": [ + { + "accountId": 1111111, + "displayName": "All Product Admin", + "name": "all_product_admin", + "organizationId": null, + "roleId": 1254, + "type": "Role::V2::Standard" + }, + { + "accountId": 1111112, + "displayName": "All Product Admin", + "name": "all_product_admin", + "organizationId": null, + "roleId": 1254, + "type": "Role::V2::Standard" + } + ] + } + }` +) + +func TestCreateAuthorizationManagement(t *testing.T) { + t.Parallel() + respJSON := fmt.Sprintf(`{ "data":%s }`, testAuthorizationManagementCreateResponse) + authorizationManagement := newMockResponse(t, respJSON, http.StatusCreated) + + roleId := "1254" + accountId := 1111111 + groupId := "1111112" + + authorizationManagementGrantAccessInput := AuthorizationManagementGrantAccess{ + AccountAccessGrants: []AuthorizationManagementAccountAccessGrant{ + { + AccountID: accountId, + RoleId: roleId, + }, + }, + GroupId: groupId, + } + + actual, err := authorizationManagement.AuthorizationManagementGrantAccess(authorizationManagementGrantAccessInput) + + assert.NoError(t, err) + assert.NotNil(t, actual) + assert.GreaterOrEqual(t, len(actual.Roles), 1) + + roleIdFound := false + accountIdFound := false + + for _, role := range actual.Roles { + if fmt.Sprint(role.RoleId) == roleId { + roleIdFound = true + } + if role.AccountID == accountId { + accountIdFound = true + } + } + + assert.True(t, roleIdFound, "Role not found") + assert.True(t, accountIdFound, "Account not found") +} diff --git a/pkg/authorizationmanagement/types.go b/pkg/authorizationmanagement/types.go new file mode 100644 index 00000000..edf9c2cc --- /dev/null +++ b/pkg/authorizationmanagement/types.go @@ -0,0 +1,62 @@ +// Code generated by tutone: DO NOT EDIT +package authorizationmanagement + +// AuthorizationManagementAccountAccessGrant - The Account and Role a Group should have access to +type AuthorizationManagementAccountAccessGrant struct { + // The Account ID the Role will grant access to + AccountID int `json:"accountId"` + // The Role ID that will define the access + RoleId string `json:"roleId"` +} + +// AuthorizationManagementGrantAccess - The input object representing the access to grant for the group +type AuthorizationManagementGrantAccess struct { + // The Role and the Account the Target Group should have access to + AccountAccessGrants []AuthorizationManagementAccountAccessGrant `json:"accountAccessGrants,omitempty"` + // The Role and the Group the Target Group should have access to + GroupAccessGrants []AuthorizationManagementGroupAccessGrant `json:"groupAccessGrants,omitempty"` + // The Group ID that will have access granted + GroupId string `json:"groupId"` + // The Role and the Organization the Target Group should have access to + OrganizationAccessGrants []AuthorizationManagementOrganizationAccessGrant `json:"organizationAccessGrants,omitempty"` +} + +// AuthorizationManagementGrantAccessPayload - Autogenerated return type of GrantAccess +type AuthorizationManagementGrantAccessPayload struct { + // the roles that were granted to this group + Roles []AuthorizationManagementGrantedRole `json:"roles"` +} + +// AuthorizationManagementGrantedRole - A Granted Role represents the access given to a group. +type AuthorizationManagementGrantedRole struct { + // the account that this role grants access to + AccountID int `json:"accountId,omitempty"` + // the name of the object + DisplayName string `json:"displayName,omitempty"` + // the group that this role grants access to + GroupId string `json:"groupId,omitempty"` + // a value that uniquely identifies this object + ID string `json:"id"` + // the name of the object + Name string `json:"name"` + // the organization this role grants access to + OrganizationId string `json:"organizationId,omitempty"` + // the role that defines this access + RoleId int `json:"roleId"` + // the type of the role + Type string `json:"type"` +} + +// AuthorizationManagementGroupAccessGrant - The Group and Role another Group should have access to +type AuthorizationManagementGroupAccessGrant struct { + // The Group ID the Role will grant access to + GroupId string `json:"groupId"` + // The Role ID that will define the access + RoleId string `json:"roleId"` +} + +// AuthorizationManagementOrganizationAccessGrant - The Organization Role a Group should have access to +type AuthorizationManagementOrganizationAccessGrant struct { + // The Role ID that will define the Organization access + RoleId string `json:"roleId"` +}