diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b097c31 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,50 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +# Unix-style newlines at the bottom of every file +end_of_line = lf +charset = utf-8 + +# indentation style and size +indent_style = space +indent_size = 2 + +# Make sure every file has a blank line at the end +insert_final_newline = true + +# Remove any whitespace characters preceding newline characters +trim_trailing_whitespace = true + +# Give operators breathing room, but not brackets +spaces_around_operators = true +spaces_around_brackets = false + +# 4 space indentation +[*.{py,java}] +indent_size = 4 + +[*.json] +indent_size = 4 + +# 2 space indentation +[*.{js,html}] +indent_size = 2 + +[*.{tf}] +indent_size = 2 + +[*.rb] +indent_size = 2 + +[*.yml] +indent_size = 2 + +[*.yaml] +indent_size = 2 + +[*.{md}] +indent_size = unset +trim_trailing_whitespace = false diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..5392490 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,5 @@ +# This is a comment. Each line is a file pattern followed by one or more owners. + +# These owners will be the default owners for everything in the repo. @oneanupam will be requested for review when someone opens a pull request. + +- @oneanupam diff --git a/.github/conventional-commit-lint.yaml b/.github/conventional-commit-lint.yaml new file mode 100644 index 0000000..ad67fc6 --- /dev/null +++ b/.github/conventional-commit-lint.yaml @@ -0,0 +1,5 @@ +# Config for GitHub App that ensures that commit messages and pull requests are based on conventionalcommits.org +# https://github.com/googleapis/repo-automation-bots/tree/main/packages/conventional-commit-lint + +enabled: true +always_check_pr_title: true \ No newline at end of file diff --git a/.github/pull-request_template.md b/.github/pull-request_template.md new file mode 100644 index 0000000..4bdd312 --- /dev/null +++ b/.github/pull-request_template.md @@ -0,0 +1,15 @@ +# Description +Please include a short summary of the update made along with the context. + +## Checklist +- [ ] Self-review of the code is performed. +- [ ] Code has been fully tested and completely functional. + +## Type of Change +- [ ] break: Resolved a breaking change. This will increment the major version. +- [ ] feat: A new feature or enhancement added to the codebase. This will increment the minor version. +- [ ] fix: A bug fix or correction to resolve an issue. This will increment the patch version. +- [ ] chore: Other changes not directly affecting the code (e.g., documentation update). No version increment. + +## Reference +https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/creating-a-pull-request-template-for-your-repository \ No newline at end of file diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml new file mode 100644 index 0000000..6ee7457 --- /dev/null +++ b/.github/workflows/pull-request.yml @@ -0,0 +1,40 @@ +name: Pull Request Checks + +on: + pull_request: + branches: + - master + types: + - opened + - synchronize + - reopened + +jobs: + pull-request-check: + runs-on: ubuntu-latest + steps: + # Checkout the repository code + - name: Code checkout + id: code_checkout + uses: actions/checkout@v4 + + # Check PR title prefix to ensure it follows the convention + - name: Check PR title prefix + run: | + echo "PR Title: '${{ github.event.pull_request.title }}'" + + if [[ ! "${{ github.event.pull_request.title }}" =~ ^(ci|feat|fix|chore|docs|refactor): ]]; then + echo "❌ PR title must start with one of: ci:, feat:, fix:, chore:, docs:, refactor:" + exit 1 + else + echo "✅ PR title is valid." + fi + + # Scan the repo for any sensitive information like secrets etc + - name: Secret Scanning + uses: trufflesecurity/trufflehog@main + with: + path: ./ # Code repository path + base: "" # Start scanning from here + head: ${{ github.head_ref || github.ref_name }} # Scan commits until here + extra_args: --only-verified diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..e3e1d37 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: no-commit-to-branch + args: [--branch, main, --branch, master] + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - id: detect-private-key + - repo: https://github.com/gitleaks/gitleaks + rev: v8.18.4 + hooks: + - id: gitleaks diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..ae7c2f6 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "EditorConfig.EditorConfig" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7b6db2d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.insertFinalNewline": true, + "files.trimTrailingWhitespace": true, + "files.trimFinalNewlines": true +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..441594c --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,59 @@ +# Contribution + +This document provides guidelines for contributing to the project. + +## Pull Request + +Pull requests are the best way to propose changes to the codebase (we use ["fork-and-pull" Git workflow](https://github.com/susam/gitpr)). We actively welcome your pull requests: + +1. Fork the repository to your own Github account. +2. Clone the project to your machine. +3. Create a branch locally with a succinct but descriptive name. +4. Commit changes to the branch following any formatting and testing guidelines specific to this repo. +5. Push changes to your forked repository. +6. Open a Pull Request in our repository. + +## Guidelines + +### Commit Message Guidelines + +Use the combination of "Commit Type" and "Commit Summary" with an optional "Commit description". + +- Commit Type: Use the proper commit type for the changes as per [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/) types. +- Commit Summary: Always use the imperative present tense (Write your commit messages as if you're giving a command or describing what the commit does, not what you did). Don’t capitalize the first letter of the commit message. Don’t use a period at the end of your text. + +``` +Ex: Suppose, You updated a file. So, the commit message could be - + docs: update readme file + feat: add application dockerfile +``` + +### PR Guidelines + +Format: [Commit Type] Short Summary + +``` +Ex: Suppose, You added some functionality. So, the title could be - + [feat] added function to read input from user +``` + +### Coding Guidelines + +- Try to put comments in your code, where required. +- Try to follow DRY (Don't Repeat Yourself) principle. +- Follow the style guide to write terraform code recommended by terraform. + +## Report Bugs + +We use GitHub issues to track bugs. Report a bug by opening a new issue. + +## Linting and Formatting + +All of the bash scripts in the repository must be linted or formatted using `shellcheck` to maintain a standard of quality. + +- On the web, Paste a shell script on https://www.shellcheck.net for instant feedback. ShellCheck.net is always synchronized to the latest git commit, and is the easiest way to give ShellCheck a go. +- From your terminal, Run shellcheck yourscript in your terminal for instant output, + +## License + +By contributing, you agree that your contributions will be licensed under its [MIT License](LICENSE). diff --git a/README.md b/README.md index c112bca..0c4104a 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,25 @@ # python-gcp-samples + This repository contains python samples intended to use in day to day operational activities on google cloud platform. Usecases for which the python scripts are written can be found [here](./usecases.md). ## Prerequisites + Below prerequisites must be fulfilled for the successful execution of code. ### Software Requirement + Resources in this repository are meant for use with Python 3.x (check the version using `python3 --version`) and pip3 (check the version using `pip3 --version`). If you don't have the compatible version, download it from official python repository. -- [python3](https://www.python.org/downloads/) >= 3.9.2 +- [python3](https://www.python.org/downloads/) >= 3.13.2 - [pip3](https://pypi.org/project/pip/) >= 20.3.4 ### Bootstrap Virtual Environment + [venv](https://docs.python.org/3/library/venv.html) is a tool that creates isolated Python environments. These isolated environments can have separate versions of Python packages, which allows you to isolate one project's dependencies from the dependencies of other projects. **Linux** + ``` cd your-project python3 -m venv env @@ -28,35 +33,41 @@ Resources in this repository are meant for use with Python 3.x (check the versio **Note:** Follow the [google article](https://cloud.google.com/python/docs/setup) to setup your Python development environment. ## Quick Start + If you want to quickly run and test Python samples without installing python, the recommended approach is to use Cloud Shell. Cloud Shell is a Compute Engine virtual machine. The service credentials associated with this virtual machine are automatic, so there is no need to set up or download a service account key. Cloud shell terminal is preloaded with softwares and utilities such as Python, gcloud command-line tool, kubectl, and more. letting you get started with less setup. + - **Step-01:** Activate Cloud Shell at the top of the Google Cloud Console. - **Step-02:** Clone this repository: `git clone https://github.com/anupam-sy/python-gcp-samples.git` - **Step-03:** Setup the python virtual environment using [Bootstrap Virtual Environment](#bootstrap-virtual-environment). ### Authentication and Authorization + This client library used in the python script supports authentication via Google Application Default Credentials, or by providing a JSON key file for a Service Account. Google Application Default Credentials (ADC) is the recommended way to authorize and authenticate clients. ## Accessing Cloud APIs + You can access Cloud APIs using client libraries available for many popular programming languages While you can use Google Cloud APIs directly by making raw requests to the server, client libraries provide simplifications that significantly reduce the amount of code you need to write. -1. Cloud Client Libraries are the recommended option for accessing Cloud APIs programmatically, where available. Cloud Client Libraries use the latest client library model. -[NEW - Recommended Way] https://github.com/googleapis/google-cloud-python +1. Cloud Client Libraries are the recommended option for accessing Cloud APIs programmatically, where available. Cloud Client Libraries use the latest client library model. + [NEW - Recommended Way] https://github.com/googleapis/google-cloud-python -2. A few Google Cloud APIs don't have Cloud Client Libraries available in all languages. If you want to use one of these APIs and there is no Cloud Client Library for your preferred language, you can still use the previous style of client library, called Google API Client Libraries. -[OLD - Not Recommended] https://github.com/googleapis/google-api-python-client +2. A few Google Cloud APIs don't have Cloud Client Libraries available in all languages. If you want to use one of these APIs and there is no Cloud Client Library for your preferred language, you can still use the previous style of client library, called Google API Client Libraries. + [OLD - Not Recommended] https://github.com/googleapis/google-api-python-client **Note:** It is recommended to use Cloud Client Libraries for Python, where possible, for new code development due to the following reasons: With Cloud Client Libraries for Python: + - There is a separate client library for each API, so you can choose which client libraries to download. Whereas, google-api-python-client is a single client library for all APIs. As a result, the total package size for google-api-python-client exceeds 50MB. - There are stricter controls for breaking changes to the underlying APIs as each client library is focused on a specific API. - There are more features in these Cloud Client Libraries as each library is focused on a specific API, and in some cases, the libraries are owned by team who specialized in that API. ## References + - https://cloud.google.com/python/docs/setup - https://cloud.google.com/apis/docs/overview - https://cloud.google.com/apis/docs/client-libraries-explained @@ -70,7 +81,9 @@ With Cloud Client Libraries for Python: - https://github.com/GoogleCloudPlatform/python-docs-samples ## License + This repository is under MIT License. ## Providing feedback -Open an issue in this GitHub repository. \ No newline at end of file + +Open an issue in this GitHub repository. diff --git a/python-samples/gcp-key-management/.gitkeep b/docs/index.md similarity index 100% rename from python-samples/gcp-key-management/.gitkeep rename to docs/index.md diff --git a/usecases.md b/docs/usecases.md similarity index 100% rename from usecases.md rename to docs/usecases.md diff --git a/python-samples/gcp-compute-engine/get_compute_engine_list.py b/python/gcp-compute/get_compute_engine_list.py similarity index 100% rename from python-samples/gcp-compute-engine/get_compute_engine_list.py rename to python/gcp-compute/get_compute_engine_list.py diff --git a/python-samples/gcp-compute-engine/get_gce_metadata.py b/python/gcp-compute/get_gce_metadata.py similarity index 100% rename from python-samples/gcp-compute-engine/get_gce_metadata.py rename to python/gcp-compute/get_gce_metadata.py diff --git a/python-samples/gcp-compute-engine/get_gce_properties.py b/python/gcp-compute/get_gce_properties.py similarity index 100% rename from python-samples/gcp-compute-engine/get_gce_properties.py rename to python/gcp-compute/get_gce_properties.py diff --git a/python-samples/gcp-compute-engine/get_instances_with_eips.py b/python/gcp-compute/get_instances_with_eips.py similarity index 100% rename from python-samples/gcp-compute-engine/get_instances_with_eips.py rename to python/gcp-compute/get_instances_with_eips.py diff --git a/python-samples/gcp-compute-engine/get_unused_compute_disks.py b/python/gcp-compute/get_unused_compute_disks.py similarity index 100% rename from python-samples/gcp-compute-engine/get_unused_compute_disks.py rename to python/gcp-compute/get_unused_compute_disks.py diff --git a/python-samples/gcp-compute-engine/get_unused_external_ips_1.py b/python/gcp-compute/get_unused_external_ips_1.py similarity index 100% rename from python-samples/gcp-compute-engine/get_unused_external_ips_1.py rename to python/gcp-compute/get_unused_external_ips_1.py diff --git a/python-samples/gcp-compute-engine/get_unused_external_ips_2.py b/python/gcp-compute/get_unused_external_ips_2.py similarity index 100% rename from python-samples/gcp-compute-engine/get_unused_external_ips_2.py rename to python/gcp-compute/get_unused_external_ips_2.py diff --git a/python-samples/gcp-compute-engine/requirements.txt b/python/gcp-compute/requirements.txt similarity index 100% rename from python-samples/gcp-compute-engine/requirements.txt rename to python/gcp-compute/requirements.txt diff --git a/python-samples/gcp-compute-engine/stop_compute_instance.py b/python/gcp-compute/stop_compute_instance.py similarity index 100% rename from python-samples/gcp-compute-engine/stop_compute_instance.py rename to python/gcp-compute/stop_compute_instance.py diff --git a/python-samples/gcp-resource-manager/get_all_projects.py b/python/gcp-resource-manager/get_all_projects.py similarity index 100% rename from python-samples/gcp-resource-manager/get_all_projects.py rename to python/gcp-resource-manager/get_all_projects.py diff --git a/python-samples/gcp-resource-manager/requirements.txt b/python/gcp-resource-manager/requirements.txt similarity index 100% rename from python-samples/gcp-resource-manager/requirements.txt rename to python/gcp-resource-manager/requirements.txt diff --git a/python-samples/gcp-key-management/get_key_details.py b/python/gcp-security/get_key_details.py similarity index 100% rename from python-samples/gcp-key-management/get_key_details.py rename to python/gcp-security/get_key_details.py diff --git a/python-samples/gcp-key-management/requirements.txt b/python/gcp-security/requirements.txt similarity index 100% rename from python-samples/gcp-key-management/requirements.txt rename to python/gcp-security/requirements.txt