Skip to content

Commit

Permalink
Add new region instance template resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Jingcong Wang committed Mar 22, 2023
1 parent 0ed7de8 commit 0d444a6
Show file tree
Hide file tree
Showing 6 changed files with 5,270 additions and 6 deletions.
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,17 +123,54 @@ func resourceComputeInstanceFromTemplateCreate(d *schema.ResourceData, meta inte
return err
}

tpl, err := ParseInstanceTemplateFieldValue(d.Get("source_instance_template").(string), d, config)
sourceInstanceTemplate := d.Get("source_instance_template").(string)

tpl, err := ParseInstanceTemplateFieldValue(sourceInstanceTemplate, d, config)
if err != nil {
return err
}

it, err := config.NewComputeClient(userAgent).InstanceTemplates.Get(project, tpl.Name).Do()
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()

} 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)
instance.Disks, err = adjustInstanceFromTemplateDisks(d, config, &it, zone, project)
if err != nil {
return err
}
Expand Down Expand Up @@ -163,7 +202,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 Down
Loading

0 comments on commit 0d444a6

Please sign in to comment.