-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebb9150
commit 457dc94
Showing
11 changed files
with
783 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package tfschema | ||
|
||
import ( | ||
dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
) | ||
|
||
// AttributeBuilder is the common interface for all attributes, it can be used to build data source attribute and resource attribute. | ||
// We need this because in terraform plugin framework, the datasource schema and resource schema are in two separate packages. | ||
// This common interface prevents us from keeping two copies of StructToSchema and CustomizableSchema. | ||
type AttributeBuilder interface { | ||
BuildDataSourceAttribute() dataschema.Attribute | ||
BuildResourceAttribute() schema.Attribute | ||
SetOptional() AttributeBuilder | ||
SetRequired() AttributeBuilder | ||
SetSensitive() AttributeBuilder | ||
SetComputed() AttributeBuilder | ||
SetReadOnly() AttributeBuilder | ||
SetDeprecated(string) AttributeBuilder | ||
} | ||
|
||
// BuildDataSourceAttributeMap takes a map from string to AttributeBuilder and returns a map from string to datasource.schema.Attribute | ||
func BuildDataSourceAttributeMap(attributes map[string]AttributeBuilder) map[string]dataschema.Attribute { | ||
dataSourceAttributes := make(map[string]dataschema.Attribute) | ||
|
||
for key, attribute := range attributes { | ||
dataSourceAttributes[key] = attribute.BuildDataSourceAttribute() | ||
} | ||
|
||
return dataSourceAttributes | ||
} | ||
|
||
// BuildResourceAttributeMap takes a map from string to AttributeBuilder and returns a map from string to resource.schema.Attribute | ||
func BuildResourceAttributeMap(attributes map[string]AttributeBuilder) map[string]schema.Attribute { | ||
resourceAttributes := make(map[string]schema.Attribute) | ||
|
||
for key, attribute := range attributes { | ||
resourceAttributes[key] = attribute.BuildResourceAttribute() | ||
} | ||
|
||
return resourceAttributes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package tfschema | ||
|
||
import ( | ||
dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
type BoolAttributeBuilder struct { | ||
Optional bool | ||
Required bool | ||
Sensitive bool | ||
Computed bool | ||
DeprecationMessage string | ||
Validators []validator.Bool | ||
} | ||
|
||
func (a BoolAttributeBuilder) BuildDataSourceAttribute() dataschema.Attribute { | ||
return dataschema.BoolAttribute{Optional: a.Optional, Required: a.Required, Sensitive: a.Sensitive, DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators} | ||
} | ||
|
||
func (a BoolAttributeBuilder) BuildResourceAttribute() schema.Attribute { | ||
return schema.BoolAttribute{Optional: a.Optional, Required: a.Required, Sensitive: a.Sensitive, DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators} | ||
} | ||
|
||
func (a BoolAttributeBuilder) SetOptional() AttributeBuilder { | ||
if a.Optional && !a.Required { | ||
panic("attribute is already optional") | ||
} | ||
a.Optional = true | ||
a.Required = false | ||
return a | ||
} | ||
|
||
func (a BoolAttributeBuilder) SetRequired() AttributeBuilder { | ||
if !a.Optional && a.Required { | ||
panic("attribute is already required") | ||
} | ||
a.Optional = false | ||
a.Required = true | ||
return a | ||
} | ||
|
||
func (a BoolAttributeBuilder) SetSensitive() AttributeBuilder { | ||
if a.Sensitive { | ||
panic("attribute is already sensitive") | ||
} | ||
a.Sensitive = true | ||
return a | ||
} | ||
|
||
func (a BoolAttributeBuilder) SetComputed() AttributeBuilder { | ||
if a.Computed { | ||
panic("attribute is already computed") | ||
} | ||
a.Computed = true | ||
return a | ||
} | ||
|
||
func (a BoolAttributeBuilder) SetReadOnly() AttributeBuilder { | ||
if a.Computed && !a.Optional && !a.Required { | ||
panic("attribute is already read only") | ||
} | ||
a.Computed = true | ||
a.Optional = false | ||
a.Required = false | ||
return a | ||
} | ||
|
||
func (a BoolAttributeBuilder) SetDeprecated(msg string) AttributeBuilder { | ||
a.DeprecationMessage = msg | ||
return a | ||
} | ||
|
||
func (a BoolAttributeBuilder) AddValidator(v validator.Bool) AttributeBuilder { | ||
a.Validators = append(a.Validators, v) | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package tfschema | ||
|
||
import ( | ||
dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
type Float64Attribute struct { | ||
Optional bool | ||
Required bool | ||
Sensitive bool | ||
Computed bool | ||
DeprecationMessage string | ||
Validators []validator.Float64 | ||
} | ||
|
||
func (a Float64Attribute) BuildDataSourceAttribute() dataschema.Attribute { | ||
return dataschema.Float64Attribute{Optional: a.Optional, Required: a.Required, Sensitive: a.Sensitive, DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators} | ||
} | ||
|
||
func (a Float64Attribute) BuildResourceAttribute() schema.Attribute { | ||
return schema.Float64Attribute{Optional: a.Optional, Required: a.Required, Sensitive: a.Sensitive, DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators} | ||
} | ||
|
||
func (a Float64Attribute) SetOptional() AttributeBuilder { | ||
if a.Optional && !a.Required { | ||
panic("attribute is already optional") | ||
} | ||
a.Optional = true | ||
a.Required = false | ||
return a | ||
} | ||
|
||
func (a Float64Attribute) SetRequired() AttributeBuilder { | ||
if !a.Optional && a.Required { | ||
panic("attribute is already required") | ||
} | ||
a.Optional = false | ||
a.Required = true | ||
return a | ||
} | ||
|
||
func (a Float64Attribute) SetSensitive() AttributeBuilder { | ||
if a.Sensitive { | ||
panic("attribute is already sensitive") | ||
} | ||
a.Sensitive = true | ||
return a | ||
} | ||
|
||
func (a Float64Attribute) SetComputed() AttributeBuilder { | ||
if a.Computed { | ||
panic("attribute is already computed") | ||
} | ||
a.Computed = true | ||
return a | ||
} | ||
|
||
func (a Float64Attribute) SetReadOnly() AttributeBuilder { | ||
if a.Computed && !a.Optional && !a.Required { | ||
panic("attribute is already read only") | ||
} | ||
a.Computed = true | ||
a.Optional = false | ||
a.Required = false | ||
return a | ||
} | ||
|
||
func (a Float64Attribute) SetDeprecated(msg string) AttributeBuilder { | ||
a.DeprecationMessage = msg | ||
return a | ||
} | ||
|
||
func (a Float64Attribute) AddValidator(v validator.Float64) AttributeBuilder { | ||
a.Validators = append(a.Validators, v) | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package tfschema | ||
|
||
import ( | ||
dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
type Int64Attribute struct { | ||
Optional bool | ||
Required bool | ||
Sensitive bool | ||
Computed bool | ||
DeprecationMessage string | ||
Validators []validator.Int64 | ||
} | ||
|
||
func (a Int64Attribute) BuildDataSourceAttribute() dataschema.Attribute { | ||
return dataschema.Int64Attribute{Optional: a.Optional, Required: a.Required, Sensitive: a.Sensitive, DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators} | ||
} | ||
|
||
func (a Int64Attribute) BuildResourceAttribute() schema.Attribute { | ||
return schema.Int64Attribute{Optional: a.Optional, Required: a.Required, Sensitive: a.Sensitive, DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators} | ||
} | ||
|
||
func (a Int64Attribute) SetOptional() AttributeBuilder { | ||
if a.Optional && !a.Required { | ||
panic("attribute is already optional") | ||
} | ||
a.Optional = true | ||
a.Required = false | ||
return a | ||
} | ||
|
||
func (a Int64Attribute) SetRequired() AttributeBuilder { | ||
if !a.Optional && a.Required { | ||
panic("attribute is already required") | ||
} | ||
a.Optional = false | ||
a.Required = true | ||
return a | ||
} | ||
|
||
func (a Int64Attribute) SetSensitive() AttributeBuilder { | ||
if a.Sensitive { | ||
panic("attribute is already sensitive") | ||
} | ||
a.Sensitive = true | ||
return a | ||
} | ||
|
||
func (a Int64Attribute) SetComputed() AttributeBuilder { | ||
if a.Computed { | ||
panic("attribute is already computed") | ||
} | ||
a.Computed = true | ||
return a | ||
} | ||
|
||
func (a Int64Attribute) SetReadOnly() AttributeBuilder { | ||
if a.Computed && !a.Optional && !a.Required { | ||
panic("attribute is already read only") | ||
} | ||
a.Computed = true | ||
a.Optional = false | ||
a.Required = false | ||
return a | ||
} | ||
|
||
func (a Int64Attribute) SetDeprecated(msg string) AttributeBuilder { | ||
a.DeprecationMessage = msg | ||
return a | ||
} | ||
|
||
func (a Int64Attribute) AddValidator(v validator.Int64) AttributeBuilder { | ||
a.Validators = append(a.Validators, v) | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package tfschema | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
// ListAttributte represents a list of primitive types. | ||
type ListAttribute struct { | ||
ElementType attr.Type | ||
Optional bool | ||
Required bool | ||
Sensitive bool | ||
Computed bool | ||
DeprecationMessage string | ||
Validators []validator.List | ||
} | ||
|
||
func (a ListAttribute) BuildDataSourceAttribute() dataschema.Attribute { | ||
return dataschema.ListAttribute{ElementType: a.ElementType, Optional: a.Optional, Required: a.Required, Sensitive: a.Sensitive, DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators} | ||
} | ||
|
||
func (a ListAttribute) BuildResourceAttribute() schema.Attribute { | ||
return schema.ListAttribute{ElementType: a.ElementType, Optional: a.Optional, Required: a.Required, Sensitive: a.Sensitive, DeprecationMessage: a.DeprecationMessage, Computed: a.Computed, Validators: a.Validators} | ||
} | ||
|
||
func (a ListAttribute) SetOptional() AttributeBuilder { | ||
if a.Optional && !a.Required { | ||
panic("attribute is already optional") | ||
} | ||
a.Optional = true | ||
a.Required = false | ||
return a | ||
} | ||
|
||
func (a ListAttribute) SetRequired() AttributeBuilder { | ||
if !a.Optional && a.Required { | ||
panic("attribute is already required") | ||
} | ||
a.Optional = false | ||
a.Required = true | ||
return a | ||
} | ||
|
||
func (a ListAttribute) SetSensitive() AttributeBuilder { | ||
if a.Sensitive { | ||
panic("attribute is already sensitive") | ||
} | ||
a.Sensitive = true | ||
return a | ||
} | ||
|
||
func (a ListAttribute) SetComputed() AttributeBuilder { | ||
if a.Computed { | ||
panic("attribute is already computed") | ||
} | ||
a.Computed = true | ||
return a | ||
} | ||
|
||
func (a ListAttribute) SetReadOnly() AttributeBuilder { | ||
if a.Computed && !a.Optional && !a.Required { | ||
panic("attribute is already read only") | ||
} | ||
a.Computed = true | ||
a.Optional = false | ||
a.Required = false | ||
return a | ||
} | ||
|
||
func (a ListAttribute) SetDeprecated(msg string) AttributeBuilder { | ||
a.DeprecationMessage = msg | ||
return a | ||
} | ||
|
||
func (a ListAttribute) AddValidator(v validator.List) AttributeBuilder { | ||
a.Validators = append(a.Validators, v) | ||
return a | ||
} |
Oops, something went wrong.