datasource: replace metadata map with struct

The loosely-typed metadata map is a load of crap. Make it a struct and
let the compiler help us out.
This commit is contained in:
Alex Crawford
2015-01-22 17:35:39 -08:00
parent d4c617fc23
commit 3e47c09b41
17 changed files with 248 additions and 276 deletions

View File

@@ -15,13 +15,14 @@
package waagent
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net"
"os"
"path"
"github.com/coreos/coreos-cloudinit/datasource"
)
type waagent struct {
@@ -46,13 +47,13 @@ func (a *waagent) ConfigRoot() string {
return a.root
}
func (a *waagent) FetchMetadata() ([]byte, error) {
metadataBytes, err := a.tryReadFile(path.Join(a.root, "SharedConfig.xml"))
if err != nil {
return nil, err
func (a *waagent) FetchMetadata() (metadata datasource.Metadata, err error) {
var metadataBytes []byte
if metadataBytes, err = a.tryReadFile(path.Join(a.root, "SharedConfig.xml")); err != nil {
return
}
if len(metadataBytes) == 0 {
return metadataBytes, nil
return
}
type Instance struct {
@@ -74,30 +75,28 @@ func (a *waagent) FetchMetadata() ([]byte, error) {
}
}
var metadata SharedConfig
if err := xml.Unmarshal(metadataBytes, &metadata); err != nil {
return nil, err
var m SharedConfig
if err = xml.Unmarshal(metadataBytes, &m); err != nil {
return
}
var instance Instance
for _, i := range metadata.Instances.Instances {
if i.Id == metadata.Incarnation.Instance {
for _, i := range m.Instances.Instances {
if i.Id == m.Incarnation.Instance {
instance = i
break
}
}
attrs := map[string]string{
"local-ipv4": instance.Address,
}
metadata.PrivateIPv4 = net.ParseIP(instance.Address)
for _, e := range instance.InputEndpoints.Endpoints {
host, _, err := net.SplitHostPort(e.LoadBalancedPublicAddress)
if err == nil {
attrs["public-ipv4"] = host
metadata.PublicIPv4 = net.ParseIP(host)
break
}
}
return json.Marshal(attrs)
return
}
func (a *waagent) FetchUserdata() ([]byte, error) {