Skip to content
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
merged 4 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions nomad/data_source_regions.go
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this name seems wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Fixed.

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)
}
53 changes: 53 additions & 0 deletions nomad/data_source_regions_test.go
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
}
4 changes: 4 additions & 0 deletions nomad/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func Provider() terraform.ResourceProvider {
"nomad_acl_token": resourceACLToken(),
"nomad_job": resourceJob(),
},

DataSourcesMap: map[string]*schema.Resource{
"nomad_regions": dataSourceRegions(),
},
}
}

Expand Down
44 changes: 44 additions & 0 deletions website/docs/d/regions.html.markdown
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.
9 changes: 9 additions & 0 deletions website/nomad.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
<a href="/docs/providers/nomad/index.html">Nomad Provider</a>
</li>

<li<%= sidebar_current("docs-nomad-datasource") %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-nomad-datasource-regions") %>>
<a href="/docs/providers/nomad/d/regions.html">nomad_regions</a>
</li>
</ul>
</li>

<li<%= sidebar_current("docs-nomad-resource") %>>
<a href="#">Resources</a>
<ul class="nav nav-visible">
Expand Down