From 78a7636467cb33c2a36646a60e6fa0610ba0eca2 Mon Sep 17 00:00:00 2001 From: KoLiBer Date: Sat, 31 Aug 2024 18:33:07 +0000 Subject: [PATCH] feat: add project --- .github/.releaserc.yml | 16 ++++++++++ .github/workflows/cicd.yml | 42 ++++++++++++++++++++++++ .gitignore | 37 ++++++++++++++++++++++ .terraform.lock.hcl | 25 +++++++++++++++ LICENSE.md | 21 ++++++++++++ README.md | 65 ++++++++++++++++++++++++++++++++++++-- main.tf | 9 ++++++ outputs.tf | 24 ++++++++++++++ projects.tf | 19 +++++++++++ root.tf | 11 +++++++ teams.tf | 7 ++++ variables.tf | 42 ++++++++++++++++++++++++ 12 files changed, 316 insertions(+), 2 deletions(-) create mode 100644 .github/.releaserc.yml create mode 100644 .github/workflows/cicd.yml create mode 100644 .gitignore create mode 100644 .terraform.lock.hcl create mode 100644 LICENSE.md create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 projects.tf create mode 100644 root.tf create mode 100644 teams.tf create mode 100644 variables.tf diff --git a/.github/.releaserc.yml b/.github/.releaserc.yml new file mode 100644 index 0000000..07b949b --- /dev/null +++ b/.github/.releaserc.yml @@ -0,0 +1,16 @@ +tagFormat: $${q}{version} +branches: + - main + - master + - +([0-9])?(.{+([0-9]),x}).x + - { name: next, prerelease: true } + - { name: beta, prerelease: true } + - { name: alpha, prerelease: true } + - { name: stable, prerelease: true } +plugins: + - "@semantic-release/commit-analyzer" + - "@semantic-release/release-notes-generator" + - "@semantic-release/changelog" + - "@semantic-release/github" + - - "@semantic-release/git" + - assets: [CHANGELOG.md] diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..f96ec94 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,42 @@ +name: CI/CD + +on: + pull_request: { branches: ["*"] } + push: + branches: + - "+([0-9])?(.{+([0-9]),x}).x" + - master + - main + - next + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: hashicorp/setup-terraform@v3 + with: + terraform_version: "1.9.5" + - uses: actions/cache@v2 + with: + path: .terraform/ + key: ${{ hashFiles('.terraform.lock.hcl') }} + + - run: terraform init + - run: terraform fmt -check + - run: terraform validate -no-color + + bump: + needs: build + runs-on: ubuntu-latest + if: github.event_name == 'push' + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - run: npm i -g semantic-release @semantic-release/changelog @semantic-release/github @semantic-release/git + - run: cat .github/.releaserc.yml | envsubst > .releaserc.yml && semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c4c242 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log + +# Exclude all .tfvars files, which are likely to contain sentitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +# +*.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc + +# dotenv environment variables file +.env diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..85b4c8e --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,25 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/jianyuan/sentry" { + version = "0.13.1" + constraints = ">= 0.13.0" + hashes = [ + "h1:tPFv6cLf04pWB9YVi9IYha4PjwXwgho06wjZ9tG7NZw=", + "zh:0dde99e7b343fa01f8eefc378171fb8621bedb20f59157d6cc8e3d46c738105f", + "zh:30ed398792e511f41555be5f3a3ae024858972b05a6e3c5ff31d9a745d6a8d08", + "zh:497ddbe21689fb08145b79f7a1a9d499997b24ec458f0b4f63e3dc4454830207", + "zh:50f8ad867e9367c2bb99c09003a85f1db7b0db381e7e34644468d67cf516958a", + "zh:54b24ae567d08a6d8af0688be22e0b8e74077f21101bdc936e8b0913f6847122", + "zh:65bdc167e23fc978336e934b481c984e4696eaeadf6753332cab39a2f969fdd2", + "zh:689cafb3b7033d84a2c951f1a76fe3f6b79a9313c9725162bf9b01c817b5d79e", + "zh:7b34307a54a1a390251a5429c799d4bc43d84ea29439f0a14d8f2b6c500ea897", + "zh:7ed669e745e7a40773a6d9f6559b42ac1b2b9b06ca757700c6c2d5f31a9f79bb", + "zh:8f808a6653bf998ee17ccc9d1f7b40036e2611c04371fa57830ad48053852dd6", + "zh:b376cd99f4b5b7e18754a859e13d911edeb1db57b7518974f40a221036d50ab6", + "zh:bcb86759ce8761fb88a3652772f8eceb9b83238a3ea249e65dffab9212790f36", + "zh:be4b848c74285c9b023e85b15df46b16076be1cf418efd2fc28d370d65c095e2", + "zh:d628d39faaacd5b7dc6cd34d7984b3ae7a0dd5d81803be6e72058954d3261303", + "zh:e7b48d7de5dd1133dc0f500320e2a626478fd88656b15aa13d4cd51582cb764a", + ] +} diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..22a22b6 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2022-present, KoLiBer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index ecd294b..d9fabce 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,63 @@ -# terraform-sentry-organization -Terraform Sentry Organization Module +# Terraform Sentry Organization + +![pipeline](https://github.com/cktf/terraform-sentry-organization/actions/workflows/cicd.yml/badge.svg) +![release](https://img.shields.io/github/v/release/cktf/terraform-sentry-organization?display_name=tag) +![license](https://img.shields.io/github/license/cktf/terraform-sentry-organization) + +**Organization** is a Terraform module useful for creating multiple teams and projects in **Sentry** + +## Installation + +Add the required configurations to your terraform config file and install module using command bellow: + +```bash +terraform init +``` + +## Usage + +```hcl +module "sentry" { + source = "cktf/organization/sentry" + + slug = "myorg" + name = "MyOrg" + + teams = { + backend = { + slug = "backend" + name = "Backend" + } + frontend = { + slug = "backend" + name = "Frontend" + } + } + + projects = { + app1 = { + slug = "app1" + name = "App1" + teams = ["backend"] + } + app2 = { + slug = "app2" + name = "App2" + teams = ["backend", "frontend"] + platform = "django" + } + } +} + +``` + +## Contributing + +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. + +Please make sure to update tests as appropriate. + +## License + +This project is licensed under the [MIT](LICENSE.md). +Copyright (c) KoLiBer (koliberr136a1@gmail.com) diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..46c2763 --- /dev/null +++ b/main.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.5.0" + required_providers { + sentry = { + source = "jianyuan/sentry" + version = ">= 0.13.0" + } + } +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..3924d53 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,24 @@ +output "id" { + value = local.org_id + sensitive = false + description = "Organization ID" +} + +output "teams" { + value = { for key, val in sentry_team.this : key => val.id } + sensitive = false + description = "Organization Teams" +} + +output "projects" { + value = { + for key, val in sentry_project.this : key => { + id = val.id + dsn_csp = sentry_key.this[key].dsn_csp + dsn_public = sentry_key.this[key].dsn_public + dsn_secret = sentry_key.this[key].dsn_secret + } + } + sensitive = false + description = "Organization Projects" +} diff --git a/projects.tf b/projects.tf new file mode 100644 index 0000000..d61e4f0 --- /dev/null +++ b/projects.tf @@ -0,0 +1,19 @@ +resource "sentry_project" "this" { + depends_on = [sentry_team.this] + for_each = var.projects + + organization = local.org_id + slug = each.value.slug + name = each.value.name + teams = each.value.teams + platform = each.value.platform + default_key = false +} + +resource "sentry_key" "this" { + for_each = var.projects + + organization = local.org_id + project = sentry_project.this[each.key].id + name = "default" +} diff --git a/root.tf b/root.tf new file mode 100644 index 0000000..aef2ae7 --- /dev/null +++ b/root.tf @@ -0,0 +1,11 @@ +resource "sentry_organization" "this" { + count = var.id == null ? 1 : 0 + + slug = var.slug + name = var.name + agree_terms = true +} + +locals { + org_id = coalesce(var.id, sentry_organization.this[0].id) +} diff --git a/teams.tf b/teams.tf new file mode 100644 index 0000000..e6571fa --- /dev/null +++ b/teams.tf @@ -0,0 +1,7 @@ +resource "sentry_team" "this" { + for_each = var.teams + + organization = local.org_id + slug = each.value.slug + name = each.value.name +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..3697b45 --- /dev/null +++ b/variables.tf @@ -0,0 +1,42 @@ +variable "id" { + type = string + default = null + sensitive = false + description = "Organization ID" +} + +variable "slug" { + type = string + default = "" + sensitive = false + description = "Organization Slug" +} + +variable "name" { + type = string + default = "" + sensitive = false + description = "Organization Name" +} + +variable "teams" { + type = map(object({ + slug = string + name = string + })) + default = {} + sensitive = false + description = "Project Teams" +} + +variable "projects" { + type = map(object({ + slug = string + name = string + teams = optional(list(string), []) + platform = optional(string) + })) + default = {} + sensitive = false + description = "Project Projects" +}