Exemplo n.º 1
0
func (s *MetadataService) Metadata(c request.Client) (metadata.Interface, error) {
	/*
	 *if _, ok := s.SupportedVersions[v]; ok != true {
	 *    return nil, fmt.Errorf("metadata: version %v for %v is not supported", v, s.Name)
	 *}
	 */

	var m *Metadata

	responce, err := request.Get(instanceMetadataURL.WithVersion("v1"), request.Header("Metadata-Flavor", "Google"))
	if err != nil {
		return nil, err
	}

	err = response.JSON(&m.Instance)
	if err != nil {
		return nil, err
	}

	pu := projectMetadataURL.WithVersion(v)
	responce, err := request.Get(projectMetadataURL.WithVersion("v1"), request.Header("Metadata-Flavor", "Google"))
	if err != nil {
		return nil, err
	}

	err = response.JSON(&m.Project)
	if err != nil {
		return nil, err
	}

	return m, nil
}
Exemplo n.º 2
0
func (s *MetadataService) fetchMetadata() (*Metadata, error) {
	// currently only supported metadata version is "v1"
	version := "v1"
	/*
	 *if _, ok := s.supportedversions[v]; ok != true {
	 *    return nil, fmt.errorf("metadata: version %v for %v is not supported", v, s.name)
	 *}
	 */

	var m Metadata

	instanceurl := s.URL.Fill(version, "instance")
	response, err := request.Get(instanceurl, request.Header("Metadata-Flavor", "Google"))
	if err != nil {
		return nil, err
	}

	err = response.JSON(&m.Instance)
	if err != nil {
		return nil, err
	}

	projecturl := s.URL.Fill(version, "project")
	response, err = request.Get(projecturl, request.Header("Metadata-Flavor", "Google"))
	if err != nil {
		return nil, err
	}

	err = response.JSON(&m.Project)
	return &m, err
}
Exemplo n.º 3
0
func TestRESTfulClient(t *testing.T) {
	Convey("Given a URL and a REST client", t, func() {
		localServer := &http.Server{
			Handler: mockHandler{},
		}
		localServer.ListenAndServe()

		url := localServer.Addr

		fmt.Printf("url %\n", url)
		//client := rest.DefaultClient
		Convey("When the client requests the contents", func() {
			response, err := request.Get(url)
			So(err, ShouldBeNil)

			Convey("Then the response should be the raw contents of the response", func() {
				var json interface{}
				err := response.JSON(&json)
				So(err, ShouldBeNil)

				So(json.isJSON, ShouldBeTrue)
			})
		})
	})
}
Exemplo n.º 4
0
func (s *MetadataService) FetchMetadata() (*metadata.Digest, error) {
	version := LatestSupportedMetadataVersion

	fetchMetadataAttribute := func(attr string) (string, error) {
		url := s.URL.Fill(version, "meta-data", attr)

		response, err := request.Get(url)
		if err != nil {
			return "", err
		}

		buf, err := response.Text()
		if err != nil {
			return "", err
		}

		return string(buf), nil
	}

	attributes := map[string]string{
		"hostname":                  "",
		"local-ipv4":                "",
		"public-ipv4":               "",
		"public-keys/0/openssh-key": "",
	}

	for attr := range attributes {
		value, err := fetchMetadataAttribute(attr)
		if err != nil {
			return nil, err
		}

		attributes[attr] = value
	}

	metadata := Metadata{
		Hostname:   attributes["hostname"],
		LocalIPv4:  net.ParseIP(attributes["local-ipv4"]),
		PublicIPv4: net.ParseIP(attributes["public-ipv4"]),
		PublicKeys: map[string]string{
			"openssh-key": attributes["public-keys/0/openssh-key"],
		},
	}

	digest := metadata.Digest()

	return &digest, nil
}
Exemplo n.º 5
0
// FetchMetadata retrieves meta_data.json from OpenStack Metadata
// service and parses it.
func (s *MetadataService) FetchUserdata() (userdata.Map, error) {
	url := s.URL.Fill(LatestSupportedMetadataVersion, "user_data")
	response, err := request.Get(url)
	if err != nil {
		return nil, err
	}

	buf, err := response.Text()
	if err != nil {
		return nil, err
	}

	u := userdata.Map{
		"user-data": string(buf),
	}

	return u, nil
}
Exemplo n.º 6
0
// FetchMetadata retrieves meta_data.json from OpenStack Metadata
// service and parses it.
func (s *MetadataService) FetchMetadata() (*metadata.Digest, error) {
	var m Metadata

	url := s.URL.Fill(LatestSupportedMetadataVersion, "meta_data.json")
	response, err := request.Get(url)
	if err != nil {
		return nil, err
	}

	err = response.JSON(&m)
	if err != nil {
		return nil, err
	}

	digest := m.Digest()

	return &digest, nil
}
Exemplo n.º 7
0
func (s *MetadataService) FetchUserdata() (userdata.Map, error) {
	// TODO(tmrts): Make sure if /user-data/ and /user-data paths are both valid.
	url := s.URL.Fill(LatestSupportedMetadataVersion, "user-data", "")

	response, err := request.Get(url[:len(url)-1])
	if err != nil {
		return nil, err
	}

	buf, err := response.Text()
	if err != nil {
		return nil, err
	}

	return userdata.Map{
		"user-data": string(buf),
	}, nil
}
Exemplo n.º 8
0
func TestRESTfulClient(t *testing.T) {
	Convey("Given a URL and a REST client", t, func() {
		server := testutil.NewMockServer(func(w http.ResponseWriter, r *http.Request) {
			buf, _ := ioutil.ReadFile("test_data/some.json")
			w.Write(buf)
		})

		Convey("When the client requests the contents", func() {
			response, err := request.Get(server.URL)
			So(err, ShouldBeNil)

			Convey("Then the response should be the raw contents of the response", func() {
				var data map[string]interface{}

				err := response.JSON(&data)
				So(err, ShouldBeNil)

				So(data["isJSON"], ShouldBeTrue)
			})
		})
	})
}