Skip to content

Commit 792a1c7

Browse files
Add singular data source for retrieving a package from an Artifact Registry repository (#14693) (#23901)
[upstream:7e4360975761b7bf7adcbe39773037880d63b4a1] Signed-off-by: Modular Magician <[email protected]>
1 parent 97273e8 commit 792a1c7

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed

.changelog/14693.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_package`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
187187
"google_artifact_registry_docker_images": artifactregistry.DataSourceArtifactRegistryDockerImages(),
188188
"google_artifact_registry_locations": artifactregistry.DataSourceGoogleArtifactRegistryLocations(),
189189
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
190+
"google_artifact_registry_package": artifactregistry.DataSourceArtifactRegistryPackage(),
190191
"google_artifact_registry_version": artifactregistry.DataSourceArtifactRegistryVersion(),
191192
"google_apphub_discovered_workload": apphub.DataSourceApphubDiscoveredWorkload(),
192193
"google_app_engine_default_service_account": appengine.DataSourceGoogleAppEngineDefaultServiceAccount(),
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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_package.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 DataSourceArtifactRegistryPackage() *schema.Resource {
30+
return &schema.Resource{
31+
Read: DataSourceArtifactRegistryPackageRead,
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+
"name": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
},
46+
"project": {
47+
Type: schema.TypeString,
48+
Optional: true,
49+
},
50+
"display_name": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
},
54+
"create_time": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"update_time": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"annotations": {
63+
Type: schema.TypeMap,
64+
Computed: true,
65+
Elem: &schema.Schema{Type: schema.TypeString},
66+
},
67+
},
68+
}
69+
}
70+
71+
func DataSourceArtifactRegistryPackageRead(d *schema.ResourceData, meta interface{}) error {
72+
config := meta.(*transport_tpg.Config)
73+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
74+
if err != nil {
75+
return fmt.Errorf("Error setting Artifact Registry user agent: %s", err)
76+
}
77+
78+
project, err := tpgresource.GetProject(d, config)
79+
if err != nil {
80+
return fmt.Errorf("Error setting Artifact Registry project: %s", err)
81+
}
82+
83+
basePath, err := tpgresource.ReplaceVars(d, config, "{{ArtifactRegistryBasePath}}")
84+
if err != nil {
85+
return fmt.Errorf("Error setting Artifact Registry base path: %s", err)
86+
}
87+
88+
resourcePath, err := tpgresource.ReplaceVars(d, config, fmt.Sprintf("projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}/packages/{{name}}"))
89+
if err != nil {
90+
return fmt.Errorf("Error setting resource path: %s", err)
91+
}
92+
93+
urlRequest := basePath + resourcePath
94+
headers := make(http.Header)
95+
96+
u, err := url.Parse(urlRequest)
97+
if err != nil {
98+
return fmt.Errorf("Error parsing URL: %s", err)
99+
}
100+
101+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
102+
Config: config,
103+
Method: "GET",
104+
RawURL: u.String(),
105+
UserAgent: userAgent,
106+
Headers: headers,
107+
})
108+
if err != nil {
109+
return fmt.Errorf("Error getting Artifact Registry package: %s", err)
110+
}
111+
112+
annotations := make(map[string]string)
113+
if anno, ok := res["annotations"].(map[string]interface{}); ok {
114+
for k, v := range anno {
115+
if val, ok := v.(string); ok {
116+
annotations[k] = val
117+
}
118+
}
119+
}
120+
121+
getString := func(m map[string]interface{}, key string) string {
122+
if v, ok := m[key].(string); ok {
123+
return v
124+
}
125+
return ""
126+
}
127+
128+
name := getString(res, "name")
129+
130+
if err := d.Set("project", project); err != nil {
131+
return err
132+
}
133+
if err := d.Set("name", name); err != nil {
134+
return err
135+
}
136+
if err := d.Set("display_name", getString(res, "displayName")); err != nil {
137+
return err
138+
}
139+
if err := d.Set("create_time", getString(res, "createTime")); err != nil {
140+
return err
141+
}
142+
if err := d.Set("update_time", getString(res, "updateTime")); err != nil {
143+
return err
144+
}
145+
if err := d.Set("annotations", annotations); err != nil {
146+
return err
147+
}
148+
149+
d.SetId(name)
150+
151+
return nil
152+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_package_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 TestAccDataSourceArtifactRegistryPackage_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: testAccDataSourceArtifactRegistryPackageConfig,
35+
Check: resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttr("data.google_artifact_registry_package.this", "name", "projects/go-containerregistry/locations/us/repositories/gcr.io/packages/gcrane"),
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 testAccDataSourceArtifactRegistryPackageConfig = `
47+
data "google_artifact_registry_package" "this" {
48+
project = "go-containerregistry"
49+
location = "us"
50+
repository_id = "gcr.io"
51+
name = "gcrane"
52+
}
53+
`
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_package.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 package within a Google Artifact Registry repository.
19+
---
20+
21+
# google_artifact_registry_package
22+
This data source fetches information of a package from a provided Artifact Registry repository.
23+
24+
## Example Usage
25+
26+
```hcl
27+
resource "google_artifact_registry_package" "my_package" {
28+
location = "us-west1"
29+
repository_id = "my-repository"
30+
}
31+
```
32+
33+
## Argument Reference
34+
35+
The following arguments are supported:
36+
37+
* `location` - (Required) The location of the artifact registry.
38+
39+
* `repository_id` - (Required) The last part of the repository name to fetch from.
40+
41+
* `name` - (Required) The name of the package.
42+
43+
* `project` - (Optional) The project ID in which the resource belongs. If it is not provided, the provider project is used.
44+
45+
## Attributes Reference
46+
47+
The following computed attributes are exported:
48+
49+
* `display_name` - The display name of the package.
50+
51+
* `create_time` - The time, as a RFC 3339 string, this package was created.
52+
53+
* `update_time` - The time, as a RFC 3339 string, this package was last updated. This includes publishing a new version of the package.
54+
55+
* `annotations` - Client specified annotations.

0 commit comments

Comments
 (0)