Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data source no-code module #1242

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ FEATURES:
* **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)
* **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)
* **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)
* **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)
* **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))
* **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))

Expand Down
135 changes: 135 additions & 0 deletions internal/provider/data_source_no_code_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"
"fmt"

"github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &dataSourceTFENoCodeModule{}
_ datasource.DataSourceWithConfigure = &dataSourceTFENoCodeModule{}
)

// NewNoCodeModuleDataSource is a helper function to simplify the implementation.
func NewNoCodeModuleDataSource() datasource.DataSource {
return &dataSourceTFENoCodeModule{}
}

// dataSourceTFENoCodeModule is the data source implementation.
type dataSourceTFENoCodeModule struct {
config ConfiguredClient
}

// modelNoCodeModule maps the data source schema data.
type modelNoCodeModule struct {
ID types.String `tfsdk:"id"`
Organization types.String `tfsdk:"organization"`
Namespace types.String `tfsdk:"namespace"`
VersionPin types.String `tfsdk:"version_pin"`
RegistryModuleID types.String `tfsdk:"registry_module_id"`
Enabled types.Bool `tfsdk:"enabled"`
}

// Metadata returns the data source type name.
func (d *dataSourceTFENoCodeModule) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_no_code_module"
}

// Schema defines the schema for the data source.
func (d *dataSourceTFENoCodeModule) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "This data source can be used to retrieve a public or private no-code module.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "ID of the no-code module.",
Required: true,
},
"organization": schema.StringAttribute{
Description: "Name of the organization.",
Computed: true,
},
"namespace": schema.StringAttribute{
Description: "The namespace of the no-code module.",
Computed: true,
},
"registry_module_id": schema.StringAttribute{
Description: "ID of the registry module.",
Computed: true,
},
"version_pin": schema.StringAttribute{
Description: "Version pin of the no-code module.",
Computed: true,
},
"enabled": schema.BoolAttribute{
Description: "Indiate if this no-code module is currently enabled.",
Computed: true,
},
},
}
}

// Configure adds the provider configured client to the data source.
func (d *dataSourceTFENoCodeModule) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(ConfiguredClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData),
)

return
}
d.config = client
}

// Read refreshes the Terraform state with the latest data.
func (d *dataSourceTFENoCodeModule) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data modelNoCodeModule

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

options := &tfe.RegistryNoCodeModuleReadOptions{
Include: []tfe.RegistryNoCodeModuleIncludeOpt{tfe.RegistryNoCodeIncludeVariableOptions},
}

tflog.Debug(ctx, "Reading no code module")
module, err := d.config.Client.RegistryNoCodeModules.Read(ctx, data.ID.ValueString(), options)
if err != nil {
resp.Diagnostics.AddError("Unable to read no code module", err.Error())
return
}

data = modelFromTFENoCodeModule(module)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func modelFromTFENoCodeModule(v *tfe.RegistryNoCodeModule) modelNoCodeModule {
return modelNoCodeModule{
ID: types.StringValue(v.ID),
Organization: types.StringValue(v.Organization.Name),
RegistryModuleID: types.StringValue(v.RegistryModule.ID),
Namespace: types.StringValue(v.RegistryModule.Namespace),
VersionPin: types.StringValue(v.VersionPin),
Enabled: types.BoolValue(v.Enabled),
}
}
45 changes: 45 additions & 0 deletions internal/provider/data_source_no_code_module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccTFENoCodeModuleDataSource_public(t *testing.T) {
skipUnlessBeta(t)
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccMuxedProviders,
Steps: []resource.TestStep{
{
Config: testAccTFENoCodeModuleDataSourceConfig_public(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.tfe_no_code_module.foobar", "id"),
resource.TestCheckResourceAttr("data.tfe_no_code_module.foobar", "organization", orgName),
resource.TestCheckResourceAttr("data.tfe_no_code_module.foobar", "enabled", "true"),
resource.TestCheckResourceAttrSet("data.tfe_no_code_module.foobar", "registry_module_id"),
),
},
},
})
}

func testAccTFENoCodeModuleDataSourceConfig_public(rInt int) string {
return fmt.Sprintf(`
%s

data "tfe_no_code_module" "foobar" {
id = tfe_no_code_module.foobar.id
}
`, testAccTFENoCodeModule_basic(rInt))
}
1 change: 1 addition & 0 deletions internal/provider/provider_next.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (p *frameworkProvider) DataSources(ctx context.Context) []func() datasource
NewRegistryGPGKeysDataSource,
NewRegistryProviderDataSource,
NewRegistryProvidersDataSource,
NewNoCodeModuleDataSource,
NewSAMLSettingsDataSource,
}
}
Expand Down
38 changes: 38 additions & 0 deletions website/docs/d/no_code_module.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: "tfe"
page_title: "Terraform Enterprise: tfe_no_code_module"
description: |-
Get information on a no-code module.
---

# Data Source: tfe_registry_provider

Use this data source to read the details of an existing No-Code-Allowed module.

## Example Usage

```hcl
resource "tfe_no_code_module" "foobar" {
organization = tfe_organization.foobar.id
registry_module = tfe_registry_module.foobar.id
}

data "tfe_no_code_module" "foobar" {
id = tfe_no_code_module.foobar.id
}
```

## Argument Reference

The following arguments are supported:

* `id` - (Required) ID of the no-code module.

## Attributes Reference

* `id` - ID of the no-code module.
* `organization` - Organization name that the no-code module belongs to.
* `namespace` - Namespace name that the no-code module belongs to.
* `registry_module_id` - ID of the registry module for the no-code module.
* `version_pin` - Version number the no-code module is pinned to.
* `enabled` - Indicates if this no-code module is currently enabled
Loading