Python binding for Terraform.

Overview

Python libterraform

libterraform libterraform libterraform Test libterraform

Python binding for Terraform.

Installation

$ pip install libterraform

NOTE

  • Please install version 0.3.1 or above, which solves the memory leak problem.
  • This library does not support multithreading.

Usage

Terraform CLI

TerraformCommand is used to invoke various Terraform commands.

Now, support all commands (plan, apply, destroy etc.), and return a CommandResult object. The CommandResult object has the following properties:

  • retcode indicates the command return code. A value of 0 or 2 is normal, otherwise is abnormal.
  • value represents command output. If json=True is specified when executing the command, the output will be loaded as json.
  • json indicates whether to load the output as json.
  • error indicates command error output.

To get Terraform verison:

>>> from libterraform import TerraformCommand
>>> TerraformCommand().version()
<CommandResult retcode=0 json=True>
>>> _.value
{'terraform_version': '1.1.7', 'platform': 'darwin_arm64', 'provider_selections': {}, 'terraform_outdated': False}
>>> TerraformCommand().version(json=False)
<CommandResult retcode=0 json=False>
>>> _.value
'Terraform v1.1.7\non darwin_arm64\n'

To init and apply according to Terraform configuration files in the specified directory:

>>> from libterraform import TerraformCommand
>>> cli = TerraformCommand('your_terraform_configuration_directory')
>>> cli.init()
<CommandResult retcode=0 json=False>
>>> cli.apply()
<CommandResult retcode=0 json=True>

Additionally, run() can execute arbitrary commands, returning a tuple (retcode, stdout, stderr).

>>> TerraformCommand.run('version')
(0, 'Terraform v1.1.7\non darwin_arm64\n', '')
>>> TerraformCommand.run('invalid')
(1, '', 'Terraform has no command named "invalid".\n\nTo see all of Terraform\'s top-level commands, run:\n  terraform -help\n\n')

Terraform Config Parser

TerraformConfig is used to parse Terraform config files.

For now, only supply TerraformConfig.load_config_dir method which reads the .tf and .tf.json files in the given directory as config files and then combines these files into a single Module. This method returns (mod, diags) which are both dict, corresponding to the *Module and hcl.Diagnostic structures in Terraform respectively.

>>> from libterraform import TerraformConfig
>>> mod, _ = TerraformConfig.load_config_dir('your_terraform_configuration_directory')
>>> mod['ManagedResources'].keys()
dict_keys(['time_sleep.wait1', 'time_sleep.wait2'])

Building & Testing

If you want to develop this library, should first prepare the following environments:

  • GoLang (Version 1.17.x or 1.16.x)
  • Python (Version 3.6~3.10)
  • GCC

Then, initialize git submodule:

$ git submodule init
$ git submodule update

pip install necessary tools:

$ pip install poetry pytest

Now, we can build and test:

$ poetry build -f wheel
$ pytest

Why use this library?

Terraform is a great tool for deploying resources. If you need to call the Terraform command in the Python program for deployment, a new process needs to be created to execute the Terraform command on the system. A typical example of this is the python-terraform library. Doing so has the following problems:

  • Requires Terraform commands on the system.
  • The overhead of starting a new process is relatively high.

This library compiles Terraform as a dynamic link library in advance, and then loads it for calling. So there is no need to install Terraform, nor to start a new process.

In addition, since the Terraform dynamic link library is loaded, this library can further call Terraform's internal capabilities, such as parsing Terraform config files.

You might also like...
PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

PRAW: The Python Reddit API Wrapper PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's AP

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

PRAW: The Python Reddit API Wrapper PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's AP

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API.

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API. It allows rapid trading algo development easily, with support for both REST and streaming data interfaces

🖥️ Python - P1 Monitor API Asynchronous Python Client

🖥️ Asynchronous Python client for the P1 Monitor

Volt is yet another discord api wrapper for Python. It supports python 3.8 +

Volt Volt is yet another discord api wrapper for Python. It supports python 3.8 + How to install [Currently Not Supported.] pip install volt.py Speed

Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring files on the Internet to our beloved Google Drive. Based on python-aria-mirror-bot
Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring files on the Internet to our beloved Google Drive. Based on python-aria-mirror-bot

- [ MAYBE UPDATE & ADD MORE MODULE ] Bagas Mirror&Leech Bot Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring file

A python Discord wrapper made in well, python.

discord.why A python Discord wrapper made in well, python. Made to be used by devs who want something a bit more, general. Basic Examples Sending a me

A wrapper for aqquiring Choice Coin directly through a Python Terminal. Leverages the TinyMan Python-SDK.

CHOICE_TinyMan_Wrapper A wrapper that allows users to acquire Choice Coin directly through their Terminal using ALGO and various Algorand Standard Ass

Python On WhatsApp - Run your python codes on whatsapp along with talking to a chatbot
Python On WhatsApp - Run your python codes on whatsapp along with talking to a chatbot

