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