Skip to content

Commit

Permalink
Merge pull request #57 from vantage-sh/andy/20240709/update-billing-r…
Browse files Browse the repository at this point in the history
…ules-percentage

Change adjusted_rate to percentage in adjustment billing rule
  • Loading branch information
whereandy authored Jul 10, 2024
2 parents 6ea4573 + d02d44a commit 2968c51
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 109 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.14.1
github.com/hashicorp/terraform-plugin-framework v1.5.0
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/vantage-sh/vantage-go v0.0.29
github.com/vantage-sh/vantage-go v0.0.30
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/vantage-sh/vantage-go v0.0.29 h1:PMJTNdDRywmrChbktJ4QQ6qfBYpd7WbdIKsf2DqlgL4=
github.com/vantage-sh/vantage-go v0.0.29/go.mod h1:MsSI4gX/Wvkzo9kqWphZfE6puolmnbHcXSaoGkDCmXg=
github.com/vantage-sh/vantage-go v0.0.30 h1:9+9SnjLDi3dWx2yN/Do0t9eZgem/Dvi359gpQ9LaXPY=
github.com/vantage-sh/vantage-go v0.0.30/go.mod h1:MsSI4gX/Wvkzo9kqWphZfE6puolmnbHcXSaoGkDCmXg=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
16 changes: 8 additions & 8 deletions vantage/billing_rule_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func (r *billingRuleResource) ValidateConfig(ctx context.Context, req resource.V
}

if data.Type.ValueString() == "adjustment" {
if data.AdjustedRate.IsNull() {
if data.Percentage.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("adjusted_rate"),
path.Root("percentage"),
"Missing Attribute Configuration",
"Expected adjusted_rate to be configured with adjustment type",
"Expected percentage to be configured with adjustment type",
)
}

