|
| 1 | +// Copyright IBM Corp. 2021, 2026 |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fwserver |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/internal/logging" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/statestore" |
| 12 | +) |
| 13 | + |
| 14 | +type LockStateRequest struct { |
| 15 | + StateID string |
| 16 | + Operation string |
| 17 | + StateStore statestore.StateStore |
| 18 | +} |
| 19 | + |
| 20 | +type LockStateResponse struct { |
| 21 | + LockID string |
| 22 | + Diagnostics diag.Diagnostics |
| 23 | +} |
| 24 | + |
| 25 | +func (s *Server) LockState(ctx context.Context, req *LockStateRequest, resp *LockStateResponse) { |
| 26 | + if req == nil { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + if stateStoreWithConfigure, ok := req.StateStore.(statestore.StateStoreWithConfigure); ok { |
| 31 | + logging.FrameworkTrace(ctx, "StateStore implements StateStoreWithConfigure") |
| 32 | + |
| 33 | + configureReq := statestore.ConfigureRequest{ |
| 34 | + StateStoreData: s.StateStoreConfigureData.StateStoreConfigureData, |
| 35 | + } |
| 36 | + configureResp := statestore.ConfigureResponse{} |
| 37 | + |
| 38 | + logging.FrameworkTrace(ctx, "Calling provider defined StateStore Configure") |
| 39 | + stateStoreWithConfigure.Configure(ctx, configureReq, &configureResp) |
| 40 | + logging.FrameworkTrace(ctx, "Called provider defined StateStore Configure") |
| 41 | + |
| 42 | + resp.Diagnostics.Append(configureResp.Diagnostics...) |
| 43 | + |
| 44 | + if resp.Diagnostics.HasError() { |
| 45 | + return |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + lockReq := statestore.LockRequest{ |
| 50 | + StateID: req.StateID, |
| 51 | + Operation: req.Operation, |
| 52 | + } |
| 53 | + |
| 54 | + lockResp := statestore.LockResponse{} |
| 55 | + |
| 56 | + logging.FrameworkTrace(ctx, "Calling provider defined StateStore Lock") |
| 57 | + req.StateStore.Lock(ctx, lockReq, &lockResp) |
| 58 | + logging.FrameworkTrace(ctx, "Called provider defined StateStore Lock") |
| 59 | + |
| 60 | + resp.Diagnostics.Append(lockResp.Diagnostics...) |
| 61 | + if resp.Diagnostics.HasError() { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + resp.LockID = lockResp.LockID |
| 66 | +} |
0 commit comments