Skip to content

Commit 513a965

Browse files
Add singular data source for retrieving an Artifact Registry tag (#14717) (#23994)
[upstream:a25d28a8ff654b4b6d6ca70ab5071f30077aa57d] Signed-off-by: Modular Magician <[email protected]>
1 parent d5fcdfa commit 513a965

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed

.changelog/14717.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_tag`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
189189
"google_artifact_registry_package": artifactregistry.DataSourceArtifactRegistryPackage(),
190190
"google_artifact_registry_repositories": artifactregistry.DataSourceArtifactRegistryRepositories(),
191191
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
192+
"google_artifact_registry_tag": artifactregistry.DataSourceArtifactRegistryTag(),
192193
"google_artifact_registry_tags": artifactregistry.DataSourceArtifactRegistryTags(),
193194
"google_artifact_registry_version": artifactregistry.DataSourceArtifactRegistryVersion(),
194195
"google_apphub_discovered_workload": apphub.DataSourceApphubDiscoveredWorkload(),
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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_tag.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 DataSourceArtifactRegistryTag() *schema.Resource {
30+
return &schema.Resource{
31+
Read: DataSourceArtifactRegistryTagRead,
32+
33+
Schema: map[string]*schema.Schema{
34+
"location": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
},
38+
"repository_id": {
39+
Type: schema.TypeString,
40+
Required: true,
41+
},
42+
"package_name": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
},
46+
"tag_name": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
},
50+
"project": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
},
54+
"name": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"version": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
},
63+
}
64+
}
65+
66+
func DataSourceArtifactRegistryTagRead(d *schema.ResourceData, meta interface{}) error {
67+
config := meta.(*transport_tpg.Config)
68+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
69+
if err != nil {
70+
return fmt.Errorf("Error setting Artifact Registry user agent: %s", err)
71+
}
72+
73+
project, err := tpgresource.GetProject(d, config)
74+
if err != nil {
75+
return fmt.Errorf("Error setting Artifact Registry project: %s", err)
76+
}
77+
78+
basePath, err := tpgresource.ReplaceVars(d, config, "{{ArtifactRegistryBasePath}}")
79+
if err != nil {
80+
return fmt.Errorf("Error setting Artifact Registry base path: %s", err)
81+
}
82+
83+
resourcePath, err := tpgresource.ReplaceVars(d, config, fmt.Sprintf("projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}/packages/{{package_name}}/tags/{{tag_name}}"))
84+
if err != nil {
85+
return fmt.Errorf("Error setting resource path: %s", err)
86+
}
87+
88+
urlRequest := basePath + resourcePath
89+
headers := make(http.Header)
90+
91+
u, err := url.Parse(urlRequest)
92+
if err != nil {
93+
return fmt.Errorf("Error parsing URL: %s", err)
94+
}
95+
96+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
97+
Config: config,
98+
Method: "GET",
99+
RawURL: u.String(),
100+
UserAgent: userAgent,
101+
Headers: headers,
102+
})
103+
if err != nil {
104+
return fmt.Errorf("Error getting Artifact Registry tag: %s", err)
105+
}
106+
107+
annotations := make(map[string]string)
108+
if anno, ok := res["annotations"].(map[string]interface{}); ok {
109+
for k, v := range anno {
110+
if val, ok := v.(string); ok {
111+
annotations[k] = val
112+
}
113+
}
114+
}
115+
116+
getString := func(m map[string]interface{}, key string) string {
117+
if v, ok := m[key].(string); ok {
118+
return v
119+
}
120+
return ""
121+
}
122+
123+
name := getString(res, "name")
124+
125+
if err := d.Set("project", project); err != nil {
126+
return err
127+
}
128+
if err := d.Set("name", name); err != nil {
129+
return err
130+
}
131+
if err := d.Set("version", res["version"].(string)); err != nil {
132+
return err
133+
}
134+
135+
d.SetId(name)
136+
137+
return nil
138+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_tag_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 TestAccDataSourceArtifactRegistryTag_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: testAccDataSourceArtifactRegistryTagConfig,
35+
Check: resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttr("data.google_artifact_registry_tag.this", "name", "projects/go-containerregistry/locations/us/repositories/gcr.io/packages/gcrane/tags/latest"),
37+
),
38+
},
39+
},
40+
})
41+
}
42+
43+
// Test the data source against the public AR repos
44+
// https://console.cloud.google.com/artifacts/docker/cloudrun/us/container
45+
// https://console.cloud.google.com/artifacts/docker/go-containerregistry/us/gcr.io
46+
const testAccDataSourceArtifactRegistryTagConfig = `
47+
data "google_artifact_registry_tag" "this" {
48+
project = "go-containerregistry"
49+
location = "us"
50+
repository_id = "gcr.io"
51+
package_name = "gcrane"
52+
tag_name = "latest"
53+
}
54+
`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_tag.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 a tag within a Google Artifact Registry repository.
19+
---
20+
21+
# google_artifact_registry_tag
22+
This data source fetches information of a tag from a provided Artifact Registry repository.
23+
24+
## Example Usage
25+
26+
```hcl
27+
data "google_artifact_registry_tags" "my_tags" {
28+
location = "us-central1"
29+
repository_id = "example-repo"
30+
package_name = "example-package"
31+
tag_name = "latest"
32+
}
33+
```
34+
35+
## Argument Reference
36+
37+
The following arguments are supported:
38+
39+
* `location` - (Required) The location of the artifact registry.
40+
41+
* `repository_id` - (Required) The last part of the repository name to fetch from.
42+
43+
* `package_name` - (Required) The name of the package.
44+
45+
* `tag_name` - (Required) The name of the tag.
46+
47+
* `project` - (Optional) The project ID in which the resource belongs. If it is not provided, the provider project is used.
48+
49+
## Attributes Reference
50+
51+
The following computed attributes are exported:
52+
53+
* `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.
54+
55+
* `version` - The version of the tag.

0 commit comments

Comments
 (0)