diff --git a/.github/workflows/orgpolicy.yaml b/.github/workflows/orgpolicy.yaml new file mode 100644 index 0000000000..857286b7fb --- /dev/null +++ b/.github/workflows/orgpolicy.yaml @@ -0,0 +1,67 @@ +name: orgpolicy +on: + push: + branches: + - main + paths: + - "orgpolicy/**" + pull_request: + paths: + - "orgpolicy/**" + pull_request_target: + types: [labeled] + schedule: + - cron: "0 0 * * 0" +jobs: + test: + if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }} + runs-on: ubuntu-latest + timeout-minutes: 60 + permissions: + contents: "write" + pull-requests: "write" + id-token: "write" + steps: + - uses: actions/checkout@v3.1.0 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + - uses: "google-github-actions/auth@v0.8.3" + with: + workload_identity_provider: "projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider" + service_account: "kokoro-system-test@long-door-651.iam.gserviceaccount.com" + create_credentials_file: "true" + access_token_lifetime: 600s + - uses: actions/setup-node@v3.5.1 + with: + node-version: 16 + - run: npm install + working-directory: orgpolicy + - run: npm test + working-directory: orgpolicy + env: + MOCHA_REPORTER_SUITENAME: org_policy + MOCHA_REPORTER_OUTPUT: org_policy_sponge_log.xml + MOCHA_REPORTER: xunit + - if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + try { + await github.rest.issues.removeLabel({ + name: 'actions:force-run', + owner: 'GoogleCloudPlatform', + repo: 'nodejs-docs-samples', + issue_number: context.payload.pull_request.number + }); + } catch (e) { + if (!e.message.includes('Label does not exist')) { + throw e; + } + } + - if: ${{ github.event_name == 'schedule'}} + run: | + curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L + chmod +x ./flakybot + ./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/.github/workflows/workflows.json b/.github/workflows/workflows.json index 5fc1908212..e3acffc447 100644 --- a/.github/workflows/workflows.json +++ b/.github/workflows/workflows.json @@ -43,6 +43,7 @@ "mediatranslation", "monitoring/opencensus", "monitoring/prometheus", + "orgpolicy", "datacatalog/cloud-client", "datacatalog/quickstart", "datastore/functions" diff --git a/CODEOWNERS b/CODEOWNERS index fba3bb4a41..40e74a6822 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -17,6 +17,9 @@ cloud-tasks @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/nodejs-samples-r workflows @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/nodejs-samples-reviewers eventarc @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/nodejs-samples-reviewers +# Infrastructure +orgpolicy @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/nodejs-samples-reviewers + # Other functions samples functions/scheduleinstance @askmeegs @GoogleCloudPlatform/nodejs-samples-reviewers functions/speech-to-speech @ricalo @GoogleCloudPlatform/nodejs-samples-reviewers diff --git a/orgpolicy/README.md b/orgpolicy/README.md new file mode 100644 index 0000000000..efe1026b1a --- /dev/null +++ b/orgpolicy/README.md @@ -0,0 +1,48 @@ +Google Cloud Platform logo + +# [Organization Policy: Node.js Samples](https://github.com/GoogleCloudPlatform/nodejs-docs-samples) + +[![Open in Cloud Shell][shell_img]][shell_link] + + + +## Table of Contents + +- [Organization Policy: Node.js Samples](#organization-policy-nodejs-samples) + - [Table of Contents](#table-of-contents) + - [Before you begin](#before-you-begin) + - [Samples](#samples) + - [Quickstart](#quickstart) + +## Before you begin + +Before running the samples, make sure you've followed the steps outlined in +[Using the client library](https://github.com/googleapis/nodejs-org-policy#using-the-client-library). + +`npm install` + +`cd ..` + +## Samples + + + +### Quickstart + +View the [source code](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/main/orgpolicy/quickstart.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/nodejs-docs-samples&page=editor&open_in_editor=orgpolicy/quickstart.js,orgpolicy/README.md) + +__Usage:__ + + +`node quickstart.js` + + + + + + +[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png +[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/nodejs-docs-samples&page=editor&open_in_editor=orgpolicy/README.md +[product-docs]: https://cloud.google.com/resource-manager/docs/organization-policy/overview diff --git a/orgpolicy/package.json b/orgpolicy/package.json new file mode 100644 index 0000000000..f8f4ae39d0 --- /dev/null +++ b/orgpolicy/package.json @@ -0,0 +1,23 @@ +{ + "name": "nodejs-org-policy", + "private": true, + "license": "Apache-2.0", + "author": "Google LLC", + "engines": { + "node": ">=12.0.0" + }, + "files": [ + "*.js" + ], + "scripts": { + "test": "c8 mocha --timeout 600000 test/*.js" + }, + "dependencies": { + "@google-cloud/org-policy": "^2.0.2" + }, + "devDependencies": { + "c8": "^7.1.0", + "chai": "^4.2.0", + "mocha": "^8.0.0" + } +} diff --git a/orgpolicy/quickstart.js b/orgpolicy/quickstart.js new file mode 100644 index 0000000000..501956a7f0 --- /dev/null +++ b/orgpolicy/quickstart.js @@ -0,0 +1,49 @@ +// 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. +// + +'use strict'; + +async function main(projectId) { + // [START nodejs_org_policy_quickstart] + // Imports the Google Cloud client library + + // remove this line after package is released + // eslint-disable-next-line node/no-missing-require + const {OrgPolicyClient} = require('@google-cloud/org-policy'); + + // TODO(developer): replace with your prefered project ID. + // const projectId = 'my-project' + + // Creates a client + // eslint-disable-next-line no-unused-vars + const client = new OrgPolicyClient(); + + //TODO(library generator): write the actual function you will be testing + async function listConstraints() { + const constraints = await client.listConstraints({ + parent: `projects/${projectId}`, + }); + console.info(constraints); + } + listConstraints(); + // [END nodejs_org_policy_quickstart] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/orgpolicy/test/quickstart.js b/orgpolicy/test/quickstart.js new file mode 100644 index 0000000000..010f0af88b --- /dev/null +++ b/orgpolicy/test/quickstart.js @@ -0,0 +1,43 @@ +// +// 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. +// +// ** This file is automatically generated by gapic-generator-typescript. ** +// ** https://github.com/googleapis/gapic-generator-typescript ** +// ** All changes to this file may be overwritten. ** + +'use strict'; + +const path = require('path'); +const cp = require('child_process'); +const {before, describe, it} = require('mocha'); +const {OrgPolicyClient} = require('@google-cloud/org-policy'); +const {assert} = require('chai'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const cwd = path.join(__dirname, '..'); + +const client = new OrgPolicyClient(); + +describe('Quickstart', () => { + let projectId; + + before(async () => { + projectId = await client.getProjectId(); + }); + + it('should run quickstart', async () => { + const stdout = execSync(`node ./quickstart.js ${projectId}`, {cwd}); + assert.match(stdout, /\[[\s\S]*\]/); + }); +});