Skip to content

Commit

Permalink
Enabled Costs to be retrieved by just subscription and or resource (#11)
Browse files Browse the repository at this point in the history
* Used queryText to get information by resource id

* Added onBlur Handler

* Added New Line
  • Loading branch information
MartinBelton-gov authored Jan 11, 2024
1 parent 92ee1e0 commit 2634f6c
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 41 deletions.
2 changes: 1 addition & 1 deletion dfe-azurecostbackend-datasource/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "1",
"name": "azurecost-backend",
"version": "1.0.2",
"version": "1.0.3",
"description": "Azure cost backend",
"scripts": {
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
Expand Down
23 changes: 16 additions & 7 deletions dfe-azurecostbackend-datasource/pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataReques
return response, nil
}

type queryModel struct{}
type queryModel struct {
QueryText string `json:"queryText"`
Constant float64 `json:"constant"`
}

func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse {
var response backend.DataResponse
Expand All @@ -214,6 +217,7 @@ func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query
//MAB Added
log.Print("Starting Datasource")
log.Println("URL:", d.config.AzureCostSubscriptionUrl)
log.Println("ResourceId:", qm.QueryText)

// Call the fetchToken function
token, err := fetchToken(d.config)
Expand All @@ -228,12 +232,12 @@ func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query
start, end := getCurrentYearDates()

timeRange := query.TimeRange
if !timeRange.From.IsZero() && !timeRange.To.IsZero(){
if !timeRange.From.IsZero() && !timeRange.To.IsZero() {
start = timeRange.From.Format("2006-01-02")
end = timeRange.To.Format("2006-01-02")
end = timeRange.To.Format("2006-01-02")
}

costs, err := getCosts(token, d.config, start, end)
costs, err := getCosts(token, d.config, start, end, qm.QueryText)
if err != nil {
log.Println("Error getting costs:", err)
return response
Expand Down Expand Up @@ -373,9 +377,15 @@ func parseAccessToken(body []byte) (string, error) {
}

// Fetch Costs
func getCosts(token string, config Config, start string, end string) (CostResponse, error) {
func getCosts(token string, config Config, start string, end string, resourceid string) (CostResponse, error) {
url := config.SubscriptionID + "/providers/Microsoft.CostManagement/query?api-version=2023-03-01"

if len(resourceid) > 2 {
url = config.SubscriptionID + "/resourceGroups/" + resourceid + "/providers/Microsoft.CostManagement/query?api-version=2023-03-01"
}

log.Println("CostUrl:", url)

bodyParameters := map[string]interface{}{
"type": "Usage",
"timeframe": "Custom",
Expand Down Expand Up @@ -495,5 +505,4 @@ func getCurrentYearDates() (string, string) {
thirtyFirstOfDecemberFormatted := thirtyFirstOfDecember.Format("2006-01-02")

return firstOfJanuaryFormatted, thirtyFirstOfDecemberFormatted
}

}
10 changes: 5 additions & 5 deletions dfe-azurecostbackend-datasource/pkg/plugin/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"encoding/json"
Expand Down Expand Up @@ -145,7 +146,7 @@ func TestGetCosts(t *testing.T) {
token := "your-mock-token"
config.TokenURL = server.URL
start, end := getCurrentYearDates()
costs, err := getCosts(token, config, start, end)
costs, err := getCosts(token, config, start, end, "resourceid")
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
Expand Down Expand Up @@ -204,8 +205,8 @@ func TestGetCurrentYearDates(t *testing.T) {
firstOfJanuary, thirtyFirstOfDecember := getCurrentYearDates()

// Expected results for the mock date
expectedFirstOfJanuary := "2023-01-01"
expectedThirtyFirstOfDecember := "2023-12-31"
expectedFirstOfJanuary := strconv.Itoa(time.Now().Year()) + "-01-01"
expectedThirtyFirstOfDecember := strconv.Itoa(time.Now().Year()) + "-12-31"

// Check if the actual results match the expected results
if firstOfJanuary != expectedFirstOfJanuary {
Expand All @@ -215,5 +216,4 @@ func TestGetCurrentYearDates(t *testing.T) {
if thirtyFirstOfDecember != expectedThirtyFirstOfDecember {
t.Errorf("Thirty-First of December: expected %s, got %s", expectedThirtyFirstOfDecember, thirtyFirstOfDecember)
}
}

}
40 changes: 36 additions & 4 deletions dfe-azurecostbackend-datasource/src/components/ConfigEditor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2634f6c

Please sign in to comment.