Skip to content

Commit

Permalink
chore(samples): Snippet Generating System (part 1) (#199)
Browse files Browse the repository at this point in the history
* chore(samples): Snippet Generating System

Preparing the SGS (Snippet Generating System) script
to handle the burden of duplicated code in the samples.

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2022
1 parent 3c1c56e commit 7446464
Show file tree
Hide file tree
Showing 14 changed files with 598 additions and 21 deletions.
53 changes: 53 additions & 0 deletions packages/google-cloud-compute/samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Code samples for the Compute Engine library

In this folder you can find the source code for the code samples used throughout the
[public documentation](https://cloud.google.com/compute/docs/) of Google Compute Engine.

The samples can be found in the `snippets` folder, where they are organized to mimic the
structure of the public documentation. Files that are saved there are generated by the `sgs.py`
script from pieces found in `ingredients` and `recipes`. This way, one piece of code can be easily
included in multiple snippets and updating the code requires less work.

## Working with the SGS

SGS (Snippet Generating System) works by scanning the `recipes` folder, finding all files
and filling them with pieces of code found in `ingredients`. The folder structure of `recipes` is
reconstructed in the `snippets` folder.

### Adding new sample

To create a new sample, just prepare a new file in one of the `recipes` subfolders. The SGS will pick it up
automatically when you run it, by executing `python3 sgs.py generate` in this (`samples/`) directory.

### Removing/moving a sample

To remove or move a sample, you need to simply modify the `recipes` folder to match your desired structure, then delete
the generated snippet from the `snippets` directory. The SGS script will create the snippet in the new location next
time you run `python3 sgs.py generate`.

### Interacting with GIT

SGS will not interact with Git repository in any way. All changes made by the script need to be committed manually -
preferably in the same commit as the update to the source files.

## Preparing an ingredient
To add a new ingredient, create a new `.py` file with the code you want to later use in the snippets. Mark the beginning
of the code you want to include with `# <INGREDIENT name>` and the end with `# </INGREDIENT>`.

Please leave the imports required by this ingredient **OUTSIDE** the area marked with ingredient comments. The SGS
script will automatically collect all the required imports and put them in the final snippet in the right place and in
right order.

## Preparing a recipe
Each recipe is a file located in the `recipes` folder. It should have the `.py` extension and should be a valid Python
script. Each recipe has to have an `# <IMPORTS/>` line and at least one `# <INGREDIENT name/>` line.
Apart from those restrictions, the contents of the file can be whatever you want.

The SGS will copy the recipe file to the destination folder in `snippets` and replace the `# <IMPORTS/>` and
`# <INGREDIENT name/>` lines with the `import` statements required by the used ingredients and with the ingredient
body.

### Regions
You should use `# <REGION region_name>` and `# </REGION region_name>` lines to indicate where start and end
of a region should be placed in the generated snippet. Those lines will be simply replaced with the proper
`START region_name` and `END region_name` lines.
18 changes: 18 additions & 0 deletions packages/google-cloud-compute/samples/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2022 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
#
# http://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.

TEST_CONFIG_OVERRIDE = {
# Tests in test_sample_default_values.py require separate projects to not interfere with each other.
"gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT",
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest==7.0.1
flaky==3.7.0
google-cloud-storage==2.1.0; python_version == '3.6'
google-cloud-storage==2.1.0; python_version >= '3.7'
google-cloud-storage==2.1.0; python_version >= '3.7'
3 changes: 3 additions & 0 deletions packages/google-cloud-compute/samples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
isort==5.10.1
black==22.1.0
google-cloud-compute==1.0.0
Loading

0 comments on commit 7446464

Please sign in to comment.