Keycloak Custom Decorator for Resources - nestjs

Is there any way to customize decorator of resources i-e #Resource() Decorator in nest-key cloak-connect.
In this #Resource() decorator we have to pass name of resource in string,
I have tried it with hard coded value but I want to make it dynamic
example:
#Resource('my bag') // name of resource
and I want to make it dynamic like =>
#Resource(${Resource-Name}) // It will pick resource name automatically or is there any way to pass resource name here in that variable or function?
any guidance will be appreciated.

Related

Why resource provides has two labels - terraform

I am currently learning terraform, still i can't understand, why resource provider block needs two labels as below ? What are the use cases for two labels ?
resource "aws_instance" "example"
Regards,
Chin
You are telling Terraform that you are declaring a resource of type aws_instance with name example. Only one of those things is a label, the other is the type of resource you want Terraform to create and manage for you.
I am assuming that you are familiar with OOP concepts. So, I am gonna use java to explain.
Lets say you have a java class box defined.
class box {
double length;
double width;
double height;
}
Now you usually need to create an object to use that class or to refer that class. You can have multiple object of the same class. right ?
new myBox1 = box(length=11,width=7,height=5)
new myBox2 = box(length=9,width=4,height=8)
Now lets try to understand the terraform.
There are three components in resource "aws_instance" "example"
resource : It tells the terraform core engine that you want to use a terraform resource. In your case its an aws_instance. It could have also been any other resource like aws_s3 or aws_rds_cluster etc. Imagine it's your keyword class.
aws_instance : It's your selected resource you wanna use. Imagine it's your class box.
example : It's the identifier you are gonna use to store your terraform state. The instances may have separate values. One can be t2.micro and another can be something else. Imagine it as your object myBox1 or myBox2.
Terraform stores the current state of your infrastructure in a state file be it local or remote. And, in practice you will have multiple aws instances. How can you or terraform differentiate between them. It identifies each of your aws_instace by the name you provide. For instance,
resource "aws_instance" "example1"
resource "aws_instance" "example2"
aws_instance.example1 and aws_instance.example2 are two different object or two different instances. For the same reason you can not have the same identifier. (If your terraform state is same).
In short, think of that as class and object. :)

How to use an Azure Arm Template Output in a variable?

I'm trying to solve a problem I'm having with an Azure ARM template, whereby I need to capture the output of a queried resource in my ARM template, then in that same template, feed that output into a variable / inject that output into a script on another resource that depends on it.
An example -
"outputs":{
"downloadLocation": {
"type": "string",
"value": "[reference(resourceId('randomResource', variables('ResourceName'))).downloadLocation]"
}
}
And
variables: {
"downloadLocation": "[outputs('downloadLocation')]"
}
This variable is then referenced in one of the resources that depends on the source of the queried output.
The downloadLocation can't be formatted in anyway, it contains several signatures and unpredictable strings.
FYI - the below code stops the arm template from being used, by producing the error 'The template function 'outputs' is not valid.'
I'm not locked to storing it as an output, I just need to be able to use that value in another resource - however it's achieved!
The only other route I know would work, but I don't want to explore yet, is that the output could be stored in a file somewhere, then a subsequent script picks it up and injects it into a second ARM template.
If there is a way to use this please let me know, it would help me out significantly!
Thanks
I have found the solution.
The reference function can be called when inside a resource field. So I've had to use reference function as seen above, but it can't be stored in a variable or parameter, it instead needs to be called directly in the resource that's requiring it. Which in this case was the osProfile - customData field.

Creating a list of existing aws_instance resources in Terraform

I'm currently trying to find a way to create a list of aws_instance type resources.
The ec2s are already configured. I imported them using Terraform, but I would like to have them as a single list, so I could perform actions like remote-exec on all of them at the same time.
I'm just not sure how can I declare a list type variable to include all existing aws_instance resources.
Any help would be much appreciated! Cheers.
EDIT:
As asked, I'm going to add some of the HCL:
As I stated, each instance is imported from an existing aws configuration.
This means I already have aws_instance blocks for each ec2.
resource "aws_instance" "ec2_1" {
*truncated*
}
I was wondering if there was a way to take these resources, and append them into a list.
I would like to create this list in order to perform actions on all instances at once, using the Provisioner remote-exec.
I tried creating a variable, but I'm afraid it doesn't function that way:
variable "ec2_list" {
type = list
default = [aws_instance.ec2_1, aws_instance.ec2_2,...]
}
But variables from main.tf cannot be used for variables.
I'm just curious if you can make a general resource to create a list under.
If you know anything, please let me know.

Terraform config file : Using variable inside a variable

I am tring to create a terraform configuration file to create an ec2 instance. I am using the variables.tf file to put all my variables. It works for most of the cases but there are two cases which I am not able to achieve. Any pointers is much appreciated.
1.using variable for aws instance name. Using var.service_name or "${service_name}" does not work.
resource "aws_instance" var.service_name {
ami = "ami-010fae13a16763bb4"
instance_type = "t2.micro"
.....
}
This post explains that resource name cannot be variables. But this is pretty old. Not sure if this is still the case.
2.Using variable inside another variable. For example I have connection parameters defined like this
connection {
host = "${aws_instance.terraformtest.public_ip}"
type = "ssh"
user = "ec2-user"
private_key = "${file("C:/Users/phani/Downloads/microservices.pem")}"
}
This works. I am using the ip generated from aws instance resource. But If i use it like this. It doesn't work
host = "${aws_instance.${service_name}.public_ip}"
Am I missing something or is there a workaround
Resource names in Terraform are required to be constants. In practice this usually isn't a problem, because a resource name only needs to be unique within a given module and so it shouldn't ever be necessary for a caller to customize what a resource is named in a child module. In the case where a particular resource is the primary object declared by a module, a common idiom is to name the resource "main", like resource "aws_instance" "main".
In situations where you need to declare several instances whose definitions share a common configuration, you can use for_each or count to produce multiple instances from a same resource block. In that case, the resource addresses include an additional "index" component, like aws_instance.example["foo"] or aws_instance.example[0], where the index can be a variable to dynamically select one of the instances produced by a single resource block.

Need to pass arguments to method decorator from inherited class

I am working in pycharm, and am trying to pass arguments to a pagination decorator which i used to decorate a method of a class.
I have created a class where to abstract the attributes of a given resource the API returns. I also have a dictionary resource_vars where I keep track of the different variables that particular resource has been given by the api developer (for example, the name I want to give to that resource, what is the pagination limit for that resource etc.).
class Resource:
def __init__(self, resource, resource_vars):
self.resource = resource
self.resource_vars = resource_vars
self.resource_name = resource_vars["name"]
self.total_results = resource["total_results"] if "total_results" in resource else None
self.max_items_allowed = resource_vars["items_per_page"] if "items_per_page" in resource_vars else None
I then have a second class, where I am trying to implement the factory design pattern, because every Resource returned has a certain set of conditionals and mutation that needs to happen before inserting the object in the database.
class ResourceDispatcher(Resource): # My understanding is that i am inheriting the class Resource attributes here.
def dispatch(self, resource, resource_name):
dispatcher = get_dispatcher(resource_name) # factory function defined outside the class.
return dispatcher(resource)
# pycharm underlines total_results and max_items_allowed in red with
# warning "unresolved reference total_results"
#paginate(pages=total_results, items_per_page=max_items_allowed)
def dispatch_object_1(...) :
...
The warning pops up when I am trying to decorate the method dispatch_object_1.
I am wondering if it is at all possible to pass arguments to a decorator using attributes of an inherited class. If someone has any idea of how I could refactor to obtain this results I'd be happy to take them into account!

Resources