Skip to content

Commit

Permalink
Merge pull request #36232 from hashicorp/validate-resource-config-eph…
Browse files Browse the repository at this point in the history
…emeral

ephemeral: add WriteOnlyAttributesAllowed client capability to validate resource
  • Loading branch information
DanielMSchmidt authored Dec 18, 2024
2 parents 572d12b + e29708c commit b311240
Show file tree
Hide file tree
Showing 10 changed files with 1,252 additions and 1,219 deletions.
1 change: 1 addition & 0 deletions docs/plugin-protocol/tfplugin5.8.proto
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ message ValidateResourceTypeConfig {
message Request {
string type_name = 1;
DynamicValue config = 2;
ClientCapabilities client_capabilities = 3;
}
message Response {
repeated Diagnostic diagnostics = 1;
Expand Down
1 change: 1 addition & 0 deletions docs/plugin-protocol/tfplugin6.8.proto
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ message ValidateResourceConfig {
message Request {
string type_name = 1;
DynamicValue config = 2;
ClientCapabilities client_capabilities = 3;
}
message Response {
repeated Diagnostic diagnostics = 1;
Expand Down
3 changes: 3 additions & 0 deletions internal/grpcwrap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func (p *provider) ValidateResourceTypeConfig(_ context.Context, req *tfplugin5.
validateResp := p.provider.ValidateResourceConfig(providers.ValidateResourceConfigRequest{
TypeName: req.TypeName,
Config: configVal,
ClientCapabilities: providers.ClientCapabilities{
WriteOnlyAttributesAllowed: true,
},
})

resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, validateResp.Diagnostics)
Expand Down
56 changes: 26 additions & 30 deletions internal/plugin/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ func (p *GRPCProvider) ValidateResourceConfig(r providers.ValidateResourceConfig
}

protoReq := &proto.ValidateResourceTypeConfig_Request{
TypeName: r.TypeName,
Config: &proto.DynamicValue{Msgpack: mp},
TypeName: r.TypeName,
Config: &proto.DynamicValue{Msgpack: mp},
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

protoResp, err := p.client.ValidateResourceTypeConfig(p.ctx, protoReq)
Expand Down Expand Up @@ -355,9 +356,7 @@ func (p *GRPCProvider) ConfigureProvider(r providers.ConfigureProviderRequest) (
Config: &proto.DynamicValue{
Msgpack: mp,
},
ClientCapabilities: &proto.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

protoResp, err := p.client.Configure(p.ctx, protoReq)
Expand Down Expand Up @@ -407,12 +406,10 @@ func (p *GRPCProvider) ReadResource(r providers.ReadResourceRequest) (resp provi
}

protoReq := &proto.ReadResource_Request{
TypeName: r.TypeName,
CurrentState: &proto.DynamicValue{Msgpack: mp},
Private: r.Private,
ClientCapabilities: &proto.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
TypeName: r.TypeName,
CurrentState: &proto.DynamicValue{Msgpack: mp},
Private: r.Private,
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

if metaSchema.Block != nil {
Expand Down Expand Up @@ -488,14 +485,12 @@ func (p *GRPCProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
}

protoReq := &proto.PlanResourceChange_Request{
TypeName: r.TypeName,
PriorState: &proto.DynamicValue{Msgpack: priorMP},
Config: &proto.DynamicValue{Msgpack: configMP},
ProposedNewState: &proto.DynamicValue{Msgpack: propMP},
PriorPrivate: r.PriorPrivate,
ClientCapabilities: &proto.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
TypeName: r.TypeName,
PriorState: &proto.DynamicValue{Msgpack: priorMP},
Config: &proto.DynamicValue{Msgpack: configMP},
ProposedNewState: &proto.DynamicValue{Msgpack: propMP},
PriorPrivate: r.PriorPrivate,
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

if metaSchema.Block != nil {
Expand Down Expand Up @@ -625,11 +620,9 @@ func (p *GRPCProvider) ImportResourceState(r providers.ImportResourceStateReques
}

protoReq := &proto.ImportResourceState_Request{
TypeName: r.TypeName,
Id: r.ID,
ClientCapabilities: &proto.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
TypeName: r.TypeName,
Id: r.ID,
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

protoResp, err := p.client.ImportResourceState(p.ctx, protoReq)
Expand Down Expand Up @@ -740,9 +733,7 @@ func (p *GRPCProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp p
Config: &proto.DynamicValue{
Msgpack: config,
},
ClientCapabilities: &proto.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

if metaSchema.Block != nil {
Expand Down Expand Up @@ -833,9 +824,7 @@ func (p *GRPCProvider) OpenEphemeralResource(r providers.OpenEphemeralResourceRe
Config: &proto.DynamicValue{
Msgpack: config,
},
ClientCapabilities: &proto.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

protoResp, err := p.client.OpenEphemeralResource(p.ctx, protoReq)
Expand Down Expand Up @@ -1026,3 +1015,10 @@ func decodeDynamicValue(v *proto.DynamicValue, ty cty.Type) (cty.Value, error) {
}
return res, err
}

func clientCapabilitiesToProto(c providers.ClientCapabilities) *proto.ClientCapabilities {
return &proto.ClientCapabilities{
DeferralAllowed: c.DeferralAllowed,
WriteOnlyAttributesAllowed: c.WriteOnlyAttributesAllowed,
}
}
57 changes: 26 additions & 31 deletions internal/plugin6/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ func (p *GRPCProvider) ValidateResourceConfig(r providers.ValidateResourceConfig
}

protoReq := &proto6.ValidateResourceConfig_Request{
TypeName: r.TypeName,
Config: &proto6.DynamicValue{Msgpack: mp},
TypeName: r.TypeName,
Config: &proto6.DynamicValue{Msgpack: mp},
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

protoResp, err := p.client.ValidateResourceConfig(p.ctx, protoReq)
Expand Down Expand Up @@ -344,10 +345,7 @@ func (p *GRPCProvider) ConfigureProvider(r providers.ConfigureProviderRequest) (
Config: &proto6.DynamicValue{
Msgpack: mp,
},
ClientCapabilities: &proto6.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
WriteOnlyAttributesAllowed: r.ClientCapabilities.WriteOnlyAttributesAllowed,
},
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

protoResp, err := p.client.ConfigureProvider(p.ctx, protoReq)
Expand Down Expand Up @@ -397,12 +395,10 @@ func (p *GRPCProvider) ReadResource(r providers.ReadResourceRequest) (resp provi
}

protoReq := &proto6.ReadResource_Request{
TypeName: r.TypeName,
CurrentState: &proto6.DynamicValue{Msgpack: mp},
Private: r.Private,
ClientCapabilities: &proto6.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
TypeName: r.TypeName,
CurrentState: &proto6.DynamicValue{Msgpack: mp},
Private: r.Private,
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

if metaSchema.Block != nil {
Expand Down Expand Up @@ -478,14 +474,12 @@ func (p *GRPCProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
}

protoReq := &proto6.PlanResourceChange_Request{
TypeName: r.TypeName,
PriorState: &proto6.DynamicValue{Msgpack: priorMP},
Config: &proto6.DynamicValue{Msgpack: configMP},
ProposedNewState: &proto6.DynamicValue{Msgpack: propMP},
PriorPrivate: r.PriorPrivate,
ClientCapabilities: &proto6.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
TypeName: r.TypeName,
PriorState: &proto6.DynamicValue{Msgpack: priorMP},
Config: &proto6.DynamicValue{Msgpack: configMP},
ProposedNewState: &proto6.DynamicValue{Msgpack: propMP},
PriorPrivate: r.PriorPrivate,
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

if metaSchema.Block != nil {
Expand Down Expand Up @@ -615,11 +609,9 @@ func (p *GRPCProvider) ImportResourceState(r providers.ImportResourceStateReques
}

protoReq := &proto6.ImportResourceState_Request{
TypeName: r.TypeName,
Id: r.ID,
ClientCapabilities: &proto6.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
TypeName: r.TypeName,
Id: r.ID,
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

protoResp, err := p.client.ImportResourceState(p.ctx, protoReq)
Expand Down Expand Up @@ -730,9 +722,7 @@ func (p *GRPCProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp p
Config: &proto6.DynamicValue{
Msgpack: config,
},
ClientCapabilities: &proto6.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

if metaSchema.Block != nil {
Expand Down Expand Up @@ -823,9 +813,7 @@ func (p *GRPCProvider) OpenEphemeralResource(r providers.OpenEphemeralResourceRe
Config: &proto6.DynamicValue{
Msgpack: config,
},
ClientCapabilities: &proto6.ClientCapabilities{
DeferralAllowed: r.ClientCapabilities.DeferralAllowed,
},
ClientCapabilities: clientCapabilitiesToProto(r.ClientCapabilities),
}

protoResp, err := p.client.OpenEphemeralResource(p.ctx, protoReq)
Expand Down Expand Up @@ -1016,3 +1004,10 @@ func decodeDynamicValue(v *proto6.DynamicValue, ty cty.Type) (cty.Value, error)
}
return res, err
}

func clientCapabilitiesToProto(c providers.ClientCapabilities) *proto6.ClientCapabilities {
return &proto6.ClientCapabilities{
DeferralAllowed: c.DeferralAllowed,
WriteOnlyAttributesAllowed: c.WriteOnlyAttributesAllowed,
}
}
3 changes: 3 additions & 0 deletions internal/providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ type ValidateResourceConfigRequest struct {
// Config is the configuration value to validate, which may contain unknown
// values.
Config cty.Value

// ClientCapabilities contains information about the client's capabilities.
ClientCapabilities ClientCapabilities
}

type ValidateResourceConfigResponse struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/terraform/node_resource_abstract_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,9 @@ func (n *NodeAbstractResourceInstance) plan(
providers.ValidateResourceConfigRequest{
TypeName: n.Addr.Resource.Resource.Type,
Config: unmarkedConfigVal,
ClientCapabilities: providers.ClientCapabilities{
WriteOnlyAttributesAllowed: true,
},
},
)
diags = diags.Append(validateResp.Diagnostics.InConfigBody(config.Config, n.Addr.String()))
Expand Down
3 changes: 3 additions & 0 deletions internal/terraform/node_resource_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ func (n *NodeValidatableResource) validateResource(ctx EvalContext) tfdiags.Diag
req := providers.ValidateResourceConfigRequest{
TypeName: n.Config.Type,
Config: unmarkedConfigVal,
ClientCapabilities: providers.ClientCapabilities{
WriteOnlyAttributesAllowed: true,
},
}

resp := provider.ValidateResourceConfig(req)
Expand Down
Loading

0 comments on commit b311240

Please sign in to comment.