2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2021-11-26 04:10:03 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2021-11-26 04:10:03 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { GrafanaCredentials } from './types';
|
2021-11-26 04:10:03 -08:00
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
export function tolerateTrailingSlash(baseUrl: string) {
|
|
|
|
return baseUrl.endsWith('/') ? baseUrl.substr(0, baseUrl.length - 1) : baseUrl;
|
|
|
|
}
|
|
|
|
|
2021-11-26 04:10:03 -08:00
|
|
|
export async function grafanaApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const { baseUrl: rawBaseUrl } = (await this.getCredentials('grafanaApi')) as GrafanaCredentials;
|
2021-11-26 04:10:03 -08:00
|
|
|
|
|
|
|
const baseUrl = tolerateTrailingSlash(rawBaseUrl);
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: `${baseUrl}/api${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
feat: Add more credentials tests (#3668)
* ✨ Add injection to notion,
Add test to notion in cred
* 🔥 Remove unuse method
* 🎨 Move testing from node file to cred file
* ✨ Add injection and testing in facebook graph
* Add cred injec with testing
* Add Cred injection and cred test
* Add cred injection, and cred testing for typeform, fix issue in clickup
* Add cred injection, move testing inside creds
* Add cred injection and cred testing to SendGrid
* Add cred injection and cred testing to woocommerce
* Add cred injection, add cred test to gitlab
* 🔥 Fix duplicated imports in Mautic cred
* 🔥 removed unused credentials testing in node
* Add cred injection, cred testing, handles slash trailing for Grafana node
* Add cred injection, cred testing to shopify
* Add cred injection , add cred testing to stripe
* changed cred injection, add testing to cred for mattermost
* add cred injection and testing for dropbox
* Add cred injection, cred testing to webflow
* ✨ Add cred injection and cred test to nocodb
* ✨ Add cred injection, cred testing to mailchimp
* 🐛 fix a bug In credentials testing
* ✨ Add cred injection, cred testing to sms77
* ✨ Add cred injection, cred testing to ActiveCampaign
* Add cred injection, cred testing to TheHive
* ✨ Add cred injection, add cred testing to ApiTemplateio
* ✨ Add cred injection, add cred testing for zoom
* ✨ Add cred injection, cred testing to rocketchat
* ✨ Add cred injection, add cred test to getResponse
* 🔥 Remove useless authentcate creds and testing from facebookGraphApp
* 🔥 Remove useless imports in FacebookGrappApp credentials file
* 🔥 Removed useless imports and if statement
* 🐛 Add version to header when testing cred
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
2022-07-15 07:20:41 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'grafanaApi', options);
|
2021-11-26 04:10:03 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (error?.response?.data?.message === 'Team member not found') {
|
|
|
|
error.response.data.message += '. Are you sure the user is a member of this team?';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error?.response?.data?.message === 'Team not found') {
|
|
|
|
error.response.data.message += ' with the provided ID';
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
error?.response?.data?.message ===
|
|
|
|
'A dashboard with the same name in the folder already exists'
|
|
|
|
) {
|
|
|
|
error.response.data.message =
|
|
|
|
'A dashboard with the same name already exists in the selected folder';
|
2021-11-26 04:10:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error?.response?.data?.message === 'Team name taken') {
|
|
|
|
error.response.data.message = 'This team name is already taken. Please choose a new one.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error?.code === 'ECONNREFUSED') {
|
2022-08-17 08:50:24 -07:00
|
|
|
error.message =
|
|
|
|
'Invalid credentials or error in establishing connection with given credentials';
|
2021-11-26 04:10:03 -08:00
|
|
|
}
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-11-26 04:10:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function throwOnEmptyUpdate(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
resource: string,
|
|
|
|
updateFields: IDataObject,
|
|
|
|
) {
|
|
|
|
if (!Object.keys(updateFields).length) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deriveUid(this: IExecuteFunctions, uidOrUrl: string) {
|
|
|
|
if (!uidOrUrl.startsWith('http')) return uidOrUrl;
|
|
|
|
|
|
|
|
const urlSegments = uidOrUrl.split('/');
|
|
|
|
const uid = urlSegments[urlSegments.indexOf('d') + 1];
|
|
|
|
|
|
|
|
if (!uid) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Failed to derive UID from URL');
|
|
|
|
}
|
|
|
|
|
|
|
|
return uid;
|
feat: Add more credentials tests (#3668)
* ✨ Add injection to notion,
Add test to notion in cred
* 🔥 Remove unuse method
* 🎨 Move testing from node file to cred file
* ✨ Add injection and testing in facebook graph
* Add cred injec with testing
* Add Cred injection and cred test
* Add cred injection, and cred testing for typeform, fix issue in clickup
* Add cred injection, move testing inside creds
* Add cred injection and cred testing to SendGrid
* Add cred injection and cred testing to woocommerce
* Add cred injection, add cred test to gitlab
* 🔥 Fix duplicated imports in Mautic cred
* 🔥 removed unused credentials testing in node
* Add cred injection, cred testing, handles slash trailing for Grafana node
* Add cred injection, cred testing to shopify
* Add cred injection , add cred testing to stripe
* changed cred injection, add testing to cred for mattermost
* add cred injection and testing for dropbox
* Add cred injection, cred testing to webflow
* ✨ Add cred injection and cred test to nocodb
* ✨ Add cred injection, cred testing to mailchimp
* 🐛 fix a bug In credentials testing
* ✨ Add cred injection, cred testing to sms77
* ✨ Add cred injection, cred testing to ActiveCampaign
* Add cred injection, cred testing to TheHive
* ✨ Add cred injection, add cred testing to ApiTemplateio
* ✨ Add cred injection, add cred testing for zoom
* ✨ Add cred injection, cred testing to rocketchat
* ✨ Add cred injection, add cred test to getResponse
* 🔥 Remove useless authentcate creds and testing from facebookGraphApp
* 🔥 Remove useless imports in FacebookGrappApp credentials file
* 🔥 Removed useless imports and if statement
* 🐛 Add version to header when testing cred
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
2022-07-15 07:20:41 -07:00
|
|
|
}
|