Skip to content

Commit da70afa

Browse files
authored
Add plural data source for retrieving Maven artifacts from an Artifact Registry repository (#14785)
1 parent 42540ef commit da70afa

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed

mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl

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

0 commit comments

Comments
 (0)