[
  {
    "path": ".github/workflows/deploy.yml",
    "content": "name: Pull Request CD\n\non:\n   push:\n     branches:\n       - main \n\njobs:\n  terraform:\n    name: terraform\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v4\n    - uses: hashicorp/setup-terraform@v3\n    - name: Terraform\n      id: credentials\n      run: |\n        # Get GCP credentials\n        echo \"${KEY}\" | base64 -d > ~/key.json\n        export GOOGLE_APPLICATION_CREDENTIALS=~/key.json\n        cd terraform\n        terraform init && terraform validate && terraform apply -auto-approve\n      env:\n        KEY: ${{secrets.SERVICE_ACCOUNT}}\n        GOOGLE_APPLICATION_CREDENTIALS: ~/key.json"
  },
  {
    "path": ".github/workflows/pull-request.yml",
    "content": "name: Pull Request CI\n\non: [pull_request, workflow_dispatch]\n\njobs:\n  tests:\n    runs-on: ubuntu-latest\n\n    steps:\n      - uses: actions/checkout@v4\n      - name: Set up Python 3.11\n        uses: actions/setup-python@v3\n      - name: Install dependencies\n        id: dependencies\n        run: |\n          make dev\n      - name: Unit Test with pytest\n        id: tests\n        run: |\n          make test\n      - name: Linting\n        id: lint\n        run: |\n          make pylint\n  tflint:\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v4\n      name: Checkout source code\n\n    - uses: actions/cache@v4\n      name: Cache plugin dir\n      with:\n        path: ~/.tflint.d/plugins\n        key: tflint-${{ hashFiles('.tflint.hcl') }}\n\n    - uses: terraform-linters/setup-tflint@v4\n      name: Setup TFLint\n      with:\n        tflint_version: v0.50.3\n\n    - name: Show version\n      run: tflint --version\n\n    - name: Init TFLint\n      run: tflint --init\n      env:\n        # https://github.com/terraform-linters/tflint/blob/master/docs/user-guide/plugins.md#avoiding-rate-limiting\n        GITHUB_TOKEN: ${{ github.token }}\n    - name: Run TFLint\n      run: tflint --chdir=terraform"
  },
  {
    "path": ".github/workflows/release.yml",
    "content": "name: Release to Prod\n\non:\n  release:\n    types:\n      - \"released\"\n\njobs:\n  terraform:\n    name: terraform\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v4\n    - uses: hashicorp/setup-terraform@v3\n    - name: Terraform\n      id: credentials\n      run: |\n        # Get GCP credentials\n        echo \"${KEY}\" | base64 -d > ~/key.json\n        export GOOGLE_APPLICATION_CREDENTIALS=~/key.json\n        cd terraform\n        terraform init && terraform validate \n        terraform workspace new prod || terraform workspace select prod\n        terraform apply -auto-approve\n      env:\n        KEY: ${{secrets.SERVICE_ACCOUNT}}\n        GOOGLE_APPLICATION_CREDENTIALS: ~/key.json"
  },
  {
    "path": ".gitignore",
    "content": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packaging\n.Python\nbuild/\ndevelop-eggs/\ndist/\ndownloads/\neggs/\n.eggs/\nlib/\nlib64/\nparts/\nsdist/\nvar/\nwheels/\nshare/python-wheels/\n*.egg-info/\n.installed.cfg\n*.egg\nMANIFEST\n\n# PyInstaller\n#  Usually these files are written by a python script from a template\n#  before PyInstaller builds the exe, so as to inject date/other infos into it.\n*.manifest\n*.spec\n\n# Installer logs\npip-log.txt\npip-delete-this-directory.txt\n\n# Unit test / coverage reports\nhtmlcov/\n.tox/\n.nox/\n.coverage\n.coverage.*\n.cache\nnosetests.xml\ncoverage.xml\n*.cover\n*.py,cover\n.hypothesis/\n.pytest_cache/\ncover/\n\n# Translations\n*.mo\n*.pot\n\n# Django stuff:\n*.log\nlocal_settings.py\ndb.sqlite3\ndb.sqlite3-journal\n\n# Flask stuff:\ninstance/\n.webassets-cache\n\n# Scrapy stuff:\n.scrapy\n\n# Sphinx documentation\ndocs/_build/\n\n# PyBuilder\n.pybuilder/\ntarget/\n\n# Jupyter Notebook\n.ipynb_checkpoints\n\n# IPython\nprofile_default/\nipython_config.py\n\n# pyenv\n#   For a library or package, you might want to ignore these files since the code is\n#   intended to run in multiple environments; otherwise, check them in:\n# .python-version\n\n# pipenv\n#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.\n#   However, in case of collaboration, if having platform-specific dependencies or dependencies\n#   having no cross-platform support, pipenv may install dependencies that don't work, or not\n#   install all needed dependencies.\n#Pipfile.lock\n\n# poetry\n#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.\n#   This is especially recommended for binary packages to ensure reproducibility, and is more\n#   commonly ignored for libraries.\n#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control\n#poetry.lock\n\n# pdm\n#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.\n#pdm.lock\n#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it\n#   in version control.\n#   https://pdm.fming.dev/#use-with-ide\n.pdm.toml\n\n# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm\n__pypackages__/\n\n# Celery stuff\ncelerybeat-schedule\ncelerybeat.pid\n\n# SageMath parsed files\n*.sage.py\n\n# Environments\n.env\n.venv\nenv/\nvenv/\nENV/\nenv.bak/\nvenv.bak/\n\n# Spyder project settings\n.spyderproject\n.spyproject\n\n# Rope project settings\n.ropeproject\n\n# mkdocs documentation\n/site\n\n# mypy\n.mypy_cache/\n.dmypy.json\ndmypy.json\n\n# Pyre type checker\n.pyre/\n\n# pytype static type analyzer\n.pytype/\n\n# Cython debug symbols\ncython_debug/\n\n# PyCharm\n#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can\n#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore\n#  and can be added to the global gitignore or merged into this file.  For a more nuclear\n#  option (not recommended) you can uncomment the following to ignore the entire idea folder.\n#.idea/\n\n.terraform/\nterraform/.files/"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2024 HexCoder Tech\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "Makefile",
    "content": "venv:\n\tpython3 -m venv .venv\n\techo \"Please run 'source .venv/bin/activate'\"\ndev:\n\tpip install -r src/api/requirements.txt\n\tpip install -r requirements-dev.txt\n\ntest:\n\tPYTHONPATH=src/ pytest tests/\n\npylint:\n\tflake8 src/ tests/\n\ntflint:\n\ttflint terraform/"
  },
  {
    "path": "README.md",
    "content": "# github-actions-demo\nThis repo shows the features of GitHub Actions\n\nThis sample project requests weather information from the Open Weather API and enables users to request it via HTTP requests.\n"
  },
  {
    "path": "requirements-dev.txt",
    "content": "pytest==8.1.1\nflake8==7.0.0"
  },
  {
    "path": "src/api/main.py",
    "content": "import functions_framework\nfrom flask import abort\n\nfrom weather import get_weather\n\n\n@functions_framework.http\ndef handle_request(request):\n    if request.method == \"GET\":\n        city = request.args.get(\"city\")\n        if not city:\n            return abort(404, \"Please provide a city.\")\n\n        success, response = get_weather(city)\n        if success:\n            return response\n        else:\n            return abort(500, response)\n    else:\n        return abort(403)\n"
  },
  {
    "path": "src/api/requirements.txt",
    "content": "requests==2.31.0"
  },
  {
    "path": "src/api/weather.py",
    "content": "import requests\n\n\napi_key = \"KEY\"\n\n\ndef format_weather(city: str, data: dict) -> str:\n    weather = data[\"weather\"][0][\"description\"]\n    temperature = data[\"main\"][\"temp\"]\n    return (\n        f\"The weather in {city} is {weather} \"\n        f\"with a temperature of {temperature} Celcius.\"\n    )\n\n\ndef get_lat_lon(city):\n    url = (\n        f\"http://api.openweathermap.org/geo/1.0/direct?\"\n        f\"q={city}&limit=1&appid={api_key}\"\n    )\n\n    print(f\"Retrieving latitude and longitude for {city}.\")\n    response = requests.get(url)\n    data = response.json()\n\n    if response.status_code == 200 and data:\n        lat = data[0][\"lat\"]\n        lon = data[0][\"lon\"]\n        return lat, lon\n    else:\n        print(\"Failed to retrieve latitude and longitude.\", data)\n        return None, None\n\n\ndef get_weather(city):\n    lat, lon = get_lat_lon(city)\n    if lat is None or lon is None:\n        return False, f\"Failed to retrieve weather information for {city}.\"\n\n    print(f\"Got Lat Lon for {city}: {lat}, {lon}\")\n    url = (\n        f\"https://api.openweathermap.org/data/2.5/weather?\"\n        f\"units=metric&lat={lat}&lon={lon}&appid={api_key}\"\n    )\n\n    response = requests.get(url)\n    data = response.json()\n\n    if response.status_code == 200:\n        print(f\"Retrieved weather information for {city}.\", data)\n        return True, format_weather(city, data)\n    else:\n        print(\"Failed to retrieve weather information.\", data)\n        return False, \"Failed to retrieve weather information.\"\n\n\nif __name__ == \"__main__\":\n    city = \"Munich\"\n    print(get_weather(city))\n"
  },
  {
    "path": "terraform/.terraform.lock.hcl",
    "content": "# This file is maintained automatically by \"terraform init\".\n# Manual edits may be lost in future updates.\n\nprovider \"registry.terraform.io/hashicorp/archive\" {\n  version = \"2.4.2\"\n  hashes = [\n    \"h1:1eOz9vM/55vnQjxk23RhnYga7PZq8n2rGxG+2Vx2s6w=\",\n    \"zh:08faed7c9f42d82bc3d406d0d9d4971e2d1c2d34eae268ad211b8aca57b7f758\",\n    \"zh:3564112ed2d097d7e0672378044a69b06642c326f6f1584d81c7cdd32ebf3a08\",\n    \"zh:53cd9afd223c15828c1916e68cb728d2be1cbccb9545568d6c2b122d0bac5102\",\n    \"zh:5ae4e41e3a1ce9d40b6458218a85bbde44f21723943982bca4a3b8bb7c103670\",\n    \"zh:5b65499218b315b96e95c5d3463ea6d7c66245b59461217c99eaa1611891cd2c\",\n    \"zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3\",\n    \"zh:7f45b35a8330bebd184c2545a41782ff58240ed6ba947274d9881dd5da44b02e\",\n    \"zh:87e67891033214e55cfead1391d68e6a3bf37993b7607753237e82aa3250bb71\",\n    \"zh:de3590d14037ad81fc5cedf7cfa44614a92452d7b39676289b704a962050bc5e\",\n    \"zh:e7e6f2ea567f2dbb3baa81c6203be69f9cd6aeeb01204fd93e3cf181e099b610\",\n    \"zh:fd24d03c89a7702628c2e5a3c732c0dede56fa75a08da4a1efe17b5f881c88e2\",\n    \"zh:febf4b7b5f3ff2adff0573ef6361f09b6638105111644bdebc0e4f575373935f\",\n  ]\n}\n\nprovider \"registry.terraform.io/hashicorp/google\" {\n  version = \"5.19.0\"\n  hashes = [\n    \"h1:8zAxRX/sRwMky2DwdYV1jJradiK1bAhdk2Yxf3SVWDs=\",\n    \"zh:10f545f850aadab0da068b9d5023954f2983c4c6acdb999342feb90db373b4b3\",\n    \"zh:136244af528a17c74e18e83182d96590330a03d7f830c06ae2c9ff4323ebbcf5\",\n    \"zh:3312be8229c0e4c72f0cae49cbde7a15078793b48d25c5dc0327c5c277475865\",\n    \"zh:3db97229beb5b3bb586b80756146e16d4f4f8fc60802edd23ac167fd8c10ca07\",\n    \"zh:764051d375deb983c08c999fc82e622eb68c82bc0361991c3dae7ba6a05dafff\",\n    \"zh:9309f7140de8394ade88254ff18f2746525143e36d5f4946259097bc0d19dee9\",\n    \"zh:b8c2e1b837efa3a91e01a06296eb978a11cecf40685664b9d07b2daa6abd5814\",\n    \"zh:c8a12e2f0daf01e7d9e56b963c9fa64c097a437ed45ffa1ebb2d1db8fd381a00\",\n    \"zh:d235bbf145d028cb512ad10f4ec5b4e7db3e59c7a862a12fdcbc250173e77ef6\",\n    \"zh:d997eb3ef64b0e2c99c12aec572d19a5b819edddcdcade8b3b53e3a80d064a9d\",\n    \"zh:e12dd062d5ad9f8223527de3221ab994a929f47b114f04460f94f11af93d6226\",\n    \"zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c\",\n  ]\n}\n\nprovider \"registry.terraform.io/hashicorp/google-beta\" {\n  version = \"5.19.0\"\n  hashes = [\n    \"h1:KQihN6QlyCJQweQV0KdQiU9nlO/9Idejo7j05Q5U1Ho=\",\n    \"zh:18504b12732d42b818415fd190845732e67cfc64f175ea985fca6378cf46073f\",\n    \"zh:3c3c2bed588f61301d3a12680a37e5e3863c7e47f23418b658dedad70494d502\",\n    \"zh:3edf1b797a209486b8d574145eb71034b48d98e5bf9c5afce0dbc04b1d7d46f2\",\n    \"zh:411297c82d4abdf99128fad94de41d338cc3602f20da9f87fe3c3766bc7cca5e\",\n    \"zh:8dec9ffbd09073eecd947493633ca8f99951206018625969a380e53002616c82\",\n    \"zh:d2248af5131d2684a30f99602e7ad930e8287dba1a57669cd319ad0e1596b411\",\n    \"zh:d9ccd7e8210b6ee122340c747cc84cb94017a6be72115bdd7e9a9056c163dc84\",\n    \"zh:df1a377c74b2b4a43971ac39f7b983d92ec9ae4f4b81cb86e763630d1ca23e5d\",\n    \"zh:ea56337757203cb414c00d751b1b9eae28b3d5c0c660fd6b439d0f3597c14cf1\",\n    \"zh:ec2f17ad4095b6aaa3648ae7b5eb227797a1d2ece70762c3e0cf2696f403536f\",\n    \"zh:f0938877a10f70cd58d95353b0b4d2c31f6b921a2ebfdc5cec43e127d547c14d\",\n    \"zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c\",\n  ]\n}\n\nprovider \"registry.terraform.io/kreuzwerker/docker\" {\n  version = \"3.0.2\"\n  hashes = [\n    \"h1:XjdpVL61KtTsuPE8swok3GY8A+Bu3TZs8T2DOEpyiXo=\",\n    \"zh:15b0a2b2b563d8d40f62f83057d91acb02cd0096f207488d8b4298a59203d64f\",\n    \"zh:23d919de139f7cd5ebfd2ff1b94e6d9913f0977fcfc2ca02e1573be53e269f95\",\n    \"zh:38081b3fe317c7e9555b2aaad325ad3fa516a886d2dfa8605ae6a809c1072138\",\n    \"zh:4a9c5065b178082f79ad8160243369c185214d874ff5048556d48d3edd03c4da\",\n    \"zh:5438ef6afe057945f28bce43d76c4401254073de01a774760169ac1058830ac2\",\n    \"zh:60b7fadc287166e5c9873dfe53a7976d98244979e0ab66428ea0dea1ebf33e06\",\n    \"zh:61c5ec1cb94e4c4a4fb1e4a24576d5f39a955f09afb17dab982de62b70a9bdd1\",\n    \"zh:a38fe9016ace5f911ab00c88e64b156ebbbbfb72a51a44da3c13d442cd214710\",\n    \"zh:c2c4d2b1fd9ebb291c57f524b3bf9d0994ff3e815c0cd9c9bcb87166dc687005\",\n    \"zh:d567bb8ce483ab2cf0602e07eae57027a1a53994aba470fa76095912a505533d\",\n    \"zh:e83bf05ab6a19dd8c43547ce9a8a511f8c331a124d11ac64687c764ab9d5a792\",\n    \"zh:e90c934b5cd65516fbcc454c89a150bfa726e7cf1fe749790c7480bbeb19d387\",\n    \"zh:f05f167d2eaf913045d8e7b88c13757e3cf595dd5cd333057fdafc7c4b7fed62\",\n    \"zh:fcc9c1cea5ce85e8bcb593862e699a881bd36dffd29e2e367f82d15368659c3d\",\n  ]\n}\n"
  },
  {
    "path": "terraform/cloud_function.tf",
    "content": "resource \"google_storage_bucket\" \"cloudfunctions_bucket\" {\n  name          = join(\"-\", concat([\"adopt-a-book-cf-bucket\", terraform.workspace]))\n  provider      = google\n  location      = var.region\n  force_destroy = true\n}\n\ndata \"archive_file\" \"weather_service_file\" {\n  type        = \"zip\"\n  provider    = google-beta\n  output_path = \"${path.module}/.files/weather_service.zip\"\n  source {\n    content  = file(\"${local.src_dir}/api/main.py\")\n    filename = \"main.py\"\n  }\n  source {\n    content  = file(\"${local.src_dir}/api/weather.py\")\n    filename = \"weather.py\"\n  }\n  source {\n    content  = file(\"${local.src_dir}/api/requirements.txt\")\n    filename = \"requirements.txt\"\n  }\n\n}\n\nresource \"google_storage_bucket_object\" \"weather_service_zip\" {\n  # To ensure the Cloud Function gets redeployed when the zip file is updated.\n  name       = format(\"%s#%s\", \"weather_service.zip\", data.archive_file.weather_service_file.output_md5)\n  bucket     = google_storage_bucket.cloudfunctions_bucket.name\n  source     = \"${path.module}/.files/weather_service.zip\"\n  depends_on = [data.archive_file.weather_service_file]\n}\n\n\nresource \"google_cloudfunctions2_function\" \"weather_service\" {\n  name        = join(\"-\", concat([\"weather-service\", terraform.workspace]))\n  description = \"Small Cloud Function to get Weather Information\"\n  location    = var.region\n  build_config {\n    runtime     = \"python311\"\n    entry_point = \"handle_request\" # Set the entry point \n    source {\n      storage_source {\n        bucket = google_storage_bucket.cloudfunctions_bucket.name\n        object = google_storage_bucket_object.weather_service_zip.name\n      }\n    }\n  }\n  service_config {\n    max_instance_count = 1\n    available_memory   = \"256M\"\n    timeout_seconds    = 60\n  }\n\n\n  depends_on = [google_storage_bucket_object.weather_service_zip]\n}"
  },
  {
    "path": "terraform/locals.tf",
    "content": "locals {\n  project_root_dir = abspath(\"${path.root}/..\")\n  src_dir          = \"${local.project_root_dir}/src\"\n}"
  },
  {
    "path": "terraform/main.tf",
    "content": "provider \"google\" {\n  project = var.project\n  region  = var.region\n}\n\nprovider \"google-beta\" {\n  project = var.project\n  region  = var.region\n}\n\nterraform {\n  required_version = \"1.7.4\"\n  backend \"gcs\" {\n    bucket = \"adopt-a-book-terraform-state\"\n    prefix = \"terraform/edge\"\n  }\n  required_providers {\n    google = {\n      source  = \"hashicorp/google\"\n      version = \"~> 5.19.0\"\n    }\n    google-beta = {\n      source  = \"hashicorp/google\"\n      version = \"~> 5.19.0\"\n    }\n  }\n}\n\n# Getting the project number for assigning permissions\ndata \"google_project\" \"project\" {}\n\ndata \"google_client_config\" \"default\" {\n  depends_on = [data.google_project.project]\n}\n\n\nresource \"google_project_service\" \"enable_api\" {\n  for_each = toset([\n    \"logging.googleapis.com\",\n    \"artifactregistry.googleapis.com\",\n    \"run.googleapis.com\",\n    \"iamcredentials.googleapis.com\",\n  ])\n\n  service = each.key # value and key are the same for a set\n\n  timeouts {\n    create = \"30m\"\n    update = \"40m\"\n  }\n\n  disable_on_destroy = false\n  depends_on         = [data.google_client_config.default]\n}\n"
  },
  {
    "path": "terraform/variables.tf",
    "content": "variable \"project\" {\n  description = \"The GCP project id\"\n  default     = \"adopt-a-book\"\n  type        = string\n}\n\nvariable \"region\" {\n  description = \"The GCP region for this project\"\n  default     = \"europe-west4\"\n  type        = string\n}\n"
  },
  {
    "path": "tests/test_weather.py",
    "content": "from api.weather import format_weather\n\n\ndef test_format_weather():\n    # Test case 1: Sunny weather\n    city = \"Los Angeles\"\n    data = {\"weather\": [{\"description\": \"sunny\"}], \"main\": {\"temp\": 25}}\n    expected = (\n                \"The weather in Los Angeles is sunny \"\n                \"with a temperature of 25 Celcius.\"\n                )\n    assert format_weather(city, data) == expected\n\n    # Test case 2: Cloudy weather\n    city = \"London\"\n    data = {\"weather\": [{\"description\": \"cloudy\"}], \"main\": {\"temp\": 15}}\n    expected = (\n                \"The weather in London is cloudy \"\n                \"with a temperature of 15 Celcius.\"\n                )\n    assert format_weather(city, data) == expected\n\n    # Test case 3: Rainy weather\n    city = \"Seattle\"\n    data = {\"weather\": [{\"description\": \"rainy\"}], \"main\": {\"temp\": 10}}\n    expected = (\n                \"The weather in Seattle is rainy \"\n                \"with a temperature of 10 Celcius.\"\n                )\n    assert format_weather(city, data) == expected\n"
  }
]