spacelift-io / terraform-provider-spacelift

Terraform provider to interact with Spacelift
MIT License
76 stars 29 forks source link

Add spacelift_policies data source #284

Closed alexjurkiewicz closed 2 years ago

alexjurkiewicz commented 2 years ago

This can be used to find all policies of a certain type (PLAN, ACCESS, etc) with specific tags.

One example use-case is in attaching all policies with a specific tag to new stacks:

resource "spacelift_stack" "this" {
 # ...
}

data "spacelift_policies" "plan_autoattach" {
 type = "PLAN"
 tags = ["standard_policies"]
}

resource "spacelift_policy_attachment" "this" {
 for_each  = data.spcelift_policies.plan_autoattach
 policy_id = each.value.id
 stack_id  = spacelift_stack.this.id
}

Type of change

Related issues

Checklists

Development

Code review

alexjurkiewicz commented 2 years ago

Three questions:

  1. What's the process to generate docs?
  2. What's the process to run only my new acceptance tests locally?
  3. What's the next step?
marcinwyszynski commented 2 years ago

What's the process to generate docs?

We use this tool.

What's the process to run only my new acceptance tests locally?

You can populate these 3 variables to point at your account and just run this single unit test.

What's the next step?

Once the suggestions are implemented, we will merge this PR and create a new provider release.

alexjurkiewicz commented 2 years ago

As requested previously, please have the enumeration return a richer collection, not just a list of IDs. Otherwise LGTM.

I was modelling this data source on aws_subnet_ids (link) which only returns the list of IDs. But do you suggest I return a map of policies with more metadata? Like returning map(object({name=string,type=string,labels=list(string)})) (and the policy ID is the map key).

alexjurkiewicz commented 2 years ago

I ended up writing my own smoke test to be sure this data source works as expected. Here's an example Terraform configuration that tests all four possible states:

  1. Load all policies
  2. Load policies of a given type
  3. Load policies with a given tag
  4. Load policies of a given type with a given tag

It worked 😊

main.tf ```terraform terraform { required_providers { spacelift = { source = "spacelift-io/spacelift" } } } provider "spacelift" {} resource "spacelift_policy" "test" { count = 5 name = "test ${count.index}" labels = ["spacelift_policies_test"] type = "ACCESS" body = <
Apply output ``` Apply complete! Resources: 6 added, 0 changed, 0 destroyed. Outputs: login_policy = { "id" = "login-policy" "labels" = toset([]) "name" = "login-policy" "type" = "LOGIN" } number_of_labelled_access_policies = 5 number_of_labelled_policies = 6 number_of_total_policies = 1028 ```
alexjurkiewicz commented 2 years ago

I tried generating docs with v0.8.1 of terraform-plugin-docs, but it tried to rewrite every docs page, it looks like there have been substantial changes to the output format. So I haven't committed any docs page.

adamconnelly commented 2 years ago

I tried generating docs with v0.8.1 of terraform-plugin-docs, but it tried to rewrite every docs page, it looks like there have been substantial changes to the output format. So I haven't committed any docs page.

@alexjurkiewicz I've just merged a change to update to tfplugindocs v0.8.1, so can you pull the latest changes from future and try again?

alexjurkiewicz commented 2 years ago

yup, LGTM!