|
| 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/attr" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 16 | +) |
| 17 | + |
| 18 | +// Ensure the implementation satisfies the expected interfaces. |
| 19 | +var ( |
| 20 | + _ datasource.DataSource = &dataSourceTFERegistryGPGKeys{} |
| 21 | + _ datasource.DataSourceWithConfigure = &dataSourceTFERegistryGPGKeys{} |
| 22 | +) |
| 23 | + |
| 24 | +// NewRegistryGPGKeysDataSource is a helper function to simplify the provider implementation. |
| 25 | +func NewRegistryGPGKeysDataSource() datasource.DataSource { |
| 26 | + return &dataSourceTFERegistryGPGKeys{} |
| 27 | +} |
| 28 | + |
| 29 | +// dataSourceTFERegistryGPGKeys is the data source implementation. |
| 30 | +type dataSourceTFERegistryGPGKeys struct { |
| 31 | + config ConfiguredClient |
| 32 | +} |
| 33 | + |
| 34 | +// modelTFERegistryGPGKeys maps the data source schema data. |
| 35 | +type modelTFERegistryGPGKeys struct { |
| 36 | + ID types.String `tfsdk:"id"` |
| 37 | + Organization types.String `tfsdk:"organization"` |
| 38 | + Keys []modelTFERegistryGPGKey `tfsdk:"keys"` |
| 39 | +} |
| 40 | + |
| 41 | +// Metadata returns the data source type name. |
| 42 | +func (d *dataSourceTFERegistryGPGKeys) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 43 | + resp.TypeName = req.ProviderTypeName + "_registry_gpg_keys" |
| 44 | +} |
| 45 | + |
| 46 | +// Schema defines the schema for the data source. |
| 47 | +func (d *dataSourceTFERegistryGPGKeys) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 48 | + resp.Schema = schema.Schema{ |
| 49 | + Description: "This data source can be used to retrieve all private registry GPG keys of an organization.", |
| 50 | + Attributes: map[string]schema.Attribute{ |
| 51 | + "id": schema.StringAttribute{ |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "organization": schema.StringAttribute{ |
| 55 | + Description: "Name of the organization. If omitted, organization must be defined in the provider config.", |
| 56 | + Optional: true, |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + "keys": schema.ListAttribute{ |
| 60 | + Description: "List of GPG keys in the organization.", |
| 61 | + Computed: true, |
| 62 | + ElementType: types.ObjectType{ |
| 63 | + AttrTypes: map[string]attr.Type{ |
| 64 | + "id": types.StringType, |
| 65 | + "organization": types.StringType, |
| 66 | + "ascii_armor": types.StringType, |
| 67 | + "created_at": types.StringType, |
| 68 | + "updated_at": types.StringType, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Configure adds the provider configured client to the data source. |
| 77 | +func (d *dataSourceTFERegistryGPGKeys) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 78 | + if req.ProviderData == nil { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + client, ok := req.ProviderData.(ConfiguredClient) |
| 83 | + if !ok { |
| 84 | + resp.Diagnostics.AddError( |
| 85 | + "Unexpected Data Source Configure Type", |
| 86 | + fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), |
| 87 | + ) |
| 88 | + |
| 89 | + return |
| 90 | + } |
| 91 | + d.config = client |
| 92 | +} |
| 93 | + |
| 94 | +// Read refreshes the Terraform state with the latest data. |
| 95 | +func (d *dataSourceTFERegistryGPGKeys) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 96 | + var data modelTFERegistryGPGKeys |
| 97 | + |
| 98 | + // Read Terraform configuration data into the model |
| 99 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 100 | + |
| 101 | + if resp.Diagnostics.HasError() { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + var organization string |
| 106 | + resp.Diagnostics.Append(d.config.dataOrDefaultOrganization(ctx, req.Config, &organization)...) |
| 107 | + |
| 108 | + if resp.Diagnostics.HasError() { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + options := tfe.GPGKeyListOptions{ |
| 113 | + Namespaces: []string{organization}, |
| 114 | + } |
| 115 | + tflog.Debug(ctx, "Listing private registry GPG keys") |
| 116 | + keyList, err := d.config.Client.GPGKeys.ListPrivate(ctx, options) |
| 117 | + if err != nil { |
| 118 | + resp.Diagnostics.AddError("Unable to list private registry GPG keys", err.Error()) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + data.ID = types.StringValue(organization) |
| 123 | + data.Organization = types.StringValue(organization) |
| 124 | + data.Keys = []modelTFERegistryGPGKey{} |
| 125 | + |
| 126 | + for { |
| 127 | + for _, key := range keyList.Items { |
| 128 | + data.Keys = append(data.Keys, modelFromTFEVGPGKey(key)) |
| 129 | + } |
| 130 | + |
| 131 | + if keyList.CurrentPage >= keyList.TotalPages { |
| 132 | + break |
| 133 | + } |
| 134 | + options.PageNumber = keyList.NextPage |
| 135 | + |
| 136 | + tflog.Debug(ctx, "Listing private registry GPG keys") |
| 137 | + keyList, err = d.config.Client.GPGKeys.ListPrivate(ctx, options) |
| 138 | + if err != nil { |
| 139 | + resp.Diagnostics.AddError("Unable to list private registry GPG keys", err.Error()) |
| 140 | + return |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Save data into Terraform state |
| 145 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 146 | +} |
0 commit comments