This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from crawford/gce
datasource/gce: add initial support
- Loading branch information
Showing
9 changed files
with
219 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2016 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gce | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
|
||
"github.com/coreos/coreos-cloudinit/datasource" | ||
"github.com/coreos/coreos-cloudinit/datasource/metadata" | ||
) | ||
|
||
const ( | ||
apiVersion = "computeMetadata/v1/" | ||
metadataPath = apiVersion + "instance/" | ||
userdataPath = apiVersion + "instance/attributes/user-data" | ||
) | ||
|
||
type metadataService struct { | ||
metadata.MetadataService | ||
} | ||
|
||
func NewDatasource(root string) *metadataService { | ||
return &metadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath, http.Header{"Metadata-Flavor": {"Google"}})} | ||
} | ||
|
||
func (ms metadataService) FetchMetadata() (datasource.Metadata, error) { | ||
public, err := ms.fetchIP("network-interfaces/0/access-configs/0/external-ip") | ||
if err != nil { | ||
return datasource.Metadata{}, err | ||
} | ||
local, err := ms.fetchIP("network-interfaces/0/ip") | ||
if err != nil { | ||
return datasource.Metadata{}, err | ||
} | ||
hostname, err := ms.fetchString("hostname") | ||
if err != nil { | ||
return datasource.Metadata{}, err | ||
} | ||
|
||
return datasource.Metadata{ | ||
PublicIPv4: public, | ||
PrivateIPv4: local, | ||
Hostname: hostname, | ||
}, nil | ||
} | ||
|
||
func (ms metadataService) Type() string { | ||
return "gce-metadata-service" | ||
} | ||
|
||
func (ms metadataService) fetchString(key string) (string, error) { | ||
data, err := ms.FetchData(ms.MetadataUrl() + key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(data), nil | ||
} | ||
|
||
func (ms metadataService) fetchIP(key string) (net.IP, error) { | ||
str, err := ms.fetchString(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if str == "" { | ||
return nil, nil | ||
} | ||
|
||
if ip := net.ParseIP(str); ip != nil { | ||
return ip, nil | ||
} else { | ||
return nil, fmt.Errorf("couldn't parse %q as IP address", str) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2015 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gce | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/coreos/coreos-cloudinit/datasource" | ||
"github.com/coreos/coreos-cloudinit/datasource/metadata" | ||
"github.com/coreos/coreos-cloudinit/datasource/metadata/test" | ||
"github.com/coreos/coreos-cloudinit/pkg" | ||
) | ||
|
||
func TestType(t *testing.T) { | ||
want := "gce-metadata-service" | ||
if kind := (metadataService{}).Type(); kind != want { | ||
t.Fatalf("bad type: want %q, got %q", want, kind) | ||
} | ||
} | ||
|
||
func TestFetchMetadata(t *testing.T) { | ||
for _, tt := range []struct { | ||
root string | ||
metadataPath string | ||
resources map[string]string | ||
expect datasource.Metadata | ||
clientErr error | ||
expectErr error | ||
}{ | ||
{ | ||
root: "/", | ||
metadataPath: "computeMetadata/v1/instance/", | ||
resources: map[string]string{}, | ||
}, | ||
{ | ||
root: "/", | ||
metadataPath: "computeMetadata/v1/instance/", | ||
resources: map[string]string{ | ||
"/computeMetadata/v1/instance/hostname": "host", | ||
}, | ||
expect: datasource.Metadata{ | ||
Hostname: "host", | ||
}, | ||
}, | ||
{ | ||
root: "/", | ||
metadataPath: "computeMetadata/v1/instance/", | ||
resources: map[string]string{ | ||
"/computeMetadata/v1/instance/hostname": "host", | ||
"/computeMetadata/v1/instance/network-interfaces/0/ip": "1.2.3.4", | ||
"/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip": "5.6.7.8", | ||
}, | ||
expect: datasource.Metadata{ | ||
Hostname: "host", | ||
PrivateIPv4: net.ParseIP("1.2.3.4"), | ||
PublicIPv4: net.ParseIP("5.6.7.8"), | ||
}, | ||
}, | ||
{ | ||
clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")}, | ||
expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")}, | ||
}, | ||
} { | ||
service := &metadataService{metadata.MetadataService{ | ||
Root: tt.root, | ||
Client: &test.HttpClient{Resources: tt.resources, Err: tt.clientErr}, | ||
MetadataPath: tt.metadataPath, | ||
}} | ||
metadata, err := service.FetchMetadata() | ||
if Error(err) != Error(tt.expectErr) { | ||
t.Fatalf("bad error (%q): want %q, got %q", tt.resources, tt.expectErr, err) | ||
} | ||
if !reflect.DeepEqual(tt.expect, metadata) { | ||
t.Fatalf("bad fetch (%q): want %#v, got %#v", tt.resources, tt.expect, metadata) | ||
} | ||
} | ||
} | ||
|
||
func Error(err error) string { | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters