Skip to content

Commit

Permalink
Add support for LKE, Volume, NodeBalancer, and network transfer prici…
Browse files Browse the repository at this point in the history
…ng endpoints (#1591)

* Added LKE Types datasource

* Added Node Balancer Types datasource

* Added Volume Types datasource

* Added Network Transfer Prices datasource

* Abstracted base pricing types

* Addressed PR comments

* Fixed filters for new endpoints
  • Loading branch information
ezilber-akamai authored Oct 7, 2024
1 parent 158e8e9 commit 1603370
Show file tree
Hide file tree
Showing 28 changed files with 1,219 additions and 25 deletions.
11 changes: 9 additions & 2 deletions linode/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package linode
import (
"context"

"github.com/linode/terraform-provider-linode/v2/linode/vpcips"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
Expand Down Expand Up @@ -46,13 +44,16 @@ import (
"github.com/linode/terraform-provider-linode/v2/linode/lke"
"github.com/linode/terraform-provider-linode/v2/linode/lkeclusters"
"github.com/linode/terraform-provider-linode/v2/linode/lkenodepool"
"github.com/linode/terraform-provider-linode/v2/linode/lketypes"
"github.com/linode/terraform-provider-linode/v2/linode/lkeversions"
"github.com/linode/terraform-provider-linode/v2/linode/nb"
"github.com/linode/terraform-provider-linode/v2/linode/nbconfig"
"github.com/linode/terraform-provider-linode/v2/linode/nbconfigs"
"github.com/linode/terraform-provider-linode/v2/linode/nbnode"
"github.com/linode/terraform-provider-linode/v2/linode/nbs"
"github.com/linode/terraform-provider-linode/v2/linode/nbtypes"
"github.com/linode/terraform-provider-linode/v2/linode/networkingip"
"github.com/linode/terraform-provider-linode/v2/linode/networktransferprices"
"github.com/linode/terraform-provider-linode/v2/linode/objbucket"
"github.com/linode/terraform-provider-linode/v2/linode/objcluster"
"github.com/linode/terraform-provider-linode/v2/linode/objkey"
Expand All @@ -73,7 +74,9 @@ import (
"github.com/linode/terraform-provider-linode/v2/linode/vlan"
"github.com/linode/terraform-provider-linode/v2/linode/volume"
"github.com/linode/terraform-provider-linode/v2/linode/volumes"
"github.com/linode/terraform-provider-linode/v2/linode/volumetypes"
"github.com/linode/terraform-provider-linode/v2/linode/vpc"
"github.com/linode/terraform-provider-linode/v2/linode/vpcips"
"github.com/linode/terraform-provider-linode/v2/linode/vpcs"
"github.com/linode/terraform-provider-linode/v2/linode/vpcsubnet"
"github.com/linode/terraform-provider-linode/v2/linode/vpcsubnets"
Expand Down Expand Up @@ -237,6 +240,7 @@ func (p *FrameworkProvider) DataSources(ctx context.Context) []func() datasource
profile.NewDataSource,
nb.NewDataSource,
networkingip.NewDataSource,
networktransferprices.NewDataSource,
lkeversions.NewDataSource,
regions.NewDataSource,
ipv6range.NewDataSource,
Expand All @@ -253,6 +257,7 @@ func (p *FrameworkProvider) DataSources(ctx context.Context) []func() datasource
domain.NewDataSource,
user.NewDataSource,
nbconfig.NewDataSource,
nbtypes.NewDataSource,
instancetype.NewDataSource,
instancetypes.NewDataSource,
image.NewDataSource,
Expand All @@ -276,12 +281,14 @@ func (p *FrameworkProvider) DataSources(ctx context.Context) []func() datasource
vpcsubnets.NewDataSource,
vpcs.NewDataSource,
volumes.NewDataSource,
volumetypes.NewDataSource,
accountavailability.NewDataSource,
nbconfigs.NewDataSource,
ipv6ranges.NewDataSource,
domains.NewDataSource,
lke.NewDataSource,
lkeclusters.NewDataSource,
lketypes.NewDataSource,
placementgroup.NewDataSource,
placementgroups.NewDataSource,
childaccount.NewDataSource,
Expand Down
50 changes: 50 additions & 0 deletions linode/helper/base_pricing_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package helper

import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var PriceObjectType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"hourly": types.Float64Type,
"monthly": types.Float64Type,
},
}

var RegionPriceObjectType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.StringType,
"hourly": types.Float64Type,
"monthly": types.Float64Type,
},
}

