Skip to content

Commit

Permalink
request and response types
Browse files Browse the repository at this point in the history
Add request and response types for Provider.Configure and the resource CRUD
functions.
  • Loading branch information
kmoe committed Jun 1, 2021
1 parent 668dd44 commit 5858d6e
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 13 deletions.
12 changes: 0 additions & 12 deletions framework.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/hashicorp/terraform-plugin-framework

go 1.16

require github.com/hashicorp/terraform-plugin-go v0.2.1
require github.com/hashicorp/terraform-plugin-go v0.3.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9
github.com/hashicorp/go-plugin v1.3.0/go.mod h1:F9eH4LrE/ZsRdbwhfjs9k9HoDUwAHnYtXdgmf1AVNs0=
github.com/hashicorp/terraform-plugin-go v0.2.1 h1:EW/R8bB2Zbkjmugzsy1d27yS8/0454b3MtYHkzOknqA=
github.com/hashicorp/terraform-plugin-go v0.2.1/go.mod h1:10V6F3taeDWVAoLlkmArKttR3IULlRWFAGtQIQTIDr4=
github.com/hashicorp/terraform-plugin-go v0.3.0 h1:AJqYzP52JFYl9NABRI7smXI1pNjgR5Q/y2WyVJ/BOZA=
github.com/hashicorp/terraform-plugin-go v0.3.0/go.mod h1:dFHsQMaTLpON2gWhVWT96fvtlc/MF1vSy3OdMhWBzdM=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down
83 changes: 83 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tfsdk

