Quote

Hi @all, how are your weekend ? Stay tune okay, and this week topic will take around wrapper, what is this and what we can deal with warpper to push efficiency for your work with them. Let digest about that πŸ‘πŸ‘πŸ‘

What is wrapper ? Is it a design pattern ?

Quote

So a lot of question when you learn about new things in technologies, that make this game become unique and interested in something, and wrapper is not exception

First of all, you may need some articles talk about wrapper, let’s see what is it. Check the down below

Info

Wrapper

A wrapper is a programming language function for encapsulating and organizing elements within a well-defined interface. You can understand wrapper become factor to call a second subroutine or a system call with little or no additional computation

Wrapper, is one most of things can make your time when programing to become shorten and you will handle multiple things with wrapper via interface that provide. Why you need that, let define that through example in refactoring.guru

Example

Imagine that you’re working on a notification library which lets other programs notify their users about important events.

When you want to open or add a new provider to your notification class, it make you hard to change logical of your service, such as if your client want facebook and slack notification but your service just provide only sms, that will make you hard to control or changing big deals when you want. You can handle that to add more subclasses to handle that, but accidentally, you make the code bloated and difficult to control. That is reason why you need Decorator or Wrapper Pattern

Why and what useful wrapper can bring back ?

Follow the example, you can understand the problems when you want make template of your functionality or your application, additional will not the best solution to pull back anything follow only concept. But do you think about Inheritance, an extend class ?

Let make the deals with Inheritance and subclasses before you dive deeper to wrapper pattern

Follow the instruction refactoring.guru, Inheritance will the first thing you need to reserve when think about change behavior of object, but it actually exists several things you need to be awareness

  • Inheritance can’t change behavior in runtime, it means you can only replace whole object with another different classes
  • You will have one parent class when use inheritance, most of languages does support inherit behavior from multiple classes at the same time

Quote

I do not familiar with much about inheritance because I am not working like Developer with long time, so maybe I talk about wrong, but in my opinion you will have mess up with inheritance, but can’t negate that one is the strongest kinds in OOP (Object Oriented Programing)

Additional, you can have another solution which fix problem inheritance meet with patterns called Aggregation and Composition. You can read more at theses articles

And that means, you have superb stand behind as components to make Decorator or Wrapper pattern

And Yup, you have wrapper is an object that can be linked with some target object. The wrapper contains the same set of methods as the target and delegates to it all requests it receives. However, the wrapper may alter the result by doing something either before or after it passes the request to the target.

To make wrapper become helpful, It comes from property of wrapper to make the wrapper’s reference field accept any object that follows that interface. This will let you cover an object in multiple wrappers, adding the combined behavior of all the wrappers to it.

Quote

I dunno this is right or not, but you can image when you want upgrade methodology but not want to make big difference for your functionals, you will not touch any about original function, you just make a change on the components you want base on the decorate with take from original function.

For example, You can dynamic and give safety when you want to upgrade version from 1.x β‡’ 2.x like me DevOps will work Gradle, Wrapper of Gradle will take responsibility to handle when you want to use between 6.x β‡’ 8.x and it will install new things and make functionality base on original function but on different version

Like notification, you can see it will make different between multiple method base decorator which have original function from notifier class, you can make big deals when create multiple version just base on decorator instead of root. On the client side, just use specific decorator and do not care about root or decorator one. That make wrapper is such helpful when you design a pattern for your application

Conclusion

That why you need wrapper or decorator when you want to expand or change behavior of your class or functionality to different method but keep the same work, it just alternative for inheritance. I know you will understand that more than me, let give evident and reason when you need that from refactoring.guru, and do not hesitate the example from this website, super cool. Let brief them to some concludes, Okay πŸ€”πŸ€”

  • Use the Decorator pattern when you need to be able to assign extra behaviors to objects at runtime without breaking the code that uses these objects.
  • Β Use the pattern when it’s awkward or not possible to extend an object’s behavior using inheritance.

How about the wrapper in DevOps ? Any use

Honestly to say, I dunno bro but with wrapper is have seen some optional, and I can be listed of them we can learn from them to understand Role or function of them in DevOps

With wrapper, base on DevOps culture, it submits a role for

  • Dynamic the way to setup and control parameter to give for root tools - Gradlew
  • Make the your code of configuration to becoming Dry, and can easily to reuse - Terragrunt
  • Reduce the complexity of your command-lines - Shell Wrapper

That has different between them, just release to do with specify purpose and just one thing, users can have the simple way to reach goals when use original tools but dynamic way through wrapper

So go with me to specific to give perspective different between of them 🀭

Gradlew

So like from above, Gradlew is called wrapper of Gradle, which can bring up you some features

  • Download and install the correctΒ gradleΒ version
  • Parse the arguments
  • Call aΒ gradleΒ task

That make your game become easily, because gradlew will help you install base Gradle base on your configuration, and execution method or functionality just via gradlew and you do not need to care about install Gradle in your host. gradlew support both file to execution gradlew.bat (Windows CMD) or gradlew (Mac or Linux)

To simple create wrapper for Gradle, easily job just

gradle wrapper

that will create gradle directory, and put wrapper inside like these

But when you work with Android project, It will auto generate when you create project base on your selection configuration when you create project.

