forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TPU Tensorflow datasource (hashicorp#561)
Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
1 parent
52f49b5
commit 9a12bc6
Showing
7 changed files
with
198 additions
and
9 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,82 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sort" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceTpuTensorflowVersions() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceTpuTensorFlowVersionsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"zone": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"versions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceTpuTensorFlowVersionsRead(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 err | ||
} | ||
|
||
url, err := replaceVars(d, config, "https://tpu.googleapis.com/v1/projects/{{project}}/locations/{{zone}}/tensorflowVersions") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
versionsRaw, err := paginatedListRequest(url, config, flattenTpuTensorflowVersions) | ||
if err != nil { | ||
return fmt.Errorf("Error listing TPU Tensorflow versions: %s", err) | ||
} | ||
|
||
versions := make([]string, len(versionsRaw)) | ||
for i, ver := range versionsRaw { | ||
versions[i] = ver.(string) | ||
} | ||
sort.Strings(versions) | ||
|
||
log.Printf("[DEBUG] Received Google TPU Tensorflow Versions: %q", versions) | ||
|
||
d.Set("versions", versions) | ||
d.Set("zone", zone) | ||
d.Set("project", project) | ||
d.SetId(time.Now().UTC().String()) | ||
|
||
return nil | ||
} | ||
|
||
func flattenTpuTensorflowVersions(resp map[string]interface{}) []interface{} { | ||
verObjList := resp["tensorflowVersions"].([]interface{}) | ||
versions := make([]interface{}, len(verObjList)) | ||
for i, v := range verObjList { | ||
verObj := v.(map[string]interface{}) | ||
versions[i] = verObj["version"] | ||
} | ||
return versions | ||
} |
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,72 @@ | ||
package google | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"regexp" | ||
) | ||
|
||
func TestAccTpuTensorflowVersions_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTpuTensorFlowVersionsConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckGoogleTpuTensorflowVersions("data.google_tpu_tensorflow_versions.available"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckGoogleTpuTensorflowVersions(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Can't find TPU Tensorflow versions data source: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return errors.New("data source ID not set.") | ||
} | ||
|
||
count, ok := rs.Primary.Attributes["versions.#"] | ||
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 < 2 { | ||
return fmt.Errorf("expected at least 2 versions, received %d, this is most likely a bug", cnt) | ||
} | ||
|
||
for i := 0; i < cnt; i++ { | ||
idx := fmt.Sprintf("versions.%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 version format for %q, value is %v", idx, v) | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
var testAccTpuTensorFlowVersionsConfig = ` | ||
data "google_tpu_tensorflow_versions" "available" {} | ||
` |
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