Skip to content

Commit

Permalink
Merge pull request #565 from pulumi/wjones/564
Browse files Browse the repository at this point in the history
Support negative literals when generating YAML
  • Loading branch information
lunaris authored Apr 9, 2024
2 parents 3017e34 + 4bf5392 commit 953c9ec
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### Improvements

### Bug Fixes

- Support negative literals [#565](https://github.com/pulumi/pulumi-yaml/pull/565)
21 changes: 21 additions & 0 deletions pkg/pulumiyaml/codegen/gen_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ettle/strcase"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -523,6 +524,26 @@ func (g *generator) expr(e model.Expression) syn.Node {
panic("unreachable")
}

case *model.UnaryOpExpression:
if e.Operation == hclsyntax.OpNegate {
operand := e.Operand
switch operand := operand.(type) {
case *model.LiteralValueExpression:
if operand.Value.Type().Equals(cty.Number) {
v := operand.Value.AsBigFloat()
f, _ := v.Float64()
return syn.Number(-f)
}
}
}

YAMLError{
kind: "Unsupported unary operation",
detail: fmt.Sprintf("Invalid unary application encountered (only negation of numeric literals is supported at present): %v", e),
rng: e.SyntaxNode().Range(),
}.AppendTo(g)
return syn.String("Unimplemented")

case *model.FunctionCallExpression:
return g.function(e)
case *model.RelativeTraversalExpression:
Expand Down
8 changes: 7 additions & 1 deletion pkg/pulumiyaml/codegen/gen_program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,13 @@ func TestGenerateProgram(t *testing.T) {
OutputFile: "Main.yaml",
Check: check,
GenProgram: GenerateProgram,
TestCases: filter(test.PulumiPulumiProgramTests),
TestCases: append(
filter(test.PulumiPulumiProgramTests),
test.ProgramTest{
Directory: "negative-literals",
Description: "Negative literals in Pulumi Programs",
},
),
},
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
resource ecsTarget1 "aws:appautoscaling:Target" {
maxCapacity = 4
minCapacity = 1
resourceId = "service/clustername/serviceName"
scalableDimension = "ecs:service:DesiredCount"
serviceNamespace = "ecs"
}

resource ecsPolicy1 "aws:appautoscaling:Policy" {
name = "scale-down-integers"
policyType = "StepScaling"
resourceId = ecsTarget1.resourceId
scalableDimension = ecsTarget1.scalableDimension
serviceNamespace = ecsTarget1.serviceNamespace

stepScalingPolicyConfiguration = {
adjustmentType = "ChangeInCapacity"
cooldown = 60
metricAggregationType = "Maximum"

stepAdjustments = [
{
metricIntervalUpperBound = 0
scalingAdjustment = -1
}
]
}
}

resource ecsTarget2 "aws:appautoscaling:Target" {
maxCapacity = 4
minCapacity = 1
resourceId = "service/clustername/serviceName"
scalableDimension = "ecs:service:DesiredCount"
serviceNamespace = "ecs"
}

resource ecsPolicy2 "aws:appautoscaling:Policy" {
name = "scale-down-floats"
policyType = "StepScaling"
resourceId = ecsTarget2.resourceId
scalableDimension = ecsTarget2.scalableDimension
serviceNamespace = ecsTarget2.serviceNamespace

stepScalingPolicyConfiguration = {
adjustmentType = "ChangeInCapacity"
cooldown = 60
metricAggregationType = "Maximum"

stepAdjustments = [
{
metricIntervalUpperBound = 0
scalingAdjustment = -2.0
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
resources:
ecsTarget1:
type: aws:appautoscaling:Target
properties:
maxCapacity: 4
minCapacity: 1
resourceId: service/clustername/serviceName
scalableDimension: ecs:service:DesiredCount
serviceNamespace: ecs
ecsPolicy1:
type: aws:appautoscaling:Policy
properties:
name: scale-down-integers
policyType: StepScaling
resourceId: ${ecsTarget1.resourceId}
scalableDimension: ${ecsTarget1.scalableDimension}
serviceNamespace: ${ecsTarget1.serviceNamespace}
stepScalingPolicyConfiguration:
adjustmentType: ChangeInCapacity
cooldown: 60
metricAggregationType: Maximum
stepAdjustments:
- metricIntervalUpperBound: 0
scalingAdjustment: -1
ecsTarget2:
type: aws:appautoscaling:Target
properties:
maxCapacity: 4
minCapacity: 1
resourceId: service/clustername/serviceName
scalableDimension: ecs:service:DesiredCount
serviceNamespace: ecs
ecsPolicy2:
type: aws:appautoscaling:Policy
properties:
name: scale-down-floats
policyType: StepScaling
resourceId: ${ecsTarget2.resourceId}
scalableDimension: ${ecsTarget2.scalableDimension}
serviceNamespace: ${ecsTarget2.serviceNamespace}
stepScalingPolicyConfiguration:
adjustmentType: ChangeInCapacity
cooldown: 60
metricAggregationType: Maximum
stepAdjustments:
- metricIntervalUpperBound: 0
scalingAdjustment: -2

0 comments on commit 953c9ec

Please sign in to comment.