import (
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// ConfigureProviderRequest represents a request supplying the provider with
// the values the user supplied for the provider configuration block, along with
// other runtime information supplied by Terraform or the Plugin SDK. An
// instance of this request struct is supplied as an argument to the provider's
// Configure function.
type ConfigureProviderRequest struct {
// TerraformVersion is the version of Terraform executing the request.
// This is supplied for logging, analytics, and User-Agent purposes
// only. Providers should not try to gate provider behavior on
// Terraform versions.
TerraformVersion string

// Config is the configuration the user supplied for the provider. This
// information should usually be persisted to the underlying type
// that's implementing the Provider interface, for use in later
// resource CRUD operations.
Config *tftypes.Value
}

// CreateResourceRequest represents a request for the provider to create a
// resource. An instance of this request struct is supplied as an argument to
// the resource's Create function.
type CreateResourceRequest struct {
// Config is the configuration the user supplied for the resource.
//
// This configuration may contain unknown values if a user uses
// interpolation or other functionality that would prevent Terraform
// from knowing the value at request time.
Config Config

// Plan is the planned state for the resource.
Plan Plan
}

// ReadResourceRequest represents a request for the provider to read a
// resource, i.e., update values in state according to the real state of the
// resource. An instance of this request struct is supplied as an argument to
// the resource's Read function.
type ReadResourceRequest struct {
// tfprotov6.ReadResourceRequest has no Config field
// Config Config

// State is the current state of the resource prior to the Read
// operation.
State State
}

// UpdateResourceRequest represents a request for the provider to update a
// resource. An instance of this request struct is supplied as an argument to
// the resource's Update function.
type UpdateResourceRequest struct {
// Config is the configuration the user supplied for the resource.
//
// This configuration may contain unknown values if a user uses
// interpolation or other functionality that would prevent Terraform
// from knowing the value at request time.
Config Config

// Plan is the planned state for the resource.
Plan Plan

// State is the current state of the resource prior to the Update
// operation.
State State
}

// DeleteResourceRequest represents a request for the provider to delete a
// resource. An instance of this request struct is supplied as an argument to
// the resource's Delete function.
type DeleteResourceRequest struct {
// Config is the configuration the user supplied for the resource.
//
// This configuration may contain unknown values if a user uses
// interpolation or other functionality that would prevent Terraform
// from knowing the value at request time.
Config Config
}
286 changes: 286 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
package tfsdk

import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// ConfigureProviderResponse represents a response to a
// ConfigureProviderRequest. An instance of this response struct is supplied as
// an argument to the provider's Configure function, in which the provider
// should set values on the ConfigureProviderResponse as appropriate.
type ConfigureProviderResponse struct {
// Diagnostics report errors or warnings related to configuring the
// provider. An empty slice indicates success, with no warnings or
// errors generated.
Diagnostics []*tfprotov6.Diagnostic
}

// AddWarning appends a warning diagnostic to the response. If the warning
// concerns a particular attribute, AddAttributeWarning should be used instead.
func (r *ConfigureProviderResponse) AddWarning(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddAttributeWarning appends a warning diagnostic to the response and labels
// it with a specific attribute.
func (r *ConfigureProviderResponse) AddAttributeWarning(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddError appends an error diagnostic to the response. If the error concerns a
// particular attribute, AddAttributeError should be used instead.
func (r *ConfigureProviderResponse) AddError(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// AddAttributeError appends an error diagnostic to the response and labels it
// with a specific attribute.
func (r *ConfigureProviderResponse) AddAttributeError(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// CreateResourceResponse represents a response to a CreateResourceRequest. An
// instance of this response struct is supplied as
// an argument to the resource's Create function, in which the provider
// should set values on the CreateResourceResponse as appropriate.
type CreateResourceResponse struct {
// State is the state of the resource following the Create operation.
// This field is pre-populated from CreateResourceRequest.Plan and
// should be set during the resource's Create operation.
State State

// Diagnostics report errors or warnings related to creating the
// resource. An empty slice indicates a successful operation with no
// warnings or errors generated.
Diagnostics []*tfprotov6.Diagnostic
}

// AddWarning appends a warning diagnostic to the response. If the warning
// concerns a particular attribute, AddAttributeWarning should be used instead.
func (r *CreateResourceResponse) AddWarning(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddAttributeWarning appends a warning diagnostic to the response and labels
// it with a specific attribute.
func (r *CreateResourceResponse) AddAttributeWarning(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddError appends an error diagnostic to the response. If the error concerns a
// particular attribute, AddAttributeError should be used instead.
func (r *CreateResourceResponse) AddError(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// AddAttributeError appends an error diagnostic to the response and labels it
// with a specific attribute.
func (r *CreateResourceResponse) AddAttributeError(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// ReadResourceResponse represents a response to a ReadResourceRequest. An
// instance of this response struct is supplied as
// an argument to the resource's Read function, in which the provider
// should set values on the ReadResourceResponse as appropriate.
type ReadResourceResponse struct {
// State is the state of the resource following the Read operation.
// This field is pre-populated from ReadResourceRequest.State and
// should be set during the resource's Read operation.
State State

// Diagnostics report errors or warnings related to reading the
// resource. An empty slice indicates a successful operation with no
// warnings or errors generated.
Diagnostics []*tfprotov6.Diagnostic
}

// AddWarning appends a warning diagnostic to the response. If the warning
// concerns a particular attribute, AddAttributeWarning should be used instead.
func (r *ReadResourceResponse) AddWarning(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddAttributeWarning appends a warning diagnostic to the response and labels
// it with a specific attribute.
func (r *ReadResourceResponse) AddAttributeWarning(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddError appends an error diagnostic to the response. If the error concerns a
// particular attribute, AddAttributeError should be used instead.
func (r *ReadResourceResponse) AddError(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// AddAttributeError appends an error diagnostic to the response and labels it
// with a specific attribute.
func (r *ReadResourceResponse) AddAttributeError(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// UpdateResourceResponse represents a response to an UpdateResourceRequest. An
// instance of this response struct is supplied as
// an argument to the resource's Update function, in which the provider
// should set values on the UpdateResourceResponse as appropriate.
type UpdateResourceResponse struct {
// State is the state of the resource following the Update operation.
// This field is pre-populated from UpdateResourceRequest.Plan and
// should be set during the resource's Update operation.
State State

// Diagnostics report errors or warnings related to updating the
// resource. An empty slice indicates a successful operation with no
// warnings or errors generated.
Diagnostics []*tfprotov6.Diagnostic
}

// AddWarning appends a warning diagnostic to the response. If the warning
// concerns a particular attribute, AddAttributeWarning should be used instead.
func (r *UpdateResourceResponse) AddWarning(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddAttributeWarning appends a warning diagnostic to the response and labels
// it with a specific attribute.
func (r *UpdateResourceResponse) AddAttributeWarning(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddError appends an error diagnostic to the response. If the error concerns a
// particular attribute, AddAttributeError should be used instead.
func (r *UpdateResourceResponse) AddError(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// AddAttributeError appends an error diagnostic to the response and labels it
// with a specific attribute.
func (r *UpdateResourceResponse) AddAttributeError(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// DeleteResourceResponse represents a response to a DeleteResourceRequest. An
// instance of this response struct is supplied as
// an argument to the resource's Delete function, in which the provider
// should set values on the DeleteResourceResponse as appropriate.
type DeleteResourceResponse struct {
// Diagnostics report errors or warnings related to deleting the
// resource. An empty slice indicates a successful operation with no
// warnings or errors generated.
Diagnostics []*tfprotov6.Diagnostic
}

// AddWarning appends a warning diagnostic to the response. If the warning
// concerns a particular attribute, AddAttributeWarning should be used instead.
func (r *DeleteResourceResponse) AddWarning(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddAttributeWarning appends a warning diagnostic to the response and labels
// it with a specific attribute.
func (r *DeleteResourceResponse) AddAttributeWarning(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityWarning,
})
}

// AddError appends an error diagnostic to the response. If the error concerns a
// particular attribute, AddAttributeError should be used instead.
func (r *DeleteResourceResponse) AddError(summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

// AddAttributeError appends an error diagnostic to the response and labels it
// with a specific attribute.
func (r *DeleteResourceResponse) AddAttributeError(attributePath *tftypes.AttributePath, summary, detail string) {
r.Diagnostics = append(r.Diagnostics, &tfprotov6.Diagnostic{
Attribute: attributePath,
Summary: summary,
Detail: detail,
Severity: tfprotov6.DiagnosticSeverityError,
})
}

0 comments on commit 5858d6e

Please sign in to comment.