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 new region instance template resource #7507

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package google

import (
"encoding/json"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand Down Expand Up @@ -121,19 +123,60 @@ func resourceComputeInstanceFromTemplateCreate(d *schema.ResourceData, meta inte
return err
}

tpl, err := ParseInstanceTemplateFieldValue(d.Get("source_instance_template").(string), d, config)
if err != nil {
return err
}
sourceInstanceTemplate := d.Get("source_instance_template").(string)

it, err := config.NewComputeClient(userAgent).InstanceTemplates.Get(project, tpl.Name).Do()
tpl, err := ParseInstanceTemplateFieldValue(sourceInstanceTemplate, d, config)
if err != nil {
return err
}

instance.Disks, err = adjustInstanceFromTemplateDisks(d, config, it, zone, project)
if err != nil {
return err
it := compute.InstanceTemplate{}
var relativeUrl string

if strings.Contains(sourceInstanceTemplate, "global/instanceTemplates") {
instanceTemplate, err := config.NewComputeClient(userAgent).InstanceTemplates.Get(project, tpl.Name).Do()
if err != nil {
return err
}

it = *instanceTemplate
relativeUrl = tpl.RelativeLink()

instance.Disks, err = adjustInstanceFromTemplateDisks(d, config, &it, zone, project, false)
if err != nil {
return err
}
} else {
relativeUrl, err = replaceVars(d, config, "projects/{{project}}/regions/{{region}}/instanceTemplates/"+tpl.Name)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/instanceTemplates/" + tpl.Name)
if err != nil {
return err
}

instanceTemplate, err := SendRequest(config, "GET", project, url, userAgent, nil)
if err != nil {
return err
}

instancePropertiesObj, err := json.Marshal(instanceTemplate)
if err != nil {
fmt.Println(err)
return err
}

if err := json.Unmarshal(instancePropertiesObj, &it); err != nil {
fmt.Println(err)
return err
}

instance.Disks, err = adjustInstanceFromTemplateDisks(d, config, &it, zone, project, true)
if err != nil {
return err
}
}

// when we make the original call to expandComputeInstance expandScheduling is called, which sets default values.
Expand Down Expand Up @@ -163,7 +206,7 @@ func resourceComputeInstanceFromTemplateCreate(d *schema.ResourceData, meta inte
}

log.Printf("[INFO] Requesting instance creation")
op, err := config.NewComputeClient(userAgent).Instances.Insert(project, zone.Name, instance).SourceInstanceTemplate(tpl.RelativeLink()).Do()
op, err := config.NewComputeClient(userAgent).Instances.Insert(project, zone.Name, instance).SourceInstanceTemplate(relativeUrl).Do()
if err != nil {
return fmt.Errorf("Error creating instance: %s", err)
}
Expand All @@ -185,7 +228,7 @@ func resourceComputeInstanceFromTemplateCreate(d *schema.ResourceData, meta inte

// Instances have disks spread across multiple schema properties. This function
// ensures that overriding one of these properties does not override the others.
func adjustInstanceFromTemplateDisks(d *schema.ResourceData, config *Config, it *compute.InstanceTemplate, zone *compute.Zone, project string) ([]*compute.AttachedDisk, error) {
func adjustInstanceFromTemplateDisks(d *schema.ResourceData, config *Config, it *compute.InstanceTemplate, zone *compute.Zone, project string, isFromRegionalTemplate bool) ([]*compute.AttachedDisk, error) {
disks := []*compute.AttachedDisk{}
if _, hasBootDisk := d.GetOk("boot_disk"); hasBootDisk {
bootDisk, err := expandBootDisk(d, config, project)
Expand All @@ -197,7 +240,7 @@ func adjustInstanceFromTemplateDisks(d *schema.ResourceData, config *Config, it
// boot disk was not overridden, so use the one from the instance template
for _, disk := range it.Properties.Disks {
if disk.Boot {
if disk.Source != "" {
if disk.Source != "" && !isFromRegionalTemplate {
// Instances need a URL for the disk, but instance templates only have the name
disk.Source = fmt.Sprintf("projects/%s/zones/%s/disks/%s", project, zone.Name, disk.Source)
}
Expand Down Expand Up @@ -251,7 +294,7 @@ func adjustInstanceFromTemplateDisks(d *schema.ResourceData, config *Config, it
// attached disks were not overridden, so use the ones from the instance template
for _, disk := range it.Properties.Disks {
if !disk.Boot && disk.Type != "SCRATCH" {
if s := disk.Source; s != "" {
if s := disk.Source; s != "" && !isFromRegionalTemplate {
// Instances need a URL for the disk source, but instance templates
// only have the name (since they're global).
disk.Source = fmt.Sprintf("zones/%s/disks/%s", zone.Name, s)
Expand Down
Loading