|
| 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 | +} |
0 commit comments