Python On WhatsApp Run your python codes on whatsapp along with talking to a chatbot This is a small python project to run python on whatsapp. and i c

Comments
  • Could not find a version that satisfies the requirement libterraform

    Could not find a version that satisfies the requirement libterraform

    $ pip install libterraform ERROR: Could not find a version that satisfies the requirement libterraform (from versions: none) ERROR: No matching distribution found for libterraform $

    opened by judab5ericom 0
  • ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    getting the following error when trying to compile local Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done /home/judab/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 0.23ubuntu1 is an invalid version and will not be supported in a future release warnings.warn( /home/judab/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 0.1.36ubuntu1 is an invalid version and will not be supported in a future release warnings.warn( Building wheels for collected packages: libterraform Building wheel for libterraform (PEP 517) ... error ERROR: Command errored out with exit status 1: command: /usr/bin/python3 /tmp/tmpjxsbgvg0 build_wheel /tmp/tmpmuv6vjmq cwd: /tmp/pip-req-build-4kwbn1bw Complete output (33 lines):

    sigs.k8s.io/json/internal/golang/encoding/json

    /home/judab/go/pkg/mod/sigs.k8s.io/[email protected]/internal/golang/encoding/json/encode.go:1249:12: sf.IsExported undefined (type reflect.StructField has no field or method IsExported) /home/judab/go/pkg/mod/sigs.k8s.io/[email protected]/internal/golang/encoding/json/encode.go:1255:18: sf.IsExported undefined (type reflect.StructField has no field or method IsExported) - Patching go-plugin package - Building libterraform Traceback (most recent call last): File "build.py", line 72, in build({}) File "build.py", line 54, in build subprocess.check_call( File "/usr/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['go', 'build', '-buildmode=c-shared', '-o=libterraform.so', 'github.com/hashicorp/terraform']' returned non-zero exit status 2. Traceback (most recent call last): File "/tmp/tmpjxsbgvg0", line 280, in main() File "/tmp/tmpjxsbgvg0", line 263, in main json_out['return_val'] = hook(**hook_input['kwargs']) File "/tmp/tmpjxsbgvg0", line 204, in build_wheel return _build_backend().build_wheel(wheel_directory, config_settings, File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/api.py", line 68, in build_wheel return unicode(WheelBuilder.make_in(poetry, Path(wheel_directory))) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 78, in make_in wb.build() File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 106, in build self._build(zip_file) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 152, in _build self._run_build_script(self._package.build_script) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 202, in _run_build_script subprocess.check_call([self.executable.as_posix(), build_script]) File "/usr/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['/usr/bin/python3', 'build.py']' returned non-zero exit status 1.

    ERROR: Failed building wheel for libterraform Failed to build libterraform ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    opened by judab5ericom 0
Releases(v0.4.0)
  • v0.3.1(Apr 19, 2022)

  • v0.3.0(Apr 6, 2022)

    TerraformCommand supports all Terraform commands. Compared with v0.2.1, the current version supports the following commands:

    • fmt
    • force unlock (The corresponding function is force_unlock)
    • graph
    • import (The corresponding function is import_resource)
    • refresh
    • state and all sub commands of state
    • taint
    • untaint
    • test
    • workspace and all sub commands of workspace

    The project adds Makefile so we can use make command, like make init, make test, make build etc.

    Source code(tar.gz)
    Source code(zip)
  • v0.2.1(Mar 31, 2022)

    Implement TerraformCommand which is used to invoke various Terraform commands.

    Currently, supports version and all main commands (init, validate, plan, show, apply and destroy), returning a CommandResult object. The CommandResult object has the following properties:

    • retcode indicates the command return code. A value of 0 or 2 is normal, otherwise is abnormal.
    • value represents command output. If json=True is specified when executing the command, the output will be loaded as json.
    • json indicates whether to load the output as json.
    • error indicates command error output.
    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Mar 11, 2022)

    Implement TerraformConfig which is used to parse Terraform config files.

    For now, only supply TerraformConfig.load_config_dir method which reads the .tf and .tf.json files in the given directory as config files and then combines these files into a single Module. This method returns (mod, diags) which are both dict, corresponding to the *Module and hcl.Diagnostic structures in Terraform respectively.

    Source code(tar.gz)
    Source code(zip)
Owner
Prodesire
Pythonista. Microsoft MVP.
Prodesire
Senditapp.com bot spammer, spam your friends

Sendit Spammer Python ⚠️ I am not responsible for how you use this tool. This tool is against "Sendit" ToS and shall not be used in a production envir

Glaukio 1 Dec 31, 2021
A discord Server Bot made with Python, This bot helps people feel better by inspiring them with motivational quotes or by responding with a great message, also the users of the server can create custom messages by telling the bot with Commands.

A discord Server Bot made with Python, This bot helps people feel better by inspiring them with motivational quotes or by responding with a great message, also the users of the server can create cust

Aran 1 Oct 13, 2021
Cloudshell-sandbox-reporter - Helper modules and classes for writing to Cloudshell sandbox console

Cloudshell Sandbox Reporter This project provides utility classes for formatting

QualiLab 2 Sep 07, 2022
Tamil Voicechat UserBot. Powerd By TamilBots. Https://T.me/TamilSupport

Tamil Voicechat UserBot A Telegram UserBot to Play music 🎶 in Voice Chats. It's recommended to use an USA number.(if your real number is suspended I'

Tamil Bots 78 Nov 01, 2022
An open souce video/music streamer based on MPV and piped.

🎶 Harmony Music An easy way to stream videos or music from Youtube from the command line while regaining your privacy. 📖 Table Of Contents ❔ What's

Zingy Tomato 16 Nov 15, 2022
A discord bot that manages your server's hedge fund

Can't Hide Money Bot A discord bot that manages your server's hedge fund Installing Install wkhtmltopdf sudo apt-get install wkhtmltopdf OR brew insta

Kelvin Abrokwa-Johnson 0 Oct 16, 2021
Thread-safe Python RabbitMQ Client & Management library

AMQPStorm Thread-safe Python RabbitMQ Client & Management library. Introduction AMQPStorm is a library designed to be consistent, stable and thread-sa

Erik Olof Gunnar Andersson 167 Nov 20, 2022
Market calendar RESTful API with holiday, late open, and early close. Over 50+ unique exchange calendars for global equity and futures markets.

Trading Calendar Market calendar RESTful API with holiday, late open, and early close. Over 50+ unique exchange calendars for global equity and future

Apptastic Software 1 Feb 03, 2022
Telegram bot untuk mencari jawaban dibrainly, support inline juga

Brainly-Telebot Bot Untuk Mencari Jawaban Dibrainly Jika ingin clone. Boleh kok Dibuat dengan python menggunakan MTproto Library. Yaitu Pyrogram Bot y

... 7 Mar 17, 2022
A simple python discord bot with commands for moderation and utility.

Discord Bot A simple python discord bot with commands for moderation, utility and fun. Moderation $kick user reason - Kick a user from the server

syn 3 Feb 06, 2022
A python script to send sms anonymously with SMS Gateway API. Works on command line terminal.

incognito-sms-sender A python script to send sms anonymously with SMS Gateway API. Works on command line terminal. Download and run script Go to API S

ʀᴇxɪɴᴀᴢᴏʀ 1 Oct 25, 2021
Emo-Fun is a bot which emojifies the text you send it

About Emo-Fun is a bot which emojifies the text you send it. It is easier to understand by an example Input : Hey this is to show my working!! Output

Suvodeep Sinha 3 Sep 30, 2022
Simple Telegram AI Chat bot made using OpenAI and Luna API

Yui Yui, is a simple telegram chat bot made using OpenAI and Luna Chat bot Deployment 👀 Deploying is easy 🤫 ! You can deploy this bot in Heroku or i

I'm Not A Bot #Left_TG 21 Dec 29, 2022
A httpx token generator for discord [ hcaptcha bypass ]

Discord-Token-Generator-Yazato A httpx token generator for discord This generator was developed by Aced#0001, Dreamy Tos Follower#0001, Scripted#0131

23 Oct 26, 2021
Best DDoS Attack Script Python3, Cyber Attack With 40 Methods

MXDDoS - DDoS Attack Script With 40 Methods (Code Lang - Python 3) Please Don't Attack '.gov' and '.ir' Websites :) Features And Methods 💣 Layer7 GET

7 Mar 07, 2022
A Telegram Bot which notifies the user when a vaccine is available on CoWin Platform.

Cowin Vaccine Availability Notifier Telegram Bot A bot that notifies the available vaccines at given district in realtime. Introduction • Requirements

Arham Shah 7 Jul 31, 2021
A stable telegram bot to get restricted messages with custom thumbnail support

Save restricted content Bot A stable telegram bot to get restricted messages with custom thumbnail support

DEVANSH 3 Feb 09, 2022
The successor of GeoSnipe, a pythonic Minecraft username sniper based on AsyncIO.

OneSnipe The successor of GeoSnipe, a pythonic Minecraft username sniper based on AsyncIO. Documentation View Documentation Features • Mojang & Micros

1 Jan 14, 2022
Telegram Group Calls Streaming bot with some useful features, written in Python with Pyrogram and Py-Tgcalls. Supporting platforms like Youtube, Spotify, Resso, AppleMusic, Soundcloud and M3u8 Links.

Yukki Music Bot Yukki Music Bot is a Powerful Telegram Music+Video Bot written in Python using Pyrogram and Py-Tgcalls by which you can stream songs,

Team Yukki 996 Dec 28, 2022
Template to create a telegram bot in python

Template for Telegram Bot Template to create a telegram bot in python. How to Run First add src to PYTHONPATH: export PYTHONPATH=${PWD} Then run: pyt

Ali Hejazizo 12 Dec 24, 2022