Skip to content

Commit df8fe7b

Browse files
Add plural data source for retrieving Artifact Registry tags (#14702) (#23969)
[upstream:f05fb980b32dc22a1bc9c0213f1200b6106cabf5] Signed-off-by: Modular Magician <[email protected]>
1 parent b1779d2 commit df8fe7b

File tree

5 files changed

+311
-1
lines changed

5 files changed

+311
-1
lines changed

.changelog/14702.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_artifact_registry_tags`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,10 @@ var handwrittenDatasources = map[string]*schema.Resource{
186186
"google_artifact_registry_docker_image": artifactregistry.DataSourceArtifactRegistryDockerImage(),
187187
"google_artifact_registry_docker_images": artifactregistry.DataSourceArtifactRegistryDockerImages(),
188188
"google_artifact_registry_locations": artifactregistry.DataSourceGoogleArtifactRegistryLocations(),
189+
"google_artifact_registry_package": artifactregistry.DataSourceArtifactRegistryPackage(),
189190
"google_artifact_registry_repositories": artifactregistry.DataSourceArtifactRegistryRepositories(),
190191
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
191-
"google_artifact_registry_package": artifactregistry.DataSourceArtifactRegistryPackage(),
192+
"google_artifact_registry_tags": artifactregistry.DataSourceArtifactRegistryTags(),
192193
"google_artifact_registry_version": artifactregistry.DataSourceArtifactRegistryVersion(),
193194
"google_apphub_discovered_workload": apphub.DataSourceApphubDiscoveredWorkload(),
194195
"google_app_engine_default_service_account": appengine.DataSourceGoogleAppEngineDefaultServiceAccount(),
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/artifactregistry/data_source_artifact_registry_tags.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
package artifactregistry
18+
19+
import (
20+
"fmt"
21+
"net/http"
22+
"net/url"
23+
24+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
25+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
26+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
27+
)
28+
29+
func DataSourceArtifactRegistryTags() *schema.Resource {
30+
return &schema.Resource{
31+
Read: dataSourceArtifactRegistryTagsRead,
32+
Schema: map[string]*schema.Schema{
33+
"location": {
34+
Type: schema.TypeString,
35+
Required: true,
36+
},
37+
"repository_id": {
38+
Type: schema.TypeString,
39+
Required: true,
40+
},
41+
"package_name": {
42+
Type: schema.TypeString,
43+
Required: true,
44+
},
45+
"filter": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
},
49+
"project": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
},
53+
"tags": {
54+
Type: schema.TypeList,
55+
Computed: true,
56+
Elem: &schema.Resource{
57+
Schema: map[string]*schema.Schema{
58+
"name": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"version": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
},
66+
},
67+
},
68+
},
69+
},
70+
}
71+
}
72+
73+
func dataSourceArtifactRegistryTagsRead(d *schema.ResourceData, meta interface{}) error {
74+
config := meta.(*transport_tpg.Config)
75+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
76+
if err != nil {
77+
return err
78+
}
79+
80+
project, err := tpgresource.GetProject(d, config)
81+
if err != nil {
82+
return err
83+
}
84+
85+
basePath, err := tpgresource.ReplaceVars(d, config, "{{ArtifactRegistryBasePath}}")
86+
if err != nil {
87+
return fmt.Errorf("Error setting Artifact Registry base path: %s", err)
88+
}
89+
90+
resourcePath, err := tpgresource.ReplaceVars(d, config, fmt.Sprintf("projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}/packages/{{package_name}}/tags"))
91+
if err != nil {
92+
return fmt.Errorf("Error setting resource path: %s", err)
93+
}
94+
95+
urlRequest := basePath + resourcePath
96+
97+
filter := ""
98+
if v, ok := d.GetOk("filter"); ok {
99+
filter = v.(string)
100+
101+
u, err := url.Parse(urlRequest)
102+
if err != nil {
103+
return fmt.Errorf("Error parsing URL: %s", err)
104+
}
105+
106+
q := u.Query()
107+
q.Set("filter", filter)
108+
u.RawQuery = q.Encode()
109+
urlRequest = u.String()
110+
}
111+
112+
headers := make(http.Header)
113+
tags := make([]map[string]interface{}, 0)
114+
pageToken := ""
115+
116+
for {
117+
u, err := url.Parse(urlRequest)
118+
if err != nil {
119+
return fmt.Errorf("Error parsing URL: %s", err)
120+
}
121+
122+
q := u.Query()
123+
if pageToken != "" {
124+
q.Set("pageToken", pageToken)
125+
}
126+
u.RawQuery = q.Encode()
127+
128+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
129+
Config: config,
130+
Method: "GET",
131+
RawURL: u.String(),
132+
UserAgent: userAgent,
133+
Headers: headers,
134+
})
135+
136+
if err != nil {
137+
return fmt.Errorf("Error listing Artifact Registry tags: %s", err)
138+
}
139+
140+
if items, ok := res["tags"].([]interface{}); ok {
141+
for _, item := range items {
142+
tag := item.(map[string]interface{})
143+
144+
annotations := make(map[string]string)
145+
if anno, ok := tag["annotations"].(map[string]interface{}); ok {
146+
for k, v := range anno {
147+
if val, ok := v.(string); ok {
148+
annotations[k] = val
149+
}
150+
}
151+
}
152+
153+
getString := func(m map[string]interface{}, key string) string {
154+
if v, ok := m[key].(string); ok {
155+
return v
156+
}
157+
return ""
158+
}
159+
160+
tags = append(tags, map[string]interface{}{
161+
"name": getString(tag, "name"),
162+
"version": getString(tag, "version"),
163+
})
164+
}
165+
}
166+
167+
if nextToken, ok := res["nextPageToken"].(string); ok && nextToken != "" {
168+
pageToken = nextToken
169+
} else {
170+
break
171+
}
172+
}
173+
174+
if err := d.Set("project", project); err != nil {
175+
return fmt.Errorf("Error setting project: %s", err)
176+
}
177+
178+
if err := d.Set("tags", tags); err != nil {
179+
return fmt.Errorf("Error setting Artifact Registry tags: %s", err)
180+
}
181+
182+
d.SetId(resourcePath)
183+
184+
return nil
185+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/artifactregistry/data_source_artifact_registry_tags_test.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
package artifactregistry_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
23+
"github.com/hashicorp/terraform-provider-google/google/acctest"
24+
)
25+
26+
func TestAccDataSourceArtifactRegistryTags_basic(t *testing.T) {
27+
t.Parallel()
28+
29+
acctest.VcrTest(t, resource.TestCase{
30+
PreCheck: func() { acctest.AccTestPreCheck(t) },
31+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
32+
Steps: []resource.TestStep{
33+
{
34+
Config: testAccDataSourceArtifactRegistryTagsConfig,
35+
Check: resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_tags.this", "project"),
37+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_tags.this", "location"),
38+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_tags.this", "repository_id"),
39+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_tags.this", "package_name"),
40+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_tags.this", "tags.0.name"),
41+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_tags.this", "tags.0.version"),
42+
),
43+
},
44+
},
45+
})
46+
}
47+
48+
// Test the data source against the public AR repos
49+
// https://console.cloud.google.com/artifacts/docker/cloudrun/us/container
50+
// https://console.cloud.google.com/artifacts/docker/go-containerregistry/us/gcr.io
51+
const testAccDataSourceArtifactRegistryTagsConfig = `
52+
data "google_artifact_registry_tags" "this" {
53+
project = "go-containerregistry"
54+
location = "us"
55+
repository_id = "gcr.io"
56+
package_name = "gcrane"
57+
# Filter doesn't work with gcr.io
58+
# filter = "name=\"projects/go-containerregistry/locations/us/repositories/gcr.io/packages/gcrane/tags/latest\""
59+
}
60+
`
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
# ----------------------------------------------------------------------------
3+
#
4+
# *** AUTO GENERATED CODE *** Type: Handwritten ***
5+
#
6+
# ----------------------------------------------------------------------------
7+
#
8+
# This code is generated by Magic Modules using the following:
9+
#
10+
# Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/website/docs/d/artifact_registry_tags.html.markdown
11+
#
12+
# DO NOT EDIT this file directly. Any changes made to this file will be
13+
# overwritten during the next generation cycle.
14+
#
15+
# ----------------------------------------------------------------------------
16+
subcategory: "Artifact Registry"
17+
description: |-
18+
Get information about tags within a Google Artifact Registry package.
19+
---
20+
21+
# google_artifact_registry_tags
22+
23+
Get information about Artifact Registry tags.
24+
See [the official documentation](https://cloud.google.com/artifact-registry/docs/overview)
25+
and [API](https://cloud.google.com/artifact-registry/docs/reference/rest/v1/projects.locations.repositories.packages.tags/list).
26+
27+
## Example Usage
28+
29+
```hcl
30+
data "google_artifact_registry_tags" "my_tags" {
31+
location = "us-central1"
32+
repository_id = "example-repo"
33+
package_name = "example-package"
34+
}
35+
```
36+
37+
## Argument Reference
38+
39+
The following arguments are supported:
40+
41+
* `location` - (Required) The location of the Artifact Registry repository.
42+
43+
* `repository_id` - (Required) The last part of the repository name to fetch from.
44+
45+
* `package_name` - (Required) The name of the package.
46+
47+
* `filter` - (Optional) An expression for filtering the results of the request. Filter rules are case insensitive. The fields eligible for filtering are `name` and `version`. Further information can be found in the [REST API](https://cloud.google.com/artifact-registry/docs/reference/rest/v1/projects.locations.repositories.packages.tags/list#query-parameters).
48+
49+
* `project` - (Optional) The project ID in which the resource belongs. If it is not provided, the provider project is used.
50+
51+
## Attributes Reference
52+
53+
The following attributes are exported:
54+
55+
* `tags` - A list of all retrieved Artifact Registry tags. Structure is [defined below](#nested_tags).
56+
57+
<a name="nested_tags"></a>The `tags` block supports:
58+
59+
* `name` - The name of the tag, for example: `projects/p1/locations/us-central1/repositories/repo1/packages/pkg1/tags/tag1`. If the package part contains slashes, the slashes are escaped.
60+
61+
* `version` - The version of the tag.

0 commit comments

Comments
 (0)