func GetPricingTypeAttributes(typeName string) map[string]schema.Attribute {
return map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The unique ID assigned to this " + typeName + ".",
Required: true,
},
"label": schema.StringAttribute{
Description: "The " + typeName + "'s label.",
Computed: true,
Optional: true,
},
"price": schema.ListAttribute{
Description: "Cost in US dollars, broken down into hourly and monthly charges.",
Computed: true,
ElementType: PriceObjectType,
},
"region_prices": schema.ListAttribute{
Description: "A list of region-specific prices for this " + typeName + ".",
Computed: true,
ElementType: RegionPriceObjectType,
},
"transfer": schema.Int64Attribute{
Description: "The monthly outbound transfer amount, in MB.",
Computed: true,
},
}
}
24 changes: 5 additions & 19 deletions linode/instancetype/framework_datasource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,13 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/linode/terraform-provider-linode/v2/linode/helper"
)

var priceObjectType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"hourly": types.Float64Type,
"monthly": types.Float64Type,
},
}

var regionPriceObjectType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.StringType,
"hourly": types.Float64Type,
"monthly": types.Float64Type,
},
}

var backupsObjectType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"price": types.ListType{ElemType: priceObjectType},
"region_prices": types.ListType{ElemType: regionPriceObjectType},
"price": types.ListType{ElemType: helper.PriceObjectType},
"region_prices": types.ListType{ElemType: helper.RegionPriceObjectType},
},
}

