-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add regions data source for Nomad. #24
Merged
+147
−0
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8ffd182
Add regions data source for Nomad.
paddycarver 98cc0e8
Merge branch 'master' into paddy_region_datasource
paddycarver 2fbe202
Fix embarrassing copy/paste error.
paddycarver 893ef7b
Merge branch 'master' into paddy_region_datasource
paddycarver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package nomad | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/nomad/api" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceRegions() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: genericSecretDataSourceRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"regions": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func genericSecretDataSourceRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*api.Client) | ||
|
||
log.Printf("[DEBUG] Reading regions from Nomad") | ||
resp, err := client.Regions().List() | ||
if err != nil { | ||
return fmt.Errorf("error reading regions from Nomad: %s", err) | ||
} | ||
log.Printf("[DEBUG] Read regions from Nomad") | ||
d.SetId(client.Address() + "/regions") | ||
|
||
return d.Set("regions", resp) | ||
} |
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,53 @@ | ||
package nomad | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestDataSourceRegions(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
Providers: testProviders, | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceRegions_config, | ||
Check: testDataSourceRegions_check, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
var testDataSourceRegions_config = ` | ||
|
||
data "nomad_regions" "test" { | ||
} | ||
|
||
` | ||
|
||
func testDataSourceRegions_check(s *terraform.State) error { | ||
resourceState := s.Modules[0].Resources["data.nomad_regions.test"] | ||
if resourceState == nil { | ||
return fmt.Errorf("resource not found in state %v", s.Modules[0].Resources) | ||
} | ||
|
||
iState := resourceState.Primary | ||
if iState == nil { | ||
return fmt.Errorf("resource has no primary instance") | ||
} | ||
|
||
results, err := strconv.ParseInt(iState.Attributes["regions.#"], 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("expected integer in state, got %s (%T)", iState.Attributes["regions.#"], iState.Attributes["regions.#"]) | ||
} | ||
|
||
if results < 1 { | ||
return fmt.Errorf("got %d regions, expected at least 1", results) | ||
} | ||
|
||
return nil | ||
} |
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,44 @@ | ||
--- | ||
layout: "nomad" | ||
page_title: "Nomad: nomad_regions" | ||
sidebar_current: "docs-nomad-datasource-regions" | ||
description: |- | ||
Retrieve a list of regions available in Nomad. | ||
--- | ||
|
||
# nomad_regions | ||
|
||
Retrieve a list of regions available in Nomad. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "nomad_regions" "regions" { | ||
} | ||
|
||
data "template_file" "jobs" { | ||
count = "${length(data.nomad_regions.regions.regions)}" | ||
template = <<EOT | ||
job "foo" { | ||
datacenters = ["dc1"] | ||
type = "service" | ||
region = "$$region" | ||
# ... rest of your job here | ||
} | ||
EOT | ||
vars { | ||
region = "${data.nomad_regions.regions[count.index]}" | ||
} | ||
} | ||
|
||
resource "nomad_job" "app" { | ||
count = "${length(data.nomad_regions.regions.regions)}" | ||
jobspec = "${data.template_file.jobs[count.index].rendered}" | ||
} | ||
``` | ||
|
||
## Attribute Reference | ||
|
||
The following attributes are exported: | ||
|
||
- `regions` `(list of strings)` - a list of regions available in the cluster. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this name seems wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. Fixed.