-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add node templates resource and node types data source
- Loading branch information
Showing
10 changed files
with
312 additions
and
0 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
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
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,14 @@ | ||
data "google_compute_node_types" "central1a" { | ||
zone = "us-central1-a" | ||
} | ||
|
||
resource "google_compute_node_template" "<%= ctx[:primary_resource_id] %>" { | ||
name = "test-template" | ||
region = "us-central1" | ||
|
||
node_affinity_labels = { | ||
foo = "baz" | ||
} | ||
|
||
node_type = "${data.google_compute_node_types.central1a.names[0]}" | ||
} |
71 changes: 71 additions & 0 deletions
71
third_party/terraform/data_sources/data_source_google_compute_node_types.go
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,71 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sort" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"google.golang.org/api/compute/v1" | ||
) | ||
|
||
func dataSourceGoogleComputeNodeTypes() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleComputeNodeTypesRead, | ||
Schema: map[string]*schema.Schema{ | ||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"zone": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"names": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleComputeNodeTypesRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
zone, err := getZone(d, config) | ||
if err != nil { | ||
return fmt.Errorf("Please specify zone to get appropriate node types for zone. Unable to get zone: %s", err) | ||
} | ||
|
||
resp, err := config.clientCompute.NodeTypes.List(project, zone).Do() | ||
if err != nil { | ||
return err | ||
} | ||
nodeTypes := flattenComputeNodeTypes(resp.Items) | ||
log.Printf("[DEBUG] Received Google Compute Regions: %q", nodeTypes) | ||
|
||
d.Set("names", nodeTypes) | ||
d.Set("project", project) | ||
d.Set("zone", zone) | ||
d.SetId(time.Now().UTC().String()) | ||
|
||
return nil | ||
} | ||
|
||
func flattenComputeNodeTypes(nodeTypes []*compute.NodeType) []string { | ||
result := make([]string, len(nodeTypes), len(nodeTypes)) | ||
for i, nodeType := range nodeTypes { | ||
result[i] = nodeType.Name | ||
} | ||
sort.Strings(result) | ||
return result | ||
} |
74 changes: 74 additions & 0 deletions
74
third_party/terraform/tests/data_source_google_compute_node_types_test.go
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,74 @@ | ||
package google | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"regexp" | ||
) | ||
|
||
func TestAccDataSourceComputeNodeTypes_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceComputeNodeTypes_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckGoogleComputeNodeTypes("data.google_compute_node_types.available"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckGoogleComputeNodeTypes(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Can't find node types data source: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return errors.New("node types data source ID not set.") | ||
} | ||
|
||
count, ok := rs.Primary.Attributes["names.#"] | ||
if !ok { | ||
return errors.New("can't find 'names' attribute") | ||
} | ||
|
||
cnt, err := strconv.Atoi(count) | ||
if err != nil { | ||
return errors.New("failed to read number of version") | ||
} | ||
if cnt < 1 { | ||
return fmt.Errorf("expected at least one node type, got %d", cnt) | ||
} | ||
|
||
for i := 0; i < cnt; i++ { | ||
idx := fmt.Sprintf("names.%d", i) | ||
v, ok := rs.Primary.Attributes[idx] | ||
if !ok { | ||
return fmt.Errorf("expected %q, version not found", idx) | ||
} | ||
|
||
if !regexp.MustCompile(`-[0-9]+-[0-9]+$`).MatchString(v) { | ||
return fmt.Errorf("unexpected type format for %q, value is %v", idx, v) | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
var testAccDataSourceComputeNodeTypes_basic = ` | ||
data "google_compute_node_types" "available" { | ||
zone = "us-central1-a" | ||
} | ||
` |
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
44 changes: 44 additions & 0 deletions
44
third_party/terraform/website/docs/d/google_compute_node_types.html.markdown
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: "google" | ||
page_title: "Google: google_compute_node_types" | ||
sidebar_current: "docs-google-datasource-compute-node-types" | ||
description: |- | ||
Provides list of available Google Compute Engine node types for | ||
sole-tenant nodes. | ||
--- | ||
|
||
# google\_compute\_node\_types | ||
|
||
Provides available node types for Compute Engine sole-tenant nodes in a zone | ||
for a given project. For more information, see [the official documentation](https://cloud.google.com/compute/docs/nodes/#types) and [API](https://cloud.google.com/compute/docs/reference/rest/v1/nodeTypes). | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "google_compute_node_types" "central1b" { | ||
zone = "us-central1-b" | ||
} | ||
resource "google_compute_node_template" "tmpl" { | ||
name = "terraform-test-tmpl" | ||
region = "us-central1" | ||
node_type = "${data.google_compute_node_types.types.names[0]}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `zone` (Optional) - The zone to list node types for. Should be in zone of intended node groups and region of referencing node template. If `zone` is not specified, the provider-level zone must be set and is used | ||
instead. | ||
|
||
* `project` (Optional) - ID of the project to list available node types for. | ||
Should match the project the nodes of this type will be deployed to. | ||
Defaults to the project that the provider is authenticated with. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `names` - A list of node types available in the given zone and project. |