Expand Down Expand Up @@ -125,11 +125,6 @@ func (r *billingRuleResource) ValidateConfig(ctx context.Context, req resource.V
func (r *billingRuleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"adjusted_rate": schema.Float64Attribute{
Optional: true,
Description: "The adjusted rate of the Billing Rule. Example value: 0.85",
MarkdownDescription: "The adjusted rate of the Billing Rule. Example value: 0.85",
},
"amount": schema.Float64Attribute{
Optional: true,
Description: "The credit amount for the Billing Rule. Example value: 300",
Expand All @@ -155,6 +150,11 @@ func (r *billingRuleResource) Schema(ctx context.Context, req resource.SchemaReq
Description: "The token of the User who created the Billing Rule.",
MarkdownDescription: "The token of the User who created the Billing Rule.",
},
"percentage": schema.Float64Attribute{
Optional: true,
Description: "The percentage of the cost shown. Example value: 75.0",
MarkdownDescription: "The percentage of the cost shown. Example value: 75.0",
},
"service": schema.StringAttribute{
Optional: true,
Description: "The service of the Billing Rule.",
Expand Down
46 changes: 23 additions & 23 deletions vantage/billing_rule_resource_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type datasourceBillingRuleModel struct {
AdjustedRate types.String `tfsdk:"adjusted_rate"`
Percentage types.String `tfsdk:"percentage"`
Amount types.String `tfsdk:"amount"`
Category types.String `tfsdk:"category"`
ChargeType types.String `tfsdk:"charge_type"`
Expand All @@ -28,11 +28,11 @@ type datasourceBillingRuleModel struct {
type billingRuleModel resource_billing_rule.BillingRuleModel

func (m *billingRuleModel) toDatasourceModel() datasourceBillingRuleModel {
adjustedRate := strconv.FormatFloat(m.AdjustedRate.ValueFloat64(), 'g', -1, 64)
percentage := strconv.FormatFloat(m.Percentage.ValueFloat64(), 'g', -1, 64)
amount := strconv.FormatFloat(m.Amount.ValueFloat64(), 'g', -1, 64)

return datasourceBillingRuleModel{
AdjustedRate: types.StringValue(adjustedRate),
Percentage: types.StringValue(percentage),
Amount: types.StringValue(amount),
Category: m.Category,
ChargeType: m.ChargeType,
Expand All @@ -50,15 +50,15 @@ func (m *billingRuleModel) toDatasourceModel() datasourceBillingRuleModel {
func (m *billingRuleModel) applyPayload(_ context.Context, payload *modelsv2.BillingRule) diag.Diagnostics {
m.Token = types.StringValue(payload.Token)
m.Title = types.StringValue(payload.Title)
if payload.AdjustedRate != "" {
rate, err := strconv.ParseFloat(payload.AdjustedRate, 64)
if payload.Percentage != "" {
rate, err := strconv.ParseFloat(payload.Percentage, 64)
if err != nil {
d := diag.Diagnostics{}
d.AddError("error converting rate to int", err.Error())
return d
}

m.AdjustedRate = types.Float64Value(rate)
m.Percentage = types.Float64Value(rate)
}

if payload.Amount != "" {
Expand Down Expand Up @@ -101,28 +101,28 @@ func (m *billingRuleModel) applyPayload(_ context.Context, payload *modelsv2.Bil

func (m *billingRuleModel) toCreateModel(_ context.Context, _ *diag.Diagnostics) *modelsv2.CreateBillingRule {
return &modelsv2.CreateBillingRule{
AdjustedRate: m.AdjustedRate.ValueFloat64Pointer(),
Amount: m.Amount.ValueFloat64Pointer(),
Category: m.Category.ValueStringPointer(),
ChargeType: m.ChargeType.ValueStringPointer(),
Service: m.Service.ValueStringPointer(),
StartPeriod: m.StartPeriod.ValueStringPointer(),
SubCategory: m.SubCategory.ValueStringPointer(),
Title: m.Title.ValueStringPointer(),
Type: m.Type.ValueStringPointer(),
Percentage: m.Percentage.ValueFloat64Pointer(),
Amount: m.Amount.ValueFloat64Pointer(),
Category: m.Category.ValueStringPointer(),
ChargeType: m.ChargeType.ValueStringPointer(),
Service: m.Service.ValueStringPointer(),
StartPeriod: m.StartPeriod.ValueStringPointer(),
SubCategory: m.SubCategory.ValueStringPointer(),
Title: m.Title.ValueStringPointer(),
Type: m.Type.ValueStringPointer(),
}
}

func (m *billingRuleModel) toUpdateModel(_ context.Context, _ *diag.Diagnostics) *modelsv2.UpdateBillingRule {

return &modelsv2.UpdateBillingRule{
AdjustedRate: m.AdjustedRate.ValueFloat64(),
Amount: m.Amount.ValueFloat64(),
Category: m.Category.ValueString(),
ChargeType: m.ChargeType.ValueString(),
Service: m.Service.ValueString(),
StartPeriod: m.StartPeriod.ValueString(),
SubCategory: m.SubCategory.ValueString(),
Title: m.Title.ValueString(),
Percentage: m.Percentage.ValueFloat64(),
Amount: m.Amount.ValueFloat64(),
Category: m.Category.ValueString(),
ChargeType: m.ChargeType.ValueString(),
Service: m.Service.ValueString(),
StartPeriod: m.StartPeriod.ValueString(),
SubCategory: m.SubCategory.ValueString(),
Title: m.Title.ValueString(),
}
}
14 changes: 7 additions & 7 deletions vantage/billing_rule_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ func TestAccBillingRule_basic(t *testing.T) {
),
},
{ // create adjustment
Config: testAccBillingRule_adjustment("test3", "service", "category", 0.5),
Config: testAccBillingRule_adjustment("test3", "service", "category", 50),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "title", "test3"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "type", "adjustment"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "service", "service"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "category", "category"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "adjusted_rate", "0.5"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "percentage", "50"),
resource.TestCheckResourceAttrSet("vantage_billing_rule.test_adjustment", "token"),
),
},
{ // update existing adjustment rule
Config: testAccBillingRule_adjustment("test4", "service2", "category2", 0.6),
Config: testAccBillingRule_adjustment("test4", "service2", "category2", 60),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "title", "test4"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "service", "service2"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "category", "category2"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "adjusted_rate", "0.6"),
resource.TestCheckResourceAttr("vantage_billing_rule.test_adjustment", "percentage", "60"),
),
},
{
Expand Down Expand Up @@ -86,16 +86,16 @@ resource "vantage_billing_rule" "test_exclusion" {
`, title, chargeType)
}

func testAccBillingRule_adjustment(title, service, category string, adjustedRate float32) string {
func testAccBillingRule_adjustment(title, service, category string, percentage float32) string {
return fmt.Sprintf(`
resource "vantage_billing_rule" "test_adjustment" {
title = %[1]q
type = "adjustment"
service = %[2]q
category = %[3]q
adjusted_rate = %[4]f
percentage = %[4]f
}
`, title, service, category, adjustedRate)
`, title, service, category, percentage)
}

func testAccBillingRule_charge(title, service, category, subCategory, startPeriod string, amount float32) string {
Expand Down
Loading

0 comments on commit 2968c51

Please sign in to comment.