datasource: Move datasources into their own packages.
This commit is contained in:
@@ -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) {
|
@@ -1,4 +1,4 @@
|
||||
package datasource
|
||||
package configdrive
|
||||
|
||||
import (
|
||||
"os"
|
@@ -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}
|
||||
}
|
||||
|
@@ -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 {
|
@@ -1,4 +1,4 @@
|
||||
package datasource
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"bytes"
|
@@ -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}
|
||||
}
|
||||
|
@@ -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 {
|
@@ -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}
|
||||
}
|
||||
|
Reference in New Issue
Block a user