Skip to content

Commit

Permalink
Add self_link support to google_compute_instance datasource.
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson committed Sep 6, 2018
1 parent a044216 commit d0fdebc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 45 deletions.
9 changes: 1 addition & 8 deletions google/data_source_google_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,11 @@ func dataSourceGoogleComputeInstance() *schema.Resource {
func dataSourceGoogleComputeInstanceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
project, zone, name, err := GetZonalResourcePropertiesFromSelfLinkOrSchema(d, config)
if err != nil {
return err
}

zone, err := getZone(d, config)
if err != nil {
return err
}

name := d.Get("name").(string)

instance, err := config.clientComputeBeta.Instances.Get(project, zone, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Instance %s", name))
Expand Down
35 changes: 5 additions & 30 deletions google/data_source_google_compute_region_instance_group.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package google

import (
"errors"
"fmt"
"log"
"net/url"
"strconv"
"strings"

"github.com/hashicorp/terraform/helper/schema"
compute "google.golang.org/api/compute/v1"

"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)

Expand Down Expand Up @@ -90,32 +88,9 @@ func dataSourceGoogleComputeRegionInstanceGroup() *schema.Resource {

func dataSourceComputeRegionInstanceGroupRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
var project, region, name string
if self_link, ok := d.GetOk("self_link"); ok {
parsed, err := url.Parse(self_link.(string))
if err != nil {
return err
}
s := strings.Split(parsed.Path, "/")
project, region, name = s[4], s[6], s[8]
// e.g. https://www.googleapis.com/compute/beta/projects/project_name/regions/region_name/instanceGroups/foobarbaz

} else {
var err error
project, err = getProject(d, config)
if err != nil {
return err
}

region, err = getRegion(d, config)
if err != nil {
return err
}
n, ok := d.GetOk("name")
name = n.(string)
if !ok {
return errors.New("Must provide either `self_link` or `name`.")
}
project, region, name, err := GetRegionalResourcePropertiesFromSelfLinkOrSchema(d, config)
if err != nil {
return err
}

instanceGroup, err := config.clientCompute.RegionInstanceGroups.Get(
Expand Down
57 changes: 57 additions & 0 deletions google/self_link_helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package google

import (
"errors"
"fmt"
"net/url"
"regexp"
"strings"

Expand Down Expand Up @@ -85,3 +87,58 @@ func NameFromSelfLinkStateFunc(v interface{}) string {
func StoreResourceName(resourceLink interface{}) string {
return GetResourceNameFromSelfLink(resourceLink.(string))
}

type LocationType int

const (
Zonal LocationType = iota
Regional
Global
)

func GetZonalResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, config *Config) (string, string, string, error) {
return getResourcePropertiesFromSelfLinkOrSchema(d, config, Zonal)
}

func GetRegionalResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, config *Config) (string, string, string, error) {
return getResourcePropertiesFromSelfLinkOrSchema(d, config, Regional)
}

func getResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, config *Config, locationType LocationType) (string, string, string, error) {
if selfLink, ok := d.GetOk("self_link"); ok {
parsed, err := url.Parse(selfLink.(string))
if err != nil {
return "", "", "", err
}

s := strings.Split(parsed.Path, "/")
// https://www.googleapis.com/compute/beta/projects/project_name/regions/region_name/instanceGroups/foobarbaz
// => project_name, region_name, foobarbaz
return s[4], s[6], s[8], nil
} else {
project, err := getProject(d, config)
if err != nil {
return "", "", "", err
}

location := ""
if locationType == Regional {
location, err = getRegion(d, config)
if err != nil {
return "", "", "", err
}
} else if locationType == Zonal {
location, err = getZone(d, config)
if err != nil {
return "", "", "", err
}
}

n, ok := d.GetOk("name")
name := n.(string)
if !ok {
return "", "", "", errors.New("must provide either `self_link` or `name`")
}
return project, location, name, nil
}
}
17 changes: 12 additions & 5 deletions website/docs/d/datasource_compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |-

# google\_compute\_instance

Get a VM instance resource within GCE. For more information see
Get information about a VM instance resource within GCE. For more information see
[the official documentation](https://cloud.google.com/compute/docs/instances)
and
[API](https://cloud.google.com/compute/docs/reference/latest/instances).
Expand All @@ -27,12 +27,19 @@ data "google_compute_instance" "appserver" {

The following arguments are supported:

* `name` - (Required) The name of the instance.
* `self_link` - (Optional) The self link of the instance. One of `name` or `self_link` must be provided.

* `project` - (Optional) The ID of the project in which the resource belongs. If it
is not provided, the provider project is used.
* `name` - (Optional) The name of the instance. One of `name` or `self_link` must be provided.

* `zone` - (Optional) The zone of the instance. If it is not provided, the provider `zone` is used.
---

* `project` - (Optional) The ID of the project in which the resource belongs.
If `self_link` is provided, this value is ignored. If neither `self_link`
nor `project` are provided, the provider project is used.

* `zone` - (Optional) The zone of the instance. If `self_link` is provided, this
value is ignored. If neither `self_link` nor `zone` are provided, the
provider zone is used.

## Attributes Reference

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ The following arguments are supported:

- - -

* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.
* `project` - (Optional) The ID of the project in which the resource belongs.
If `self_link` is provided, this value is ignored. If neither `self_link`
nor `project` are provided, the provider project is used.

* `region` - (Optional) The region in which the resource belongs. If `self_link`
is provided, this value is ignored. If neither `self_link` nor `region` are
Expand Down

0 comments on commit d0fdebc

Please sign in to comment.