From 71ee6329459f1743879766c5baf833ee4c520589 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 22 Nov 2024 11:54:33 +0000 Subject: [PATCH] Add new TPUv2 QueuedResource resource in beta (#12277) [upstream:67c66d2f33f82fb285fb99aa2b6a63f6a9c9f69e] Signed-off-by: Modular Magician --- .changelog/12277.txt | 3 + .../provider/provider_mmv1_resources.go | 5 +- .../tpuv2/resource_tpu_v2_queued_resource.go | 549 ++++++++++++++++++ ...tpu_v2_queued_resource_generated_meta.yaml | 5 + ...e_tpu_v2_queued_resource_generated_test.go | 122 ++++ ...resource_tpu_v2_queued_resource_sweeper.go | 139 +++++ .../r/tpu_v2_queued_resource.html.markdown | 168 ++++++ 7 files changed, 989 insertions(+), 2 deletions(-) create mode 100644 .changelog/12277.txt create mode 100644 google-beta/services/tpuv2/resource_tpu_v2_queued_resource.go create mode 100644 google-beta/services/tpuv2/resource_tpu_v2_queued_resource_generated_meta.yaml create mode 100644 google-beta/services/tpuv2/resource_tpu_v2_queued_resource_generated_test.go create mode 100644 google-beta/services/tpuv2/resource_tpu_v2_queued_resource_sweeper.go create mode 100644 website/docs/r/tpu_v2_queued_resource.html.markdown diff --git a/.changelog/12277.txt b/.changelog/12277.txt new file mode 100644 index 0000000000..e80018e3ba --- /dev/null +++ b/.changelog/12277.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +`google_tpu_v2_queued_resource` (beta) +``` \ No newline at end of file diff --git a/google-beta/provider/provider_mmv1_resources.go b/google-beta/provider/provider_mmv1_resources.go index cc61ec778f..efcfe7fa45 100644 --- a/google-beta/provider/provider_mmv1_resources.go +++ b/google-beta/provider/provider_mmv1_resources.go @@ -505,9 +505,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{ } // Resources -// Generated resources: 557 +// Generated resources: 558 // Generated IAM resources: 291 -// Total generated resources: 848 +// Total generated resources: 849 var generatedResources = map[string]*schema.Resource{ "google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(), "google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(), @@ -1307,6 +1307,7 @@ var generatedResources = map[string]*schema.Resource{ "google_tags_tag_value_iam_member": tpgiamresource.ResourceIamMember(tags.TagsTagValueIamSchema, tags.TagsTagValueIamUpdaterProducer, tags.TagsTagValueIdParseFunc), "google_tags_tag_value_iam_policy": tpgiamresource.ResourceIamPolicy(tags.TagsTagValueIamSchema, tags.TagsTagValueIamUpdaterProducer, tags.TagsTagValueIdParseFunc), "google_tpu_node": tpu.ResourceTPUNode(), + "google_tpu_v2_queued_resource": tpuv2.ResourceTpuV2QueuedResource(), "google_tpu_v2_vm": tpuv2.ResourceTpuV2Vm(), "google_transcoder_job": transcoder.ResourceTranscoderJob(), "google_transcoder_job_template": transcoder.ResourceTranscoderJobTemplate(), diff --git a/google-beta/services/tpuv2/resource_tpu_v2_queued_resource.go b/google-beta/services/tpuv2/resource_tpu_v2_queued_resource.go new file mode 100644 index 0000000000..653c71cacf --- /dev/null +++ b/google-beta/services/tpuv2/resource_tpu_v2_queued_resource.go @@ -0,0 +1,549 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** Type: MMv1 *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package tpuv2 + +import ( + "fmt" + "log" + "net/http" + "reflect" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +func ResourceTpuV2QueuedResource() *schema.Resource { + return &schema.Resource{ + Create: resourceTpuV2QueuedResourceCreate, + Read: resourceTpuV2QueuedResourceRead, + Delete: resourceTpuV2QueuedResourceDelete, + + Importer: &schema.ResourceImporter{ + State: resourceTpuV2QueuedResourceImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(20 * time.Minute), + Delete: schema.DefaultTimeout(20 * time.Minute), + }, + + CustomizeDiff: customdiff.All( + tpgresource.DefaultProviderProject, + ), + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The immutable name of the Queued Resource.`, + }, + "tpu": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Description: `Defines a TPU resource.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "node_spec": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Description: `The TPU node(s) being requested.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "node": { + Type: schema.TypeList, + Required: true, + ForceNew: true, + Description: `The node.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "runtime_version": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `Runtime version for the TPU.`, + }, + "accelerator_type": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + Description: `TPU accelerator type for the TPU. If not specified, this defaults to 'v2-8'.`, + }, + "description": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: `Text description of the TPU.`, + }, + }, + }, + }, + "parent": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: tpgresource.ProjectNumberDiffSuppress, + Description: `The parent resource name.`, + }, + "node_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: `Unqualified node identifier used to identify the node in the project once provisioned.`, + }, + }, + }, + }, + }, + }, + }, + "zone": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + Description: `The GCP location for the Queued Resource. If it is not provided, the provider zone is used.`, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + UseJSONNumber: true, + } +} + +func resourceTpuV2QueuedResourceCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + nameProp, err := expandTpuV2QueuedResourceName(d.Get("name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { + obj["name"] = nameProp + } + tpuProp, err := expandTpuV2QueuedResourceTpu(d.Get("tpu"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("tpu"); !tpgresource.IsEmptyValue(reflect.ValueOf(tpuProp)) && (ok || !reflect.DeepEqual(v, tpuProp)) { + obj["tpu"] = tpuProp + } + + url, err := tpgresource.ReplaceVars(d, config, "{{TpuV2BasePath}}projects/{{project}}/locations/{{zone}}/queuedResources?queuedResourceId={{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new QueuedResource: %#v", obj) + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for QueuedResource: %s", err) + } + billingProject = project + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + headers := make(http.Header) + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutCreate), + Headers: headers, + }) + if err != nil { + return fmt.Errorf("Error creating QueuedResource: %s", err) + } + + // Store the ID now + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + // Use the resource in the operation response to populate + // identity fields and d.Id() before read + var opRes map[string]interface{} + err = TpuV2OperationWaitTimeWithResponse( + config, res, &opRes, project, "Creating QueuedResource", userAgent, + d.Timeout(schema.TimeoutCreate)) + if err != nil { + // The resource didn't actually create + d.SetId("") + + return fmt.Errorf("Error waiting to create QueuedResource: %s", err) + } + + if err := d.Set("name", flattenTpuV2QueuedResourceName(opRes["name"], d, config)); err != nil { + return err + } + + // This may have caused the ID to update - update it if so. + id, err = tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating QueuedResource %q: %#v", d.Id(), res) + + return resourceTpuV2QueuedResourceRead(d, meta) +} + +func resourceTpuV2QueuedResourceRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + url, err := tpgresource.ReplaceVars(d, config, "{{TpuV2BasePath}}projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for QueuedResource: %s", err) + } + billingProject = project + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + headers := make(http.Header) + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Headers: headers, + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("TpuV2QueuedResource %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading QueuedResource: %s", err) + } + + if err := d.Set("name", flattenTpuV2QueuedResourceName(res["name"], d, config)); err != nil { + return fmt.Errorf("Error reading QueuedResource: %s", err) + } + if err := d.Set("tpu", flattenTpuV2QueuedResourceTpu(res["tpu"], d, config)); err != nil { + return fmt.Errorf("Error reading QueuedResource: %s", err) + } + + return nil +} + +func resourceTpuV2QueuedResourceDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for QueuedResource: %s", err) + } + billingProject = project + + url, err := tpgresource.ReplaceVars(d, config, "{{TpuV2BasePath}}projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + headers := make(http.Header) + + log.Printf("[DEBUG] Deleting QueuedResource %q", d.Id()) + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + Headers: headers, + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "QueuedResource") + } + + err = TpuV2OperationWaitTime( + config, res, project, "Deleting QueuedResource", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting QueuedResource %q: %#v", d.Id(), res) + return nil +} + +func resourceTpuV2QueuedResourceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*transport_tpg.Config) + if err := tpgresource.ParseImportId([]string{ + "^projects/(?P[^/]+)/locations/(?P[^/]+)/queuedResources/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)$", + "^(?P[^/]+)$", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenTpuV2QueuedResourceName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return v + } + return tpgresource.NameFromSelfLinkStateFunc(v) +} + +func flattenTpuV2QueuedResourceTpu(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return nil + } + original := v.(map[string]interface{}) + if len(original) == 0 { + return nil + } + transformed := make(map[string]interface{}) + transformed["node_spec"] = + flattenTpuV2QueuedResourceTpuNodeSpec(original["nodeSpec"], d, config) + return []interface{}{transformed} +} +func flattenTpuV2QueuedResourceTpuNodeSpec(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return v + } + l := v.([]interface{}) + transformed := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + if len(original) < 1 { + // Do not include empty json objects coming back from the api + continue + } + transformed = append(transformed, map[string]interface{}{ + "parent": flattenTpuV2QueuedResourceTpuNodeSpecParent(original["parent"], d, config), + "node_id": flattenTpuV2QueuedResourceTpuNodeSpecNodeId(original["nodeId"], d, config), + "node": flattenTpuV2QueuedResourceTpuNodeSpecNode(original["node"], d, config), + }) + } + return transformed +} +func flattenTpuV2QueuedResourceTpuNodeSpecParent(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenTpuV2QueuedResourceTpuNodeSpecNodeId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenTpuV2QueuedResourceTpuNodeSpecNode(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return nil + } + original := v.(map[string]interface{}) + if len(original) == 0 { + return nil + } + transformed := make(map[string]interface{}) + transformed["runtime_version"] = + flattenTpuV2QueuedResourceTpuNodeSpecNodeRuntimeVersion(original["runtimeVersion"], d, config) + transformed["accelerator_type"] = + flattenTpuV2QueuedResourceTpuNodeSpecNodeAcceleratorType(original["acceleratorType"], d, config) + transformed["description"] = + flattenTpuV2QueuedResourceTpuNodeSpecNodeDescription(original["description"], d, config) + return []interface{}{transformed} +} +func flattenTpuV2QueuedResourceTpuNodeSpecNodeRuntimeVersion(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenTpuV2QueuedResourceTpuNodeSpecNodeAcceleratorType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenTpuV2QueuedResourceTpuNodeSpecNodeDescription(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func expandTpuV2QueuedResourceName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandTpuV2QueuedResourceTpu(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + l := v.([]interface{}) + if len(l) == 0 || l[0] == nil { + return nil, nil + } + raw := l[0] + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedNodeSpec, err := expandTpuV2QueuedResourceTpuNodeSpec(original["node_spec"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedNodeSpec); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["nodeSpec"] = transformedNodeSpec + } + + return transformed, nil +} + +func expandTpuV2QueuedResourceTpuNodeSpec(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + l := v.([]interface{}) + req := make([]interface{}, 0, len(l)) + for _, raw := range l { + if raw == nil { + continue + } + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedParent, err := expandTpuV2QueuedResourceTpuNodeSpecParent(original["parent"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedParent); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["parent"] = transformedParent + } + + transformedNodeId, err := expandTpuV2QueuedResourceTpuNodeSpecNodeId(original["node_id"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedNodeId); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["nodeId"] = transformedNodeId + } + + transformedNode, err := expandTpuV2QueuedResourceTpuNodeSpecNode(original["node"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedNode); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["node"] = transformedNode + } + + req = append(req, transformed) + } + return req, nil +} + +func expandTpuV2QueuedResourceTpuNodeSpecParent(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandTpuV2QueuedResourceTpuNodeSpecNodeId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandTpuV2QueuedResourceTpuNodeSpecNode(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + l := v.([]interface{}) + if len(l) == 0 || l[0] == nil { + return nil, nil + } + raw := l[0] + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedRuntimeVersion, err := expandTpuV2QueuedResourceTpuNodeSpecNodeRuntimeVersion(original["runtime_version"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedRuntimeVersion); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["runtimeVersion"] = transformedRuntimeVersion + } + + transformedAcceleratorType, err := expandTpuV2QueuedResourceTpuNodeSpecNodeAcceleratorType(original["accelerator_type"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedAcceleratorType); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["acceleratorType"] = transformedAcceleratorType + } + + transformedDescription, err := expandTpuV2QueuedResourceTpuNodeSpecNodeDescription(original["description"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedDescription); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["description"] = transformedDescription + } + + return transformed, nil +} + +func expandTpuV2QueuedResourceTpuNodeSpecNodeRuntimeVersion(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandTpuV2QueuedResourceTpuNodeSpecNodeAcceleratorType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandTpuV2QueuedResourceTpuNodeSpecNodeDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} diff --git a/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_generated_meta.yaml b/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_generated_meta.yaml new file mode 100644 index 0000000000..59a36691e0 --- /dev/null +++ b/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_generated_meta.yaml @@ -0,0 +1,5 @@ +resource: 'google_tpu_v2_queued_resource' +generation_type: 'mmv1' +api_service_name: 'tpu.googleapis.com' +api_version: 'v2' +api_resource_type_kind: 'QueuedResource' diff --git a/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_generated_test.go b/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_generated_test.go new file mode 100644 index 0000000000..6b4501dc96 --- /dev/null +++ b/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_generated_test.go @@ -0,0 +1,122 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** Type: MMv1 *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package tpuv2_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +func TestAccTpuV2QueuedResource_tpuV2QueuedResourceBasicExample(t *testing.T) { + acctest.SkipIfVcr(t) + t.Parallel() + + context := map[string]interface{}{ + "project": envvar.GetTestProjectFromEnv(), + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), + CheckDestroy: testAccCheckTpuV2QueuedResourceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccTpuV2QueuedResource_tpuV2QueuedResourceBasicExample(context), + }, + { + ResourceName: "google_tpu_v2_queued_resource.qr", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"zone"}, + }, + }, + }) +} + +func testAccTpuV2QueuedResource_tpuV2QueuedResourceBasicExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_tpu_v2_queued_resource" "qr" { + provider = google-beta + + name = "tf-test-test-qr%{random_suffix}" + zone = "us-central1-c" + project = "%{project}" + + tpu { + node_spec { + parent = "projects/%{project}/locations/us-central1-c" + node_id = "tf-test-test-tpu%{random_suffix}" + node { + runtime_version = "tpu-vm-tf-2.13.0" + accelerator_type = "v2-8" + description = "Text description of the TPU." + } + } + } +} +`, context) +} + +func testAccCheckTpuV2QueuedResourceDestroyProducer(t *testing.T) func(s *terraform.State) error { + return func(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "google_tpu_v2_queued_resource" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := acctest.GoogleProviderConfig(t) + + url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{TpuV2BasePath}}projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + if config.BillingProject != "" { + billingProject = config.BillingProject + } + + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, + }) + if err == nil { + return fmt.Errorf("TpuV2QueuedResource still exists at %s", url) + } + } + + return nil + } +} diff --git a/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_sweeper.go b/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_sweeper.go new file mode 100644 index 0000000000..eae9a15e81 --- /dev/null +++ b/google-beta/services/tpuv2/resource_tpu_v2_queued_resource_sweeper.go @@ -0,0 +1,139 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** Type: MMv1 *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package tpuv2 + +import ( + "context" + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/sweeper" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +func init() { + sweeper.AddTestSweepers("TpuV2QueuedResource", testSweepTpuV2QueuedResource) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepTpuV2QueuedResource(region string) error { + resourceName := "TpuV2QueuedResource" + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) + + config, err := sweeper.SharedConfigForRegion(region) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) + return err + } + + err = config.LoadAndValidate(context.Background()) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) + return err + } + + t := &testing.T{} + billingId := envvar.GetTestBillingAccountFromEnv(t) + + // Setup variables to replace in list template + d := &tpgresource.ResourceDataMock{ + FieldsInSchema: map[string]interface{}{ + "project": config.Project, + "region": region, + "location": region, + "zone": "-", + "billing_account": billingId, + }, + } + + listTemplate := strings.Split("https://tpu.googleapis.com/v2/projects/{{project}}/locations/{{zone}}/queuedResources", "?")[0] + listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) + return nil + } + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: config.Project, + RawURL: listUrl, + UserAgent: config.UserAgent, + }) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) + return nil + } + + resourceList, ok := res["queuedResources"] + if !ok { + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") + return nil + } + + rl := resourceList.([]interface{}) + + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) + // Keep count of items that aren't sweepable for logging. + nonPrefixCount := 0 + for _, ri := range rl { + obj := ri.(map[string]interface{}) + if obj["name"] == nil { + log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) + return nil + } + + name := tpgresource.GetResourceNameFromSelfLink(obj["name"].(string)) + // Skip resources that shouldn't be sweeped + if !sweeper.IsSweepableTestResource(name) { + nonPrefixCount++ + continue + } + + deleteTemplate := "https://tpu.googleapis.com/v2/projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}" + deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) + return nil + } + deleteUrl = deleteUrl + name + + // Don't wait on operations as we may have a lot to delete + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: config.Project, + RawURL: deleteUrl, + UserAgent: config.UserAgent, + }) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) + } else { + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) + } + } + + if nonPrefixCount > 0 { + log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) + } + + return nil +} diff --git a/website/docs/r/tpu_v2_queued_resource.html.markdown b/website/docs/r/tpu_v2_queued_resource.html.markdown new file mode 100644 index 0000000000..37d080ef8a --- /dev/null +++ b/website/docs/r/tpu_v2_queued_resource.html.markdown @@ -0,0 +1,168 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** Type: MMv1 *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in +# .github/CONTRIBUTING.md. +# +# ---------------------------------------------------------------------------- +subcategory: "Cloud TPU v2" +description: |- + A Cloud TPU Queued Resource. +--- + +# google_tpu_v2_queued_resource + +A Cloud TPU Queued Resource. + +~> **Warning:** This resource is in beta, and should be used with the terraform-provider-google-beta provider. +See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources. + +To get more information about QueuedResource, see: + +* [API documentation](https://cloud.google.com/tpu/docs/reference/rest/v2/projects.locations.queuedResources) +* How-to Guides + * [Official Documentation](https://cloud.google.com/tpu/docs/) + +## Example Usage - Tpu V2 Queued Resource Basic + + +```hcl +resource "google_tpu_v2_queued_resource" "qr" { + provider = google-beta + + name = "test-qr" + zone = "us-central1-c" + project = "my-project-name" + + tpu { + node_spec { + parent = "projects/my-project-name/locations/us-central1-c" + node_id = "test-tpu" + node { + runtime_version = "tpu-vm-tf-2.13.0" + accelerator_type = "v2-8" + description = "Text description of the TPU." + } + } + } +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `name` - + (Required) + The immutable name of the Queued Resource. + + +- - - + + +* `tpu` - + (Optional) + Defines a TPU resource. + Structure is [documented below](#nested_tpu). + +* `zone` - + (Optional) + The GCP location for the Queued Resource. If it is not provided, the provider zone is used. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +The `tpu` block supports: + +* `node_spec` - + (Optional) + The TPU node(s) being requested. + Structure is [documented below](#nested_node_spec). + + +The `node_spec` block supports: + +* `parent` - + (Required) + The parent resource name. + +* `node_id` - + (Optional) + Unqualified node identifier used to identify the node in the project once provisioned. + +* `node` - + (Required) + The node. + Structure is [documented below](#nested_node). + + +The `node` block supports: + +* `runtime_version` - + (Required) + Runtime version for the TPU. + +* `accelerator_type` - + (Optional) + TPU accelerator type for the TPU. If not specified, this defaults to 'v2-8'. + +* `description` - + (Optional) + Text description of the TPU. + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}` + + +## Timeouts + +This resource provides the following +[Timeouts](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts) configuration options: + +- `create` - Default is 20 minutes. +- `delete` - Default is 20 minutes. + +## Import + + +QueuedResource can be imported using any of these accepted formats: + +* `projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}` +* `{{project}}/{{zone}}/{{name}}` +* `{{zone}}/{{name}}` +* `{{name}}` + + +In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import QueuedResource using one of the formats above. For example: + +```tf +import { + id = "projects/{{project}}/locations/{{zone}}/queuedResources/{{name}}" + to = google_tpu_v2_queued_resource.default +} +``` + +When using the [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import), QueuedResource can be imported using one of the formats above. For example: + +``` +$ terraform import google_tpu_v2_queued_resource.default projects/{{project}}/locations/{{zone}}/queuedResources/{{name}} +$ terraform import google_tpu_v2_queued_resource.default {{project}}/{{zone}}/{{name}} +$ terraform import google_tpu_v2_queued_resource.default {{zone}}/{{name}} +$ terraform import google_tpu_v2_queued_resource.default {{name}} +``` + +## User Project Overrides + +This resource supports [User Project Overrides](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#user_project_override).