Skip to content

Instantly share code, notes, and snippets.

@ardeay
Forked from weienwong/index.js
Created June 3, 2022 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardeay/5be713c8a227f6fd879ea995076febb0 to your computer and use it in GitHub Desktop.
Save ardeay/5be713c8a227f6fd879ea995076febb0 to your computer and use it in GitHub Desktop.
Webhook creation and test script
require('dotenv').config()
const request = require('request')
// webhooks instances-api
function scheduleItemPublishWebhook () {
// create webhook
const createItemPublishWebhookOption = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: null,
resource: 'items',
eventAction: 4,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: '',
body: {
text: `Item has been published in ${process.env.ENV}`
}
},
json: true
}
request(createItemPublishWebhookOption, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
// scheduled item publishing
const options = {
method: 'POST',
url: `${process.env.INSTANCE_API_HOST}/v1/content/models/${process.env.MODEL_ZUID}/items/${process.env.ITEM_ZUID}/publishings`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.DEV_TOKEN}`
},
body: {
version: 1,
publishAt: 'now',
unpublishAt: '2020-02-24'
},
json: true
}
request(options, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
}
function stylesheetCreation () {
const createStylesheetWebhookOption = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: null,
resource: 'stylesheets',
eventAction: 1,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: '',
body: {
text: `New stylesheet created for instance ${process.env.INSTANCE_ZUID} in ${process.env.ENV}`
}
},
json: true
}
request(createStylesheetWebhookOption, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
const stylesheetCreationOption = {
method: 'POST',
url: `${process.env.INSTANCE_API_HOST}/v1/web/stylesheets`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
code: '@pale-green-color: #4D926F;#header {color: @pale-green-color;}h2{color: @pale-green-color;}',
filename: 'style.less',
type: 'text/less'
},
json: true
}
request(stylesheetCreationOption, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
}
// webhooks accounts-api
function addTeamToInstance () {
const addDomainToInstanceWebhookOption = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: '8-a0a29cebe6-32shdn',
resource: 'domains',
eventAction: 1,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: '',
body: {
text: 'Domain has been added to instance 8-a0a29cebe6-32shdn'
}
},
json: true
}
request(addDomainToInstanceWebhookOption, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
const addDomainToInstance = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/instances/${process.env.INSTANCE_ZUID}/domains`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
domain: 'www.aasdfs1asdf2asdf34dfbeasdfpasasdfdfbops.com'
},
json: true
}
request(addDomainToInstance, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
}
function addRole () {
const roleCreationWebhook = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: null,
resource: 'roles',
eventAction: 1,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: '',
body: {
text: `New role created for instance ${process.env.INSTANCE_ZUID} in ${process.env.ENV}`
}
},
json: true
}
request(roleCreationWebhook, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
const roleCreationOption = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/roles`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
name: 'bane',
entityZUID: process.env.INSTANCE_ZUID,
systemRoleZUID: '31-71cfc74-d3v3l0p3r'
},
json: true
}
request(roleCreationOption, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
}
function generateInstanceRole () {
const roleGenerationWebhook = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: '8-a0a29cebe6-32shdn',
resource: 'roles',
eventAction: 1,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: '',
body: {
text: `New role generated for instance ${process.env.INSTANCE_ZUID} in ${process.env.ENV}`
}
},
json: true
}
request(roleGenerationWebhook, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
const roleCreationOption = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/instances/${process.env.INSTANCE_ZUID}/roles`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {},
json: true
}
request(roleCreationOption, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
}
function createTeamInvite () {
const createTeamInviteWebhook = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: '',
resource: 'invites',
eventAction: 1,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: `Bearer ${process.env.APP_SID}`,
body: {
text: `New invite creted for instance ${process.env.INSTANCE_ZUID} in ${process.env.ENV}`
}
},
json: true
}
request(createTeamInviteWebhook, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
const createInviteReq = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/invites`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
inviteeName: 'Test testington',
inviteeEmail: 'test@email.com',
entityZUID: process.env.INSTANCE_ZUID,
accessLevel: 1
},
json: true
}
request(createInviteReq, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
}
function createAndUpdateGranularRoles () {
const createGranularWebhook = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: '',
resource: 'granulars',
eventAction: 1,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: '',
body: {
text: `New granular role created for instance ${process.env.INSTANCE_ZUID} in ${process.env.ENV}`
}
},
json: true
}
request(createGranularWebhook, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
const updateGranularWebhook = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: '',
resource: 'granulars',
eventAction: 2,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: '',
body: {
text: `Granular role has been updated for instance ${process.env.INSTANCE_ZUID} in ${process.env.ENV}`
}
},
json: true
}
request(updateGranularWebhook, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
// WEBHOOKS
const roleCreationOption = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/roles`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
name: 'bane',
entityZUID: process.env.INSTANCE_ZUID,
systemRoleZUID: '31-71cfc74-d3v3l0p3r'
},
json: true
}
request(roleCreationOption, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
const roleZUID = body.data.ZUID
const createGranularReq = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/roles/${roleZUID}/granulars`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
resourceZUID: '6-e3d0e0-965qp6',
create: false,
read: true,
update: true,
delete: true,
publish: true,
grant: true
},
json: true
}
request(createGranularReq, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
const createGranularReq = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/roles/${roleZUID}/granulars`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
resourceZUID: '6-e3d0e0-965qp6',
create: false,
read: true,
update: true,
delete: true,
publish: true,
grant: true
},
json: true
}
request(createGranularReq, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
const granularZUID = body.data.ZUID
const updateGranularReq = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/roles/${roleZUID}/granulars`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
ZUID: granularZUID,
resourceZUID: '6-e3d0e0-965qp6',
create: false,
read: true,
update: true,
delete: true,
publish: true,
grant: true
},
json: true
}
request(updateGranularReq, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
})
})
})
}
function addDomainToInstance () {
const addDomainWebhook = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: '8-a0a29cebe6-32shdn',
resource: 'domains',
eventAction: 1,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: `Bearer ${process.env.APP_SID}`,
body: {
text: `Domain has been added to instance ${process.env.INSTANCE_ZUID} in ${process.env.ENV}`
}
},
json: true
}
request(addDomainWebhook, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
const addDomainReq = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/instances/${process.env.INSTANCE_ZUID}/domains`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
domain: '1234asdsadsafdsfdsasdf.com'
},
json: true
}
request(addDomainReq, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
}
function deleteRole () {
const roleDeletionWebhook = {
method: 'POST',
url: `${process.env.ACCOUNTS_API_HOST}/v1/webhooks`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {
scopedResource: '8-a0a29cebe6-32shdn',
parentResourceZUID: null,
resource: 'roles',
eventAction: 3,
method: 'POST',
URL: process.env.SLACK_WEBHOOK_URL,
contentType: 'application/json',
authorization: '',
body: {
text: `Role deleted from ${process.env.INSTANCE_ZUID} in ${process.env.ENV}`
}
},
json: true
}
request(roleDeletionWebhook, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
const roleCreationOption = {
method: 'DELETE',
url: `${process.env.ACCOUNTS_API_HOST}/v1/roles/30-f0bcf087b5-2rbkn8`,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.APP_SID}`
},
body: {},
json: true
}
request(roleCreationOption, function (error, response, body) {
if (error) throw new Error(error)
console.log(body)
})
}
scheduleItemPublishWebhook()
addTeamToInstance()
stylesheetCreation()
addRole()
generateInstanceRole()
createTeamInvite()
createAndUpdateGranularRoles()
addDomainToInstance()
deleteRole()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment