Skip to content

Commit 86e291d

Browse files
Add singular data source for retrieving an Artifact Registry version (#14721) (#10468)
[upstream:89296409d0a567a444c2ff4763d013d29e7744c3] Signed-off-by: Modular Magician <[email protected]>
1 parent 30d9173 commit 86e291d

File tree

5 files changed

+345
-0
lines changed

5 files changed

+345
-0
lines changed

.changelog/14721.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_version`
3+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
201201
"google_artifact_registry_docker_images": artifactregistry.DataSourceArtifactRegistryDockerImages(),
202202
"google_artifact_registry_locations": artifactregistry.DataSourceGoogleArtifactRegistryLocations(),
203203
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
204+
"google_artifact_registry_version": artifactregistry.DataSourceArtifactRegistryVersion(),
204205
"google_apphub_discovered_workload": apphub.DataSourceApphubDiscoveredWorkload(),
205206
"google_app_engine_default_service_account": appengine.DataSourceGoogleAppEngineDefaultServiceAccount(),
206207
"google_apphub_application": apphub.DataSourceGoogleApphubApplication(),
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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_version.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-beta/google-beta/tpgresource"
26+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
27+
)
28+
29+
func DataSourceArtifactRegistryVersion() *schema.Resource {
30+
return &schema.Resource{
31+
Read: DataSourceArtifactRegistryVersionRead,
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+
"version_name": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
},
50+
"view": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Default: "BASIC",
54+
ValidateFunc: validateViewArtifactRegistryVersion,
55+
},
56+
"project": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
},
60+
"name": {
61+
Type: schema.TypeString,
62+
Computed: true,
63+
},
64+
"description": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
},
68+
"related_tags": {
69+
Type: schema.TypeList,
70+
Computed: true,
71+
Elem: &schema.Resource{
72+
Schema: map[string]*schema.Schema{
73+
"name": {
74+
Type: schema.TypeString,
75+
Computed: true,
76+
},
77+
"version": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
},
81+
},
82+
},
83+
},
84+
"create_time": {
85+
Type: schema.TypeString,
86+
Computed: true,
87+
},
88+
"update_time": {
89+
Type: schema.TypeString,
90+
Computed: true,
91+
},
92+
"annotations": {
93+
Type: schema.TypeMap,
94+
Computed: true,
95+
Elem: &schema.Schema{Type: schema.TypeString},
96+
},
97+
},
98+
}
99+
}
100+
101+
func DataSourceArtifactRegistryVersionRead(d *schema.ResourceData, meta interface{}) error {
102+
config := meta.(*transport_tpg.Config)
103+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
104+
if err != nil {
105+
return fmt.Errorf("Error setting Artifact Registry user agent: %s", err)
106+
}
107+
108+
project, err := tpgresource.GetProject(d, config)
109+
if err != nil {
110+
return fmt.Errorf("Error setting Artifact Registry project: %s", err)
111+
}
112+
113+
basePath, err := tpgresource.ReplaceVars(d, config, "{{ArtifactRegistryBasePath}}")
114+
if err != nil {
115+
return fmt.Errorf("Error setting Artifact Registry base path: %s", err)
116+
}
117+
118+
resourcePath, err := tpgresource.ReplaceVars(d, config, fmt.Sprintf("projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}/packages/{{package_name}}/versions/{{version_name}}"))
119+
if err != nil {
120+
return fmt.Errorf("Error setting resource path: %s", err)
121+
}
122+
123+
view := d.Get("view").(string)
124+
125+
urlRequest := basePath + resourcePath
126+
127+
u, err := url.Parse(urlRequest)
128+
if err != nil {
129+
return fmt.Errorf("Error parsing URL: %s", err)
130+
}
131+
132+
q := u.Query()
133+
q.Set("view", view)
134+
u.RawQuery = q.Encode()
135+
urlRequest = u.String()
136+
137+
headers := make(http.Header)
138+
139+
u, err = url.Parse(urlRequest)
140+
if err != nil {
141+
return fmt.Errorf("Error parsing URL: %s", err)
142+
}
143+
144+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
145+
Config: config,
146+
Method: "GET",
147+
RawURL: u.String(),
148+
UserAgent: userAgent,
149+
Headers: headers,
150+
})
151+
if err != nil {
152+
return fmt.Errorf("Error getting Artifact Registry version: %s", err)
153+
}
154+
155+
var relatedTags []map[string]interface{}
156+
if rawTags, ok := res["relatedTags"].([]interface{}); ok {
157+
for _, rawTag := range rawTags {
158+
if tagMap, ok := rawTag.(map[string]interface{}); ok {
159+
entry := map[string]interface{}{
160+
"name": tagMap["name"],
161+
"version": tagMap["version"],
162+
}
163+
relatedTags = append(relatedTags, entry)
164+
}
165+
}
166+
}
167+
168+
annotations := make(map[string]string)
169+
if anno, ok := res["annotations"].(map[string]interface{}); ok {
170+
for k, v := range anno {
171+
if val, ok := v.(string); ok {
172+
annotations[k] = val
173+
}
174+
}
175+
}
176+
177+
getString := func(m map[string]interface{}, key string) string {
178+
if v, ok := m[key].(string); ok {
179+
return v
180+
}
181+
return ""
182+
}
183+
184+
name := getString(res, "name")
185+
186+
if err := d.Set("project", project); err != nil {
187+
return err
188+
}
189+
if err := d.Set("name", name); err != nil {
190+
return err
191+
}
192+
if err := d.Set("description", getString(res, "description")); err != nil {
193+
return err
194+
}
195+
if err := d.Set("related_tags", relatedTags); err != nil {
196+
return err
197+
}
198+
if err := d.Set("create_time", getString(res, "createTime")); err != nil {
199+
return err
200+
}
201+
if err := d.Set("update_time", getString(res, "updateTime")); err != nil {
202+
return err
203+
}
204+
if err := d.Set("annotations", annotations); err != nil {
205+
return err
206+
}
207+
208+
d.SetId(name)
209+
210+
return nil
211+
}
212+
213+
func validateViewArtifactRegistryVersion(val interface{}, key string) ([]string, []error) {
214+
v := val.(string)
215+
var errs []error
216+
217+
if v != "BASIC" && v != "FULL" {
218+
errs = append(errs, fmt.Errorf("%q must be either 'BASIC' or 'FULL', got %q", key, v))
219+
}
220+
221+
return nil, errs
222+
}
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_version_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-beta/google-beta/acctest"
24+
)
25+
26+
func TestAccDataSourceArtifactRegistryVersion_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: testAccDataSourceArtifactRegistryVersionConfig,
35+
Check: resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttr("data.google_artifact_registry_version.this", "name", "projects/go-containerregistry/locations/us/repositories/gcr.io/packages/gcrane/versions/sha256:c0cf52c2bd8c636bbf701c6c74c5ff819447d384dc957d52a52a668de63e8f5d"),
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 testAccDataSourceArtifactRegistryVersionConfig = `
47+
data "google_artifact_registry_version" "this" {
48+
project = "go-containerregistry"
49+
location = "us"
50+
repository_id = "gcr.io"
51+
package_name = "gcrane"
52+
version_name = "sha256:c0cf52c2bd8c636bbf701c6c74c5ff819447d384dc957d52a52a668de63e8f5d"
53+
}
54+
`
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_version.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 version within a Google Artifact Registry repository.
19+
---
20+
21+
# google_artifact_registry_version
22+
This data source fetches information of a version from a provided Artifact Registry repository.
23+
24+
## Example Usage
25+
26+
```hcl
27+
data "google_artifact_registry_versions" "my_versions" {
28+
location = "us-central1"
29+
repository_id = "example-repo"
30+
package_name = "example-package"
31+
version_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+
* `version_name` - (Required) The name of the version.
46+
47+
* `view` - (Optional) The view, which determines what version information is returned in a response. Possible values are `"BASIC"` and `"FULL"`. Defaults to `"BASIC"`.
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 computed attributes are exported:
54+
55+
* `name` - The name of the version, for example: `projects/p1/locations/us-central1/repositories/repo1/packages/pkg1/versions/version1`. If the package part contains slashes, the slashes are escaped.
56+
57+
* `description` - Description of the version, as specified in its metadata.
58+
59+
* `related_tags` - A list of related tags. Will contain up to 100 tags that reference this version.
60+
61+
* `create_time` - The time, as a RFC 3339 string, this package was created.
62+
63+
* `update_time` - The time, as a RFC 3339 string, this package was last updated. This includes publishing a new version of the package.
64+
65+
* `annotations` - Client specified annotations.

0 commit comments

Comments
 (0)