Skip to content

Commit

Permalink
add node templates resource and node types data source
Browse files Browse the repository at this point in the history
  • Loading branch information
emilymye committed Apr 22, 2019
1 parent 7fa80ee commit 7a72be3
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 0 deletions.
4 changes: 4 additions & 0 deletions products/compute/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ datasources: !ruby/object:Overrides::ResourceOverrides
exclude: true
MachineType: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
NodeTemplate: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
RegionAutoscaler: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
Region: !ruby/object:Overrides::Ansible::ResourceOverride
Expand Down Expand Up @@ -238,6 +240,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
exclude: true
MachineType: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
NodeTemplate: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
Region: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
RegionDiskType: !ruby/object:Overrides::Ansible::ResourceOverride
Expand Down
82 changes: 82 additions & 0 deletions products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,88 @@ objects:
values:
- :REGIONAL
- :GLOBAL
- !ruby/object:Api::Resource
name: 'NodeTemplate'
kind: 'compute#nodeTemplate'
base_url: projects/{{project}}/regions/{{region}}/nodeTemplates
has_self_link: true
description: |
Represents a NodeTemplate resource. Node templates specify properties
for creating sole-tenant nodes, such as node type, vCPU and memory
requirments, node affinity labels, and region.
input: true
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Sole-Tenant Nodes': 'https://cloud.google.com/compute/docs/nodes/'
api: 'https://cloud.google.com/compute/docs/reference/rest/v1/nodeTemplates'
async: !ruby/object:Api::Async
operation: !ruby/object:Api::Async::Operation
kind: 'compute#operation'
path: 'name'
base_url: 'projects/{{project}}/regions/{{region}}/operations/{{op_id}}'
wait_ms: 1000
result: !ruby/object:Api::Async::Result
path: 'targetLink'
status: !ruby/object:Api::Async::Status
path: 'status'
complete: 'DONE'
allowed:
- 'PENDING'
- 'RUNNING'
- 'DONE'
error: !ruby/object:Api::Async::Error
path: 'error/errors'
message: 'message'
parameters:
- !ruby/object:Api::Type::ResourceRef
name: 'region'
resource: 'Region'
imports: 'selfLink'
required: true
description: |
Region where nodes using this the node template will be created
properties:
- !ruby/object:Api::Type::Time
name: 'creationTimestamp'
description: 'Creation timestamp in RFC3339 text format.'
output: true
- !ruby/object:Api::Type::String
name: 'description'
description: 'An optional textual description of the resource.'
- !ruby/object:Api::Type::String
name: 'name'
description: 'Name of the resource.'
- !ruby/object:Api::Type::KeyValuePairs
name: 'nodeAffinityLabels'
description: |
Labels to use for node affinity, which will be used in
instance scheduling.
- !ruby/object:Api::Type::String
name: 'nodeType'
description: |
Node type to use for nodes group that are created from this template.
Only one of nodeTypeFlexibility and nodeType can be specified.
- !ruby/object:Api::Type::NestedObject
name: 'nodeTypeFlexibility'
description: |
Flexible properties for the desired node type. Node groups that
use this node template will create nodes of a type that matches
these properties. Only one of nodeTypeFlexibility and nodeType can
be specified.
properties:
- !ruby/object:Api::Type::String
name: cpus
description: |
Number of virtual CPUs to use.
- !ruby/object:Api::Type::String
name: memory
description: |
Physical memory available to the node, defined in MB.
- !ruby/object:Api::Type::String
name: localSsd
description: |
Local SSD
output: true
- !ruby/object:Api::Resource
name: 'Region'
kind: 'compute#region'
Expand Down
2 changes: 2 additions & 0 deletions products/compute/inspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
exclude: true
Network: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
NodeTemplate: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
Region: !ruby/object:Overrides::Inspec::ResourceOverride
additional_functions: |
<%= lines(indent(compile('templates/inspec/custom_functions/google_compute_region.rb'), 6)) -%>
Expand Down
13 changes: 13 additions & 0 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,19 @@ overrides: !ruby/object:Overrides::ResourceOverrides
optional_properties: |
* `delete_default_routes_on_create`: If set to `true`, default routes (`0.0.0.0/0`) will be deleted
immediately after network creation. Defaults to `false`.
NodeTemplate: !ruby/object:Overrides::Terraform::ResourceOverride
examples:
- !ruby/object:Provider::Terraform::Examples
name: "node_template_basic"
primary_resource_id: "template"
vars:
template_name: "soletenant-tmpl"
properties:
region: !ruby/object:Overrides::Terraform::PropertyOverride
required: false
default_from_api: true
custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb'

Region: !ruby/object:Overrides::Terraform::ResourceOverride
exclude: true
RegionAutoscaler: !ruby/object:Overrides::Terraform::ResourceOverride
Expand Down
14 changes: 14 additions & 0 deletions templates/terraform/examples/node_template_basic.tf.erb
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]}"
}
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
}
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"
}
`
1 change: 1 addition & 0 deletions third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Provider() terraform.ResourceProvider {
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
"google_compute_lb_ip_ranges": dataSourceGoogleComputeLbIpRanges(),
"google_compute_network": dataSourceGoogleComputeNetwork(),
"google_compute_node_types": dataSourceGoogleComputeNodeTypes(),
"google_compute_regions": dataSourceGoogleComputeRegions(),
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
"google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(),
Expand Down
7 changes: 7 additions & 0 deletions third_party/terraform/website-compiled/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
<a href="/docs/providers/google/d/google_project_services.html">google_project_services</a>
</li>
<li<%%= sidebar_current("docs-google-datasource-compute-regions") %>>
<a href="/docs/providers/google/d/google_compute_node_types.html">google_compute_node_types</a>
</li>
<li<%%= sidebar_current("docs-google-datasource-compute-regions") %>>
<a href="/docs/providers/google/d/google_compute_regions.html">google_compute_regions</a>
</li>
<li<%%= sidebar_current("docs-google-datasource-compute-ssl-policy") %>>
Expand Down Expand Up @@ -448,6 +451,10 @@
<a href="/docs/providers/google/r/compute_interconnect_attachment.html">google_compute_interconnect_attachment</a>
</li>

<li<%%= sidebar_current("docs-google-compute-project-metadata-item") %>>
<a href="/docs/providers/google/r/compute_node_template.html">google_compute_node_template</a>
</li>

<li<%%= sidebar_current("docs-google-compute-network-x") %>>
<a href="/docs/providers/google/r/compute_network.html">google_compute_network</a>
</li>
Expand Down
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.

0 comments on commit 7a72be3

Please sign in to comment.