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

Commit

Permalink
feat: adds getGrafeasClient() method on ContainerAnalysisClient insta…
Browse files Browse the repository at this point in the history
…nce (#46)
  • Loading branch information
bcoe authored Jun 19, 2019
1 parent 213cb47 commit 0f03c96
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 72 deletions.
4 changes: 2 additions & 2 deletions .repo-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"product_documentation": "https://cloud.google.com/container-registry/docs/container-analysis",
"client_documentation": "https://googleapis.dev/nodejs/containeranalysis/latest",
"issue_tracker": "",
"release_level": "alpha",
"release_level": "ga",
"language": "nodejs",
"repo": "googleapis/nodejs-containeranalysis",
"distribution_name": "@google-cloud/containeranalysis",
"api_id": "containeranalysis.googleapis.com",
"requires_billing": true
}
}
58 changes: 48 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# [Google Container Analysis: Node.js Client](https://github.com/googleapis/nodejs-containeranalysis)

[![release level](https://img.shields.io/badge/release%20level-alpha-orange.svg?style=flat)](https://cloud.google.com/terms/launch-stages)
[![release level](https://img.shields.io/badge/release%20level-general%20availability%20%28GA%29-brightgreen.svg?style=flat)](https://cloud.google.com/terms/launch-stages)
[![npm version](https://img.shields.io/npm/v/@google-cloud/containeranalysis.svg)](https://www.npmjs.org/package/@google-cloud/containeranalysis)
[![codecov](https://img.shields.io/codecov/c/github/googleapis/nodejs-containeranalysis/master.svg?style=flat)](https://codecov.io/gh/googleapis/nodejs-containeranalysis)

Expand Down 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 Expand Up @@ -98,11 +134,13 @@ also contains samples.
This library follows [Semantic Versioning](http://semver.org/).


This library is considered to be **General Availability (GA)**. This means it
is stable; the code surface will not change in backwards-incompatible ways
unless absolutely necessary (e.g. because of critical security issues) or with
an extensive deprecation period. Issues and requests against **GA** libraries
are addressed with the highest priority.


This library is considered to be in **alpha**. This means it is still a
work-in-progress and under active development. Any release is subject to
backwards-incompatible changes at any time.



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:36:26.600770Z",
"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

0 comments on commit 0f03c96

Please sign in to comment.