Expand Down Expand Up @@ -56,7 +42,7 @@ var Attributes = map[string]schema.Attribute{
"price": schema.ListAttribute{
Description: "Cost in US dollars, broken down into hourly and monthly charges.",
Computed: true,
ElementType: priceObjectType,
ElementType: helper.PriceObjectType,
},
"addons": schema.ListAttribute{
Description: "Information about the optional Backup service offered for Linodes.",
Expand All @@ -66,7 +52,7 @@ var Attributes = map[string]schema.Attribute{
"region_prices": schema.ListAttribute{
Description: "A list of region-specific prices for this plan.",
Computed: true,
ElementType: regionPriceObjectType,
ElementType: helper.RegionPriceObjectType,
},
"network_out": schema.Int64Attribute{
Description: "The Mbits outbound bandwidth allocation.",
Expand Down
10 changes: 6 additions & 4 deletions linode/instancetype/framework_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package instancetype
import (
"context"

"github.com/linode/terraform-provider-linode/v2/linode/helper"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -134,15 +136,15 @@ func FlattenPrice(ctx context.Context, price linodego.LinodePrice) (
result["hourly"] = types.Float64Value(float64(price.Hourly))
result["monthly"] = types.Float64Value(float64(price.Monthly))

obj, diag := types.ObjectValue(priceObjectType.AttrTypes, result)
obj, diag := types.ObjectValue(helper.PriceObjectType.AttrTypes, result)
if diag.HasError() {
return nil, diag
}

objList := []attr.Value{obj}

resultList, diag := types.ListValue(
priceObjectType,
helper.PriceObjectType,
objList,
)
if diag.HasError() {
Expand All @@ -158,7 +160,7 @@ func FlattenRegionPrices(prices []linodego.LinodeRegionPrice) (
result := make([]attr.Value, len(prices))

for i, price := range prices {
obj, d := types.ObjectValue(regionPriceObjectType.AttrTypes, map[string]attr.Value{
obj, d := types.ObjectValue(helper.RegionPriceObjectType.AttrTypes, map[string]attr.Value{
"id": types.StringValue(price.ID),
"hourly": types.Float64Value(float64(price.Hourly)),
"monthly": types.Float64Value(float64(price.Monthly)),
Expand All @@ -171,7 +173,7 @@ func FlattenRegionPrices(prices []linodego.LinodeRegionPrice) (
}

priceList, d := basetypes.NewListValue(
regionPriceObjectType,
helper.RegionPriceObjectType,
result,
)
return &priceList, d
Expand Down
35 changes: 35 additions & 0 deletions linode/lketypes/datasource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//go:build integration || lketypes

package lketypes_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/linode/terraform-provider-linode/v2/linode/acceptance"
"github.com/linode/terraform-provider-linode/v2/linode/lketypes/tmpl"
)

func TestAccDataSourceLKETypes_basic(t *testing.T) {
t.Parallel()

dataSourceName := "data.linode_lke_types.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: tmpl.DataBasic(t),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "types.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "types.0.id", "lke-sa"),
resource.TestCheckResourceAttr(dataSourceName, "types.0.label", "LKE Standard Availability"),
resource.TestCheckResourceAttrSet(dataSourceName, "types.0.transfer"),
resource.TestCheckResourceAttrSet(dataSourceName, "types.0.price.0.hourly"),
resource.TestCheckResourceAttrSet(dataSourceName, "types.0.price.0.monthly"),
),
},
},
})
}
73 changes: 73 additions & 0 deletions linode/lketypes/framework_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package lketypes

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/linode/linodego"
"github.com/linode/terraform-provider-linode/v2/linode/helper"
)

func NewDataSource() datasource.DataSource {
return &DataSource{
BaseDataSource: helper.NewBaseDataSource(
helper.BaseDataSourceConfig{
Name: "linode_lke_types",
Schema: &frameworkDataSourceSchema,
},
),
}
}

type DataSource struct {
helper.BaseDataSource
}

func (r *DataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
tflog.Debug(ctx, "Read data.linode_lke_types")

var data LKETypeFilterModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

id, d := filterConfig.GenerateID(data.Filters)
if d != nil {
resp.Diagnostics.Append(d)
return
}
data.ID = id

result, d := filterConfig.GetAndFilter(
ctx, r.Meta.Client, data.Filters, listLKETypes, data.Order, data.OrderBy)
if d != nil {
resp.Diagnostics.Append(d)
return
}

data.parseLKETypes(helper.AnySliceToTyped[linodego.LKEType](result))

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func listLKETypes(ctx context.Context, client *linodego.Client, filter string) ([]any, error) {
tflog.Debug(ctx, "Listing LKE types", map[string]any{
"filter_header": filter,
})

types, err := client.ListLKETypes(ctx, &linodego.ListOptions{
Filter: filter,
})
if err != nil {
return nil, err
}

return helper.TypedSliceToAny(types), nil
}
34 changes: 34 additions & 0 deletions linode/lketypes/framework_datasource_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lketypes

import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/linode/terraform-provider-linode/v2/linode/helper"
"github.com/linode/terraform-provider-linode/v2/linode/helper/frameworkfilter"
)

var lkeTypeSchema = schema.NestedBlockObject{
Attributes: helper.GetPricingTypeAttributes("LKE Type"),
}

var filterConfig = frameworkfilter.Config{
"label": {APIFilterable: true, TypeFunc: frameworkfilter.FilterTypeString},
"transfer": {APIFilterable: true, TypeFunc: frameworkfilter.FilterTypeInt},
}

var frameworkDataSourceSchema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The data source's unique ID.",
Computed: true,
},
"order_by": filterConfig.OrderBySchema(),
"order": filterConfig.OrderSchema(),
},
Blocks: map[string]schema.Block{
"filter": filterConfig.Schema(),
"types": schema.ListNestedBlock{
Description: "The returned list of LKE types.",
NestedObject: lkeTypeSchema,
},
},
}
Loading

0 comments on commit 1603370

Please sign in to comment.