Skip to content

Commit

Permalink
Adding readme and license
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch Davis committed Dec 30, 2018
0 parents commit 8fdf680
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
@@ -0,0 +1,23 @@
# BAZEL
#######
# Ignore backup files.
*~
# Ignore Vim swap files.
.*.swp
# Ignore files generated by IDEs.
/.classpath
/.factorypath
/.idea/
/.ijwb/
/.project
/.settings
/.vscode/
/bazel.iml
# Ignore all bazel-* symlinks. There is no full list since this can change
# based on the name of the directory bazel is cloned into.
/bazel-*
# Ignore outputs generated during Bazel bootstrapping.
/output/
# Ignore jekyll build output.
/production
/.sass-cache
24 changes: 24 additions & 0 deletions LICENSE
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
33 changes: 33 additions & 0 deletions WORKSPACE
@@ -0,0 +1,33 @@
workspace(name = "io_bazel_rules_terraform")

#load("@bazel_tools//tools/build_defs/repo:http.bzl",
# "http_archive", "http_file")
## THIRD PARTY TOOLS
###################################
#http_archive(
# name = "terraform_linux",
# url = "https://releases.hashicorp.com/terraform/0.11.2/terraform_0.11.2_linux_amd64.zip",
# build_file_content = """
#filegroup(
# name = "terraform_linux_tool",
# srcs = ["terraform"],
# visibility = ["//visibility:public"]
#)
#""", sha256 = "f728fa73ff2a4c4235a28de4019802531758c7c090b6ca4c024d48063ab8537b"
#)
#http_archive(
# name = "terraform_darwin",
# url = "https://releases.hashicorp.com/terraform/0.11.2/terraform_0.11.2_darwin_amd64.zip",
# build_file_content = """
#filegroup(
# name = "terraform_darwin_tool",
# srcs = ["terraform"],
# visibility = ["//visibility:public"]
#)
#""",
# sha256 = "ff5c3c4bcfe84e011b96a2232704b2db196383ce5d4a32e47956c883ddc94bac"
#)

load("//rules_terraform:terraform.bzl", "terraform_register_toolchains")

terraform_register_toolchains()
32 changes: 32 additions & 0 deletions readme.md
@@ -0,0 +1,32 @@
Rules_Terraform
===============

