Skip to content

Commit

Permalink
chore: update samples to reflect v1 Grafeas API (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche authored and bcoe committed Jun 20, 2019
1 parent d8523f7 commit 7ac60e4
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 117 deletions.
5 changes: 2 additions & 3 deletions container-analysis/snippets/createNote.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ async function main(
// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();
const grafeasClient = client.getGrafeasClient();

// Construct request
// Associate the Note with a metadata type
// https://cloud.google.com/container-registry/docs/container-analysis#supported_metadata_types
// Here, we use the type "vulnerabiltity"
const formattedParent = grafeasClient.projectPath(projectId);
const formattedParent = client.getGrafeasClient().projectPath(projectId);

// Creates and returns a new Note
const [note] = await grafeasClient.createNote({
const [note] = await client.getGrafeasClient().createNote({
parent: formattedParent,
noteId: noteId,
note: {
Expand Down
31 changes: 23 additions & 8 deletions container-analysis/snippets/createOccurrence.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,36 @@ async function main(
// const imageUrl = 'https://gcr.io/my-project/my-image:123' // Image to attach metadata to

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Construct request
const formattedParent = client.projectPath(occurrenceProjectId);
const formattedNote = client.notePath(noteProjectId, noteId);
const formattedParent = client
.getGrafeasClient()
.projectPath(occurrenceProjectId);
const formattedNote = client
.getGrafeasClient()
.notePath(noteProjectId, noteId);

// Creates and returns a new Occurrence associated with an existing Note
const [occurrence] = await client.createOccurrence({
const [occurrence] = await client.getGrafeasClient().createOccurrence({
parent: formattedParent,
occurrence: {
noteName: formattedNote,
vulnerability: {},
resource: {
uri: imageUrl,
resourceUri: imageUrl,
vulnerability: {
packageIssue: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
minAffectedVersion: {
kind: 'MINIMUM',
},
fixedVersion: {
kind: 'MAXIMUM',
},
},
],
},
},
});
Expand Down
6 changes: 3 additions & 3 deletions container-analysis/snippets/deleteNote.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ async function main(
// const noteId = 'my-note-id' // Id of the note

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Get the full path to the note
const formattedName = client.notePath(projectId, noteId);

// Delete the note
await client.deleteNote({name: formattedName});
await client.getGrafeasClient().deleteNote({name: formattedName});
console.log(`Note ${formattedName} deleted.`);
// [END containeranalysis_delete_note]
}
Expand Down
10 changes: 6 additions & 4 deletions container-analysis/snippets/deleteOccurrence.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ async function main(
// const occurrenceId = 'my-occurrence' // The API-generated identifier associated with the occurrence

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Get full path to occurrence
const formattedName = client.occurrencePath(projectId, occurrenceId);
const formattedName = client
.getGrafeasClient()
.occurrencePath(projectId, occurrenceId);

// Deletes an existing Occurrence from the server
await client.deleteOccurrence({
await client.getGrafeasClient().deleteOccurrence({
name: formattedName,
});

Expand Down
11 changes: 4 additions & 7 deletions container-analysis/snippets/getDiscoveryInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ async function main(
// const imageUrl = 'https://gcr.io/my-project/my-image:123' // Image to attach metadata to

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

const formattedParent = client.projectPath(projectId);
const formattedParent = client.getGrafeasClient().projectPath(projectId);
// Retrieves and prints the Discovery Occurrence created for a specified image
// The Discovery Occurrence contains information about the initial scan on the image
const [occurrences] = await client.listOccurrences({
const [occurrences] = await client.getGrafeasClient().listOccurrences({
parent: formattedParent,
filter: `kind = "DISCOVERY" AND resourceUrl = "${imageUrl}"`,
});
Expand All @@ -29,9 +29,6 @@ async function main(
console.log(`Discovery Occurrences for ${imageUrl}`);
occurrences.forEach(occurrence => {
console.log(`${occurrence.name}:`);
console.log(
` Created: ${new Date(occurrence.createTime.seconds * 1000)}`
);
});
} else {
console.log('No occurrences found.');
Expand Down
6 changes: 3 additions & 3 deletions container-analysis/snippets/getNote.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ async function main(
// const noteId = 'my-note-id' // Id of the note

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Get the full path to the note
const formattedName = client.notePath(projectId, noteId);
// Retrieve the specified note
const [note] = await client.getNote({name: formattedName});
const [note] = await client.getGrafeasClient().getNote({name: formattedName});

console.log(`Note name: ${note.name}`);
// [END containeranalysis_get_note]
Expand Down
10 changes: 6 additions & 4 deletions container-analysis/snippets/getOccurrence.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ async function main(
// const occurrenceId = 'my-occurrence' // The API-generated identifier associated with the occurrence

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Get full path to occurrence
const formattedName = client.occurrencePath(projectId, occurrenceId);
const formattedName = client
.getGrafeasClient()
.occurrencePath(projectId, occurrenceId);

// Retrieves the specified occurrence
const [occurrence] = await client.getOccurrence({
const [occurrence] = await client.getGrafeasClient().getOccurrence({
name: formattedName,
});

Expand Down
11 changes: 4 additions & 7 deletions container-analysis/snippets/highVulnerabilitiesForImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ async function main(
// const imageUrl = 'https://gcr.io/my-project/my-image:123' // Image to attach metadata to

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

const formattedParent = client.projectPath(projectId);
const formattedParent = client.getGrafeasClient().projectPath(projectId);

// Retrieve a list of vulnerability occurrences with a severity level of 'HIGH' or greater
const [occurrences] = await client.listOccurrences({
const [occurrences] = await client.getGrafeasClient().listOccurrences({
parent: formattedParent,
filter: `kind = "VULNERABILITY" AND resourceUrl = "${imageUrl}"`,
});
Expand All @@ -33,9 +33,6 @@ async function main(
occurrence.vulnerability.severity === 'CRITICAL'
) {
console.log(`${occurrence.name}:`);
console.log(
` Created: ${new Date(occurrence.createTime.seconds * 1000)}`
);
}
});
} else {
Expand Down
11 changes: 4 additions & 7 deletions container-analysis/snippets/occurrencesForImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ async function main(
// const imageUrl = 'https://gcr.io/my-project/my-image:123' // Image to attach metadata to

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

const formattedParent = client.projectPath(projectId);
const formattedParent = client.getGrafeasClient().projectPath(projectId);

// Retrieves all the Occurrences associated with a specified image
const [occurrences] = await client.listOccurrences({
const [occurrences] = await client.getGrafeasClient().listOccurrences({
parent: formattedParent,
filter: `resourceUrl = "${imageUrl}"`,
});
Expand All @@ -29,9 +29,6 @@ async function main(
console.log(`Occurrences for ${imageUrl}`);
occurrences.forEach(occurrence => {
console.log(`${occurrence.name}:`);
console.log(
` Created: ${new Date(occurrence.createTime.seconds * 1000)}`
);
});
} else {
console.log('No occurrences found.');
Expand Down
9 changes: 3 additions & 6 deletions container-analysis/snippets/occurrencesForNote.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@ async function main(
// const noteId = 'my-note-id' // Id of the note

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Get path to Note
const formattedNote = client.notePath(projectId, noteId);

// Retrieves all the Occurrences associated with a specified Note
const [occurrences] = await client.listNoteOccurrences({
const [occurrences] = await client.getGrafeasClient().listNoteOccurrences({
name: formattedNote,
});
if (occurrences.length) {
console.log('Occurrences:');
occurrences.forEach(occurrence => {
console.log(`${occurrence.name}:`);
console.log(
` Created: ${new Date(occurrence.createTime.seconds * 1000)}`
);
});
} else {
console.log('No occurrences found.');
Expand Down
14 changes: 7 additions & 7 deletions container-analysis/snippets/pollDiscoveryOccurrenceFinished.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ async function main(
// const retries = 5 // The number of retries to listen for the new Pub/Sub messages

// Import the library and create a client
const containerAnalysis = require('@google-cloud/containeranalysis');
const client = new containerAnalysis.v1beta1.GrafeasV1Beta1Client();
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

const formattedParent = client.projectPath(projectId);
const formattedParent = client.getGrafeasClient().projectPath(projectId);

let filter = `resourceUrl="${imageUrl}" AND noteProjectId="goog-analysis" AND noteId="PACKAGE_VULNERABILITY"`;
// [END containeranalysis_poll_discovery_occurrence_finished]
Expand All @@ -33,7 +33,7 @@ async function main(
const pRetry = require('p-retry');
const discoveryOccurrence = await pRetry(
async () => {
const [occurrences] = await client.listOccurrences({
const [occurrences] = await client.getGrafeasClient().listOccurrences({
parent: formattedParent,
filter: filter,
});
Expand All @@ -51,10 +51,10 @@ async function main(
const finishedOccurrence = await pRetry(
async () => {
let status = 'PENDING';
const [updated] = await client.getOccurrence({
const [updated] = await client.getGrafeasClient().getOccurrence({
name: discoveryOccurrence.name,
});
status = updated.discovered.discovered.analysisStatus;
status = updated.discovery.analysisStatus;
if (
status !== 'FINISHED_SUCCESS' &&
status !== 'FINISHED_FAILED' &&
Expand All @@ -69,7 +69,7 @@ async function main(
}
);
console.log(
`Found discovery occurrence ${finishedOccurrence.name}. Status: ${finishedOccurrence.discovered.discovered.analysisStatus}`
`Found discovery occurrence ${finishedOccurrence.name}. Status: ${finishedOccurrence.discovery.analysisStatus}`
);
// [END containeranalysis_poll_discovery_occurrence_finished]
}
Expand Down
Loading

0 comments on commit 7ac60e4

Please sign in to comment.