Skip to content

Commit 8d3d279

Browse files
authored
Merge pull request #3 from sparkfabrik/feat/2762_support_artifact_registry_tags
refs platform/#2762: Add support artifact registry tags
2 parents aabcb5e + b23c3c3 commit 8d3d279

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ In the module output you can retrieve all tags keys and values informations.
5555

5656
| Name | Description | Type | Default | Required |
5757
|------|-------------|------|---------|:--------:|
58-
| <a name="input_buckets_to_be_tagged"></a> [buckets\_to\_be\_tagged](#input\_buckets\_to\_be\_tagged) | A structured list of objects, containing the list of buckets we want to tag and the tag values, in the form `<TAG_KEY_SHORTNAME>/<TAG_VALUE_SHORTNAME>`. If no bucket\_location is specified, the value of default\_location will be used. | <pre>list(object({<br> bucket_name = string<br> tags = optional(list(string), [])<br> bucket_location = optional(string, null)<br> }))</pre> | `[]` | no |
59-
| <a name="input_cloudsql_instances_to_be_tagged"></a> [cloudsql\_instances\_to\_be\_tagged](#input\_cloudsql\_instances\_to\_be\_tagged) | A structured list of objects, containing the list of cloudSQL instances we want to tag, with instance name, instance location (region) and tag values. | <pre>list(object({<br> instance_id = string<br> tags = optional(list(string), [])<br> instance_location = optional(string, null)<br> }))</pre> | `[]` | no |
58+
| <a name="input_artifact_registry_repositories_to_be_tagged"></a> [artifact\_registry\_repositories\_to\_be\_tagged](#input\_artifact\_registry\_repositories\_to\_be\_tagged) | A structured list of objects, containing the list of repositories we want to tag, with repository id, repository location (region) and tag values. | <pre>list(object({<br> repository_id = string<br> repository_location = optional(string, null)<br> tags = optional(list(string), [])<br> }))</pre> | `[]` | no |
59+
| <a name="input_buckets_to_be_tagged"></a> [buckets\_to\_be\_tagged](#input\_buckets\_to\_be\_tagged) | A structured list of objects, containing the list of buckets we want to tag and the tag values, in the form `<TAG_KEY_SHORTNAME>/<TAG_VALUE_SHORTNAME>`. If no bucket\_location is specified, the value of default\_location will be used. | <pre>list(object({<br> bucket_name = string<br> bucket_location = optional(string, null)<br> tags = optional(list(string), [])<br> }))</pre> | `[]` | no |
60+
| <a name="input_cloudsql_instances_to_be_tagged"></a> [cloudsql\_instances\_to\_be\_tagged](#input\_cloudsql\_instances\_to\_be\_tagged) | A structured list of objects, containing the list of cloudSQL instances we want to tag, with instance name, instance location (region) and tag values. | <pre>list(object({<br> instance_id = string<br> instance_location = optional(string, null)<br> tags = optional(list(string), [])<br> }))</pre> | `[]` | no |
6061
| <a name="input_default_location"></a> [default\_location](#input\_default\_location) | The default location (region) used for the resources to be tagged. | `string` | n/a | yes |
6162
| <a name="input_global_tags"></a> [global\_tags](#input\_global\_tags) | A list of tags to be applied to all the resources, in the form tag\_key\_short\_name/tag\_value\_short\_name. If a resource specify a list of tags, the global tags will overridden and replaced by those specified in the resource. | `list(string)` | `[]` | no |
6263
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | The Google Cloud project ID. | `string` | n/a | yes |
@@ -74,9 +75,9 @@ In the module output you can retrieve all tags keys and values informations.
7475

7576
| Name | Type |
7677
|------|------|
78+
| [google_tags_location_tag_binding.artifact_registry](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/tags_location_tag_binding) | resource |
7779
| [google_tags_location_tag_binding.buckets](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/tags_location_tag_binding) | resource |
7880
| [google_tags_location_tag_binding.cloudsql](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/tags_location_tag_binding) | resource |
79-
| [google_project.project](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/project) | data source |
8081
| [google_tags_tag_key.project_tag_keys_to_discover](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/tags_tag_key) | data source |
8182
| [google_tags_tag_key.tag_keys](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/tags_tag_key) | data source |
8283
| [google_tags_tag_value.project_tag_values_to_be_discovered](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/tags_tag_value) | data source |

examples/main.tf

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,34 @@ locals {
2222
{
2323
bucket_name = "external-website"
2424
# Here we override the global tags.
25-
tags = [
25+
tags = [
2626
"dev-team/editor", "external-team/editor"
2727
]
2828
},
2929
]
30+
31+
artifact_registry_repositories_to_be_tagged = [
32+
{
33+
repository_id = "repository-1"
34+
tags = [
35+
"dev-team/editor", "external-team/editor"
36+
]
37+
},
38+
{
39+
repository_id = "repository-2"
40+
}
41+
]
3042
}
3143

3244
# ------------------------------
3345
module "project_resources_tags" {
34-
source = "../."
35-
default_location = "europe-west1"
36-
project_id = "my-project-id"
37-
tags_to_be_discovered = local.tags_to_discover
38-
buckets_to_be_tagged = local.buckets_to_tag
39-
global_tags = [
46+
source = "../."
47+
default_location = "europe-west1"
48+
project_id = "my-project-id"
49+
tags_to_be_discovered = local.tags_to_discover
50+
buckets_to_be_tagged = local.buckets_to_tag
51+
artifact_registry_repositories_to_be_tagged = local.artifact_registry_repositories_to_be_tagged
52+
global_tags = [
4053
"dev-team/editor", "external-team/viewer"
4154
]
4255
}

main.tf

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ locals {
3838
unique_tags_in_cloudsql_instances = distinct(flatten([
3939
for cloudsql in var.cloudsql_instances_to_be_tagged : cloudsql.tags
4040
]))
41-
all_used_unique_tags = distinct(concat(local.unique_tags_in_buckets, local.unique_tags_in_cloudsql_instances, var.global_tags))
41+
unique_tags_in_artifact_registry_repositories = distinct(flatten([
42+
for repository in var.artifact_registry_repositories_to_be_tagged : repository.tags
43+
]))
44+
all_used_unique_tags = distinct(concat(local.unique_tags_in_buckets, local.unique_tags_in_cloudsql_instances, local.unique_tags_in_artifact_registry_repositories, var.global_tags))
4245

4346
# Add the global tags to the buckets we want to tag and populate bucket location.
4447
buckets_to_be_tagged = [
@@ -92,6 +95,27 @@ locals {
9295
]
9396
]) : "${obj.instance_id}--${obj.tag_friendly_name}" => obj
9497
}
98+
99+
# Add the global tags to the Artifact Registry repository we want to tag.
100+
artifact_registry_repositories_to_be_tagged = [
101+
for repository in var.artifact_registry_repositories_to_be_tagged : {
102+
repository_id = repository.repository_id
103+
repository_location = repository.repository_location != null ? repository.repository_location : var.default_location
104+
tags = length(repository.tags) > 0 ? repository.tags : var.global_tags
105+
}
106+
]
107+
108+
map_of_artifact_registry_repositories_to_be_tagged = {
109+
for obj in flatten([
110+
for item in local.artifact_registry_repositories_to_be_tagged : [
111+
for tag in item.tags : {
112+
repository_id = item.repository_id
113+
repository_location = item.repository_location
114+
tag_friendly_name = tag
115+
}
116+
]
117+
]) : "${obj.repository_id}--${obj.tag_friendly_name}" => obj
118+
}
95119
}
96120

97121
# Retrieve the tag keys for the tags that we are passing to the resources.
@@ -130,3 +154,12 @@ resource "google_tags_location_tag_binding" "cloudsql" {
130154
location = each.value.instance_location
131155
tag_value = data.google_tags_tag_value.tag_values[each.value.tag_friendly_name].id
132156
}
157+
158+
# Tag bindings for Artifact Registry repositories.
159+
resource "google_tags_location_tag_binding" "artifact_registry" {
160+
for_each = local.map_of_artifact_registry_repositories_to_be_tagged
161+
# Parent full resource name reference: https://cloud.google.com/artifact-registry/docs/repositories/tag-repos#attach
162+
parent = "//artifactregistry.googleapis.com/projects/${var.project_id}/locations/${each.value.repository_location}/repositories/${each.value.repository_id}"
163+
location = each.value.repository_location
164+
tag_value = data.google_tags_tag_value.tag_values[each.value.tag_friendly_name].id
165+
}

variables.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ variable "buckets_to_be_tagged" {
2424
description = "A structured list of objects, containing the list of buckets we want to tag and the tag values, in the form `<TAG_KEY_SHORTNAME>/<TAG_VALUE_SHORTNAME>`. If no bucket_location is specified, the value of default_location will be used."
2525
type = list(object({
2626
bucket_name = string
27-
tags = optional(list(string), [])
2827
bucket_location = optional(string, null)
28+
tags = optional(list(string), [])
2929
}))
3030
default = []
3131
}
@@ -34,8 +34,18 @@ variable "cloudsql_instances_to_be_tagged" {
3434
description = "A structured list of objects, containing the list of cloudSQL instances we want to tag, with instance name, instance location (region) and tag values."
3535
type = list(object({
3636
instance_id = string
37-
tags = optional(list(string), [])
3837
instance_location = optional(string, null)
38+
tags = optional(list(string), [])
39+
}))
40+
default = []
41+
}
42+
43+
variable "artifact_registry_repositories_to_be_tagged" {
44+
description = "A structured list of objects, containing the list of repositories we want to tag, with repository id, repository location (region) and tag values."
45+
type = list(object({
46+
repository_id = string
47+
repository_location = optional(string, null)
48+
tags = optional(list(string), [])
3949
}))
4050
default = []
4151
}

0 commit comments

Comments
 (0)