question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement block-level iterators

See original GitHub issue

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave “+1” or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

To support dynamic list iterations on the block / resource level we will introduce a new API, called Iterator.

In this issue we are building the block-level Iterator API. To create dynamic blocks one can use iterator.dynamic([…]) and the block will be repeated for each item in the list.

import { TerraformStack, Iterator } from "cdktf";
import { S3Bucket, DataAwsSubnets, DataAwsAllowedOrigins } from "./.gen/providers/aws-ficticious";
import { Construct } from "constructs";

export class MyStack extends TerraformStack {
 constructor(scope: Construct, id: string) {
   super(scope, id);

  // Fictitious data source that returns [{method, origin}]
   const allowedOrigins = new DataAwsAllowedOrigins(
     this,
     "possibleOrigins",
     {}
   );
   const allowedOriginsIterator = Iterator.fromList(allowedOrigins.origins);
   new S3Bucket(this, "my-bucket", {
     corsRule: allowedOriginsIterator.dynamic(
       {
         allowedMethods: [allowedOriginsIterator.getStringProperty("method")],
         allowedOrigins: [allowedOriginsIterator.getStringProperty("origin")],
         maxAgeSeconds: 42,
       },
     ),
   });
 }
}

References

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ansgarmcommented, Jul 13, 2022

Hi @RichiCoder1, yeah, I thought about that, too. However, as we’re constrained by JSII (which translates our TypeScript bindings to the other languages we support), we can’t directly use callbacks, but rather need to pass what would for example be the equivalent of an anonymous inner class in Java. So it would need to look like this:

new S3Bucket(this, "my-bucket", {
  corsRule: allowedOriginsIterator.forEach(
    { callback: it =>
      {
        allowedMethods: [it.getStringProperty("method")],
        allowedOrigins: [it.getStringProperty("origin")],
        maxAgeSeconds: 42,
        someOtherAttribute: index++
      },
    }
  ),
});

I’m unsure whether this would still simplify things compared to .dynamic({ ... }) or even .forEach({ ... }).

1reaction
RichiCoder1commented, Jul 13, 2022

@ansgarm that’s fair, JSII strikes again. dynamic seems fine then, considering constraints

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing iterator blocks in C++ (part 1)
Recently I had to port some C# code to C++. The C# code was making a limited use of the .NET framework, so,...
Read more >
Iterators & iterables | Ben Ilegbodu
An iterable is an object that intends to make its sequential elements publicly accessible through iteration interfaces. This object does so by ...
Read more >
Is there a way I can combine two separate iterator blocks to one?
In short, no, you cannot have nested yield iterator within the same method. yield is compiled at the method level.
Read more >
BlockGraph (Soot API)
public abstract class BlockGraph; extends Object; implements DirectedGraph<Block>. Represents the control flow graph of a Body at the basic block level.
Read more >
Terraform HCL Intro 5: Loops with Dynamic Block - BoltOps Blog
For Each Difference: Resource vs Dynamic Block Level. In the previous post, we covered how to use the for_each attribute at the resource ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found