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 compute node types and templates #614

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 71 additions & 0 deletions google-beta/data_source_google_compute_node_types.go
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 google-beta/data_source_google_compute_node_types_test.go
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 node types")
}
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 google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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
1 change: 1 addition & 0 deletions google-beta/provider_compute_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var GeneratedComputeResourcesMap = map[string]*schema.Resource{
"google_compute_image": resourceComputeImage(),
"google_compute_interconnect_attachment": resourceComputeInterconnectAttachment(),
"google_compute_network": resourceComputeNetwork(),
"google_compute_node_template": resourceComputeNodeTemplate(),
"google_compute_region_autoscaler": resourceComputeRegionAutoscaler(),
"google_compute_region_disk": resourceComputeRegionDisk(),
"google_compute_route": resourceComputeRoute(),
Expand Down
Loading