Files
Oscar Hermoso 11e0302e71 🛒 Create data sources for images, regions and sizes (#68)
* 📚 add descriptions to resources

* 📷 Create `images` data source, plus codegen for `sizes`, `regions`

* 🍩 Create offerings example

* 🧣 Create `regions` and `sizes` data sources
2025-02-28 17:21:18 +08:00

777 lines
19 KiB
Go

// Code generated by terraform-plugin-framework-generator DO NOT EDIT.
package data_sources
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"strings"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)
func RegionsDataSourceSchema(ctx context.Context) schema.Schema {
return schema.Schema{
Attributes: map[string]schema.Attribute{
"regions": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"available": schema.BoolAttribute{
Computed: true,
Description: "Whether this region is available for the allocation of new resources.",
MarkdownDescription: "Whether this region is available for the allocation of new resources.",
},
"features": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "A list of features available for resources in this region.",
MarkdownDescription: "A list of features available for resources in this region.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of this region.",
MarkdownDescription: "The name of this region.",
},
"name_servers": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "A list of nameservers available for resources in this region.",
MarkdownDescription: "A list of nameservers available for resources in this region.",
},
"sizes": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "The slugs of the sizes available in this region.",
MarkdownDescription: "The slugs of the sizes available in this region.",
},
"slug": schema.StringAttribute{
Computed: true,
Description: "The unique slug for this region.",
MarkdownDescription: "The unique slug for this region.",
},
},
CustomType: RegionsType{
ObjectType: types.ObjectType{
AttrTypes: RegionsValue{}.AttributeTypes(ctx),
},
},
},
Computed: true,
},
},
}
}
type RegionsModel struct {
Regions types.List `tfsdk:"regions"`
}
var _ basetypes.ObjectTypable = RegionsType{}
type RegionsType struct {
basetypes.ObjectType
}
func (t RegionsType) Equal(o attr.Type) bool {
other, ok := o.(RegionsType)
if !ok {
return false
}
return t.ObjectType.Equal(other.ObjectType)
}
func (t RegionsType) String() string {
return "RegionsType"
}
func (t RegionsType) ValueFromObject(ctx context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
var diags diag.Diagnostics
attributes := in.Attributes()
availableAttribute, ok := attributes["available"]
if !ok {
diags.AddError(
"Attribute Missing",
`available is missing from object`)
return nil, diags
}
availableVal, ok := availableAttribute.(basetypes.BoolValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`available expected to be basetypes.BoolValue, was: %T`, availableAttribute))
}
featuresAttribute, ok := attributes["features"]
if !ok {
diags.AddError(
"Attribute Missing",
`features is missing from object`)
return nil, diags
}
featuresVal, ok := featuresAttribute.(basetypes.ListValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`features expected to be basetypes.ListValue, was: %T`, featuresAttribute))
}
nameAttribute, ok := attributes["name"]
if !ok {
diags.AddError(
"Attribute Missing",
`name is missing from object`)
return nil, diags
}
nameVal, ok := nameAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`name expected to be basetypes.StringValue, was: %T`, nameAttribute))
}
nameServersAttribute, ok := attributes["name_servers"]
if !ok {
diags.AddError(
"Attribute Missing",
`name_servers is missing from object`)
return nil, diags
}
nameServersVal, ok := nameServersAttribute.(basetypes.ListValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`name_servers expected to be basetypes.ListValue, was: %T`, nameServersAttribute))
}
sizesAttribute, ok := attributes["sizes"]
if !ok {
diags.AddError(
"Attribute Missing",
`sizes is missing from object`)
return nil, diags
}
sizesVal, ok := sizesAttribute.(basetypes.ListValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`sizes expected to be basetypes.ListValue, was: %T`, sizesAttribute))
}
slugAttribute, ok := attributes["slug"]
if !ok {
diags.AddError(
"Attribute Missing",
`slug is missing from object`)
return nil, diags
}
slugVal, ok := slugAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`slug expected to be basetypes.StringValue, was: %T`, slugAttribute))
}
if diags.HasError() {
return nil, diags
}
return RegionsValue{
Available: availableVal,
Features: featuresVal,
Name: nameVal,
NameServers: nameServersVal,
Sizes: sizesVal,
Slug: slugVal,
state: attr.ValueStateKnown,
}, diags
}
func NewRegionsValueNull() RegionsValue {
return RegionsValue{
state: attr.ValueStateNull,
}
}
func NewRegionsValueUnknown() RegionsValue {
return RegionsValue{
state: attr.ValueStateUnknown,
}
}
func NewRegionsValue(attributeTypes map[string]attr.Type, attributes map[string]attr.Value) (RegionsValue, diag.Diagnostics) {
var diags diag.Diagnostics
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/521
ctx := context.Background()
for name, attributeType := range attributeTypes {
attribute, ok := attributes[name]
if !ok {
diags.AddError(
"Missing RegionsValue Attribute Value",
"While creating a RegionsValue value, a missing attribute value was detected. "+
"A RegionsValue must contain values for all attributes, even if null or unknown. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("RegionsValue Attribute Name (%s) Expected Type: %s", name, attributeType.String()),
)
continue
}
if !attributeType.Equal(attribute.Type(ctx)) {
diags.AddError(
"Invalid RegionsValue Attribute Type",
"While creating a RegionsValue value, an invalid attribute value was detected. "+
"A RegionsValue must use a matching attribute type for the value. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("RegionsValue Attribute Name (%s) Expected Type: %s\n", name, attributeType.String())+
fmt.Sprintf("RegionsValue Attribute Name (%s) Given Type: %s", name, attribute.Type(ctx)),
)
}
}
for name := range attributes {
_, ok := attributeTypes[name]
if !ok {
diags.AddError(
"Extra RegionsValue Attribute Value",
"While creating a RegionsValue value, an extra attribute value was detected. "+
"A RegionsValue must not contain values beyond the expected attribute types. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("Extra RegionsValue Attribute Name: %s", name),
)
}
}
if diags.HasError() {
return NewRegionsValueUnknown(), diags
}
availableAttribute, ok := attributes["available"]
if !ok {
diags.AddError(
"Attribute Missing",
`available is missing from object`)
return NewRegionsValueUnknown(), diags
}
availableVal, ok := availableAttribute.(basetypes.BoolValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`available expected to be basetypes.BoolValue, was: %T`, availableAttribute))
}
featuresAttribute, ok := attributes["features"]
if !ok {
diags.AddError(
"Attribute Missing",
`features is missing from object`)
return NewRegionsValueUnknown(), diags
}
featuresVal, ok := featuresAttribute.(basetypes.ListValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`features expected to be basetypes.ListValue, was: %T`, featuresAttribute))
}
nameAttribute, ok := attributes["name"]
if !ok {
diags.AddError(
"Attribute Missing",
`name is missing from object`)
return NewRegionsValueUnknown(), diags
}
nameVal, ok := nameAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`name expected to be basetypes.StringValue, was: %T`, nameAttribute))
}
nameServersAttribute, ok := attributes["name_servers"]
if !ok {
diags.AddError(
"Attribute Missing",
`name_servers is missing from object`)
return NewRegionsValueUnknown(), diags
}
nameServersVal, ok := nameServersAttribute.(basetypes.ListValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`name_servers expected to be basetypes.ListValue, was: %T`, nameServersAttribute))
}
sizesAttribute, ok := attributes["sizes"]
if !ok {
diags.AddError(
"Attribute Missing",
`sizes is missing from object`)
return NewRegionsValueUnknown(), diags
}
sizesVal, ok := sizesAttribute.(basetypes.ListValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`sizes expected to be basetypes.ListValue, was: %T`, sizesAttribute))
}
slugAttribute, ok := attributes["slug"]
if !ok {
diags.AddError(
"Attribute Missing",
`slug is missing from object`)
return NewRegionsValueUnknown(), diags
}
slugVal, ok := slugAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`slug expected to be basetypes.StringValue, was: %T`, slugAttribute))
}
if diags.HasError() {
return NewRegionsValueUnknown(), diags
}
return RegionsValue{
Available: availableVal,
Features: featuresVal,
Name: nameVal,
NameServers: nameServersVal,
Sizes: sizesVal,
Slug: slugVal,
state: attr.ValueStateKnown,
}, diags
}
func NewRegionsValueMust(attributeTypes map[string]attr.Type, attributes map[string]attr.Value) RegionsValue {
object, diags := NewRegionsValue(attributeTypes, attributes)
if diags.HasError() {
// This could potentially be added to the diag package.
diagsStrings := make([]string, 0, len(diags))
for _, diagnostic := range diags {
diagsStrings = append(diagsStrings, fmt.Sprintf(
"%s | %s | %s",
diagnostic.Severity(),
diagnostic.Summary(),
diagnostic.Detail()))
}
panic("NewRegionsValueMust received error(s): " + strings.Join(diagsStrings, "\n"))
}
return object
}
func (t RegionsType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
if in.Type() == nil {
return NewRegionsValueNull(), nil
}
if !in.Type().Equal(t.TerraformType(ctx)) {
return nil, fmt.Errorf("expected %s, got %s", t.TerraformType(ctx), in.Type())
}
if !in.IsKnown() {
return NewRegionsValueUnknown(), nil
}
if in.IsNull() {
return NewRegionsValueNull(), nil
}
attributes := map[string]attr.Value{}
val := map[string]tftypes.Value{}
err := in.As(&val)
if err != nil {
return nil, err
}
for k, v := range val {
a, err := t.AttrTypes[k].ValueFromTerraform(ctx, v)
if err != nil {
return nil, err
}
attributes[k] = a
}
return NewRegionsValueMust(RegionsValue{}.AttributeTypes(ctx), attributes), nil
}
func (t RegionsType) ValueType(ctx context.Context) attr.Value {
return RegionsValue{}
}
var _ basetypes.ObjectValuable = RegionsValue{}
type RegionsValue struct {
Available basetypes.BoolValue `tfsdk:"available"`
Features basetypes.ListValue `tfsdk:"features"`
Name basetypes.StringValue `tfsdk:"name"`
NameServers basetypes.ListValue `tfsdk:"name_servers"`
Sizes basetypes.ListValue `tfsdk:"sizes"`
Slug basetypes.StringValue `tfsdk:"slug"`
state attr.ValueState
}
func (v RegionsValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, 6)
var val tftypes.Value
var err error
attrTypes["available"] = basetypes.BoolType{}.TerraformType(ctx)
attrTypes["features"] = basetypes.ListType{
ElemType: types.StringType,
}.TerraformType(ctx)
attrTypes["name"] = basetypes.StringType{}.TerraformType(ctx)
attrTypes["name_servers"] = basetypes.ListType{
ElemType: types.StringType,
}.TerraformType(ctx)
attrTypes["sizes"] = basetypes.ListType{
ElemType: types.StringType,
}.TerraformType(ctx)
attrTypes["slug"] = basetypes.StringType{}.TerraformType(ctx)
objectType := tftypes.Object{AttributeTypes: attrTypes}
switch v.state {
case attr.ValueStateKnown:
vals := make(map[string]tftypes.Value, 6)
val, err = v.Available.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["available"] = val
val, err = v.Features.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["features"] = val
val, err = v.Name.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["name"] = val
val, err = v.NameServers.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["name_servers"] = val
val, err = v.Sizes.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["sizes"] = val
val, err = v.Slug.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["slug"] = val
if err := tftypes.ValidateValue(objectType, vals); err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
return tftypes.NewValue(objectType, vals), nil
case attr.ValueStateNull:
return tftypes.NewValue(objectType, nil), nil
case attr.ValueStateUnknown:
return tftypes.NewValue(objectType, tftypes.UnknownValue), nil
default:
panic(fmt.Sprintf("unhandled Object state in ToTerraformValue: %s", v.state))
}
}
func (v RegionsValue) IsNull() bool {
return v.state == attr.ValueStateNull
}
func (v RegionsValue) IsUnknown() bool {
return v.state == attr.ValueStateUnknown
}
func (v RegionsValue) String() string {
return "RegionsValue"
}
func (v RegionsValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
var diags diag.Diagnostics
var featuresVal basetypes.ListValue
switch {
case v.Features.IsUnknown():
featuresVal = types.ListUnknown(types.StringType)
case v.Features.IsNull():
featuresVal = types.ListNull(types.StringType)
default:
var d diag.Diagnostics
featuresVal, d = types.ListValue(types.StringType, v.Features.Elements())
diags.Append(d...)
}
if diags.HasError() {
return types.ObjectUnknown(map[string]attr.Type{
"available": basetypes.BoolType{},
"features": basetypes.ListType{
ElemType: types.StringType,
},
"name": basetypes.StringType{},
"name_servers": basetypes.ListType{
ElemType: types.StringType,
},
"sizes": basetypes.ListType{
ElemType: types.StringType,
},
"slug": basetypes.StringType{},
}), diags
}
var nameServersVal basetypes.ListValue
switch {
case v.NameServers.IsUnknown():
nameServersVal = types.ListUnknown(types.StringType)
case v.NameServers.IsNull():
nameServersVal = types.ListNull(types.StringType)
default:
var d diag.Diagnostics
nameServersVal, d = types.ListValue(types.StringType, v.NameServers.Elements())
diags.Append(d...)
}
if diags.HasError() {
return types.ObjectUnknown(map[string]attr.Type{
"available": basetypes.BoolType{},
"features": basetypes.ListType{
ElemType: types.StringType,
},
"name": basetypes.StringType{},
"name_servers": basetypes.ListType{
ElemType: types.StringType,
},
"sizes": basetypes.ListType{
ElemType: types.StringType,
},
"slug": basetypes.StringType{},
}), diags
}
var sizesVal basetypes.ListValue
switch {
case v.Sizes.IsUnknown():
sizesVal = types.ListUnknown(types.StringType)
case v.Sizes.IsNull():
sizesVal = types.ListNull(types.StringType)
default:
var d diag.Diagnostics
sizesVal, d = types.ListValue(types.StringType, v.Sizes.Elements())
diags.Append(d...)
}
if diags.HasError() {
return types.ObjectUnknown(map[string]attr.Type{
"available": basetypes.BoolType{},
"features": basetypes.ListType{
ElemType: types.StringType,
},
"name": basetypes.StringType{},
"name_servers": basetypes.ListType{
ElemType: types.StringType,
},
"sizes": basetypes.ListType{
ElemType: types.StringType,
},
"slug": basetypes.StringType{},
}), diags
}
attributeTypes := map[string]attr.Type{
"available": basetypes.BoolType{},
"features": basetypes.ListType{
ElemType: types.StringType,
},
"name": basetypes.StringType{},
"name_servers": basetypes.ListType{
ElemType: types.StringType,
},
"sizes": basetypes.ListType{
ElemType: types.StringType,
},
"slug": basetypes.StringType{},
}
if v.IsNull() {
return types.ObjectNull(attributeTypes), diags
}
if v.IsUnknown() {
return types.ObjectUnknown(attributeTypes), diags
}
objVal, diags := types.ObjectValue(
attributeTypes,
map[string]attr.Value{
"available": v.Available,
"features": featuresVal,
"name": v.Name,
"name_servers": nameServersVal,
"sizes": sizesVal,
"slug": v.Slug,
})
return objVal, diags
}
func (v RegionsValue) Equal(o attr.Value) bool {
other, ok := o.(RegionsValue)
if !ok {
return false
}
if v.state != other.state {
return false
}
if v.state != attr.ValueStateKnown {
return true
}
if !v.Available.Equal(other.Available) {
return false
}
if !v.Features.Equal(other.Features) {
return false
}
if !v.Name.Equal(other.Name) {
return false
}
if !v.NameServers.Equal(other.NameServers) {
return false
}
if !v.Sizes.Equal(other.Sizes) {
return false
}
if !v.Slug.Equal(other.Slug) {
return false
}
return true
}
func (v RegionsValue) Type(ctx context.Context) attr.Type {
return RegionsType{
basetypes.ObjectType{
AttrTypes: v.AttributeTypes(ctx),
},
}
}
func (v RegionsValue) AttributeTypes(ctx context.Context) map[string]attr.Type {
return map[string]attr.Type{
"available": basetypes.BoolType{},
"features": basetypes.ListType{
ElemType: types.StringType,
},
"name": basetypes.StringType{},
"name_servers": basetypes.ListType{
ElemType: types.StringType,
},
"sizes": basetypes.ListType{
ElemType: types.StringType,
},
"slug": basetypes.StringType{},
}
}