[Bazel](https://bazel.build/) rules for using [Hashicorp](https://www.hashicorp.com/)'s [Terraform](https://www.terraform.io/) within your bazel build.

License
=======

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
4 changes: 4 additions & 0 deletions rules_terraform/BUILD
@@ -0,0 +1,4 @@
package(default_visibility = ["//visibility:public"])
load("//rules_terraform:terraform.bzl", "setup_terraform_toolchains")

setup_terraform_toolchains()
174 changes: 174 additions & 0 deletions rules_terraform/terraform.bzl
@@ -0,0 +1,174 @@
#toolchain_type(name = "toolchain_type")

toolchains = {
"terraform_linux": {
"name": "terraform_linux",
"exec_compatible_with": [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
],
"target_compatible_with": [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
],
"toolchain":":terraform_linux",
"toolchain_type":"@io_bazel_rules_terraform//:toolchain_type",
"url":"https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip",
"sha256":"t94504f4a67bad612b5c8e3a4b7ce6ca2772b3c1559630dfd71e9c519e3d6149c"
},
"terraform_osx": {
"name": "terraform_osx",
"exec_compatible_with": [
"@bazel_tools//platforms:osx",
"@bazel_tools//platforms:x86_64",
],
"target_compatible_with": [
"@bazel_tools//platforms:osx",
"@bazel_tools//platforms:x86_64",
],
"toolchain":":terraform_osx",
"toolchain_type":"@io_bazel_rules_terraform//:toolchain_type",
"url":"https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_darwin_amd64.zip",
"sha256":"6b6e8253b678554c67d717c42209fd857bfe64a1461763c05d3d1d85c6f618d3"
}
}

TerraformInfo = provider(
doc = "Information on how to call terraform",
fields = [
"executable",
"url",
"sha256"
],
)

def _terraform_toolchain_impl(ctx):
toolchain_info = platform_common.ToolchainInfo(
terraforminfo = TerraformInfo(
executable = ctx.attr.executable,
url = ctx.attr.url,
sha256 = ctx.attr.sha256
),
)
return [toolchain_info]

terraform_toolchain = rule(
implementation = _terraform_toolchain_impl,
attrs = {
"executable": attr.string(),
"url": attr.string(),
"sha256": attr.string()
}
)

def setup_terraform_toolchains():
for name, toolchain in toolchains.items():
terraform_toolchain(
name = toolchain["name"],
executable = "",
url = toolchain["url"],
sha256 = toolchain["sha256"])
native.toolchain(
name = "{0}_toolchain".format(toolchain["name"]),
exec_compatible_with = toolchain["exec_compatible_with"],
target_compatible_with = toolchain["target_compatible_with"],
toolchain = toolchain["toolchain"],
toolchain_type = toolchain["toolchain_type"]
)

def _download_terraform_impl(ctx):
if ctx.os.name == "linux":
host = "terraform_linux"
sumurl = "https://releases.hashicorp.com/terraform/{0}/terraform_{0}_SHA256SUMS".format(ctx.attr.version)
url = "https://releases.hashicorp.com/terraform/{0}/terraform_{0}_linux_amd64.zip".format(ctx.attr.version)
elif ctx.os.name == "mac os x":
host = "terraform_osx"
else:
fail("Unsupported operating system: " + ctx.os.name)

toolchain = toolchains[host]

ctx.file("BUILD.bazel",
"""
filegroup(
name = "terraform_executable",
srcs = ["terraform/terraform"],
visibility = ["//visibility:public"]
)
""",
executable=False
)
ctx.download_and_extract(
url = toolchain["url"],
sha256 = toolchain["sha256"],
output = "terraform",
type = "zip",
)

download_terraform = repository_rule(
implementation = _download_terraform_impl,
attrs = {
"version": attr.string(
mandatory = True
)
}
)

# TODO: Need to define the plugin provider system
#def _terraform_provider_plugin_impl(ctx):
# pass
#
#terraform_provider_plugin = repository_rule(
# implementation = _terraform_provider_plugin_impl,
# attrs = {
# "name": attr.string(
# mandatory = True
# ),
# "version": attr.string(
# mandatory = True
# )
# }
#)

def _terraform_plan(ctx):
deps = depset(ctx.files.srcs)
ctx.actions.run(
executable = ctx.executable._exec,
inputs = deps.to_list(),
outputs = [ctx.outputs.out],
mnemonic = "TerraformInitialize",
arguments = [
"plan",
"-out={0}".format(ctx.outputs.out.path), deps.to_list()[0].dirname
]
)

terraform_plan = rule(
implementation = _terraform_plan,
attrs = {
"srcs": attr.label_list(
mandatory = True,
allow_files = True
),
"_exec": attr.label(
default = Label("@terraform_exec//:terraform_executable"),
allow_files = True,
executable = True,
cfg = "host"
)
},
toolchains = ["@io_bazel_rules_terraform//:toolchain_type"],
outputs = {"out": "%{name}.out"},
)

def terraform_register_toolchains(version="0.11.11"):
if "download_terraform" not in native.existing_rules():
download_terraform(
name = "terraform_exec",
version = version
)

for name, toolchain in toolchains.items():
native.register_toolchains(
"//rules_terraform:{0}_toolchain".format(toolchain["name"]),
)
7 changes: 7 additions & 0 deletions test/BUILD
@@ -0,0 +1,7 @@
package(default_visibility = ["//visibility:public"])
load("//rules_terraform:terraform.bzl", "terraform_plan")

terraform_plan(
name = "test",
srcs = glob(["*.tf"])
)
3 changes: 3 additions & 0 deletions test/terraform.tf
@@ -0,0 +1,3 @@
#resource "tls_private_key" "example" {
# algorithm = "ECDSA"
#}

0 comments on commit 8fdf680

Please sign in to comment.