Skip to content

Commit d0fdebc

Browse files
committed
Add self_link support to google_compute_instance datasource.
1 parent a044216 commit d0fdebc

5 files changed

+78
-45
lines changed

google/data_source_google_compute_instance.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,11 @@ func dataSourceGoogleComputeInstance() *schema.Resource {
2424
func dataSourceGoogleComputeInstanceRead(d *schema.ResourceData, meta interface{}) error {
2525
config := meta.(*Config)
2626

27-
project, err := getProject(d, config)
27+
project, zone, name, err := GetZonalResourcePropertiesFromSelfLinkOrSchema(d, config)
2828
if err != nil {
2929
return err
3030
}
3131

32-
zone, err := getZone(d, config)
33-
if err != nil {
34-
return err
35-
}
36-
37-
name := d.Get("name").(string)
38-
3932
instance, err := config.clientComputeBeta.Instances.Get(project, zone, name).Do()
4033
if err != nil {
4134
return handleNotFoundError(err, d, fmt.Sprintf("Instance %s", name))

google/data_source_google_compute_region_instance_group.go

+5-30
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package google
22

33
import (
4-
"errors"
54
"fmt"
65
"log"
7-
"net/url"
86
"strconv"
9-
"strings"
107

118
"github.com/hashicorp/terraform/helper/schema"
12-
compute "google.golang.org/api/compute/v1"
9+
10+
"google.golang.org/api/compute/v1"
1311
"google.golang.org/api/googleapi"
1412
)
1513

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

9189
func dataSourceComputeRegionInstanceGroupRead(d *schema.ResourceData, meta interface{}) error {
9290
config := meta.(*Config)
93-
var project, region, name string
94-
if self_link, ok := d.GetOk("self_link"); ok {
95-
parsed, err := url.Parse(self_link.(string))
96-
if err != nil {
97-
return err
98-
}
99-
s := strings.Split(parsed.Path, "/")
100-
project, region, name = s[4], s[6], s[8]
101-
// e.g. https://www.googleapis.com/compute/beta/projects/project_name/regions/region_name/instanceGroups/foobarbaz
102-
103-
} else {
104-
var err error
105-
project, err = getProject(d, config)
106-
if err != nil {
107-
return err
108-
}
109-
110-
region, err = getRegion(d, config)
111-
if err != nil {
112-
return err
113-
}
114-
n, ok := d.GetOk("name")
115-
name = n.(string)
116-
if !ok {
117-
return errors.New("Must provide either `self_link` or `name`.")
118-
}
91+
project, region, name, err := GetRegionalResourcePropertiesFromSelfLinkOrSchema(d, config)
92+
if err != nil {
93+
return err
11994
}
12095

12196
instanceGroup, err := config.clientCompute.RegionInstanceGroups.Get(

google/self_link_helpers.go

+57
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package google
22

33
import (
4+
"errors"
45
"fmt"
6+
"net/url"
57
"regexp"
68
"strings"
79

@@ -85,3 +87,58 @@ func NameFromSelfLinkStateFunc(v interface{}) string {
8587
func StoreResourceName(resourceLink interface{}) string {
8688
return GetResourceNameFromSelfLink(resourceLink.(string))
8789
}
90+
91+
type LocationType int
92+
93+
const (
94+
Zonal LocationType = iota
95+
Regional
96+
Global
97+
)
98+
99+
func GetZonalResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, config *Config) (string, string, string, error) {
100+
return getResourcePropertiesFromSelfLinkOrSchema(d, config, Zonal)
101+
}
102+
103+
func GetRegionalResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, config *Config) (string, string, string, error) {
104+
return getResourcePropertiesFromSelfLinkOrSchema(d, config, Regional)
105+
}
106+
107+
func getResourcePropertiesFromSelfLinkOrSchema(d *schema.ResourceData, config *Config, locationType LocationType) (string, string, string, error) {
108+
if selfLink, ok := d.GetOk("self_link"); ok {
109+
parsed, err := url.Parse(selfLink.(string))
110+
if err != nil {
111+
return "", "", "", err
112+
}
113+
114+
s := strings.Split(parsed.Path, "/")
115+
// https://www.googleapis.com/compute/beta/projects/project_name/regions/region_name/instanceGroups/foobarbaz
116+
// => project_name, region_name, foobarbaz
117+
return s[4], s[6], s[8], nil
118+
} else {
119+
project, err := getProject(d, config)
120+
if err != nil {
121+
return "", "", "", err
122+
}
123+
124+
location := ""
125+
if locationType == Regional {
126+
location, err = getRegion(d, config)
127+
if err != nil {
128+
return "", "", "", err
129+
}
130+
} else if locationType == Zonal {
131+
location, err = getZone(d, config)
132+
if err != nil {
133+
return "", "", "", err
134+
}
135+
}
136+
137+
n, ok := d.GetOk("name")
138+
name := n.(string)
139+
if !ok {
140+
return "", "", "", errors.New("must provide either `self_link` or `name`")
141+
}
142+
return project, location, name, nil
143+
}
144+
}

website/docs/d/datasource_compute_instance.html.markdown

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: |-
88

99
# google\_compute\_instance
1010

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

2828
The following arguments are supported:
2929

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

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

35-
* `zone` - (Optional) The zone of the instance. If it is not provided, the provider `zone` is used.
34+
---
35+
36+
* `project` - (Optional) The ID of the project in which the resource belongs.
37+
If `self_link` is provided, this value is ignored. If neither `self_link`
38+
nor `project` are provided, the provider project is used.
39+
40+
* `zone` - (Optional) The zone of the instance. If `self_link` is provided, this
41+
value is ignored. If neither `self_link` nor `zone` are provided, the
42+
provider zone is used.
3643

3744
## Attributes Reference
3845

website/docs/d/datasource_compute_region_instance_group.html.markdown

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ The following arguments are supported:
4646

4747
- - -
4848

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

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

0 commit comments

Comments
 (0)