OpsLevel / terraform-provider-opslevel

Terraform provider for OpsLevel.com
https://registry.terraform.io/providers/OpsLevel/opslevel/latest/docs
MIT License
8 stars 5 forks source link

Ft/read languages from repo data source #326

Closed vini-mw closed 5 months ago

vini-mw commented 5 months ago

Description

Reading and adding the languages attribute to the opslevel_repository data source.

the intended use here is for people to be able to programatically read the languages of any one repo, detect the language with the highest coverage, and assign it to the opslevel_service resource corresponding with the repo.

use case example:

locals {
  // identify the highest coverage out of all detected languages
  highest_lang_coverage = max([for k, v in data.opslevel_repository.this.languages : tonumber(v.usage)]...)
  // find the object of the language w the highest coverage from the returned list of languages by using the previously identified highest coverage number
  main_repo_language = element([for k, v in data.opslevel_repository.this.languages : v if lookup(v, "usage") == tostring(local.highest_lang_coverage)], 0)
}

data "opslevel_repository" "this" {
  alias = "github.com:cool-org/cool-service"
}

resource "opslevel_service" "this" {
  name = "cool-service"

  lifecycle_alias = data.opslevel_lifecycle.this.alias
  tier_alias      = data.opslevel_tier.this.alias
  owner           = opslevel_team.cool-team.id

  // set language using the newly added datasource output
  language = local.main_repo_language.name
}

// attach repo to service
resource "opslevel_service_repository" "this" {
  service    = opslevel_service.this.id
  repository = data.opslevel_repository.this.id

  name           = "cool-service"
  base_directory = "/"
}

this method allows us to populate the language attribute of the service efficiently. as even if a repo is attached to a service (like i do with the opslevel_service_repository resource in the example above), the service will not inherit the available language details of the repository.

Changelog

Tophatting

data source languages output


output "data" {
  value = data.opslevel_repository.this.languages
}

result in:

Outputs:

data = tolist([
  {
    "name" = "HCL"
    "usage" = "0.55404"
  },
  {
    "name" = "JavaScript"
    "usage" = "0.07078"
  },
  {
    "name" = "TypeScript"
    "usage" = "0.37518"
  },
])
CLAassistant commented 5 months ago

CLA assistant check
All committers have signed the CLA.

vini-mw commented 5 months ago

@davidbloss , got any thoughts on this?

davidbloss commented 5 months ago

Hey @vini-mw, thank you for your continued updates on this! This is a really great addition to the provider and the code looks good 👏 Can you run a quick changie new --kind Feature to describe your addition? After that this should be good to go 🙌

vini-mw commented 5 months ago

hey @davidbloss, any chance we could get a new release shortly with the latest changes? there been 6 PRs merged to main since the latest release, with features and fixes that we'd like to experiment with, this one included 😇