Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
Василий Толстов 2015-03-26 10:43:37 +03:00
parent 0bc1edbd9d
commit b8521294cd

View File

@ -18,7 +18,10 @@ package openstack
import ( import (
"encoding/json" "encoding/json"
"net"
"strconv"
"github.com/coreos/coreos-cloudinit/datasource"
"github.com/coreos/coreos-cloudinit/datasource/metadata" "github.com/coreos/coreos-cloudinit/datasource/metadata"
) )
@ -54,14 +57,12 @@ type DNS struct {
type Metadata struct { type Metadata struct {
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
Interfaces Interfaces `json:"interfaces,omitempty"` Interfaces Interfaces `json:"interfaces"`
PublicKeys map[string]string `json:"public_keys"` PublicKeys []string `json:"public_keys"`
DNS DNS `json:"dns,omitempty"` DNS DNS `json:"dns"`
} }
type metadataService struct { type metadataService struct {
interfaces Interfaces
dns DNS
metadata.MetadataService metadata.MetadataService
} }
@ -69,55 +70,42 @@ func NewDatasource(root string) *metadataService {
return &metadataService{MetadataService: metadata.NewDatasource(root, apiVersion, userdataUrl, metadataPath)} return &metadataService{MetadataService: metadata.NewDatasource(root, apiVersion, userdataUrl, metadataPath)}
} }
func (ms *metadataService) FetchMetadata() ([]byte, error) { func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err error) {
data, err := ms.FetchData(ms.MetadataUrl()) var data []byte
if err != nil || len(data) == 0 { var m Metadata
return []byte{}, err
if data, err = ms.FetchData(ms.MetadataUrl()); err != nil || len(data) == 0 {
return
} }
var metadata Metadata if err = json.Unmarshal(data, &m); err != nil {
if err := json.Unmarshal(data, &metadata); err != nil { return
return []byte{}, err
} }
ms.interfaces = metadata.Interfaces if len(m.Interfaces.Public) > 0 {
ms.dns = metadata.DNS if m.Interfaces.Public[0].IPv4 != nil {
metadata.PublicIPv4 = net.ParseIP(m.Interfaces.Public[0].IPv4.IPAddress)
}
if m.Interfaces.Public[0].IPv6 != nil {
metadata.PublicIPv6 = net.ParseIP(m.Interfaces.Public[0].IPv6.IPAddress)
}
}
if len(m.Interfaces.Private) > 0 {
if m.Interfaces.Private[0].IPv4 != nil {
metadata.PrivateIPv4 = net.ParseIP(m.Interfaces.Private[0].IPv4.IPAddress)
}
if m.Interfaces.Private[0].IPv6 != nil {
metadata.PrivateIPv6 = net.ParseIP(m.Interfaces.Private[0].IPv6.IPAddress)
}
}
metadata.Hostname = m.Hostname
metadata.SSHPublicKeys = map[string]string{}
for i, key := range m.PublicKeys {
metadata.SSHPublicKeys[strconv.Itoa(i)] = key
}
metadata.NetworkConfig = data
attrs := make(map[string]interface{}) return
if len(metadata.Interfaces.Public) > 0 || len(metadata.Interfaces.Private) > 0 {
if len(metadata.Interfaces.Public) > 0 {
if metadata.Interfaces.Public[0].IPv4 != nil {
attrs["public-ipv4"] = metadata.Interfaces.Public[0].IPv4.IPAddress
}
if metadata.Interfaces.Public[0].IPv6 != nil {
attrs["public-ipv6"] = metadata.Interfaces.Public[0].IPv6.IPAddress
}
}
if len(metadata.Interfaces.Private) > 0 {
if metadata.Interfaces.Private[0].IPv4 != nil {
attrs["local-ipv4"] = metadata.Interfaces.Private[0].IPv4.IPAddress
}
if metadata.Interfaces.Private[0].IPv6 != nil {
attrs["local-ipv6"] = metadata.Interfaces.Private[0].IPv6.IPAddress
}
}
}
attrs["hostname"] = metadata.Hostname
keys := make(map[string]string)
for name, key := range metadata.PublicKeys {
keys[name] = key
}
attrs["public_keys"] = keys
return json.Marshal(attrs)
}
func (ms metadataService) FetchNetworkConfig(filename string) ([]byte, error) {
return json.Marshal(Metadata{
Interfaces: ms.interfaces,
DNS: ms.dns,
})
} }
func (ms metadataService) Type() string { func (ms metadataService) Type() string {