diff --git a/products/compute/ansible.yaml b/products/compute/ansible.yaml index 770bd7840948..6b61caaa68fa 100644 --- a/products/compute/ansible.yaml +++ b/products/compute/ansible.yaml @@ -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 @@ -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 diff --git a/products/compute/api.yaml b/products/compute/api.yaml index c89e7e13fe56..60f08875d78c 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -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' diff --git a/products/compute/inspec.yaml b/products/compute/inspec.yaml index 526a32cb1ca2..b86cb06203db 100644 --- a/products/compute/inspec.yaml +++ b/products/compute/inspec.yaml @@ -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)) -%> diff --git a/products/compute/terraform.yaml b/products/compute/terraform.yaml index e51dbe976a18..07b5b76cc4ed 100644 --- a/products/compute/terraform.yaml +++ b/products/compute/terraform.yaml @@ -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 diff --git a/templates/terraform/examples/node_template_basic.tf.erb b/templates/terraform/examples/node_template_basic.tf.erb new file mode 100644 index 000000000000..5ce652eeb4c6 --- /dev/null +++ b/templates/terraform/examples/node_template_basic.tf.erb @@ -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]}" +} diff --git a/third_party/terraform/data_sources/data_source_google_compute_node_types.go b/third_party/terraform/data_sources/data_source_google_compute_node_types.go new file mode 100644 index 000000000000..9af8b7b8f7a9 --- /dev/null +++ b/third_party/terraform/data_sources/data_source_google_compute_node_types.go @@ -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 +} diff --git a/third_party/terraform/tests/data_source_google_compute_node_types_test.go b/third_party/terraform/tests/data_source_google_compute_node_types_test.go new file mode 100644 index 000000000000..1cca7e169334 --- /dev/null +++ b/third_party/terraform/tests/data_source_google_compute_node_types_test.go @@ -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" +} +` diff --git a/third_party/terraform/utils/provider.go.erb b/third_party/terraform/utils/provider.go.erb index 5476f8877127..4cb7c8725b18 100644 --- a/third_party/terraform/utils/provider.go.erb +++ b/third_party/terraform/utils/provider.go.erb @@ -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(), diff --git a/third_party/terraform/website-compiled/google.erb b/third_party/terraform/website-compiled/google.erb index 6c0842107d7b..c1d7718e7f84 100644 --- a/third_party/terraform/website-compiled/google.erb +++ b/third_party/terraform/website-compiled/google.erb @@ -90,6 +90,9 @@ google_project_services > + google_compute_node_types + + > google_compute_regions > @@ -448,6 +451,10 @@ google_compute_interconnect_attachment + > + google_compute_node_template + + > google_compute_network diff --git a/third_party/terraform/website/docs/d/google_compute_node_types.html.markdown b/third_party/terraform/website/docs/d/google_compute_node_types.html.markdown new file mode 100644 index 000000000000..be67aff07ef3 --- /dev/null +++ b/third_party/terraform/website/docs/d/google_compute_node_types.html.markdown @@ -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.