Skip to content

Commit 24ed7f4

Browse files
Add plural data source for retrieving Maven artifacts from an Artifact Registry repository (#14785) (#10785)
[upstream:da70afafbae467d80cef35d81a91b93a4f32c3e0] Signed-off-by: Modular Magician <[email protected]>
1 parent c7955ac commit 24ed7f4

File tree

5 files changed

+315
-0
lines changed

5 files changed

+315
-0
lines changed

.changelog/14785.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_maven_artifacts`
3+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
202202
"google_artifact_registry_docker_images": artifactregistry.DataSourceArtifactRegistryDockerImages(),
203203
"google_artifact_registry_locations": artifactregistry.DataSourceGoogleArtifactRegistryLocations(),
204204
"google_artifact_registry_maven_artifact": artifactregistry.DataSourceArtifactRegistryMavenArtifact(),
205+
"google_artifact_registry_maven_artifacts": artifactregistry.DataSourceArtifactRegistryMavenArtifacts(),
205206
"google_artifact_registry_npm_package": artifactregistry.DataSourceArtifactRegistryNpmPackage(),
206207
"google_artifact_registry_npm_packages": artifactregistry.DataSourceArtifactRegistryNpmPackages(),
207208
"google_artifact_registry_package": artifactregistry.DataSourceArtifactRegistryPackage(),
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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_maven_artifacts.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 DataSourceArtifactRegistryMavenArtifacts() *schema.Resource {
30+
return &schema.Resource{
31+
Read: dataSourceArtifactRegistryMavenArtifactsRead,
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+
"project": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
},
45+
"maven_artifacts": {
46+
Type: schema.TypeList,
47+
Computed: true,
48+
Elem: &schema.Resource{
49+
Schema: map[string]*schema.Schema{
50+
"name": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
},
54+
"group_id": {
55+
Type: schema.TypeString,
56+
Required: true,
57+
},
58+
"artifact_id": {
59+
Type: schema.TypeString,
60+
Required: true,
61+
},
62+
"pom_uri": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
},
66+
"version": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
},
70+
"create_time": {
71+
Type: schema.TypeString,
72+
Computed: true,
73+
},
74+
"update_time": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
},
78+
},
79+
},
80+
},
81+
},
82+
}
83+
}
84+
85+
func dataSourceArtifactRegistryMavenArtifactsRead(d *schema.ResourceData, meta interface{}) error {
86+
config := meta.(*transport_tpg.Config)
87+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
88+
if err != nil {
89+
return err
90+
}
91+
92+
project, err := tpgresource.GetProject(d, config)
93+
if err != nil {
94+
return err
95+
}
96+
97+
basePath, err := tpgresource.ReplaceVars(d, config, "{{ArtifactRegistryBasePath}}")
98+
if err != nil {
99+
return fmt.Errorf("Error setting Artifact Registry base path: %s", err)
100+
}
101+
102+
resourcePath, err := tpgresource.ReplaceVars(d, config, fmt.Sprintf("projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}/mavenArtifacts"))
103+
if err != nil {
104+
return fmt.Errorf("Error setting resource path: %s", err)
105+
}
106+
107+
urlRequest := basePath + resourcePath
108+
109+
headers := make(http.Header)
110+
mavenArtifacts := make([]map[string]interface{}, 0)
111+
pageToken := ""
112+
113+
for {
114+
u, err := url.Parse(urlRequest)
115+
if err != nil {
116+
return fmt.Errorf("Error parsing URL: %s", err)
117+
}
118+
119+
q := u.Query()
120+
if pageToken != "" {
121+
q.Set("pageToken", pageToken)
122+
}
123+
u.RawQuery = q.Encode()
124+
125+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
126+
Config: config,
127+
Method: "GET",
128+
RawURL: u.String(),
129+
UserAgent: userAgent,
130+
Headers: headers,
131+
})
132+
133+
if err != nil {
134+
return fmt.Errorf("Error listing Artifact Registry Maven artifacts: %s", err)
135+
}
136+
137+
if items, ok := res["mavenArtifacts"].([]interface{}); ok {
138+
for _, item := range items {
139+
pkg := item.(map[string]interface{})
140+
141+
name, ok := pkg["name"].(string)
142+
if !ok {
143+
return fmt.Errorf("Error getting Artifact Registry Maven artifact name: %s", err)
144+
}
145+
146+
getString := func(m map[string]interface{}, key string) string {
147+
if v, ok := m[key].(string); ok {
148+
return v
149+
}
150+
return ""
151+
}
152+
153+
mavenArtifacts = append(mavenArtifacts, map[string]interface{}{
154+
"name": name,
155+
"group_id": getString(pkg, "groupId"),
156+
"artifact_id": getString(pkg, "artifactId"),
157+
"pom_uri": getString(pkg, "pomUri"),
158+
"version": getString(pkg, "version"),
159+
"create_time": getString(pkg, "createTime"),
160+
"update_time": getString(pkg, "updateTime"),
161+
})
162+
}
163+
}
164+
165+
if nextToken, ok := res["nextPageToken"].(string); ok && nextToken != "" {
166+
pageToken = nextToken
167+
} else {
168+
break
169+
}
170+
}
171+
172+
if err := d.Set("project", project); err != nil {
173+
return fmt.Errorf("Error setting project: %s", err)
174+
}
175+
176+
if err := d.Set("maven_artifacts", mavenArtifacts); err != nil {
177+
return fmt.Errorf("Error setting Artifact Registry Maven artifacts: %s", err)
178+
}
179+
180+
d.SetId(resourcePath)
181+
182+
return nil
183+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_maven_artifacts_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 TestAccDataSourceArtifactRegistryMavenArtifacts_basic(t *testing.T) {
27+
t.Parallel()
28+
29+
// At the moment there are no public Maven artifacts available in Artifact Registry.
30+
// This test is skipped to avoid unnecessary failures.
31+
// As soon as there are public artifacts available, this test can be enabled by removing the skip and adjusting the configuration accordingly.
32+
t.Skip("No public Maven artifacts available in Artifact Registry")
33+
34+
acctest.VcrTest(t, resource.TestCase{
35+
PreCheck: func() { acctest.AccTestPreCheck(t) },
36+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
37+
Steps: []resource.TestStep{
38+
{
39+
Config: testAccDataSourceArtifactRegistryMavenArtifactsConfig,
40+
Check: resource.ComposeTestCheckFunc(
41+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_maven_artifacts.test", "project"),
42+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_maven_artifacts.test", "location"),
43+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_maven_artifacts.test", "repository_id"),
44+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_maven_artifacts.test", "maven_artifacts.0.artifact_id"),
45+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_maven_artifacts.test", "maven_artifacts.0.group_id"),
46+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_maven_artifacts.test", "maven_artifacts.0.name"),
47+
),
48+
},
49+
},
50+
})
51+
}
52+
53+
const testAccDataSourceArtifactRegistryMavenArtifactsConfig = `
54+
data "google_artifact_registry_maven_artifacts" "test" {
55+
project = "example-project"
56+
location = "us"
57+
repository_id = "example-repo"
58+
}
59+
`
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_maven_artifacts.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 Maven artifacts within a Google Artifact Registry repository.
19+
---
20+
21+
# google_artifact_registry_maven_artifacts
22+
23+
Get information about Artifact Registry Maven artifacts.
24+
See [the official documentation](https://cloud.google.com/artifact-registry/docs/java)
25+
and [API](https://cloud.google.com/artifact-registry/docs/reference/rest/v1/projects.locations.repositories.mavenArtifacts/list).
26+
27+
## Example Usage
28+
29+
```hcl
30+
data "google_artifact_registry_maven_artifacts" "my_artifacts" {
31+
location = "us-central1"
32+
repository_id = "example-repo"
33+
}
34+
```
35+
36+
## Argument Reference
37+
38+
The following arguments are supported:
39+
40+
* `location` - (Required) The location of the Artifact Registry repository.
41+
42+
* `repository_id` - (Required) The last part of the repository name to fetch from.
43+
44+
* `project` - (Optional) The project ID in which the resource belongs. If it is not provided, the provider project is used.
45+
46+
## Attributes Reference
47+
48+
The following attributes are exported:
49+
50+
* `maven_artifacts` - A list of all retrieved Artifact Registry Maven artifacts. Structure is [defined below](#nested_maven_artifacts).
51+
52+
<a name="nested_maven_artifacts"></a>The `maven_artifacts` block supports:
53+
54+
* `name` – The fully qualified name of the fetched artifact. Format:
55+
```
56+
projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}/mavenArtifacts/{{group_id}}:{{artifact_id}}:{{version}}
57+
```
58+
59+
* `group_id` – Group ID for the artifact.
60+
61+
* `artifact_id` – The name of the artifact to fetch.
62+
63+
* `pom_uri` – URL to access the pom file of the artifact.
64+
65+
* `version` – The version of the Maven artifact.
66+
67+
* `create_time` – The time the artifact was created.
68+
69+
* `update_time` – The time the artifact was last updated.

0 commit comments

Comments
 (0)