Skip to content

Commit 7121fd5

Browse files
tmatilaibrandonc
authored andcommitted
Add tfe_registry_gpg_key data source
Retrieves a private registry GPG key.
1 parent 2ac26bd commit 7121fd5

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ FEATURES:
1414
* **New Resource**: `r/tfe_organization_default_execution_mode` is a new resource to set the `default_execution_mode` and `default_agent_pool_id` for an organization, by @SwiftEngineer [1137](https://github.com/hashicorp/terraform-provider-tfe/pull/1137)'
1515
* `r/tfe_workspace`: Now uses the organization's `default_execution_mode` and `default_agent_pool_id` as the default `execution_mode`, by @SwiftEngineer [1137](https://github.com/hashicorp/terraform-provider-tfe/pull/1137)'
1616
* **New Resource**: `r/tfe_registry_gpg_key` is a new resource for managing private registry GPG keys, by @tmatilai
17+
* **New Data Source**: `d/tfe_registry_gpg_key` is a new data source to retrieve a private registry GPG key, by @tmatilai
1718

1819
ENHANCEMENTS:
1920
* `d/tfe_organization`: Make `name` argument optional if configured for the provider, by @tmatilai [1133](https://github.com/hashicorp/terraform-provider-tfe/pull/1133)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/hashicorp/go-tfe"
11+
"github.com/hashicorp/terraform-plugin-framework/datasource"
12+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
13+
"github.com/hashicorp/terraform-plugin-log/tflog"
14+
)
15+
16+
// Ensure the implementation satisfies the expected interfaces.
17+
var (
18+
_ datasource.DataSource = &dataSourceTFERegistryGPGKey{}
19+
_ datasource.DataSourceWithConfigure = &dataSourceTFERegistryGPGKey{}
20+
)
21+
22+
// NewRegistryGPGKeyDataSource is a helper function to simplify the provider implementation.
23+
func NewRegistryGPGKeyDataSource() datasource.DataSource {
24+
return &dataSourceTFERegistryGPGKey{}
25+
}
26+
27+
// dataSourceTFERegistryGPGKey is the data source implementation.
28+
type dataSourceTFERegistryGPGKey struct {
29+
config ConfiguredClient
30+
}
31+
32+
// Metadata returns the data source type name.
33+
func (d *dataSourceTFERegistryGPGKey) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
34+
resp.TypeName = req.ProviderTypeName + "_registry_gpg_key"
35+
}
36+
37+
// Schema defines the schema for the data source.
38+
func (d *dataSourceTFERegistryGPGKey) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
39+
resp.Schema = schema.Schema{
40+
Description: "This data source can be used to retrieve a private registry GPG key.",
41+
Attributes: map[string]schema.Attribute{
42+
"id": schema.StringAttribute{
43+
Required: true,
44+
},
45+
"organization": schema.StringAttribute{
46+
Description: "Name of the organization. If omitted, organization must be defined in the provider config.",
47+
Optional: true,
48+
Computed: true,
49+
},
50+
"ascii_armor": schema.StringAttribute{
51+
Description: "ASCII-armored representation of the GPG key.",
52+
Computed: true,
53+
},
54+
"created_at": schema.StringAttribute{
55+
Description: "The time when the GPG key was created.",
56+
Computed: true,
57+
},
58+
"updated_at": schema.StringAttribute{
59+
Description: "The time when the GPG key was last updated.",
60+
Computed: true,
61+
},
62+
},
63+
}
64+
}
65+
66+
// Configure adds the provider configured client to the data source.
67+
func (d *dataSourceTFERegistryGPGKey) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
68+
if req.ProviderData == nil {
69+
return
70+
}
71+
72+
client, ok := req.ProviderData.(ConfiguredClient)
73+
if !ok {
74+
resp.Diagnostics.AddError(
75+
"Unexpected Data Source Configure Type",
76+
fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData),
77+
)
78+
79+
return
80+
}
81+
d.config = client
82+
}
83+
84+
// Read refreshes the Terraform state with the latest data.
85+
func (d *dataSourceTFERegistryGPGKey) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
86+
var data modelTFERegistryGPGKey
87+
88+
// Read Terraform configuration data into the model
89+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
90+
91+
if resp.Diagnostics.HasError() {
92+
return
93+
}
94+
95+
var organization string
96+
resp.Diagnostics.Append(d.config.dataOrDefaultOrganization(ctx, req.Config, &organization)...)
97+
98+
if resp.Diagnostics.HasError() {
99+
return
100+
}
101+
102+
keyID := tfe.GPGKeyID{
103+
RegistryName: "private",
104+
Namespace: organization,
105+
KeyID: data.ID.ValueString(),
106+
}
107+
108+
tflog.Debug(ctx, "Reading private registry GPG key")
109+
key, err := d.config.Client.GPGKeys.Read(ctx, keyID)
110+
if err != nil {
111+
resp.Diagnostics.AddError("Unable to read private registry GPG key", err.Error())
112+
return
113+
}
114+
115+
data = modelFromTFEVGPGKey(key)
116+
117+
// Save data into Terraform state
118+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
119+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"fmt"
8+
"math/rand"
9+
"testing"
10+
"time"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13+
)
14+
15+
func TestAccTFERegistryGPGKeyDataSource_basic(t *testing.T) {
16+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
17+
orgName := fmt.Sprintf("tst-terraform-%d", rInt)
18+
19+
resource.Test(t, resource.TestCase{
20+
PreCheck: func() { testAccPreCheck(t) },
21+
ProtoV5ProviderFactories: testAccMuxedProviders,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccTFERegistryGPGKeyDataSourceConfig(orgName),
25+
Check: resource.ComposeAggregateTestCheckFunc(
26+
resource.TestCheckResourceAttr("data.tfe_registry_gpg_key.foobar", "organization", orgName),
27+
resource.TestCheckResourceAttrSet("data.tfe_registry_gpg_key.foobar", "id"),
28+
resource.TestCheckResourceAttrSet("data.tfe_registry_gpg_key.foobar", "ascii_armor"),
29+
resource.TestCheckResourceAttrSet("data.tfe_registry_gpg_key.foobar", "created_at"),
30+
resource.TestCheckResourceAttrSet("data.tfe_registry_gpg_key.foobar", "updated_at")),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccTFERegistryGPGKeyDataSourceConfig(orgName string) string {
37+
return fmt.Sprintf(`
38+
%s
39+
40+
data "tfe_registry_gpg_key" "foobar" {
41+
organization = tfe_organization.foobar.name
42+
43+
id = tfe_registry_gpg_key.foobar.id
44+
}
45+
`, testAccTFERegistryGPGKeyResourceConfig(orgName))
46+
}

internal/provider/provider_next.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func (p *frameworkProvider) Configure(ctx context.Context, req provider.Configur
113113

114114
func (p *frameworkProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
115115
return []func() datasource.DataSource{
116+
NewRegistryGPGKeyDataSource,
116117
NewSAMLSettingsDataSource,
117118
}
118119
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: "tfe"
3+
page_title: "Terraform Enterprise: tfe_registry_gpg_key"
4+
description: |-
5+
Get information on a private registry GPG key.
6+
---
7+
8+
# Data Source: tfe_registry_gpg_key
9+
10+
Use this data source to get information about a private registry GPG key.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "tfe_registry_gpg_key" "example" {
16+
organization = "my-org-name"
17+
id = "13DFECCA3B58CE4A"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
* `id` - (Required) ID of the GPG key.
26+
* `organization` - (Optional) Name of the organization. If omitted, organization must be defined in the provider config.
27+
28+
## Attributes Reference
29+
30+
* `ascii_armor` - ASCII-armored representation of the GPG key.
31+
* `created_at` - The time when the GPG key was created.
32+
* `updated_at` - The time when the GPG key was last updated.

0 commit comments

Comments
 (0)