/** * NewRelic API constructor. * * @param restApiUrl NewRelic REST API URL, for example https://api.newrelic.com * @param syntheticsApiUrl NewRelic Synthetics API URL * @param apiKey API Key for given NewRelic account */ public NewRelicInternalApi(String restApiUrl, String syntheticsApiUrl, String apiKey) { NewRelicClient client = new NewRelicClient(restApiUrl, apiKey); applicationsApi = new DefaultApplicationsApi(client); alertsChannelsApi = new DefaultAlertsChannelsApi(client); alertsPoliciesApi = new DefaultAlertsPoliciesApi(client); alertsConditionsApi = new DefaultAlertsConditionsApi(client); alertsExternalServiceConditionsApi = new DefaultAlertsExternalServiceConditionsApi(client); alertsNrqlConditionsApi = new DefaultAlertsNrqlConditionsApi(client); alertsSyntheticsConditionApi = new DefaultAlertsSyntheticsConditionsApi(client); keyTransactionsApi = new DefaultKeyTransactionsApi(client); deploymentsApi = new DefaultDeploymentsApi(client); serversApi = new DefaultServersApi(client); usersApi = new DefaultUsersApi(client); NewRelicClient syntheticsClient = new NewRelicClient(syntheticsApiUrl, apiKey); syntheticsMonitorsApi = new DefaultSyntheticsMonitorsApi(syntheticsClient); } }
/** * NewRelic API constructor. * * @param restApiUrl NewRelic REST API URL, for example https://api.newrelic.com * @param syntheticsApiUrl NewRelic Synthetics API URL * @param apiKey API Key for given NewRelic account */ public NewRelicApi(String restApiUrl, String syntheticsApiUrl, String apiKey) { NewRelicInternalApi internalApi = new NewRelicInternalApi(restApiUrl, syntheticsApiUrl, apiKey); applicationsApi = internalApi.getApplicationsApi(); alertsChannelsApi = internalApi.getAlertsChannelsApi(); alertsPoliciesApi = internalApi.getAlertsPoliciesApi(); alertsConditionsApi = internalApi.getAlertsConditionsApi(); alertsExternalServiceConditionsApi = internalApi.getAlertsExternalServiceConditionsApi(); alertsNrqlConditionsApi = internalApi.getAlertsNrqlConditionsApi(); alertsSyntheticsConditionApi = internalApi.getAlertsSyntheticsConditionApi(); keyTransactionsApi = internalApi.getKeyTransactionsApi(); deploymentsApi = internalApi.getDeploymentsApi(); serversApi = internalApi.getServersApi(); usersApi = internalApi.getUsersApi(); syntheticsMonitorsApi = internalApi.getSyntheticsMonitorsApi(); } }
@Override public List<AlertsChannel> list() { return getPageable( client.target(CHANNELS_URL).request(APPLICATION_JSON_TYPE), AlertsChannelList.class) .getList(); }
@Override public Optional<Monitor> getByName(String monitorName) { Invocation.Builder builder = client .target(MONITORS_URL) .request(); return getPageable(builder, MonitorList.class) .filter(monitor -> monitor.getName().equals(monitorName)) .getSingle(); }
@Override public Optional<Application> getByName(String applicationName) { Invocation.Builder builder = client .target(APPLICATIONS_URL) .queryParam("filter[name]", applicationName) .request(APPLICATION_JSON_TYPE); return getPageable(builder, ApplicationList.class) .filter(application -> application.getName().equals(applicationName)) .getSingle(); }
@Override public Optional<AlertsPolicy> getByName(String alertsPolicyName) { Invocation.Builder builder = client .target(POLICIES_URL) .queryParam("filter[name]", alertsPolicyName) .request(APPLICATION_JSON_TYPE); return getPageable(builder, AlertsPolicyList.class) .filter(alertsPolicy -> alertsPolicy.getName().equals(alertsPolicyName)) .getSingle(); }
@Override public Optional<Server> getByName(String serverName) { String serverNameEncoded = UriComponent.encode(serverName, QUERY_PARAM_SPACE_ENCODED); Invocation.Builder builder = client .target(SERVERS_URL) .queryParam("filter[name]", serverNameEncoded) .request(APPLICATION_JSON_TYPE); return getPageable(builder, ServerList.class) .filter(application -> application.getName().equals(serverName)) .getSingle(); }
@Override public Optional<User> getByEmail(String userEmail) { String userEmailEncoded = UriComponent.encode(userEmail, QUERY_PARAM_SPACE_ENCODED); Invocation.Builder builder = client .target(USERS_URL) .queryParam("filter[email]", userEmailEncoded) .request(APPLICATION_JSON_TYPE); return getPageable(builder, UserList.class) .filter(user -> user.getEmail().equals(userEmail)) .getSingle(); }
@Override public List<AlertsExternalServiceCondition> list(int policyId) { return getPageable( client.target(CONDITIONS_URL).queryParam("policy_id", policyId).request(APPLICATION_JSON_TYPE), AlertsExternalServiceConditionList.class) .getList(); }
@Override public List<AlertsSyntheticsCondition> list(int policyId) { return getPageable( client.target(CONDITIONS_URL).queryParam("policy_id", policyId).request(APPLICATION_JSON_TYPE), AlertsSyntheticsConditionList.class) .getList(); }
@Override public List<AlertsCondition> list(int policyId) { return getPageable( client.target(CONDITIONS_URL).queryParam("policy_id", policyId).request(APPLICATION_JSON_TYPE), AlertsConditionList.class) .getList(); }
@Override public List<AlertsNrqlCondition> list(int policyId) { return getPageable( client.target(CONDITIONS_URL).queryParam("policy_id", policyId).request(APPLICATION_JSON_TYPE), AlertsNrqlConditionList.class) .getList(); }
@Override public List<Deployment> list(int applicationId) { return getPageable( client.target(DEPLOYMENTS_URL) .resolveTemplate("application_id", applicationId) .request(APPLICATION_JSON_TYPE), DeploymentsList.class) .getList(); }
@Before public void setUp() { NewRelicClient clientMock = mock(NewRelicClient.class); WebTarget webTargetMock = mock(WebTarget.class); Invocation.Builder builderMock = mock(Invocation.Builder.class); when(clientMock.target("/v2/applications.json")).thenReturn(webTargetMock); when(webTargetMock.queryParam("filter[name]", "app")).thenReturn(webTargetMock); when(webTargetMock.request(APPLICATION_JSON_TYPE)).thenReturn(builderMock); when(builderMock.get()).thenReturn(responseMock); testee = new DefaultApplicationsApi(clientMock); }
@Before public void setUp() { NewRelicClient clientMock = mock(NewRelicClient.class); WebTarget webTargetMock = mock(WebTarget.class); Invocation.Builder builderMock = mock(Invocation.Builder.class); when(clientMock.target("/v2/alerts_policies.json")).thenReturn(webTargetMock); when(webTargetMock.queryParam("filter[name]", "policy")).thenReturn(webTargetMock); when(webTargetMock.request(APPLICATION_JSON_TYPE)).thenReturn(builderMock); when(builderMock.get()).thenReturn(responseMock); testee = new DefaultAlertsPoliciesApi(clientMock); }
@Before public void setUp() { NewRelicClient clientMock = mock(NewRelicClient.class); WebTarget webTargetMock = mock(WebTarget.class); Invocation.Builder builderMock = mock(Invocation.Builder.class); when(clientMock.target("/v2/servers.json")).thenReturn(webTargetMock); when(webTargetMock.queryParam("filter[name]", "server")).thenReturn(webTargetMock); when(webTargetMock.request(APPLICATION_JSON_TYPE)).thenReturn(builderMock); when(builderMock.get()).thenReturn(responseMock); testee = new DefaultServersApi(clientMock); }
<T, LT extends ObjectList<T, LT>> LT getPageable(Builder request, Class<LT> responseType) { Response response = request.get(); LT responseBody = response.readEntity(responseType); Link next = response.getLink("next"); return followPageable(next, responseBody, responseType); }