Skip to content

Commit 496b6fe

Browse files
sebasslashcatsby
andauthored
Data source no-code module (#1246)
* stub no-code module data-source * add data-source for no-code module * add changelog * remove unnecessary conversion * mark organization as just computed * add documentation for no-code module data source * remove unnecessary validation method * Rebased "data-source-module" onto a local branch --------- Co-authored-by: catsby <[email protected]>
1 parent 7c2a2be commit 496b6fe

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ FEATURES:
66
* **New Resource**: `r/tfe_registry_provider` is a new resource for managing public and private providers in the private registry, by @tmatilai [1185](https://github.com/hashicorp/terraform-provider-tfe/pull/1185)
77
* **New Data Source**: `d/tfe_registry_provider` is a new data source to retrieve information about a public or private provider in the private registry, by @tmatilai [1185](https://github.com/hashicorp/terraform-provider-tfe/pull/1185)
88
* **New Data Source**: `d/tfe_registry_providers` is a new data source to retrieve information about public and private providers in the private registry, by @tmatilai [1185](https://github.com/hashicorp/terraform-provider-tfe/pull/1185)
9+
* **New Data Source**: `d/tfe_no_code_module` is a new data source to retrieve information about a no-code module, by @catsby [1242](https://github.com/hashicorp/terraform-provider-tfe/pull/1242)
910
* **New Resource**: `r/tfe_sentinel_version` adds the ability for admins to configure settings for sentinel versions ([#1202](https://github.com/hashicorp/terraform-provider-tfe/pull/1202))
1011
* **New Resource**: `r/tfe_opa_version` adds the ability for admins to configure settings for OPA versions ([#1202](https://github.com/hashicorp/terraform-provider-tfe/pull/1202))
1112
* `r/tfe_policy_set`: Add `agent_enabled` and `policy_tool_version` attributes to allow setting a policy runtime version to the policy set, by @mrinalirao [1234](https://github.com/hashicorp/terraform-provider-tfe/pull/1234)
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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-framework/types"
14+
"github.com/hashicorp/terraform-plugin-log/tflog"
15+
)
16+
17+
// Ensure the implementation satisfies the expected interfaces.
18+
var (
19+
_ datasource.DataSource = &dataSourceTFENoCodeModule{}
20+
_ datasource.DataSourceWithConfigure = &dataSourceTFENoCodeModule{}
21+
)
22+
23+
// NewNoCodeModuleDataSource is a helper function to simplify the implementation.
24+
func NewNoCodeModuleDataSource() datasource.DataSource {
25+
return &dataSourceTFENoCodeModule{}
26+
}
27+
28+
// dataSourceTFENoCodeModule is the data source implementation.
29+
type dataSourceTFENoCodeModule struct {
30+
config ConfiguredClient
31+
}
32+
33+
// modelNoCodeModule maps the data source schema data.
34+
type modelNoCodeModule struct {
35+
ID types.String `tfsdk:"id"`
36+
Organization types.String `tfsdk:"organization"`
37+
Namespace types.String `tfsdk:"namespace"`
38+
VersionPin types.String `tfsdk:"version_pin"`
39+
RegistryModuleID types.String `tfsdk:"registry_module_id"`
40+
Enabled types.Bool `tfsdk:"enabled"`
41+
}
42+
43+
// Metadata returns the data source type name.
44+
func (d *dataSourceTFENoCodeModule) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
45+
resp.TypeName = req.ProviderTypeName + "_no_code_module"
46+
}
47+
48+
// Schema defines the schema for the data source.
49+
func (d *dataSourceTFENoCodeModule) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
50+
resp.Schema = schema.Schema{
51+
Description: "This data source can be used to retrieve a public or private no-code module.",
52+
Attributes: map[string]schema.Attribute{
53+
"id": schema.StringAttribute{
54+
Description: "ID of the no-code module.",
55+
Required: true,
56+
},
57+
"organization": schema.StringAttribute{
58+
Description: "Name of the organization.",
59+
Computed: true,
60+
},
61+
"namespace": schema.StringAttribute{
62+
Description: "The namespace of the no-code module.",
63+
Computed: true,
64+
},
65+
"registry_module_id": schema.StringAttribute{
66+
Description: "ID of the registry module.",
67+
Computed: true,
68+
},
69+
"version_pin": schema.StringAttribute{
70+
Description: "Version pin of the no-code module.",
71+
Computed: true,
72+
},
73+
"enabled": schema.BoolAttribute{
74+
Description: "Indiate if this no-code module is currently enabled.",
75+
Computed: true,
76+
},
77+
},
78+
}
79+
}
80+
81+
// Configure adds the provider configured client to the data source.
82+
func (d *dataSourceTFENoCodeModule) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
83+
if req.ProviderData == nil {
84+
return
85+
}
86+
87+
client, ok := req.ProviderData.(ConfiguredClient)
88+
if !ok {
89+
resp.Diagnostics.AddError(
90+
"Unexpected Data Source Configure Type",
91+
fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData),
92+
)
93+
94+
return
95+
}
96+
d.config = client
97+
}
98+
99+
// Read refreshes the Terraform state with the latest data.
100+
func (d *dataSourceTFENoCodeModule) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
101+
var data modelNoCodeModule
102+
103+
// Read Terraform configuration data into the model
104+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
105+
106+
if resp.Diagnostics.HasError() {
107+
return
108+
}
109+
110+
options := &tfe.RegistryNoCodeModuleReadOptions{
111+
Include: []tfe.RegistryNoCodeModuleIncludeOpt{tfe.RegistryNoCodeIncludeVariableOptions},
112+
}
113+
114+
tflog.Debug(ctx, "Reading no code module")
115+
module, err := d.config.Client.RegistryNoCodeModules.Read(ctx, data.ID.ValueString(), options)
116+
if err != nil {
117+
resp.Diagnostics.AddError("Unable to read no code module", err.Error())
118+
return
119+
}
120+
121+
data = modelFromTFENoCodeModule(module)
122+
123+
// Save data into Terraform state
124+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
125+
}
126+
func modelFromTFENoCodeModule(v *tfe.RegistryNoCodeModule) modelNoCodeModule {
127+
return modelNoCodeModule{
128+
ID: types.StringValue(v.ID),
129+
Organization: types.StringValue(v.Organization.Name),
130+
RegistryModuleID: types.StringValue(v.RegistryModule.ID),
131+
Namespace: types.StringValue(v.RegistryModule.Namespace),
132+
VersionPin: types.StringValue(v.VersionPin),
133+
Enabled: types.BoolValue(v.Enabled),
134+
}
135+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 TestAccTFENoCodeModuleDataSource_public(t *testing.T) {
16+
skipUnlessBeta(t)
17+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
18+
orgName := fmt.Sprintf("tst-terraform-%d", rInt)
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { testAccPreCheck(t) },
22+
ProtoV5ProviderFactories: testAccMuxedProviders,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccTFENoCodeModuleDataSourceConfig_public(rInt),
26+
Check: resource.ComposeAggregateTestCheckFunc(
27+
resource.TestCheckResourceAttrSet("data.tfe_no_code_module.foobar", "id"),
28+
resource.TestCheckResourceAttr("data.tfe_no_code_module.foobar", "organization", orgName),
29+
resource.TestCheckResourceAttr("data.tfe_no_code_module.foobar", "enabled", "true"),
30+
resource.TestCheckResourceAttrSet("data.tfe_no_code_module.foobar", "registry_module_id"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccTFENoCodeModuleDataSourceConfig_public(rInt int) string {
38+
return fmt.Sprintf(`
39+
%s
40+
41+
data "tfe_no_code_module" "foobar" {
42+
id = tfe_no_code_module.foobar.id
43+
}
44+
`, testAccTFENoCodeModule_basic(rInt))
45+
}

internal/provider/provider_next.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func (p *frameworkProvider) DataSources(ctx context.Context) []func() datasource
127127
NewRegistryGPGKeysDataSource,
128128
NewRegistryProviderDataSource,
129129
NewRegistryProvidersDataSource,
130+
NewNoCodeModuleDataSource,
130131
NewSAMLSettingsDataSource,
131132
}
132133
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: "tfe"
3+
page_title: "Terraform Enterprise: tfe_no_code_module"
4+
description: |-
5+
Get information on a no-code module.
6+
---
7+
8+
# Data Source: tfe_registry_provider
9+
10+
Use this data source to read the details of an existing No-Code-Allowed module.
11+
12+
## Example Usage
13+
14+
```hcl
15+
resource "tfe_no_code_module" "foobar" {
16+
organization = tfe_organization.foobar.id
17+
registry_module = tfe_registry_module.foobar.id
18+
}
19+
20+
data "tfe_no_code_module" "foobar" {
21+
id = tfe_no_code_module.foobar.id
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
The following arguments are supported:
28+
29+
* `id` - (Required) ID of the no-code module.
30+
31+
## Attributes Reference
32+
33+
* `id` - ID of the no-code module.
34+
* `organization` - Organization name that the no-code module belongs to.
35+
* `namespace` - Namespace name that the no-code module belongs to.
36+
* `registry_module_id` - ID of the registry module for the no-code module.
37+
* `version_pin` - Version number the no-code module is pinned to.
38+
* `enabled` - Indicates if this no-code module is currently enabled

0 commit comments

Comments
 (0)