Skip to content

Commit

Permalink
fix(provider-generator): refactor logic to determine if a block is op…
Browse files Browse the repository at this point in the history
…tional or required
  • Loading branch information
ansgarm authored and DanielMSchmidt committed Apr 8, 2024
1 parent 6dd95ca commit 0c2dde4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5567,6 +5567,12 @@ export interface SpansMetricConfig extends cdktf.TerraformMetaArguments {
*/
readonly name: string;
/**
* compute block
*
* Docs at Terraform Registry: {@link https://registry.terraform.io/providers/datadog/datadog/latest/docs/resources/spans_metric#compute SpansMetric#compute}
*/
readonly compute?: SpansMetricCompute;
/**
* filter block
*
* Docs at Terraform Registry: {@link https://registry.terraform.io/providers/datadog/datadog/latest/docs/resources/spans_metric#filter SpansMetric#filter}
Expand Down Expand Up @@ -6039,6 +6045,7 @@ export class SpansMetric extends cdktf.TerraformResource {
forEach: config.forEach
});
this._name = config.name;
this._compute.internalValue = config.compute;
this._filter.internalValue = config.filter;
this._groupBy.internalValue = config.groupBy;
}
Expand All @@ -6065,11 +6072,21 @@ export class SpansMetric extends cdktf.TerraformResource {
return this._name;
}

// compute - computed: false, optional: false, required: false
// compute - computed: false, optional: true, required: false
private _compute = new SpansMetricComputeOutputReference(this, "compute");
public get compute() {
return this._compute;
}
public putCompute(value: SpansMetricCompute) {
this._compute.internalValue = value;
}
public resetCompute() {
this._compute.internalValue = undefined;
}
// Temporarily expose input value. Use with caution.
public get computeInput() {
return this._compute.internalValue;
}

// filter - computed: false, optional: true, required: false
private _filter = new SpansMetricFilterOutputReference(this, "filter");
Expand Down Expand Up @@ -6110,6 +6127,7 @@ export class SpansMetric extends cdktf.TerraformResource {
protected synthesizeAttributes(): { [name: string]: any } {
return {
name: cdktf.stringToTerraform(this._name),
compute: spansMetricComputeToTerraform(this._compute.internalValue),
filter: spansMetricFilterToTerraform(this._filter.internalValue),
group_by: cdktf.listMapper(spansMetricGroupByToTerraform, true)(this._groupBy.internalValue),
};
Expand All @@ -6123,6 +6141,12 @@ export class SpansMetric extends cdktf.TerraformResource {
type: "simple",
storageClassType: "string",
},
compute: {
value: spansMetricComputeToHclTerraform(this._compute.internalValue),
isBlock: true,
type: "struct",
storageClassType: "SpansMetricCompute",
},
filter: {
value: spansMetricFilterToHclTerraform(this._filter.internalValue),
isBlock: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ test("generate a security group", async () => {
expect(output).toMatchSnapshot();
});

test.only("generate a datadog spans metric", async () => {
test("generate a datadog spans metric", async () => {
const code = new CodeMaker();
const workdir = fs.mkdtempSync(path.join(os.tmpdir(), "sg.test"));
const spec = JSON.parse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,6 @@ class Parser {
public renderAttributesForBlock(parentType: Scope, block: Block) {
const attributes = new Array<AttributeModel>();

console.log("render attributes for block", parentType.name);

for (const [terraformAttributeName, att] of Object.entries(
block.attributes || {}
)) {
Expand Down Expand Up @@ -503,8 +501,6 @@ class Parser {
);
}

console.log(attributes);

return attributes;

function attributeForBlockType(
Expand All @@ -518,22 +514,19 @@ class Parser {
let optional: boolean;
let required: boolean;

console.log("attribute for block type", terraformName, name);

switch (blockType.nesting_mode) {
case "single":
optional = !struct.attributes.some((x) => !x.optional);
required = !struct.attributes.some((x) => !x.required);

console.log("optional", optional, "required", required);

// The bug #3570 lies here, as both optional and required evaluate to false
// (this causes the computed block to not be part of assignableAttributes and thus skipped in the generated code)
// I think we need to adjust the logic for optional / required here. I don't know if it even makes sense to
// set required and optional based on the attributes in the block, but if there's no better way / we need to keep this
// I'd propose to extend the current logic: If both optional and required are false, set optional to true IF at least one
// This is for bug #3570 as both optional and required evaluate to false under some circumstances
// (this then causes the computed block to not be part of assignableAttributes and thus skipped in the generated code)
// Hence: If both optional and required are false, set optional to true IF at least one
// attribute in the block has optional = true or required = true, as this would mean that at least something can be set
// and the block is not all computed.
if (!optional && !required) {
optional = struct.attributes.some((x) => x.optional || x.required);
}

return new AttributeModel({
name,
Expand Down

0 comments on commit 0c2dde4

Please sign in to comment.