Skip to content

Commit

Permalink
feat!: monitoring convert to typescript (#360)
Browse files Browse the repository at this point in the history
* feat: monitoring conversion by delete js file and add ts from generator

* upgrade package.json

* fixed samples test by modify the matched path

* prelint, add eslint for samples test

* fix upstream proto links, get rid of synth hack

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
2 people authored and Ace Nassri committed Nov 10, 2022
1 parent 7065552 commit f004ed1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
41 changes: 30 additions & 11 deletions monitoring/snippets/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ async function restorePolicies(projectId) {
// Restore each policy one at a time
let policy = policies[index];
if (await doesAlertPolicyExist(policy.name)) {
policy = await client.updateAlertPolicy({alertPolicy: policy});
policy = await client.updateAlertPolicy({
alertPolicy: policy,
});
} else {
// Clear away output-only fields
delete policy.name;
Expand All @@ -101,7 +103,9 @@ async function restorePolicies(projectId) {
}
async function doesAlertPolicyExist(name) {
try {
const [policy] = await client.getAlertPolicy({name});
const [policy] = await client.getAlertPolicy({
name,
});
return policy ? true : false;
} catch (err) {
if (err && err.code === 5) {
Expand Down Expand Up @@ -131,7 +135,10 @@ async function deleteChannels(projectId, filter) {
// const projectId = 'YOUR_PROJECT_ID';
// const filter = 'A filter for selecting policies, e.g. description:"cloud"';

const request = {name: client.projectPath(projectId), filter};
const request = {
name: client.projectPath(projectId),
filter,
};
const channels = await client.listNotificationChannels(request);
console.log(channels);
for (const channel of channels[0]) {
Expand Down Expand Up @@ -173,15 +180,19 @@ async function replaceChannels(projectId, alertPolicyId, channelIds) {
// ];

const notificationChannels = channelIds.map(id =>
notificationClient.notificationChannelPath(projectId, id)
notificationClient.projectNotificationChannelPath(projectId, id)
);

for (const channel of notificationChannels) {
const updateChannelRequest = {
updateMask: {paths: ['enabled']},
updateMask: {
paths: ['enabled'],
},
notificationChannel: {
name: channel,
enabled: {value: true},
enabled: {
value: true,
},
},
};
try {
Expand All @@ -190,7 +201,9 @@ async function replaceChannels(projectId, alertPolicyId, channelIds) {
const createChannelRequest = {
notificationChannel: {
name: channel,
notificationChannel: {type: 'email'},
notificationChannel: {
type: 'email',
},
},
};
const newChannel = await notificationClient.createNotificationChannel(
Expand All @@ -201,9 +214,11 @@ async function replaceChannels(projectId, alertPolicyId, channelIds) {
}

const updateAlertPolicyRequest = {
updateMask: {paths: ['notification_channels']},
updateMask: {
paths: ['notification_channels'],
},
alertPolicy: {
name: alertClient.alertPolicyPath(projectId, alertPolicyId),
name: alertClient.projectAlertPolicyPath(projectId, alertPolicyId),
notificationChannels: notificationChannels,
},
};
Expand Down Expand Up @@ -243,10 +258,14 @@ async function enablePolicies(projectId, enabled, filter) {
policies
.map(policy => {
return {
updateMask: {paths: ['enabled']},
updateMask: {
paths: ['enabled'],
},
alertPolicy: {
name: policy.name,
enabled: {value: enabled},
enabled: {
value: enabled,
},
},
};
})
Expand Down
9 changes: 6 additions & 3 deletions monitoring/snippets/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async function getMetricDescriptor(projectId, metricId) {
// const metricId = 'custom.googleapis.com/your/id';

const request = {
name: client.metricDescriptorPath(projectId, metricId),
name: client.projectMetricDescriptorPath(projectId, metricId),
};

// Retrieves a metric descriptor
Expand Down Expand Up @@ -143,7 +143,7 @@ async function deleteMetricDescriptor(projectId, metricId) {
// const metricId = 'custom.googleapis.com/stores/daily_sales';

const request = {
name: client.metricDescriptorPath(projectId, metricId),
name: client.projectMetricDescriptorPath(projectId, metricId),
};

// Deletes a metric descriptor
Expand Down Expand Up @@ -432,7 +432,10 @@ async function getMonitoredResourceDescriptor(projectId, resourceType) {
// const resourceType = 'some_resource_type, e.g. cloudsql_database';

const request = {
name: client.monitoredResourceDescriptorPath(projectId, resourceType),
name: client.projectMonitoredResourceDescriptorPath(
projectId,
resourceType
),
};

// Lists monitored resource descriptors
Expand Down
31 changes: 22 additions & 9 deletions monitoring/snippets/uptime.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ async function createUptimeCheckConfig(projectId, hostname) {
monitoredResource: {
// See the Uptime Check docs for supported MonitoredResource types
type: 'uptime_url',
labels: {host: hostname},
labels: {
host: hostname,
},
},
httpCheck: {
path: '/',
port: 80,
},
timeout: {
seconds: 10,
},
period: {
seconds: 300,
},
httpCheck: {path: '/', port: 80},
timeout: {seconds: 10},
period: {seconds: 300},
},
};

Expand Down Expand Up @@ -154,7 +163,7 @@ async function getUptimeCheckConfig(projectId, uptimeCheckConfigId) {

const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId),
name: client.projectUptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};

console.log(`Retrieving ${request.name}`);
Expand Down Expand Up @@ -197,7 +206,7 @@ async function deleteUptimeCheckConfig(projectId, uptimeCheckConfigId) {

const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId),
name: client.projectUptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};

console.log(`Deleting ${request.name}`);
Expand Down Expand Up @@ -232,7 +241,7 @@ async function updateUptimeCheckConfigDisplayName(

const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId),
name: client.projectUptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};

console.log(`Updating ${request.name} to ${displayName}`);
Expand All @@ -241,10 +250,14 @@ async function updateUptimeCheckConfigDisplayName(
request.uptimeCheckConfig = {
name: request.name,
displayName: displayName,
httpCheck: {path: path},
httpCheck: {
path: path,
},
};

request.updateMask = {paths: ['display_name', 'http_check.path']};
request.updateMask = {
paths: ['display_name', 'http_check.path'],
};

const [response] = await client.updateUptimeCheckConfig(request);
console.log(`${response.name} config updated.`);
Expand Down

0 comments on commit f004ed1

Please sign in to comment.