datasource: Move datasources into their own packages.

This commit is contained in:
Alex Crawford
2014-07-30 13:56:36 -07:00
parent 49ac083af5
commit 8566a2c118
11 changed files with 54 additions and 33 deletions

View File

@@ -1,4 +1,4 @@
package datasource
package configdrive
import (
"io/ioutil"
@@ -6,13 +6,18 @@ import (
"path"
)
const (
ec2ApiVersion = "2009-04-04"
openstackApiVersion = "latest"
)
type configDrive struct {
root string
readFile func(filename string) ([]byte, error)
}
func NewConfigDrive(root string) *configDrive {
return &configDrive{root, ioutil.ReadFile}
func NewDatasource(root string) *configDrive {
return &configDrive{path.Join(root, "openstack"), ioutil.ReadFile}
}
func (cd *configDrive) IsAvailable() bool {
@@ -48,11 +53,11 @@ func (cd *configDrive) Type() string {
}
func (cd *configDrive) ec2Root() string {
return path.Join(cd.root, "ec2", Ec2ApiVersion)
return path.Join(cd.root, "ec2", ec2ApiVersion)
}
func (cd *configDrive) openstackRoot() string {
return path.Join(cd.root, "openstack", "latest")
return path.Join(cd.root, "openstack", openstackApiVersion)
}
func (cd *configDrive) tryReadFile(filename string) ([]byte, error) {

View File

@@ -1,4 +1,4 @@
package datasource
package configdrive
import (
"os"

View File

@@ -1,4 +1,4 @@
package datasource
package file
import (
"io/ioutil"
@@ -9,7 +9,7 @@ type localFile struct {
path string
}
func NewLocalFile(path string) *localFile {
func NewDatasource(path string) *localFile {
return &localFile{path}
}

View File

@@ -1,4 +1,4 @@
package datasource
package metadata
import (
"bufio"
@@ -21,6 +21,8 @@ import (
// [2] http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html#instancedata-data-categories
const (
Ec2ApiVersion = "2009-04-04"
OpenstackApiVersion = "2012-08-10"
BaseUrl = "http://169.254.169.254/"
Ec2UserdataUrl = BaseUrl + Ec2ApiVersion + "/user-data"
Ec2MetadataUrl = BaseUrl + Ec2ApiVersion + "/meta-data"
@@ -29,11 +31,7 @@ const (
type metadataService struct{}
type getter interface {
GetRetry(string) ([]byte, error)
}
func NewMetadataService() *metadataService {
func NewDatasource() *metadataService {
return &metadataService{}
}
@@ -76,7 +74,7 @@ func (ms *metadataService) Type() string {
return "metadata-service"
}
func fetchMetadata(client getter) ([]byte, error) {
func fetchMetadata(client pkg.Getter) ([]byte, error) {
attrs := make(map[string]interface{})
if keynames, err := fetchAttributes(client, fmt.Sprintf("%s/public-keys", Ec2MetadataUrl)); err == nil {
keyIDs := make(map[string]string)
@@ -131,7 +129,7 @@ func fetchMetadata(client getter) ([]byte, error) {
return json.Marshal(attrs)
}
func fetchAttributes(client getter, url string) ([]string, error) {
func fetchAttributes(client pkg.Getter, url string) ([]string, error) {
resp, err := client.GetRetry(url)
if err != nil {
return nil, err
@@ -144,7 +142,7 @@ func fetchAttributes(client getter, url string) ([]string, error) {
return data, scanner.Err()
}
func fetchAttribute(client getter, url string) (string, error) {
func fetchAttribute(client pkg.Getter, url string) (string, error) {
if attrs, err := fetchAttributes(client, url); err == nil && len(attrs) > 0 {
return attrs[0], nil
} else {

View File

@@ -1,4 +1,4 @@
package datasource
package metadata
import (
"bytes"

View File

@@ -1,4 +1,4 @@
package datasource
package proc_cmdline
import (
"errors"
@@ -18,7 +18,7 @@ type procCmdline struct {
Location string
}
func NewProcCmdline() *procCmdline {
func NewDatasource() *procCmdline {
return &procCmdline{Location: ProcCmdlineLocation}
}

View File

@@ -1,4 +1,4 @@
package datasource
package proc_cmdline
import (
"fmt"
@@ -75,7 +75,7 @@ func TestProcCmdlineAndFetchConfig(t *testing.T) {
t.Errorf("Test produced error: %v", err)
}
p := NewProcCmdline()
p := NewDatasource()
p.Location = file.Name()
cfg, err := p.FetchUserdata()
if err != nil {

View File

@@ -1,4 +1,4 @@
package datasource
package url
import "github.com/coreos/coreos-cloudinit/pkg"
@@ -6,7 +6,7 @@ type remoteFile struct {
url string
}
func NewRemoteFile(url string) *remoteFile {
func NewDatasource(url string) *remoteFile {
return &remoteFile{url}
}