PrintResultsJSONWithGuardrail writes the estimate results as JSON, optionally embedding a guardrail verdict under the top-level "guardrail" key. Passing a nil guardrail omits the field entirely (identical to PrintResultsJSON).
(results []resources.EstimateResult, guardrail *GuardrailResult)
| 107 | // embedding a guardrail verdict under the top-level "guardrail" key. Passing a |
| 108 | // nil guardrail omits the field entirely (identical to PrintResultsJSON). |
| 109 | func PrintResultsJSONWithGuardrail(results []resources.EstimateResult, guardrail *GuardrailResult) { |
| 110 | jsonResources := make([]JSONResource, 0, len(results)) |
| 111 | for _, r := range results { |
| 112 | jr := JSONResource{ |
| 113 | Name: r.ResourceName, |
| 114 | SubLabel: r.SubLabel, |
| 115 | Product: r.Product, |
| 116 | RegionFallback: r.RegionFallback, |
| 117 | Props: r.Props, |
| 118 | Status: r.StatusMsg, |
| 119 | } |
| 120 | |
| 121 | // Extract region from Props if present. |
| 122 | if r.Props != nil { |
| 123 | if region, ok := r.Props["region"]; ok { |
| 124 | jr.Region = region |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if r.OnDemandRate.IsPositive() { |
| 129 | jr.OnDemandRate = r.OnDemandRate.String() |
| 130 | } |
| 131 | |
| 132 | if r.HourlyQty.IsPositive() { |
| 133 | jr.HourlyQty = r.HourlyQty.String() |
| 134 | } |
| 135 | |
| 136 | if r.IsUsageBased { |
| 137 | jr.IsUsageBased = true |
| 138 | jr.UsageUnit = r.UsageUnit |
| 139 | jr.UsageQty = r.UsageQty |
| 140 | jr.UsageDefault = r.UsageDefault |
| 141 | jr.UsageMonthly = r.UsageMonthly.String() |
| 142 | } |
| 143 | |
| 144 | for _, p := range r.Prices { |
| 145 | jp := JSONPriceEntry{ |
| 146 | Model: p.Model, |
| 147 | PurchaseOption: p.PurchaseOption, |
| 148 | Term: p.Term, |
| 149 | UpfrontFee: p.UpfrontFee, |
| 150 | RatePerHr: p.RatePerHr.String(), |
| 151 | Unit: p.Unit, |
| 152 | IsCurrent: p.IsCurrent, |
| 153 | } |
| 154 | for _, t := range p.Tiers { |
| 155 | jp.Tiers = append(jp.Tiers, JSONTier{ |
| 156 | StartRange: t.StartRange, |
| 157 | EndRange: t.EndRange, |
| 158 | Price: t.Price, |
| 159 | }) |
| 160 | } |
| 161 | jr.Prices = append(jr.Prices, jp) |
| 162 | } |
| 163 | |
| 164 | if jr.IsUsageBased { |
| 165 | for _, jp := range jr.Prices { |
| 166 | if jp.IsCurrent { |
no test coverage detected