Skip to content

Commit 160cd33

Browse files
author
sajit
committed
refactoring
Signed-off-by: Sajit Kunnumkal <[email protected]>
1 parent 7f86326 commit 160cd33

File tree

8 files changed

+759
-30
lines changed

8 files changed

+759
-30
lines changed

pkg/common/go.mod

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
module github.com/opencost/opencost-plugins/common
22

3-
go 1.22.2
3+
go 1.22.7
4+
5+
toolchain go1.22.11
6+
7+
require github.com/opencost/opencost/core v0.0.0-20250117200701-47c4b6817505
8+
9+
require (
10+
github.com/fsnotify/fsnotify v1.6.0 // indirect
11+
github.com/golang/protobuf v1.5.3 // indirect
12+
github.com/hashicorp/hcl v1.0.0 // indirect
13+
github.com/magiconair/properties v1.8.5 // indirect
14+
github.com/mitchellh/mapstructure v1.5.0 // indirect
15+
github.com/pelletier/go-toml v1.9.3 // indirect
16+
github.com/rs/zerolog v1.26.1 // indirect
17+
github.com/spf13/afero v1.6.0 // indirect
18+
github.com/spf13/cast v1.3.1 // indirect
19+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
20+
github.com/spf13/pflag v1.0.5 // indirect
21+
github.com/spf13/viper v1.8.1 // indirect
22+
github.com/subosito/gotenv v1.2.0 // indirect
23+
golang.org/x/net v0.23.0 // indirect
24+
golang.org/x/sys v0.18.0 // indirect
25+
golang.org/x/text v0.14.0 // indirect
26+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect
27+
google.golang.org/grpc v1.62.0 // indirect
28+
google.golang.org/protobuf v1.33.0 // indirect
29+
gopkg.in/ini.v1 v1.67.0 // indirect
30+
gopkg.in/yaml.v2 v2.4.0 // indirect
31+
)

pkg/common/go.sum

Lines changed: 612 additions & 0 deletions
Large diffs are not rendered by default.

pkg/common/request/request_helpers.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package request
2+
3+
import (
4+
"time"
5+
6+
"github.com/opencost/opencost/core/pkg/log"
7+
"github.com/opencost/opencost/core/pkg/model/pb"
8+
)
9+
10+
func ValidateRequest(req *pb.CustomCostRequest) []string {
11+
var errors []string
12+
now := time.Now()
13+
// 1. Check if resolution is less than a day
14+
if req.Resolution.AsDuration() < 24*time.Hour {
15+
var resolutionMessage = "Resolution should be at least one day."
16+
log.Warnf(resolutionMessage)
17+
errors = append(errors, resolutionMessage)
18+
}
19+
// Get the start of the current month
20+
currentMonthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
21+
22+
// 2. Check if start time is before the start of the current month
23+
if req.Start.AsTime().Before(currentMonthStart) {
24+
var startDateMessage = "Start date cannot be before the current month. Historical costs not currently supported"
25+
log.Warnf(startDateMessage)
26+
errors = append(errors, startDateMessage)
27+
}
28+
29+
// 3. Check if end time is before the start of the current month
30+
if req.End.AsTime().Before(currentMonthStart) {
31+
var endDateMessage = "End date cannot be before the current month. Historical costs not currently supported"
32+
log.Warnf(endDateMessage)
33+
errors = append(errors, endDateMessage)
34+
}
35+
36+
return errors
37+
}

pkg/plugins/mongodb-atlas/cmd/main/main.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/go-plugin"
1111
"github.com/icholy/digest"
1212
commonconfig "github.com/opencost/opencost-plugins/common/config"
13+
commonrequest "github.com/opencost/opencost-plugins/common/request"
1314
atlasconfig "github.com/opencost/opencost-plugins/pkg/plugins/mongodb-atlas/config"
1415
atlasplugin "github.com/opencost/opencost-plugins/pkg/plugins/mongodb-atlas/plugin"
1516
"github.com/opencost/opencost/core/pkg/log"
@@ -89,38 +90,10 @@ type HTTPClient interface {
8990
Do(req *http.Request) (*http.Response, error)
9091
}
9192