You can play with gradlew via properties file at gradle/wrapper/gradle-wrapper.properties, it will store configuration about

  • TheΒ server hostingΒ the Gradle distribution.
  • TheΒ type of Gradle distribution. By default, theΒ -binΒ distribution contains only the runtime but no sample code and documentation.
  • TheΒ Gradle versionΒ used for executing the build. By default, theΒ wrapperΒ task picks the same Gradle version used to generate the Wrapper files.
  • Optionally, aΒ timeoutΒ in ms used when downloading the Gradle distribution.
  • Optionally, aΒ booleanΒ to set theΒ validation of the distributionΒ URL.

With gradlew, it brings you more helpful when you separate the source code between teammates, or can be easily setup your build pipeline for CI/CD on different project, mostly use for Java or Android application, that actually make your game become simplify and easily to handle

You can do more such superb thing with gradlew

Terragrunt

Terragrunt comes up to solution, a wrapper for Terraform and Opentofu which can help you orchestration infrastructure as Infrastructure as Code

Terragrunt brings you some features which help you do helpful jobs instead of terraform and opentofu

  • Keep your Terraform code DRY
  • Keep your backend configuration DRY
  • Keep your Terraform CLI arguments DRY
  • Execute Terraform commands on multiple modules at once
  • Before & After hooks

With Terragrunt, your work with terraform can be reduced and simple than ever, Terragrunt support us about auto-init and auto-retry so it means you can skip init step in terraform work flow and can do plan for firstly.

Note

Terragrunt will forward almost all commands, arguments, and options directly to Terraform, but based on the settings in your terragrunt.hcl file.

That such wonderful comes up with DRY (Don’t Repeat yourself), It means terragrunt will help you remove most of copy/paste between different module or environment, It can help you prevent the mistake when you can overwrite each others module between environment, that terrible πŸ₯ΆπŸ₯ΆπŸ₯Ά

Follow the instruction from Terragrunt, To use Terragrunt, add a singleΒ terragrunt.hclΒ file to the root of your repo, in theΒ stageΒ folder, and oneΒ terragrunt.hclΒ file in each module folder

stage
β”œβ”€β”€ terragrunt.hcl
β”œβ”€β”€ frontend-app
β”‚   β”œβ”€β”€ main.tf
β”‚   └── terragrunt.hcl
└── mysql
    β”œβ”€β”€ main.tf
    └── terragrunt.hcl

Now you can define yourΒ backendΒ configuration just once in the rootΒ terragrunt.hclΒ file

# stage/terragrunt.hcl
remote_state {
  backend = "s3"
  generate = {
    path      = "backend.tf"
    if_exists = "overwrite_terragrunt"
  }
  config = {
    bucket = "my-terraform-state"
 
    key = "${path_relative_to_include()}/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "my-lock-table"
  }
}

Do you see on the Line 11, key will point to via path_relative_to_include(), which will automatically setΒ keyΒ to the relative path between the rootΒ terragrunt.hclΒ and the child module (so your Terraform state folder structure will match your Terraform code folder structure, which makes it easy to go from one to the other).

TheΒ generateΒ attribute is used to inform Terragrunt to generate the Terraform code for configuring the backend. When you run any Terragrunt command, Terragrunt will generate aΒ backend.tfΒ file with the contents set to theΒ terraformΒ block that configures theΒ s3Β backend, just like what we had before in eachΒ main.tfΒ file.

The final step is to update each of the childΒ terragrunt.hclΒ files to tell them to include the configuration from the rootΒ terragrunt.hcl

# stage/mysql/terragrunt.hcl
include "root" {
  path = find_in_parent_folders()
}

TheΒ find_in_parent_folders()Β helper will automatically search up the directory tree to find the rootΒ terragrunt.hclΒ and inherit theΒ remote_stateΒ configuration from it.

And that do same thing to prevent DRY code when Terraform can’t support us but Terragrunt can. That’s a good things and wonderful functional which Terragrunt bring up. Just take a look few example via links down below

Shell wrapper

Like I have related above about shell wrapper, that simple way to reuse system command or utility, that accepts and passes a set of parameters to that command. Mostly use sed and awk command to do incredible thing

When use shell wrapper, you have mostly kind of helpful thing, like

  1. Time saving.
  2. Customize collection of *nix commands.
  3. Passing default arguments to binaries or 3rd party apps.
  4. Call to start the desired job.
  5. Wrappers are perfect when tools or command that require customized environmental variable settings, system controls or job-submission parameter.
  6. Useful in HPC, clustered environment, *nix sysadmin, and scientific research.

On my position, I work command regularly so that trick and wrapper can help me reduce a lot of work when execute command. But do not familiar right now but if have any updated, please follow Linux profile, and I will give update about linux configuration and wrapper on that articles

Conclusion

Success

That all for today, Not long and but I have time to learning around new thing, I hope you can do same thing. Wrapper can make a graceful thing, and help a lot for perspective to do great pattern to design your application, API or such as a build or IaC tool, that actually make the game become to interesting and shorten time to handle lots of job to do different purpose in same construction, just dynamic that one

Quote

That be graceful and happiness to contribute and sharing with you about wrapper, on next stage maybe I will bring a quite fun thing again, let wait. So @all, stay safe, happy hacking and I will see you soon. Bye bye πŸ‘‹πŸ‘‹πŸ‘‹