cloudinit/datasource/metadata_service.go

39 lines
628 B
Go
Raw Normal View History

2014-03-18 20:00:41 +04:00
package datasource
2014-03-05 04:36:05 +04:00
import (
"io/ioutil"
"net/http"
)
type metadataService struct {
2014-03-05 05:06:52 +04:00
url string
2014-03-05 04:36:05 +04:00
client http.Client
}
2014-03-05 05:06:52 +04:00
func NewMetadataService(url string) *metadataService {
return &metadataService{url, http.Client{}}
2014-03-05 04:36:05 +04:00
}
2014-03-18 20:00:41 +04:00
func (ms *metadataService) Fetch() ([]byte, error) {
2014-03-05 05:06:52 +04:00
resp, err := ms.client.Get(ms.url)
2014-03-05 04:36:05 +04:00
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
if resp.StatusCode / 100 != 2 {
return []byte{}, nil
}
2014-03-05 04:36:05 +04:00
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respBytes, nil
}
2014-03-18 20:00:41 +04:00
func (ms *metadataService) Type() string {
return "metadata-service"
}