|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package tfprotov5 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "iter" |
| 9 | +) |
| 10 | + |
| 11 | +// ActionMetadata describes metadata for an action in the GetMetadata RPC. |
| 12 | +type ActionMetadata struct { |
| 13 | + // TypeName is the name of the action. |
| 14 | + TypeName string |
| 15 | +} |
| 16 | + |
| 17 | +// ActionServer is an interface containing the methods an action implementation needs to fill. |
| 18 | +type ActionServer interface { |
| 19 | + // PlanAction is called when Terraform is attempting to |
| 20 | + // calculate a plan for an action. Depending on the type defined in |
| 21 | + // the action schema, Terraform may also pass the plan of linked resources |
| 22 | + // that the action can modify or return unmodified to influence Terraform's plan. |
| 23 | + PlanAction(context.Context, *PlanActionRequest) (*PlanActionResponse, error) |
| 24 | + |
| 25 | + // InvokeAction is called when Terraform wants to execute the logic of an action. |
| 26 | + // Depending on the type defined in the action schema, Terraform may also pass the |
| 27 | + // state of linked resources. The provider runs the logic of the action, reporting progress |
| 28 | + // events as desired, then sends a final complete event that has the linked resource's resulting |
| 29 | + // state and identity. |
| 30 | + // |
| 31 | + // If an error occurs, the provider sends a complete event with the relevant diagnostics. |
| 32 | + InvokeAction(context.Context, *InvokeActionRequest) (*InvokeActionServerStream, error) |
| 33 | +} |
| 34 | + |
| 35 | +// PlanActionRequest is the request Terraform sends when it is attempting to |
| 36 | +// calculate a plan for an action. |
| 37 | +type PlanActionRequest struct { |
| 38 | + // ActionType is the name of the action being called. |
| 39 | + ActionType string |
| 40 | + |
| 41 | + // LinkedResources contains the data of the managed resource types that are linked to this action. |
| 42 | + // |
| 43 | + // - If the action schema type is Unlinked, this field will be empty. |
| 44 | + // - If the action schema type is Lifecycle, this field will be contain a single linked resource. |
| 45 | + // - If the action schema type is Linked, this field will be one or more linked resources, which |
| 46 | + // will be in the same order as the linked resource schemas are defined in the action schema. |
| 47 | + // |
| 48 | + // For Lifecycle actions, the provider may only change computed-only attributes. |
| 49 | + // |
| 50 | + // For Linked actions, the provider may change any attributes as long as it adheres to the resource schema. |
| 51 | + LinkedResources []*ProposedLinkedResource |
| 52 | + |
| 53 | + // Config is the configuration the user supplied for the action. See |
| 54 | + // the documentation on `DynamicValue` for more information about |
| 55 | + // safely accessing the configuration. |
| 56 | + Config *DynamicValue |
| 57 | + |
| 58 | + // ClientCapabilities defines optionally supported protocol features for the |
| 59 | + // PlanAction RPC, such as forward-compatible Terraform behavior changes. |
| 60 | + ClientCapabilities *PlanActionClientCapabilities |
| 61 | +} |
| 62 | + |
| 63 | +// ProposedLinkedResource represents linked resource data before PlanAction is called. |
| 64 | +type ProposedLinkedResource struct { |
| 65 | + // PriorState is the state of the linked resource before the plan is applied, |
| 66 | + // represented as a `DynamicValue`. See the documentation for |
| 67 | + // `DynamicValue` for information about safely accessing the state. |
| 68 | + PriorState *DynamicValue |
| 69 | + |
| 70 | + // PlannedState is the latest indication of what the state for the |
| 71 | + // linked resource should be after apply, represented as a `DynamicValue`. |
| 72 | + // See the documentation for `DynamicValue` for information about safely |
| 73 | + // accessing the planned state. |
| 74 | + // |
| 75 | + // Since PlannedState is the most recent plan for the linked resource, it could |
| 76 | + // be the result of an RPC call to PlanResourceChange or an RPC call to PlanAction |
| 77 | + // for a predecessor action |
| 78 | + PlannedState *DynamicValue |
| 79 | + |
| 80 | + // Config is the configuration the user supplied for the linked resource. See |
| 81 | + // the documentation on `DynamicValue` for more information about |
| 82 | + // safely accessing the configuration. |
| 83 | + Config *DynamicValue |
| 84 | + |
| 85 | + // PriorIdentity is the identity of the resource before the plan is |
| 86 | + // applied, represented as a `ResourceIdentityData`. |
| 87 | + PriorIdentity *ResourceIdentityData |
| 88 | +} |
| 89 | + |
| 90 | +// PlanActionResponse is the response from the provider when planning an action. If the action |
| 91 | +// has linked resources, it will contain any modifications made to the planned state or identity. |
| 92 | +type PlanActionResponse struct { |
| 93 | + // LinkedResources contains the provider modified data of the managed resource types that are linked to this action. |
| 94 | + // |
| 95 | + // For Lifecycle actions, the provider may only change computed-only attributes. |
| 96 | + // |
| 97 | + // For Linked actions, the provider may change any attributes as long as it adheres to the resource schema. |
| 98 | + LinkedResources []*PlannedLinkedResource |
| 99 | + |
| 100 | + // Diagnostics report errors or warnings related to plannning the action and calculating |
| 101 | + // the planned state of the requested linked resources. Returning an empty slice |
| 102 | + // indicates a successful validation with no warnings or errors generated. |
| 103 | + Diagnostics []*Diagnostic |
| 104 | + |
| 105 | + // Deferred is used to indicate to Terraform that the PlanAction operation |
| 106 | + // needs to be deferred for a reason. |
| 107 | + Deferred *Deferred |
| 108 | +} |
| 109 | + |
| 110 | +// PlannedLinkedResource represents linked resource data that was planned during PlanAction and returned. |
| 111 | +type PlannedLinkedResource struct { |
| 112 | + // PlannedState is the provider's indication of what the state for the |
| 113 | + // linked resource should be after apply, represented as a `DynamicValue`. See |
| 114 | + // the documentation for `DynamicValue` for information about safely |
| 115 | + // creating the `DynamicValue`. |
| 116 | + PlannedState *DynamicValue |
| 117 | + |
| 118 | + // PlannedIdentity is the provider's indication of what the identity for the |
| 119 | + // linked resource should be after apply, represented as a `ResourceIdentityData` |
| 120 | + PlannedIdentity *ResourceIdentityData |
| 121 | +} |
| 122 | + |
| 123 | +// InvokeActionRequest is the request Terraform sends when it wants to execute |
| 124 | +// the logic of an action. |
| 125 | +type InvokeActionRequest struct { |
| 126 | + // ActionType is the name of the action being called. |
| 127 | + ActionType string |
| 128 | + |
| 129 | + // LinkedResources contains the data of the managed resource types that are linked to this action. |
| 130 | + // |
| 131 | + // - If the action schema type is Unlinked, this field will be empty. |
| 132 | + // - If the action schema type is Lifecycle, this field will be contain a single linked resource. |
| 133 | + // - If the action schema type is Linked, this field will be one or more linked resources, which |
| 134 | + // will be in the same order as the linked resource schemas are defined in the action schema. |
| 135 | + // |
| 136 | + // For Lifecycle actions, the provider may only change computed-only attributes. |
| 137 | + // |
| 138 | + // For Linked actions, the provider may change any attributes as long as it adheres to the resource schema. |
| 139 | + LinkedResources []*InvokeLinkedResource |
| 140 | + |
| 141 | + // Config is the configuration the user supplied for the action. See |
| 142 | + // the documentation on `DynamicValue` for more information about |
| 143 | + // safely accessing the configuration. |
| 144 | + Config *DynamicValue |
| 145 | +} |
| 146 | + |
| 147 | +// InvokeLinkedResource represents linked resource data before InvokeAction is called. |
| 148 | +type InvokeLinkedResource struct { |
| 149 | + // PriorState is the state of the linked resource before changes are applied, |
| 150 | + // represented as a `DynamicValue`. See the documentation for |
| 151 | + // `DynamicValue` for information about safely accessing the state. |
| 152 | + PriorState *DynamicValue |
| 153 | + |
| 154 | + // PlannedState is the latest indication of what the state for the |
| 155 | + // linked resource should look like after changes are applied, represented |
| 156 | + // as a `DynamicValue`. See the documentation for `DynamicValue` for |
| 157 | + // information about safely accessing the planned state. |
| 158 | + // |
| 159 | + // Since PlannedState is the most recent state for the linked resource, it could |
| 160 | + // be the result of an RPC call to ApplyResourceChange or an RPC call to InvokeAction |
| 161 | + // for a predecessor action. |
| 162 | + PlannedState *DynamicValue |
| 163 | + |
| 164 | + // Config is the configuration the user supplied for the linked resource. See |
| 165 | + // the documentation on `DynamicValue` for more information about |
| 166 | + // safely accessing the configuration. |
| 167 | + Config *DynamicValue |
| 168 | + |
| 169 | + // PlannedIdentity is Terraform's plan for what the linked resource identity should |
| 170 | + // look like after the changes are applied, represented as a `ResourceIdentityData`. |
| 171 | + PlannedIdentity *ResourceIdentityData |
| 172 | +} |
| 173 | + |
| 174 | +// InvokeActionServerStream represents a streaming response to an |
| 175 | +// InvokeActionRequest. An instance of this struct is supplied as an argument |
| 176 | +// to the provider's InvokeAction implementation. The provider should set an |
| 177 | +// Events iterator function that pushes zero or more events of type InvokeActionEvent. |
| 178 | +type InvokeActionServerStream struct { |
| 179 | + // Events is the iterator that the provider can stream progress messages back to Terraform |
| 180 | + // as the action is executing. Once the provider has completed the action invocation, the provider must |
| 181 | + // respond with a completed event with the new linked resource state or diagnostics explaining why |
| 182 | + // the action failed. |
| 183 | + Events iter.Seq[InvokeActionEvent] |
| 184 | +} |
| 185 | + |
| 186 | +// InvokeActionEvent is an event sent back to Terraform during the InvokeAction RPC. |
| 187 | +type InvokeActionEvent struct { |
| 188 | + // Type is the type of event that is being sent back during InvokeAction, either a Progress event |
| 189 | + // or a Completed event. |
| 190 | + Type InvokeActionEventType |
| 191 | +} |
| 192 | + |
| 193 | +// InvokeActionEventType is an intentionally unimplementable interface that |
| 194 | +// functions as an enum, allowing us to use different strongly-typed event types |
| 195 | +// that contain additional, but different data, as a generic "event" type. |
| 196 | +type InvokeActionEventType interface { |
| 197 | + isInvokeActionEventType() // this interface is only implementable in this package |
| 198 | +} |
| 199 | + |
| 200 | +var ( |
| 201 | + _ InvokeActionEventType = ProgressInvokeActionEventType{} |
| 202 | + _ InvokeActionEventType = CompletedInvokeActionEventType{} |
| 203 | +) |
| 204 | + |
| 205 | +// ProgressInvokeActionEventType represents a progress update that should be displayed in the Terraform |
| 206 | +// CLI or external system running Terraform. |
| 207 | +type ProgressInvokeActionEventType struct { |
| 208 | + // Message is the human-readable message to display about the progress of the action invocation. |
| 209 | + Message string |
| 210 | +} |
| 211 | + |
| 212 | +func (a ProgressInvokeActionEventType) isInvokeActionEventType() {} |
| 213 | + |
| 214 | +// CompletedInvokeActionEventType represents the final completed event, along with all of the linked resource |
| 215 | +// data modified by the provider or diagnostics about an action invocation failure. |
| 216 | +type CompletedInvokeActionEventType struct { |
| 217 | + // LinkedResources contains the provider modified data of the managed resource types that are linked to this action. |
| 218 | + // |
| 219 | + // For Lifecycle actions, the provider may only change computed-only attributes. |
| 220 | + // |
| 221 | + // For Linked actions, the provider may change any attributes as long as it adheres to the resource schema. |
| 222 | + LinkedResources []*NewLinkedResource |
| 223 | + |
| 224 | + // Diagnostics report errors or warnings related to invoking an action. |
| 225 | + // Returning an empty slice indicates a successful invocation with no warnings |
| 226 | + // or errors generated. |
| 227 | + Diagnostics []*Diagnostic |
| 228 | +} |
| 229 | + |
| 230 | +func (a CompletedInvokeActionEventType) isInvokeActionEventType() {} |
| 231 | + |
| 232 | +// NewLinkedResource represents linked resource data that was changed during InvokeAction and returned. |
| 233 | +// |
| 234 | +// Depending on how the action was invoked, the modified state data will either be immediately recorded in |
| 235 | +// state or reconcicled in a future terraform apply operation. |
| 236 | +type NewLinkedResource struct { |
| 237 | + // NewState is the provider's understanding of what the linked resource's |
| 238 | + // state is after changes are applied, represented as a `DynamicValue`. |
| 239 | + // See the documentation for `DynamicValue` for information about |
| 240 | + // safely creating the `DynamicValue`. |
| 241 | + // |
| 242 | + // Any attribute, whether computed or not, that has a known value in |
| 243 | + // the PlannedState in the InvokeActionRequest must be preserved |
| 244 | + // exactly as it was in NewState. |
| 245 | + NewState *DynamicValue |
| 246 | + |
| 247 | + // NewIdentity is the provider's understanding of what the linked resource's |
| 248 | + // identity is after changes are applied, represented as a `ResourceIdentityData`. |
| 249 | + NewIdentity *ResourceIdentityData |
| 250 | + |
| 251 | + // RequiresReplace can only be set if diagnostics are returned for the action and indicate |
| 252 | + // the linked resource must be replaced as a result of the action invocation error. |
| 253 | + RequiresReplace bool |
| 254 | +} |
0 commit comments