Ejemplo n.º 1
0
func authToken() (err error) {
	if time.Now().After(TokenExp) {
		username := conf.Openstack["username"].Str
		password := conf.Openstack["password"].Str
		tentant := conf.Openstack["tenant_name"].Str
		url := conf.Openstack["auth_url"].Str + "/tokens"
		header := &map[string]string{"Content-Type": "application/json"}
		data := bytes.NewBufferString("{\"auth\":{\"passwordCredentials\":{\"username\": \"" + username + "\", \"password\":\"" + password + "\"}, \"tenantName\":\"" + tentant + "\"}}")
		if res, err := client.Post(url, header, data); err != nil {
			return err
		} else {
			r := tokenRes{}
			body, _ := ioutil.ReadAll(res.Body)
			json.Unmarshal(body, &r)
			Token = r.Access.Token.ID
			TokenExp, _ = time.Parse(time.RFC3339, r.Access.Token.Expires)
		}
	}
	return nil
}
Ejemplo n.º 2
0
func Boot(username, name string) (s *statusRes, err error) {
	authToken()
	url := conf.Openstack["nova_api"].Str + "/" + conf.Openstack["tenant_id"].Str + "/servers"
	flavor := conf.Openstack["flavor"].Str
	image := conf.Openstack["image"].Str
	userData, _ := BootScript(username)
	data := bytes.NewBufferString("{\"server\":{\"flavorRef\":\"" + flavor + "\", \"imageRef\":\"" + image + "\", \"name\":\"" + name + "\", \"user_data\":\"" + userData + "\"}}")
	header := &map[string]string{
		"Content-Type":   "application/json",
		"Content-Length": strconv.Itoa(data.Len()),
		"X-Auth-Token":   Token,
	}
	res, err := client.Post(url, header, data)
	if err != nil {
		return nil, err
	}
	r := bootRes{}
	body, _ := ioutil.ReadAll(res.Body)
	err = json.Unmarshal(body, &r)
	if err != nil {
		return nil, err
	}
	return Status(r.Server.ID)
}