Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

feat: adds getGrafeasClient() method on ContainerAnalysisClient instance #46

Merged
merged 6 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,48 @@ npm install @google-cloud/containeranalysis
### Using the client library

```javascript
async function quickstart() {
// Imports the @google-cloud/containeranalysis client library
const client = require('@google-cloud/containeranalysis');
console.log(client);
}
quickstart();
/**
* TODO(developer): Uncomment these variables before running the sample
*/
// const projectId = 'your-project-id', // Your GCP Project ID
// const noteId = 'my-note-id' // Id of the note

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();
// Fetch an instance of a Grafeas client:
// see: https://googleapis.dev/nodejs/grafeas/latest
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);

// Creates and returns a new Note
const [note] = await grafeasClient.createNote({
parent: formattedParent,
noteId: noteId,
note: {
vulnerability: {
details: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
minAffectedVersion: {
kind: 'MINIMUM',
},
fixedVersion: {
kind: 'MAXIMUM',
},
},
],
},
},
});

console.log(`Note ${note.name} created.`);

```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"Container Analysis API"
],
"dependencies": {
"@google-cloud/grafeas": "^2.0.0",
"google-gax": "^1.0.0"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,16 @@ __Usage:__

### Quickstart

fetching an instance of Grafeas and creating a note.

View the [source code](https://github.com/googleapis/nodejs-containeranalysis/blob/master/samples/quickstart.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-containeranalysis&page=editor&open_in_editor=samples/quickstart.js,samples/README.md)

__Usage:__


`node quickstart.js`
`node quickstart.js "project-id" "note-id"`


-----
Expand Down
24 changes: 19 additions & 5 deletions samples/createNote.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,35 @@ 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();
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 = client.projectPath(projectId);
const formattedParent = grafeasClient.projectPath(projectId);

// Creates and returns a new Note
const [note] = await client.createNote({
const [note] = await grafeasClient.createNote({
parent: formattedParent,
noteId: noteId,
note: {
vulnerability: {},
vulnerability: {
details: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
minAffectedVersion: {
kind: 'MINIMUM',
},
fixedVersion: {
kind: 'MAXIMUM',
},
},
],
},
},
});

Expand Down
73 changes: 52 additions & 21 deletions samples/quickstart.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// sample-metadata:
// title: Quickstart
// description: fetching an instance of Grafeas and creating a note.
// usage: node quickstart.js "project-id" "note-id"
async function main(
projectId = 'your-project-id', // Your GCP Project ID
noteId = 'my-note-id' // Id of the note
) {
// [START containeranalysis_quickstart]
/**
* TODO(developer): Uncomment these variables before running the sample
*/
// const projectId = 'your-project-id', // Your GCP Project ID
// const noteId = 'my-note-id' // Id of the note

'use strict';
// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();
// Fetch an instance of a Grafeas client:
// see: https://googleapis.dev/nodejs/grafeas/latest
const grafeasClient = client.getGrafeasClient();

// [START containeranalysis_quickstart]
async function quickstart() {
// Imports the @google-cloud/containeranalysis client library
const client = require('@google-cloud/containeranalysis');
console.log(client);
// 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);

// Creates and returns a new Note
const [note] = await grafeasClient.createNote({
parent: formattedParent,
noteId: noteId,
note: {
vulnerability: {
details: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
minAffectedVersion: {
kind: 'MINIMUM',
},
fixedVersion: {
kind: 'MAXIMUM',
},
},
],
},
},
});

console.log(`Note ${note.name} created.`);
// [END containeranalysis_quickstart]
}
quickstart();
// [END containeranalysis_quickstart]

main(...process.argv.slice(2));
27 changes: 0 additions & 27 deletions samples/test/tests.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/v1/container_analysis_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const gapicConfig = require('./container_analysis_client_config.json');
const gax = require('google-gax');
const path = require('path');
const {GrafeasClient} = require('@google-cloud/grafeas');

const VERSION = require('../../package.json').version;

Expand Down Expand Up @@ -155,10 +156,14 @@ class ContainerAnalysisClient {
throw err;
}
),

defaults[methodName],
null
);
}
// expose the fully hydrated options, for the benefit of
// the client.getGrafeas() method.
this.opts = opts;
}

/**
Expand Down Expand Up @@ -447,6 +452,19 @@ class ContainerAnalysisClient {
matchNoteFromNoteName(noteName) {
return this._pathTemplates.notePathTemplate.match(noteName).note;
}

/**
* Returns an instance of a @google-cloud/grafeas client, configured to
* connect to Google Cloud's Container Analysis API. For documentation
* on this client, see:
* <a href="https://googleapis.dev/nodejs/grafeas/latest/index.html">https://googleapis.dev/nodejs/grafeas/latest/index.html</a>
*
* @returns {GrafeasClient} - An instance of a Grafeas client.
*
*/
getGrafeasClient() {
return new GrafeasClient(this.opts);
}
}

module.exports = ContainerAnalysisClient;
6 changes: 3 additions & 3 deletions synth.metadata
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"updateTime": "2019-06-19T11:09:27.543558Z",
"updateTime": "2019-06-19T22:29:25.698001Z",
"sources": [
{
"generator": {
Expand All @@ -12,8 +12,8 @@
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "ac13167e31a20314aa05cc9911c95df250880485",
"internalRef": "253867808"
"sha": "45e125f9e30dc5d45b52752b3ab78dd4f6084f2d",
"internalRef": "254026509"
}
},
{
Expand Down
54 changes: 51 additions & 3 deletions synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,61 @@
templates = common_templates.node_library()
s.copy(templates)

# fix the URL of grafeas.io (this is already fixed upstream).
s.replace('src/v1beta1/*.js',
'cloud.google.comgrafeas.io',
'grafeas.io')

s.replace('src/v1/*.js',
'cloud.google.comgrafeas.io',
'grafeas.io')
# perform surgery inserting the Grafeas client.
s.replace("src/v1/container_analysis_client.js",
r"""const path = require\('path'\);
""",
r"""const path = require('path');
const { GrafeasClient } = require('@google-cloud/grafeas');
""")

s.replace("src/v1/container_analysis_client.js",
r""" defaults\[methodName\],
null
\);
}
}""",
r"""
defaults[methodName],
null
);
}
// expose the fully hydrated options, for the benefit of
// the client.getGrafeas() method.
this.opts = opts;
}
""")

s.replace("src/v1/container_analysis_client.js",
r""" matchNoteFromNoteName\(noteName\) {
return this\._pathTemplates\.notePathTemplate
\.match\(noteName\)
\.note;
}
""",
r""" matchNoteFromNoteName(noteName) {
return this._pathTemplates.notePathTemplate.match(noteName).note;
}


/**
* Returns an instance of a @google-cloud/grafeas client, configured to
* connect to Google Cloud's Container Analysis API. For documentation
* on this client, see:
* <a href="https://googleapis.dev/nodejs/grafeas/latest/index.html">https://googleapis.dev/nodejs/grafeas/latest/index.html</a>
*
* @returns {GrafeasClient} - An instance of a Grafeas client.
*
*/
getGrafeasClient() {
return new GrafeasClient(this.opts);
}
""")

# Node.js specific cleanup
subprocess.run(['npm', 'install'])
Expand Down