Skip to content

Commit ef4bfbd

Browse files
Add plural data source for retrieving Artifact Registry Python packages (#14658) (#11017)
[upstream:33b3e2713183f26f7603bd5a0a071d1dbde8179d] Signed-off-by: Modular Magician <[email protected]>
1 parent 9ee5590 commit ef4bfbd

File tree

5 files changed

+301
-0
lines changed

5 files changed

+301
-0
lines changed

.changelog/14658.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_python_packages`
3+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
211211
"google_artifact_registry_package": artifactregistry.DataSourceArtifactRegistryPackage(),
212212
"google_artifact_registry_packages": artifactregistry.DataSourceArtifactRegistryPackages(),
213213
"google_artifact_registry_python_package": artifactregistry.DataSourceArtifactRegistryPythonPackage(),
214+
"google_artifact_registry_python_packages": artifactregistry.DataSourceArtifactRegistryPythonPackages(),
214215
"google_artifact_registry_repositories": artifactregistry.DataSourceArtifactRegistryRepositories(),
215216
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
216217
"google_artifact_registry_tag": artifactregistry.DataSourceArtifactRegistryTag(),
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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_python_packages.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+
"strings"
24+
25+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
26+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
27+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
28+
)
29+
30+
func DataSourceArtifactRegistryPythonPackages() *schema.Resource {
31+
return &schema.Resource{
32+
Read: dataSourceArtifactRegistryPythonPackagesRead,
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+
"project": {
43+
Type: schema.TypeString,
44+
Optional: true,
45+
},
46+
"python_packages": {
47+
Type: schema.TypeList,
48+
Computed: true,
49+
Elem: &schema.Resource{
50+
Schema: map[string]*schema.Schema{
51+
"package_name": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
"name": {
56+
Type: schema.TypeString,
57+
Computed: true,
58+
},
59+
"version": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
"create_time": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
},
67+
"update_time": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
},
71+
},
72+
},
73+
},
74+
},
75+
}
76+
}
77+
78+
func dataSourceArtifactRegistryPythonPackagesRead(d *schema.ResourceData, meta interface{}) error {
79+
config := meta.(*transport_tpg.Config)
80+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
81+
if err != nil {
82+
return err
83+
}
84+
85+
project, err := tpgresource.GetProject(d, config)
86+
if err != nil {
87+
return err
88+
}
89+
90+
basePath, err := tpgresource.ReplaceVars(d, config, "{{ArtifactRegistryBasePath}}")
91+
if err != nil {
92+
return fmt.Errorf("Error setting Artifact Registry base path: %s", err)
93+
}
94+
95+
resourcePath, err := tpgresource.ReplaceVars(d, config, fmt.Sprintf("projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}/pythonPackages"))
96+
if err != nil {
97+
return fmt.Errorf("Error setting resource path: %s", err)
98+
}
99+
100+
urlRequest := basePath + resourcePath
101+
102+
headers := make(http.Header)
103+
pythonPackages := make([]map[string]interface{}, 0)
104+
pageToken := ""
105+
106+
for {
107+
u, err := url.Parse(urlRequest)
108+
if err != nil {
109+
return fmt.Errorf("Error parsing URL: %s", err)
110+
}
111+
112+
q := u.Query()
113+
if pageToken != "" {
114+
q.Set("pageToken", pageToken)
115+
}
116+
u.RawQuery = q.Encode()
117+
118+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
119+
Config: config,
120+
Method: "GET",
121+
RawURL: u.String(),
122+
UserAgent: userAgent,
123+
Headers: headers,
124+
})
125+
126+
if err != nil {
127+
return fmt.Errorf("Error listing Artifact Registry Python packages: %s", err)
128+
}
129+
130+
if items, ok := res["pythonPackages"].([]interface{}); ok {
131+
for _, item := range items {
132+
pkg := item.(map[string]interface{})
133+
134+
name, ok := pkg["name"].(string)
135+
if !ok {
136+
return fmt.Errorf("Error getting Artifact Registry Python package name: %s", err)
137+
}
138+
139+
lastComponent := name[strings.LastIndex(name, "/")+1:]
140+
packageName := strings.SplitN(lastComponent, ":", 2)[0]
141+
142+
getString := func(m map[string]interface{}, key string) string {
143+
if v, ok := m[key].(string); ok {
144+
return v
145+
}
146+
return ""
147+
}
148+
149+
pythonPackages = append(pythonPackages, map[string]interface{}{
150+
"package_name": packageName,
151+
"name": name,
152+
"version": getString(pkg, "version"),
153+
"create_time": getString(pkg, "createTime"),
154+
"update_time": getString(pkg, "updateTime"),
155+
})
156+
}
157+
}
158+
159+
if nextToken, ok := res["nextPageToken"].(string); ok && nextToken != "" {
160+
pageToken = nextToken
161+
} else {
162+
break
163+
}
164+
}
165+
166+
if err := d.Set("project", project); err != nil {
167+
return fmt.Errorf("Error setting project: %s", err)
168+
}
169+
170+
if err := d.Set("python_packages", pythonPackages); err != nil {
171+
return fmt.Errorf("Error setting Artifact Registry Python packages: %s", err)
172+
}
173+
174+
d.SetId(resourcePath)
175+
176+
return nil
177+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_python_packages_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 TestAccDataSourceArtifactRegistryPythonPackages_basic(t *testing.T) {
27+
t.Parallel()
28+
29+
// At the moment there are no public Python packages available in Artifact Registry.
30+
// This test is skipped to avoid unnecessary failures.
31+
// As soon as there are public packages available, this test can be enabled by removing the skip and adjusting the configuration accordingly.
32+
t.Skip("No public Python packages 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: testAccDataSourceArtifactRegistryPythonPackagesConfig,
40+
Check: resource.ComposeTestCheckFunc(
41+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_python_packages.test", "project"),
42+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_python_packages.test", "location"),
43+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_python_packages.test", "repository_id"),
44+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_python_packages.test", "python_packages.0.package_name"),
45+
resource.TestCheckResourceAttrSet("data.google_artifact_registry_python_packages.test", "python_packages.0.name"),
46+
),
47+
},
48+
},
49+
})
50+
}
51+
52+
const testAccDataSourceArtifactRegistryPythonPackagesConfig = `
53+
data "google_artifact_registry_python_packages" "test" {
54+
project = "example-project"
55+
location = "us"
56+
repository_id = "example-repo"
57+
}
58+
`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_python_packages.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 Python packages within a Google Artifact Registry repository.
19+
---
20+
21+
# google_artifact_registry_python_packages
22+
23+
Get information about Artifact Registry Python packages.
24+
See [the official documentation](https://cloud.google.com/artifact-registry/docs/python)
25+
and [API](https://cloud.google.com/artifact-registry/docs/reference/rest/v1/projects.locations.repositories.pythonPackages/list).
26+
27+
## Example Usage
28+
29+
```hcl
30+
data "google_artifact_registry_python_packages" "my_packages" {
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+
* `python_packages` - A list of all retrieved Artifact Registry Python packages. Structure is [defined below](#nested_python_packages).
51+
52+
<a name="nested_python_packages"></a>The `python_packages` block supports:
53+
54+
* `name` - The fully qualified name of the fetched package. This name has the form: `projects/{{project}}/locations/{{location}}/repository/{{repository_id}}/pythonPackages/{{pythonPackage}}`. For example, `projects/example-project/locations/us-central1/repository/example-repo/pythonPackages/my-test-package:0.0.1`
55+
56+
* `package_name` - Extracted short name of the package (last part of `name`, without version). For example, from `.../my-test-package:0.0.1``my-test-package`.
57+
58+
* `version` - Version of this package.
59+
60+
* `create_time` - The time, as a RFC 3339 string, this package was created.
61+
62+
* `update_time` - The time, as a RFC 3339 string, this package was updated.

0 commit comments

Comments
 (0)