-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compute node types and templates (#3446)
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
9eff6af
commit 8077953
Showing
9 changed files
with
866 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
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 | ||
} |
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 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" | ||
} | ||
` |
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
Oops, something went wrong.