-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecsResource.go
86 lines (71 loc) · 1.81 KB
/
ecsResource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package data
import (
"fmt"
"strings"
"time"
"github.com/dta4/ot3c/config"
"github.com/dta4/ot3c/enterprise"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack"
"github.com/huaweicloud/golangsdk/openstack/compute/v2/servers"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
var ecslog *logrus.Entry = logrus.WithField("module", "ecs_data")
//ECSResource ECSResource
type ECSResource struct {
ID string
consumption []enterprise.Consumption
OTCServer servers.Server
PredCost []CostItem
Tags map[string]string
}
func (c ECSResource) GetID() string {
return c.ID
}
func (c ECSResource) GetDescription() string {
bytes, _ := yaml.Marshal(c)
return string(bytes)
}
func (c ECSResource) GetCostItems() []CostItem {
var cost []CostItem
for _, con := range c.consumption {
time, _ := time.Parse("2006-01-02", strings.Split(con.ConsumptionDate, " ")[0])
cost = append(cost, CostItem{
Time: time,
Cost: con.ListpriceAmount,
})
}
return cost
}
func (c ECSResource) GetPredCostItems() []CostItem {
return c.PredCost
}
func (c ECSResource) GetTags() map[string]string {
return c.Tags
}
func (c ECSResource) Terminate(client *golangsdk.ProviderClient) error {
cs, err := openstack.NewComputeV2(client, golangsdk.EndpointOpts{
Region: "eu-de",
})
if config.Dryrun {
fmt.Printf("MOCK TERMINATE: %v\n", c.ID)
return nil
}
r := servers.Delete(cs, c.OTCServer.ID)
if r.Err != nil {
return err
}
evslog.WithField("time", time.Now()).Infof("Terminated %v\n", c.ID)
return nil
//WIP
fmt.Printf("MOCK TERMINATE: %v\n", c.ID)
return nil
}
//SetPastCostItems build past cost items
func (c *ECSResource) SetPastCostItems(cost []enterprise.Consumption) {
c.consumption = cost
}
func (c *ECSResource) SetPredCostItems(cost []CostItem) {
c.PredCost = cost
}