92-
func validateRequest(req *pb.CustomCostRequest) []string {
93-
var errors []string
94-
now := time.Now()
95-
// 1. Check if resolution is less than a day
96-
if req.Resolution.AsDuration() < 24*time.Hour {
97-
var resolutionMessage = "Resolution should be at least one day."
98-
log.Warnf(resolutionMessage)
99-
errors = append(errors, resolutionMessage)
100-
}
101-
// Get the start of the current month
102-
currentMonthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
103-
104-
// 2. Check if start time is before the start of the current month
105-
if req.Start.AsTime().Before(currentMonthStart) {
106-
var startDateMessage = "Start date cannot be before the current month. Historical costs not currently supported"
107-
log.Warnf(startDateMessage)
108-
errors = append(errors, startDateMessage)
109-
}
110-
111-
// 3. Check if end time is before the start of the current month
112-
if req.End.AsTime().Before(currentMonthStart) {
113-
var endDateMessage = "End date cannot be before the current month. Historical costs not currently supported"
114-
log.Warnf(endDateMessage)
115-
errors = append(errors, endDateMessage)
116-
}
117-
118-
return errors
119-
}
12093
func (a *AtlasCostSource) GetCustomCosts(req *pb.CustomCostRequest) []*pb.CustomCostResponse {
12194
results := []*pb.CustomCostResponse{}
12295

123-
requestErrors := validateRequest(req)
96+
requestErrors := commonrequest.ValidateRequest(req)
12497
if len(requestErrors) > 0 {
12598
//return empty response
12699
return results

pkg/plugins/snowflake/cmd/main/main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,27 @@ import (
55
"fmt"
66

77
commonconfig "github.com/opencost/opencost-plugins/common/config"
8+
commonrequest "github.com/opencost/opencost-plugins/common/request"
89
snowflakeconfig "github.com/opencost/opencost-plugins/pkg/plugins/snowflake/config"
10+
snowflakeplugin "github.com/opencost/opencost-plugins/pkg/plugins/snowflake/plugin"
911
"github.com/opencost/opencost/core/pkg/log"
12+
"github.com/opencost/opencost/core/pkg/model/pb"
13+
"github.com/opencost/opencost/core/pkg/opencost"
1014
)
1115

16+
type SnowFlakeClient struct {
17+
}
18+
type SnowflakeCostSource struct {
19+
snowflakeClient SnowFlakeClient
20+
}
21+
22+
// GetInvoices fetches invoices from Snowflake
23+
func GetInvoices(snowflakeClient SnowFlakeClient) ([]snowflakeplugin.LineItem, error) {
24+
// Implement the logic to fetch pending invoices from Snowflake
25+
// This is a placeholder implementation
26+
return [], nil
27+
}
28+
1229
func main() {
1330
//TODO get this information from the config file
1431
// Snowflake connection details
@@ -78,3 +95,41 @@ func main() {
7895
log.Fatal(err)
7996
}
8097
}
98+
func (s *SnowflakeCostSource) GetCustomCosts(req *pb.CustomCostRequest) []*pb.CustomCostResponse {
99+
results := []*pb.CustomCostResponse{}
100+
101+
requestErrors := commonrequest.ValidateRequest(req)
102+
if len(requestErrors) > 0 {
103+
//return empty response
104+
return results
105+
}
106+
107+
targets, err := opencost.GetWindows(req.Start.AsTime(), req.End.AsTime(), req.Resolution.AsDuration())
108+
if err != nil {
109+
log.Errorf("error getting windows: %v", err)
110+
errResp := pb.CustomCostResponse{
111+
Errors: []string{fmt.Sprintf("error getting windows: %v", err)},
112+
}
113+
results = append(results, &errResp)
114+
return results
115+
}
116+
117+
lineItems, err := GetInvoices(s.snowflakeClient)
118+
119+
if err != nil {
120+
log.Errorf("Error fetching invoices: %v", err)
121+
errResp := pb.CustomCostResponse{
122+
Errors: []string{fmt.Sprintf("error fetching invoices: %v", err)},
123+
}
124+
results = append(results, &errResp)
125+
return results
126+
127+
}
128+
//TODO convert target to CustomCostResponse
129+
for _, target := range targets {
130+
131+
}
132+
133+
return results
134+
135+
}

pkg/plugins/snowflake/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212

1313
require (
1414
github.com/fsnotify/fsnotify v1.6.0 // indirect
15+
github.com/golang/protobuf v1.5.3 // indirect
1516
github.com/hashicorp/hcl v1.0.0 // indirect
1617
github.com/kr/pretty v0.3.1 // indirect
1718
github.com/magiconair/properties v1.8.5 // indirect
@@ -25,8 +26,12 @@ require (
2526
github.com/spf13/viper v1.8.1 // indirect
2627
github.com/stretchr/testify v1.9.0 // indirect
2728
github.com/subosito/gotenv v1.2.0 // indirect
29+
golang.org/x/net v0.21.0 // indirect
2830
golang.org/x/sys v0.24.0 // indirect
2931
golang.org/x/text v0.17.0 // indirect
32+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect
33+
google.golang.org/grpc v1.62.0 // indirect
34+
google.golang.org/protobuf v1.32.0 // indirect
3035
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
3136
gopkg.in/ini.v1 v1.67.0 // indirect
3237
gopkg.in/yaml.v2 v2.4.0 // indirect

pkg/plugins/snowflake/go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
105105
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
106106
github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM=
107107
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
108+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
109+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
108110
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
109111
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
110112
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
@@ -118,6 +120,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
118120
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
119121
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
120122
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
123+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
124+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
121125
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
122126
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
123127
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
@@ -339,6 +343,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
339343
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
340344
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
341345
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
346+
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
347+
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
342348
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
343349
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
344350
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -548,6 +554,8 @@ google.golang.org/genproto v0.0.0-20210310155132-4ce2db91004e/go.mod h1:FWY/as6D
548554
google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
549555
google.golang.org/genproto v0.0.0-20210402141018-6c239bbf2bb1/go.mod h1:9lPAdzaEmUacj36I+k7YKbEc5CXzPIeORRgDAUOu28A=
550556
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=
557+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c h1:NUsgEN92SQQqzfA+YtqYNqYmB3DMMYLlIwUZAQFVFbo=
558+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
551559
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
552560
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
553561
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
@@ -568,6 +576,8 @@ google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAG
568576
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
569577
google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
570578
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
579+
google.golang.org/grpc v1.62.0 h1:HQKZ/fa1bXkX1oFOvSjmZEUL8wLSaZTjCcLAlmZRtdk=
580+
google.golang.org/grpc v1.62.0/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
571581
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
572582
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
573583
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
@@ -580,6 +590,8 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
580590
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
581591
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
582592
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
593+
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
594+
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
583595
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
584596
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
585597
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package plugin
2+
3+
type LineItem struct {
4+
WarehouseName string `json:"warehouseName"`
5+
CreditUsed float32 `json:"creditUsed"`
6+
Date string `json:"date"`
7+
}

0 commit comments

Comments
 (0)