aboutsummaryrefslogtreecommitdiffstats
path: root/gce
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-05-06 11:17:21 -0700
committerDmitry Vyukov <dvyukov@google.com>2017-05-06 11:17:21 -0700
commit9d41442c5defe7072ced0a733c95df4fe766acb8 (patch)
tree020a0e58430ae06d21634ed55d9e310f2a366403 /gce
parent1310076fe3bf6ca0d91547ef60c49ab44ba0bf44 (diff)
gce: use name metadata to get instance name
Simplify code by using the new name metadata now that we have it.
Diffstat (limited to 'gce')
-rw-r--r--gce/gce.go28
1 files changed, 10 insertions, 18 deletions
diff --git a/gce/gce.go b/gce/gce.go
index 27bbbf6f2..b90200c3b 100644
--- a/gce/gce.go
+++ b/gce/gce.go
@@ -62,30 +62,22 @@ func NewContext() (*Context, error) {
if i := strings.LastIndexByte(ctx.ZoneID, '/'); i != -1 {
ctx.ZoneID = ctx.ZoneID[i+1:] // the query returns some nonsense prefix
}
- instID, err := ctx.getMeta("instance/id")
+ ctx.Instance, err = ctx.getMeta("instance/name")
if err != nil {
- return nil, fmt.Errorf("failed to query gce instance id: %v", err)
+ return nil, fmt.Errorf("failed to query gce instance name: %v", err)
}
- instances, err := ctx.computeService.Instances.List(ctx.ProjectID, ctx.ZoneID).Do()
+ inst, err := ctx.computeService.Instances.Get(ctx.ProjectID, ctx.ZoneID, ctx.Instance).Do()
if err != nil {
- return nil, fmt.Errorf("error getting instance list: %v", err)
+ return nil, fmt.Errorf("error getting instance info: %v", err)
}
- // Finds this instance internal IP.
- for _, inst := range instances.Items {
- if fmt.Sprint(inst.Id) != instID {
- continue
- }
- ctx.Instance = inst.Name
- for _, iface := range inst.NetworkInterfaces {
- if strings.HasPrefix(iface.NetworkIP, "10.") {
- ctx.InternalIP = iface.NetworkIP
- break
- }
+ for _, iface := range inst.NetworkInterfaces {
+ if strings.HasPrefix(iface.NetworkIP, "10.") {
+ ctx.InternalIP = iface.NetworkIP
+ break
}
- break
}
- if ctx.Instance == "" || ctx.InternalIP == "" {
- return nil, fmt.Errorf("failed to get current instance name and internal IP")
+ if ctx.InternalIP == "" {
+ return nil, fmt.Errorf("failed to get current instance internal IP")
}
return ctx, nil
}