|
| 1 | +package fwprovider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | +) |
| 12 | + |
| 13 | +// Interface assertions for EphemeralAPIKeyResource |
| 14 | +var ( |
| 15 | + _ ephemeral.EphemeralResource = &EphemeralAPIKeyResource{} |
| 16 | + _ ephemeral.EphemeralResourceWithConfigure = &EphemeralAPIKeyResource{} |
| 17 | +) |
| 18 | + |
| 19 | +// EphemeralAPIKeyResource implements ephemeral API key resource |
| 20 | +type EphemeralAPIKeyResource struct { |
| 21 | + Api *datadogV2.KeyManagementApi |
| 22 | + Auth context.Context |
| 23 | +} |
| 24 | + |
| 25 | +// EphemeralAPIKeyModel represents the data model for the ephemeral API key resource |
| 26 | +type EphemeralAPIKeyModel struct { |
| 27 | + ID types.String `tfsdk:"id"` |
| 28 | + Name types.String `tfsdk:"name"` |
| 29 | + Key types.String `tfsdk:"key"` |
| 30 | + RemoteConfigReadEnabled types.Bool `tfsdk:"remote_config_read_enabled"` |
| 31 | +} |
| 32 | + |
| 33 | +// NewEphemeralAPIKeyResource creates a new ephemeral API key resource |
| 34 | +func NewEphemeralAPIKeyResource() ephemeral.EphemeralResource { |
| 35 | + return &EphemeralAPIKeyResource{} |
| 36 | +} |
| 37 | + |
| 38 | +// Metadata implements the core ephemeral.EphemeralResource interface |
| 39 | +func (r *EphemeralAPIKeyResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 40 | + resp.TypeName = "api_key" // Will become "datadog_api_key" via wrapper |
| 41 | +} |
| 42 | + |
| 43 | +// Schema implements the core ephemeral.EphemeralResource interface |
| 44 | +func (r *EphemeralAPIKeyResource) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 45 | + resp.Schema = schema.Schema{ |
| 46 | + Description: "Retrieves an existing Datadog API key as an ephemeral resource. The API key value is retrieved securely and made available for use in other resources without being stored in state.", |
| 47 | + Attributes: map[string]schema.Attribute{ |
| 48 | + "id": schema.StringAttribute{ |
| 49 | + Required: true, |
| 50 | + Description: "The ID of the API key to retrieve.", |
| 51 | + }, |
| 52 | + "name": schema.StringAttribute{ |
| 53 | + Computed: true, |
| 54 | + Description: "The name of the API key.", |
| 55 | + }, |
| 56 | + "key": schema.StringAttribute{ |
| 57 | + Computed: true, |
| 58 | + Sensitive: true, |
| 59 | + Description: "The actual API key value (sensitive).", |
| 60 | + }, |
| 61 | + "remote_config_read_enabled": schema.BoolAttribute{ |
| 62 | + Computed: true, |
| 63 | + Description: "Whether remote configuration reads are enabled for this key.", |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Open implements the core ephemeral.EphemeralResource interface |
| 70 | +// This is where the ephemeral resource acquires the API key data |
| 71 | +func (r *EphemeralAPIKeyResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 72 | + // 1. Extract API key ID from config |
| 73 | + var config EphemeralAPIKeyModel |
| 74 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 75 | + if resp.Diagnostics.HasError() { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + // 2. Fetch API key from Datadog API |
| 80 | + apiKey, httpResp, err := r.Api.GetAPIKey(r.Auth, config.ID.ValueString()) |
| 81 | + if err != nil { |
| 82 | + log.Printf("[ERROR] Ephemeral open operation failed for api_key: %v", err) |
| 83 | + resp.Diagnostics.AddError( |
| 84 | + "API Key Retrieval Failed", |
| 85 | + "Unable to fetch API key data from Datadog API", |
| 86 | + ) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + // Check HTTP response status |
| 91 | + if httpResp != nil && httpResp.StatusCode >= 400 { |
| 92 | + log.Printf("[WARN] Ephemeral open operation failed for api_key") |
| 93 | + resp.Diagnostics.AddError( |
| 94 | + "API Key Retrieval Failed", |
| 95 | + "Received error response from Datadog API", |
| 96 | + ) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // 3. Extract API key data from response |
| 101 | + apiKeyData := apiKey.GetData() |
| 102 | + apiKeyAttributes := apiKeyData.GetAttributes() |
| 103 | + |
| 104 | + // 4. Set result data (including the sensitive key value) |
| 105 | + result := EphemeralAPIKeyModel{ |
| 106 | + ID: config.ID, |
| 107 | + Name: types.StringValue(apiKeyAttributes.GetName()), |
| 108 | + Key: types.StringValue(apiKeyAttributes.GetKey()), // SENSITIVE |
| 109 | + RemoteConfigReadEnabled: types.BoolValue(apiKeyAttributes.GetRemoteConfigReadEnabled()), |
| 110 | + } |
| 111 | + |
| 112 | + resp.Diagnostics.Append(resp.Result.Set(ctx, &result)...) |
| 113 | + if resp.Diagnostics.HasError() { |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + log.Printf("[DEBUG] Ephemeral open operation succeeded for api_key") |
| 118 | +} |
| 119 | + |
| 120 | +// Configure implements the optional ephemeral.EphemeralResourceWithConfigure interface |
| 121 | +func (r *EphemeralAPIKeyResource) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { |
| 122 | + if req.ProviderData == nil { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + providerData, ok := req.ProviderData.(*FrameworkProvider) |
| 127 | + if !ok { |
| 128 | + resp.Diagnostics.AddError( |
| 129 | + "Unexpected Configure Type", |
| 130 | + "Expected *FrameworkProvider", |
| 131 | + ) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + r.Api = providerData.DatadogApiInstances.GetKeyManagementApiV2() |
| 136 | + r.Auth = providerData.Auth |
| 137 | +} |
0 commit comments