예제 #1
0
//Temporary hack to create an assembly from its id.
//This is used by SetStatus.
//We need add a Notifier interface duck typed by Box and Carton ?
func NewBalances(id string, m map[string]string) (*Balances, error) {
	// Here skips balances fetching for the VMs which is launched on opennebula,
	// that does not have records on vertice database
	if id == "" {
		return nil, fmt.Errorf("account_id should not be empty")
	}
	args := api.NewArgs(m)
	args.Path = GETBALANCE + id
	cl := api.NewClient(args)
	response, err := cl.Get()
	if err != nil {
		return nil, err
	}
	htmlData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	ac := &ApiBalances{}
	err = json.Unmarshal(htmlData, ac)
	if err != nil {
		return nil, err
	}
	b := &ac.Results[0]
	return b, nil
}
예제 #2
0
func (bt *BillTransaction) Transact(m map[string]string) error {
	args := api.NewArgs(m)
	args.Path = NEWTRANSACTION
	cl := api.NewClient(args)
	_, err := cl.Post(bt)
	if err != nil {
		log.Debugf(err.Error())
		return err
	}
	return nil
}
예제 #3
0
func (v *VerticeApi) NotifyContainer(eva EventAction, edata EventData) error {
	if !v.satisfied(eva) {
		return nil
	}
	sdata := parseMapToOutputFormat(edata)
	v.Args.Path = EVENTCONTAINER_NEW
	cl := api.NewClient(v.Args)
	_, err := cl.Post(sdata)
	if err != nil {
		log.Debugf(err.Error())
		return err
	}
	return nil
}
예제 #4
0
func (v *VerticeApi) NotifyVm(eva EventAction, edata EventData) error {
	if !v.satisfied(eva) {
		return nil
	}
	sdata := parseMapToOutputFormat(edata)
	v.Args.Path = EVENTSVM_NEW
	v.Args.Email = edata.M[constants.ACCOUNT_ID]
	cl := api.NewClient(v.Args)
	_, err := cl.Post(sdata)
	if err != nil {
		log.Debugf(err.Error())
		return err
	}
	return nil
}
예제 #5
0
func AccountsOrg(email string, m map[string]string) (*Organization, error) {
	args := api.NewArgs(m)
	args.Path = ACCOUNTS_GET + args.Email
	cl := api.NewClient(args)
	response, err := cl.Get()
	if err != nil {
		return nil, err
	}
	htmlData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	o := &ApiOrganizations{}
	err = json.Unmarshal(htmlData, o)
	if err != nil {
		return nil, err
	}
	org := &o.Results
	return org, nil
}
예제 #6
0
func NewAccounts(m map[string]string) (*Accounts, error) {
	args := api.NewArgs(m)
	args.Path = ACCOUNTS_GET + args.Email
	cl := api.NewClient(args)
	response, err := cl.Get()
	if err != nil {
		return nil, err
	}
	htmlData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	ac := &ApiAccounts{}
	err = json.Unmarshal(htmlData, ac)
	if err != nil {
		return nil, err
	}
	a := &ac.Results
	return a, nil
}
예제 #7
0
func (b *Balances) Deduct(bopts *BalanceOpts, m map[string]string) error {
	avail, err := strconv.ParseFloat(b.Credit, 64)
	if err != nil {
		return err
	}

	consume, cerr := strconv.ParseFloat(bopts.Consumed, 64)
	if cerr != nil {
		return cerr
	}

	b.UpdatedAt = time.Now()
	b.Credit = strconv.FormatFloat(avail-consume, 'f', 2, 64)

	args := api.NewArgs(m)
	args.Path = UPDATEBALANCES
	cl := api.NewClient(args)
	_, err = cl.Post(b)
	if err != nil {
		return err
	}

	return nil
}