-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from 18F/add-semver-module
Enable using these modules with version constraints
- Loading branch information
Showing
4 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# terraform-cloudgov/semver | ||
|
||
Find a tag for this repository matching NPM-style version constraints | ||
|
||
## Example | ||
|
||
```terraform | ||
# Specify a version constraint for each module we plan to use | ||
locals { | ||
module_versions = { | ||
database = "^0.x", # major version 0 | ||
s3 = "^0.x" # major version 0 | ||
} | ||
} | ||
# Divine the most recent versions matching those constraints... | ||
module "version" { | ||
for_each = local.module_versions | ||
source = "github.com/18f/terraform-cloudgov//semver" | ||
version_constraint = each.value | ||
} | ||
# ...then refer to the source for those modules using the calculated versions. | ||
module "database" { | ||
source = "github.com/18f/terraform-cloudgov//database?ref=v${module.version["database"].target_version}" | ||
# [...] | ||
} | ||
module "s3" { | ||
source = "github.com/18f/terraform-cloudgov//s3?ref=v${module.version["s3"].target_version}" | ||
# [...] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This is just a thin wrapper around the following module, prefilling | ||
# the 18f/terraform-cloudgov repository | ||
# https://registry.terraform.io/modules/rhythmictech/find-release-by-semver | ||
|
||
# Just accepts the one parameter | ||
variable "version_constraint" { | ||
type = string | ||
description = "The NPM-style version constraint you want to use to find the right version" | ||
} | ||
|
||
module "find-cloudgov-module-version" { | ||
source = "rhythmictech/find-release-by-semver/github" | ||
version = "~> 1.1.2" | ||
|
||
repo_name = "terraform-cloudgov" | ||
repo_owner = "18f" | ||
version_constraint = var.version_constraint | ||
} | ||
|
||
output "target_version" { | ||
description = "Version matched to constraint" | ||
value = module.find-cloudgov-module-version.target_version | ||
} | ||
|
||
output "version_info" { | ||
description = "All available info about the target release" | ||
value = module.find-cloudgov-module-version.version_info | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
terraform { | ||
required_providers { | ||
test = { | ||
# See https://developer.hashicorp.com/terraform/language/modules/testing-experiment#writing-tests-for-a-module | ||
source = "terraform.io/builtin/test" | ||
} | ||
http = { | ||
source = "hashicorp/http" | ||
} | ||
} | ||
} | ||
|
||
# Fixture constraints | ||
locals { | ||
module_versions = { | ||
# This test will break after we tag something higher than 1.0.0; fix and | ||
# expand these tests then! | ||
# https://github.com/npm/node-semver#tilde-ranges-123-12-1 | ||
greaterthan = "~0", | ||
} | ||
|
||
latest_tag = trimprefix(jsondecode(data.http.latest_version.response_body).tag_name, "v") | ||
} | ||
|
||
# Divine the most recent versions matching fixture | ||
module "version" { | ||
for_each = local.module_versions | ||
source = "../.." | ||
version_constraint = each.value | ||
} | ||
|
||
data "http" "latest_version" { | ||
url = "https://api.github.com/repos/18f/terraform-cloudgov/releases/latest" | ||
|
||
request_headers = { | ||
accept = "vnd.github+json" | ||
} | ||
} | ||
|
||
resource "test_assertions" "greater-than-is-latest" { | ||
component = "outputs" | ||
equal "target_version" { | ||
description = "greater than should always be the latest in the repo" | ||
got = module.version["greaterthan"].target_version | ||
want = local.latest_tag | ||
} | ||
} | ||
|