| // 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 compute |
| |
| import ( |
| "fmt" |
| "log" |
| "net/url" |
| "reflect" |
| "strconv" |
| "strings" |
| "time" |
| |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| |
| "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| "github.com/hashicorp/terraform-provider-google-beta/google-beta/verify" |
| ) |
| |
| func ResourceComputeReservation() *schema.Resource { |
| return &schema.Resource{ |
| Create: resourceComputeReservationCreate, |
| Read: resourceComputeReservationRead, |
| Update: resourceComputeReservationUpdate, |
| Delete: resourceComputeReservationDelete, |
| |
| Importer: &schema.ResourceImporter{ |
| State: resourceComputeReservationImport, |
| }, |
| |
| Timeouts: &schema.ResourceTimeout{ |
| Create: schema.DefaultTimeout(20 * time.Minute), |
| Update: 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: `Name of the resource. Provided by the client when the resource is |
| created. The name must be 1-63 characters long, and comply with |
| RFC1035. Specifically, the name must be 1-63 characters long and match |
| the regular expression '[a-z]([-a-z0-9]*[a-z0-9])?' which means the |
| first character must be a lowercase letter, and all following |
| characters must be a dash, lowercase letter, or digit, except the last |
| character, which cannot be a dash.`, |
| }, |
| "specific_reservation": { |
| Type: schema.TypeList, |
| Required: true, |
| Description: `Reservation for instances with specific machine shapes.`, |
| MaxItems: 1, |
| Elem: &schema.Resource{ |
| Schema: map[string]*schema.Schema{ |
| "count": { |
| Type: schema.TypeInt, |
| Required: true, |
| ValidateFunc: validation.IntAtLeast(1), |
| Description: `The number of resources that are allocated.`, |
| }, |
| "instance_properties": { |
| Type: schema.TypeList, |
| Required: true, |
| ForceNew: true, |
| Description: `The instance properties for the reservation.`, |
| MaxItems: 1, |
| Elem: &schema.Resource{ |
| Schema: map[string]*schema.Schema{ |
| "machine_type": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `The name of the machine type to reserve.`, |
| }, |
| "guest_accelerators": { |
| Type: schema.TypeList, |
| Optional: true, |
| ForceNew: true, |
| Description: `Guest accelerator type and count.`, |
| Elem: &schema.Resource{ |
| Schema: map[string]*schema.Schema{ |
| "accelerator_count": { |
| Type: schema.TypeInt, |
| Required: true, |
| ForceNew: true, |
| Description: `The number of the guest accelerator cards exposed to |
| this instance.`, |
| }, |
| "accelerator_type": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `The full or partial URL of the accelerator type to |
| attach to this instance. For example: |
| 'projects/my-project/zones/us-central1-c/acceleratorTypes/nvidia-tesla-p100' |
| |
| If you are creating an instance template, specify only the accelerator name.`, |
| }, |
| }, |
| }, |
| }, |
| "local_ssds": { |
| Type: schema.TypeList, |
| Optional: true, |
| ForceNew: true, |
| Description: `The amount of local ssd to reserve with each instance. This |
| reserves disks of type 'local-ssd'.`, |
| Elem: &schema.Resource{ |
| Schema: map[string]*schema.Schema{ |
| "disk_size_gb": { |
| Type: schema.TypeInt, |
| Required: true, |
| ForceNew: true, |
| Description: `The size of the disk in base-2 GB.`, |
| }, |
| "interface": { |
| Type: schema.TypeString, |
| Optional: true, |
| ForceNew: true, |
| ValidateFunc: verify.ValidateEnum([]string{"SCSI", "NVME", ""}), |
| Description: `The disk interface to use for attaching this disk. Default value: "SCSI" Possible values: ["SCSI", "NVME"]`, |
| Default: "SCSI", |
| }, |
| }, |
| }, |
| }, |
| "min_cpu_platform": { |
| Type: schema.TypeString, |
| Computed: true, |
| Optional: true, |
| ForceNew: true, |
| Description: `The minimum CPU platform for the reservation. For example, |
| '"Intel Skylake"'. See |
| the CPU platform availability reference](https://cloud.google.com/compute/docs/instances/specify-min-cpu-platform#availablezones) |
| for information on available CPU platforms.`, |
| }, |
| }, |
| }, |
| }, |
| "in_use_count": { |
| Type: schema.TypeInt, |
| Computed: true, |
| Description: `How many instances are in use.`, |
| }, |
| }, |
| }, |
| }, |
| "zone": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, |
| Description: `The zone where the reservation is made.`, |
| }, |
| "description": { |
| Type: schema.TypeString, |
| Optional: true, |
| ForceNew: true, |
| Description: `An optional description of this resource.`, |
| }, |
| "share_settings": { |
| Type: schema.TypeList, |
| Computed: true, |
| Optional: true, |
| Description: `The share setting for reservations.`, |
| MaxItems: 1, |
| Elem: &schema.Resource{ |
| Schema: map[string]*schema.Schema{ |
| "project_map": { |
| Type: schema.TypeSet, |
| Optional: true, |
| Description: `A map of project number and project config. This is only valid when shareType's value is SPECIFIC_PROJECTS.`, |
| Elem: &schema.Resource{ |
| Schema: map[string]*schema.Schema{ |
| "id": { |
| Type: schema.TypeString, |
| Required: true, |
| }, |
| "project_id": { |
| Type: schema.TypeString, |
| Optional: true, |
| Description: `The project id/number, should be same as the key of this project config in the project map.`, |
| }, |
| }, |
| }, |
| }, |
| "share_type": { |
| Type: schema.TypeString, |
| Computed: true, |
| Optional: true, |
| ValidateFunc: verify.ValidateEnum([]string{"LOCAL", "SPECIFIC_PROJECTS", ""}), |
| Description: `Type of sharing for this shared-reservation Possible values: ["LOCAL", "SPECIFIC_PROJECTS"]`, |
| }, |
| }, |
| }, |
| }, |
| "specific_reservation_required": { |
| Type: schema.TypeBool, |
| Optional: true, |
| ForceNew: true, |
| Description: `When set to true, only VMs that target this reservation by name can |
| consume this reservation. Otherwise, it can be consumed by VMs with |
| affinity for any reservation. Defaults to false.`, |
| Default: false, |
| }, |
| "commitment": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `Full or partial URL to a parent commitment. This field displays for |
| reservations that are tied to a commitment.`, |
| }, |
| "creation_timestamp": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `Creation timestamp in RFC3339 text format.`, |
| }, |
| "status": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `The status of the reservation.`, |
| }, |
| "project": { |
| Type: schema.TypeString, |
| Optional: true, |
| Computed: true, |
| ForceNew: true, |
| }, |
| "self_link": { |
| Type: schema.TypeString, |
| Computed: true, |
| }, |
| }, |
| UseJSONNumber: true, |
| } |
| } |
| |
| func resourceComputeReservationCreate(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{}) |
| descriptionProp, err := expandComputeReservationDescription(d.Get("description"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { |
| obj["description"] = descriptionProp |
| } |
| nameProp, err := expandComputeReservationName(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 |
| } |
| specificReservationRequiredProp, err := expandComputeReservationSpecificReservationRequired(d.Get("specific_reservation_required"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("specific_reservation_required"); !tpgresource.IsEmptyValue(reflect.ValueOf(specificReservationRequiredProp)) && (ok || !reflect.DeepEqual(v, specificReservationRequiredProp)) { |
| obj["specificReservationRequired"] = specificReservationRequiredProp |
| } |
| shareSettingsProp, err := expandComputeReservationShareSettings(d.Get("share_settings"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("share_settings"); !tpgresource.IsEmptyValue(reflect.ValueOf(shareSettingsProp)) && (ok || !reflect.DeepEqual(v, shareSettingsProp)) { |
| obj["shareSettings"] = shareSettingsProp |
| } |
| specificReservationProp, err := expandComputeReservationSpecificReservation(d.Get("specific_reservation"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("specific_reservation"); !tpgresource.IsEmptyValue(reflect.ValueOf(specificReservationProp)) && (ok || !reflect.DeepEqual(v, specificReservationProp)) { |
| obj["specificReservation"] = specificReservationProp |
| } |
| zoneProp, err := expandComputeReservationZone(d.Get("zone"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("zone"); !tpgresource.IsEmptyValue(reflect.ValueOf(zoneProp)) && (ok || !reflect.DeepEqual(v, zoneProp)) { |
| obj["zone"] = zoneProp |
| } |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations") |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Creating new Reservation: %#v", obj) |
| billingProject := "" |
| |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return fmt.Errorf("Error fetching project for Reservation: %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 |
| } |
| |
| 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), |
| }) |
| if err != nil { |
| return fmt.Errorf("Error creating Reservation: %s", err) |
| } |
| |
| // Store the ID now |
| id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/zones/{{zone}}/reservations/{{name}}") |
| if err != nil { |
| return fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| err = ComputeOperationWaitTime( |
| config, res, project, "Creating Reservation", userAgent, |
| d.Timeout(schema.TimeoutCreate)) |
| |
| if err != nil { |
| // The resource didn't actually create |
| d.SetId("") |
| return fmt.Errorf("Error waiting to create Reservation: %s", err) |
| } |
| |
| log.Printf("[DEBUG] Finished creating Reservation %q: %#v", d.Id(), res) |
| |
| return resourceComputeReservationRead(d, meta) |
| } |
| |
| func resourceComputeReservationRead(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, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}") |
| if err != nil { |
| return err |
| } |
| |
| billingProject := "" |
| |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return fmt.Errorf("Error fetching project for Reservation: %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 |
| } |
| |
| res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| Config: config, |
| Method: "GET", |
| Project: billingProject, |
| RawURL: url, |
| UserAgent: userAgent, |
| }) |
| if err != nil { |
| return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ComputeReservation %q", d.Id())) |
| } |
| |
| if err := d.Set("project", project); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| |
| if err := d.Set("creation_timestamp", flattenComputeReservationCreationTimestamp(res["creationTimestamp"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| if err := d.Set("description", flattenComputeReservationDescription(res["description"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| if err := d.Set("name", flattenComputeReservationName(res["name"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| if err := d.Set("commitment", flattenComputeReservationCommitment(res["commitment"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| if err := d.Set("specific_reservation_required", flattenComputeReservationSpecificReservationRequired(res["specificReservationRequired"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| if err := d.Set("status", flattenComputeReservationStatus(res["status"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| if err := d.Set("specific_reservation", flattenComputeReservationSpecificReservation(res["specificReservation"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| if err := d.Set("zone", flattenComputeReservationZone(res["zone"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| if err := d.Set("self_link", tpgresource.ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil { |
| return fmt.Errorf("Error reading Reservation: %s", err) |
| } |
| |
| return nil |
| } |
| |
| func resourceComputeReservationUpdate(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 Reservation: %s", err) |
| } |
| billingProject = project |
| |
| obj := make(map[string]interface{}) |
| shareSettingsProp, err := expandComputeReservationShareSettings(d.Get("share_settings"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("share_settings"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, shareSettingsProp)) { |
| obj["shareSettings"] = shareSettingsProp |
| } |
| |
| obj, err = resourceComputeReservationUpdateEncoder(d, meta, obj) |
| if err != nil { |
| return err |
| } |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}") |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Updating Reservation %q: %#v", d.Id(), obj) |
| updateMask := []string{} |
| |
| if d.HasChange("share_settings") { |
| updateMask = append(updateMask, "shareSettings") |
| } |
| // updateMask is a URL parameter but not present in the schema, so ReplaceVars |
| // won't set it |
| url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) |
| if err != nil { |
| return err |
| } |
| if d.HasChange("share_settings") { |
| url, err = tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}") |
| if err != nil { |
| return err |
| } |
| urlUpdateMask := obj["urlUpdateMask"] |
| if urlUpdateMask != nil { |
| url = url + urlUpdateMask.(string) |
| delete(obj, "urlUpdateMask") |
| } |
| } |
| |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| |
| // if updateMask is empty we are not updating anything so skip the post |
| if len(updateMask) > 0 { |
| res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| Config: config, |
| Method: "PATCH", |
| Project: billingProject, |
| RawURL: url, |
| UserAgent: userAgent, |
| Body: obj, |
| Timeout: d.Timeout(schema.TimeoutUpdate), |
| }) |
| |
| if err != nil { |
| return fmt.Errorf("Error updating Reservation %q: %s", d.Id(), err) |
| } else { |
| log.Printf("[DEBUG] Finished updating Reservation %q: %#v", d.Id(), res) |
| } |
| |
| err = ComputeOperationWaitTime( |
| config, res, project, "Updating Reservation", userAgent, |
| d.Timeout(schema.TimeoutUpdate)) |
| |
| if err != nil { |
| return err |
| } |
| } |
| d.Partial(true) |
| |
| if d.HasChange("specific_reservation") { |
| obj := make(map[string]interface{}) |
| |
| specificReservationProp, err := expandComputeReservationSpecificReservation(d.Get("specific_reservation"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("specific_reservation"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, specificReservationProp)) { |
| obj["specificReservation"] = specificReservationProp |
| } |
| |
| obj, err = resourceComputeReservationUpdateEncoder(d, meta, obj) |
| if err != nil { |
| return err |
| } |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}/resize") |
| if err != nil { |
| return err |
| } |
| |
| if d.HasChange("share_settings") { |
| url, err = tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}") |
| if err != nil { |
| return err |
| } |
| urlUpdateMask := obj["urlUpdateMask"] |
| if urlUpdateMask != nil { |
| url = url + urlUpdateMask.(string) |
| delete(obj, "urlUpdateMask") |
| } |
| } |
| |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| |
| res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| Config: config, |
| Method: "POST", |
| Project: billingProject, |
| RawURL: url, |
| UserAgent: userAgent, |
| Body: obj, |
| Timeout: d.Timeout(schema.TimeoutUpdate), |
| }) |
| if err != nil { |
| return fmt.Errorf("Error updating Reservation %q: %s", d.Id(), err) |
| } else { |
| log.Printf("[DEBUG] Finished updating Reservation %q: %#v", d.Id(), res) |
| } |
| |
| err = ComputeOperationWaitTime( |
| config, res, project, "Updating Reservation", userAgent, |
| d.Timeout(schema.TimeoutUpdate)) |
| if err != nil { |
| return err |
| } |
| } |
| |
| d.Partial(false) |
| |
| return resourceComputeReservationRead(d, meta) |
| } |
| |
| func resourceComputeReservationDelete(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 Reservation: %s", err) |
| } |
| billingProject = project |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}") |
| if err != nil { |
| return err |
| } |
| |
| var obj map[string]interface{} |
| log.Printf("[DEBUG] Deleting Reservation %q", d.Id()) |
| |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| |
| 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), |
| }) |
| if err != nil { |
| return transport_tpg.HandleNotFoundError(err, d, "Reservation") |
| } |
| |
| err = ComputeOperationWaitTime( |
| config, res, project, "Deleting Reservation", userAgent, |
| d.Timeout(schema.TimeoutDelete)) |
| |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Finished deleting Reservation %q: %#v", d.Id(), res) |
| return nil |
| } |
| |
| func resourceComputeReservationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| config := meta.(*transport_tpg.Config) |
| if err := tpgresource.ParseImportId([]string{ |
| "^projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/reservations/(?P<name>[^/]+)$", |
| "^(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)$", |
| "^(?P<zone>[^/]+)/(?P<name>[^/]+)$", |
| "^(?P<name>[^/]+)$", |
| }, d, config); err != nil { |
| return nil, err |
| } |
| |
| // Replace import id for the resource id |
| id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/zones/{{zone}}/reservations/{{name}}") |
| if err != nil { |
| return nil, fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| return []*schema.ResourceData{d}, nil |
| } |
| |
| func flattenComputeReservationCreationTimestamp(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationDescription(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationCommitment(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationSpecificReservationRequired(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationStatus(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationSpecificReservation(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["count"] = |
| flattenComputeReservationSpecificReservationCount(original["count"], d, config) |
| transformed["in_use_count"] = |
| flattenComputeReservationSpecificReservationInUseCount(original["inUseCount"], d, config) |
| transformed["instance_properties"] = |
| flattenComputeReservationSpecificReservationInstanceProperties(original["instanceProperties"], d, config) |
| return []interface{}{transformed} |
| } |
| func flattenComputeReservationSpecificReservationCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| // Handles the string fixed64 format |
| if strVal, ok := v.(string); ok { |
| if intVal, err := tpgresource.StringToFixed64(strVal); err == nil { |
| return intVal |
| } |
| } |
| |
| // number values are represented as float64 |
| if floatVal, ok := v.(float64); ok { |
| intVal := int(floatVal) |
| return intVal |
| } |
| |
| return v // let terraform core handle it otherwise |
| } |
| |
| func flattenComputeReservationSpecificReservationInUseCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| // Handles the string fixed64 format |
| if strVal, ok := v.(string); ok { |
| if intVal, err := tpgresource.StringToFixed64(strVal); err == nil { |
| return intVal |
| } |
| } |
| |
| // number values are represented as float64 |
| if floatVal, ok := v.(float64); ok { |
| intVal := int(floatVal) |
| return intVal |
| } |
| |
| return v // let terraform core handle it otherwise |
| } |
| |
| func flattenComputeReservationSpecificReservationInstanceProperties(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["machine_type"] = |
| flattenComputeReservationSpecificReservationInstancePropertiesMachineType(original["machineType"], d, config) |
| transformed["min_cpu_platform"] = |
| flattenComputeReservationSpecificReservationInstancePropertiesMinCpuPlatform(original["minCpuPlatform"], d, config) |
| transformed["guest_accelerators"] = |
| flattenComputeReservationSpecificReservationInstancePropertiesGuestAccelerators(original["guestAccelerators"], d, config) |
| transformed["local_ssds"] = |
| flattenComputeReservationSpecificReservationInstancePropertiesLocalSsds(original["localSsds"], d, config) |
| return []interface{}{transformed} |
| } |
| func flattenComputeReservationSpecificReservationInstancePropertiesMachineType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationSpecificReservationInstancePropertiesMinCpuPlatform(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationSpecificReservationInstancePropertiesGuestAccelerators(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{}{ |
| "accelerator_type": flattenComputeReservationSpecificReservationInstancePropertiesGuestAcceleratorsAcceleratorType(original["acceleratorType"], d, config), |
| "accelerator_count": flattenComputeReservationSpecificReservationInstancePropertiesGuestAcceleratorsAcceleratorCount(original["acceleratorCount"], d, config), |
| }) |
| } |
| return transformed |
| } |
| func flattenComputeReservationSpecificReservationInstancePropertiesGuestAcceleratorsAcceleratorType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationSpecificReservationInstancePropertiesGuestAcceleratorsAcceleratorCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| // Handles the string fixed64 format |
| if strVal, ok := v.(string); ok { |
| if intVal, err := tpgresource.StringToFixed64(strVal); err == nil { |
| return intVal |
| } |
| } |
| |
| // number values are represented as float64 |
| if floatVal, ok := v.(float64); ok { |
| intVal := int(floatVal) |
| return intVal |
| } |
| |
| return v // let terraform core handle it otherwise |
| } |
| |
| func flattenComputeReservationSpecificReservationInstancePropertiesLocalSsds(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{}{ |
| "interface": flattenComputeReservationSpecificReservationInstancePropertiesLocalSsdsInterface(original["interface"], d, config), |
| "disk_size_gb": flattenComputeReservationSpecificReservationInstancePropertiesLocalSsdsDiskSizeGb(original["diskSizeGb"], d, config), |
| }) |
| } |
| return transformed |
| } |
| func flattenComputeReservationSpecificReservationInstancePropertiesLocalSsdsInterface(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenComputeReservationSpecificReservationInstancePropertiesLocalSsdsDiskSizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| // Handles the string fixed64 format |
| if strVal, ok := v.(string); ok { |
| if intVal, err := tpgresource.StringToFixed64(strVal); err == nil { |
| return intVal |
| } |
| } |
| |
| // number values are represented as float64 |
| if floatVal, ok := v.(float64); ok { |
| intVal := int(floatVal) |
| return intVal |
| } |
| |
| return v // let terraform core handle it otherwise |
| } |
| |
| func flattenComputeReservationZone(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| if v == nil { |
| return v |
| } |
| return tpgresource.ConvertSelfLinkToV1(v.(string)) |
| } |
| |
| func expandComputeReservationDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservationRequired(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationShareSettings(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{}) |
| |
| transformedShareType, err := expandComputeReservationShareSettingsShareType(original["share_type"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedShareType); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["shareType"] = transformedShareType |
| } |
| |
| transformedProjectMap, err := expandComputeReservationShareSettingsProjectMap(original["project_map"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedProjectMap); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["projectMap"] = transformedProjectMap |
| } |
| |
| return transformed, nil |
| } |
| |
| func expandComputeReservationShareSettingsShareType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationShareSettingsProjectMap(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) { |
| if v == nil { |
| return map[string]interface{}{}, nil |
| } |
| m := make(map[string]interface{}) |
| for _, raw := range v.(*schema.Set).List() { |
| original := raw.(map[string]interface{}) |
| transformed := make(map[string]interface{}) |
| |
| transformedProjectId, err := expandComputeReservationShareSettingsProjectMapProjectId(original["project_id"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedProjectId); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["projectId"] = transformedProjectId |
| } |
| |
| transformedId, err := tpgresource.ExpandString(original["id"], d, config) |
| if err != nil { |
| return nil, err |
| } |
| m[transformedId] = transformed |
| } |
| return m, nil |
| } |
| |
| func expandComputeReservationShareSettingsProjectMapProjectId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservation(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{}) |
| |
| transformedCount, err := expandComputeReservationSpecificReservationCount(original["count"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedCount); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["count"] = transformedCount |
| } |
| |
| transformedInUseCount, err := expandComputeReservationSpecificReservationInUseCount(original["in_use_count"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedInUseCount); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["inUseCount"] = transformedInUseCount |
| } |
| |
| transformedInstanceProperties, err := expandComputeReservationSpecificReservationInstanceProperties(original["instance_properties"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedInstanceProperties); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["instanceProperties"] = transformedInstanceProperties |
| } |
| |
| return transformed, nil |
| } |
| |
| func expandComputeReservationSpecificReservationCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInUseCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstanceProperties(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{}) |
| |
| transformedMachineType, err := expandComputeReservationSpecificReservationInstancePropertiesMachineType(original["machine_type"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedMachineType); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["machineType"] = transformedMachineType |
| } |
| |
| transformedMinCpuPlatform, err := expandComputeReservationSpecificReservationInstancePropertiesMinCpuPlatform(original["min_cpu_platform"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedMinCpuPlatform); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["minCpuPlatform"] = transformedMinCpuPlatform |
| } |
| |
| transformedGuestAccelerators, err := expandComputeReservationSpecificReservationInstancePropertiesGuestAccelerators(original["guest_accelerators"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedGuestAccelerators); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["guestAccelerators"] = transformedGuestAccelerators |
| } |
| |
| transformedLocalSsds, err := expandComputeReservationSpecificReservationInstancePropertiesLocalSsds(original["local_ssds"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedLocalSsds); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["localSsds"] = transformedLocalSsds |
| } |
| |
| return transformed, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstancePropertiesMachineType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstancePropertiesMinCpuPlatform(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstancePropertiesGuestAccelerators(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{}) |
| |
| transformedAcceleratorType, err := expandComputeReservationSpecificReservationInstancePropertiesGuestAcceleratorsAcceleratorType(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 |
| } |
| |
| transformedAcceleratorCount, err := expandComputeReservationSpecificReservationInstancePropertiesGuestAcceleratorsAcceleratorCount(original["accelerator_count"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedAcceleratorCount); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["acceleratorCount"] = transformedAcceleratorCount |
| } |
| |
| req = append(req, transformed) |
| } |
| return req, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstancePropertiesGuestAcceleratorsAcceleratorType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstancePropertiesGuestAcceleratorsAcceleratorCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstancePropertiesLocalSsds(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{}) |
| |
| transformedInterface, err := expandComputeReservationSpecificReservationInstancePropertiesLocalSsdsInterface(original["interface"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedInterface); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["interface"] = transformedInterface |
| } |
| |
| transformedDiskSizeGb, err := expandComputeReservationSpecificReservationInstancePropertiesLocalSsdsDiskSizeGb(original["disk_size_gb"], d, config) |
| if err != nil { |
| return nil, err |
| } else if val := reflect.ValueOf(transformedDiskSizeGb); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| transformed["diskSizeGb"] = transformedDiskSizeGb |
| } |
| |
| req = append(req, transformed) |
| } |
| return req, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstancePropertiesLocalSsdsInterface(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationSpecificReservationInstancePropertiesLocalSsdsDiskSizeGb(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandComputeReservationZone(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| f, err := tpgresource.ParseGlobalFieldValue("zones", v.(string), "project", d, config, true) |
| if err != nil { |
| return nil, fmt.Errorf("Invalid value for zone: %s", err) |
| } |
| return f.RelativeLink(), nil |
| } |
| |
| func resourceComputeReservationUpdateEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { |
| newObj := make(map[string]interface{}) |
| config := meta.(*transport_tpg.Config) |
| maskId := "" |
| firstProject := true |
| urlUpdateMask := "" |
| |
| if d.HasChange("share_settings") { |
| // Get name. |
| nameProp, err := expandComputeReservationName(d.Get("name"), d, config) |
| if err != nil { |
| return nil, fmt.Errorf("Invalid value for name: %s", err) |
| } else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { |
| newObj["name"] = nameProp |
| } |
| // Get zone. |
| zoneProp, err := expandComputeReservationZone(d.Get("zone"), d, config) |
| if err != nil { |
| return nil, fmt.Errorf("Invalid value for zone: %s", err) |
| } else if v, ok := d.GetOkExists("zone"); !tpgresource.IsEmptyValue(reflect.ValueOf(zoneProp)) && (ok || !reflect.DeepEqual(v, zoneProp)) { |
| newObj["zone"] = zoneProp |
| } |
| transformed := make(map[string]interface{}) |
| // Set shareType. |
| transformed["shareType"] = "SPECIFIC_PROJECTS" |
| // Set project_map. |
| projectMap := make(map[string]interface{}) |
| old, new := d.GetChange("share_settings") |
| oldMap := old.([]interface{})[0].(map[string]interface{})["project_map"] |
| newMap := new.([]interface{})[0].(map[string]interface{})["project_map"] |
| before := oldMap.(*schema.Set) |
| after := newMap.(*schema.Set) |
| |
| for _, raw := range after.Difference(before).List() { |
| original := raw.(map[string]interface{}) |
| singleProject := make(map[string]interface{}) |
| // set up project_map. |
| transformedProjectId := original["project_id"] |
| if val := reflect.ValueOf(transformedProjectId); val.IsValid() && !tpgresource.IsEmptyValue(val) { |
| singleProject["projectId"] = transformedProjectId |
| } |
| transformedId, err := tpgresource.ExpandString(original["id"], d, config) |
| if err != nil { |
| return nil, fmt.Errorf("Invalid value for id: %s", err) |
| } |
| projectMap[transformedId] = singleProject |
| // add added projects to updateMask |
| if firstProject != true { |
| maskId = fmt.Sprintf("%s%s", "&paths=shareSettings.projectMap.", original["project_id"]) |
| } else { |
| maskId = fmt.Sprintf("%s%s", "?paths=shareSettings.projectMap.", original["project_id"]) |
| firstProject = false |
| } |
| decodedPath, _ := url.QueryUnescape(maskId) |
| urlUpdateMask = urlUpdateMask + decodedPath |
| } |
| transformed["projectMap"] = projectMap |
| newObj["shareSettings"] = transformed |
| |
| // add removed projects to updateMask |
| firstProject = true |
| for _, raw := range before.Difference(after).List() { |
| original := raw.(map[string]interface{}) |
| // To remove a project we need project number. |
| projectId := fmt.Sprintf("%s", original["project_id"]) |
| projectIdOrNum := projectId |
| _, err := strconv.Atoi(projectId) |
| // convert id to number. |
| if err != nil { |
| config := meta.(*transport_tpg.Config) |
| project, err := config.NewResourceManagerClient(config.UserAgent).Projects.Get(projectId).Do() |
| if err != nil { |
| return nil, fmt.Errorf("Invalid value for projectId: %s", err) |
| } |
| projectNum := project.ProjectNumber |
| projectIdOrNum = fmt.Sprintf("%d", projectNum) |
| } |
| if firstProject != true { |
| maskId = fmt.Sprintf("%s%s", "&paths=shareSettings.projectMap.", projectIdOrNum) |
| } else { |
| maskId = fmt.Sprintf("%s%s", "?paths=shareSettings.projectMap.", projectIdOrNum) |
| firstProject = false |
| } |
| decodedPath, _ := url.QueryUnescape(maskId) |
| urlUpdateMask = urlUpdateMask + decodedPath |
| } |
| newObj["urlUpdateMask"] = urlUpdateMask |
| } |
| |
| // Resize. |
| if obj["specificReservation"] != nil { |
| count := obj["specificReservation"].(map[string]interface{})["count"] |
| if count != nil { |
| newObj["specificSkuCount"] = obj["specificReservation"].(map[string]interface{})["count"] |
| } |
| } |
| |
| return newObj, nil |
| } |