Skip to content

Commit

Permalink
made customer_context optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dianibar committed Sep 25, 2024
1 parent 69dc4a8 commit 4c52381
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ terraform {
required_providers {
doit = {
source = "doitintl/doit"
version = "0.18.0"
version = "0.21.0"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
doit = {
source = "doitintl/doit"
version = "0.18.0"
version = "0.21.0"
}
}
}
Expand Down Expand Up @@ -47,7 +47,7 @@ resource "doit_attribution" "attri" {
name = "attritestnewname"
description = "attritestdesc"
formula = "A"
components = [{ type = "label", key = "iris_region", values = ["us-central1"] }]
components = [{ type = "system_label", key = "aws/region_code", values = ["us-east-1"] }]
}

resource "doit_attribution_group" "attributeGroup" {
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
doit = {
source = "doitintl/doit"
version = "0.18.0"
version = "0.21.0"
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions internal/provider/attribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ func (c *ClientTest) CreateAttribution(attribution Attribution) (*Attribution, e
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/analytics/v1/attributions?customerContext=%s", c.HostURL, c.Auth.CustomerContext), strings.NewReader(string(rb)))
urlRequestBase := fmt.Sprintf("%s/analytics/v1/attributions", c.HostURL)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("POST", urlRequestContext, strings.NewReader(string(rb)))
log.Println("URL----------------")
log.Println(req.URL)
if err != nil {
Expand Down Expand Up @@ -47,7 +48,9 @@ func (c *ClientTest) UpdateAttribution(attributionID string, attribution Attribu
if err != nil {
return nil, err
}
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/analytics/v1/attributions/%s?customerContext=%s", c.HostURL, attributionID, c.Auth.CustomerContext), strings.NewReader(string(rb)))
urlRequestBase := fmt.Sprintf("%s/analytics/v1/attributions/%s", c.HostURL, attributionID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("PATCH", urlRequestContext, strings.NewReader(string(rb)))
if err != nil {
return nil, err
}
Expand All @@ -69,7 +72,10 @@ func (c *ClientTest) UpdateAttribution(attributionID string, attribution Attribu
}

func (c *ClientTest) DeleteAttribution(attributionID string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/analytics/v1/attributions/%s?customerContext=%s", c.HostURL, attributionID, c.Auth.CustomerContext), nil)
urlRequestBase := fmt.Sprintf("%s/analytics/v1/attributions/%s", c.HostURL, attributionID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)

req, err := http.NewRequest("DELETE", urlRequestContext, nil)
if err != nil {
return err
}
Expand All @@ -84,7 +90,9 @@ func (c *ClientTest) DeleteAttribution(attributionID string) error {

// GetAttribution - Returns a specifc attribution
func (c *ClientTest) GetAttribution(orderID string) (*Attribution, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/analytics/v1/attributions/%s?customerContext=%s", c.HostURL, orderID, c.Auth.CustomerContext), nil)
urlRequestBase := fmt.Sprintf("%s/analytics/v1/attributions/%s", c.HostURL, orderID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("GET", urlRequestContext, nil)
if err != nil {
return nil, err
}
Expand Down
17 changes: 13 additions & 4 deletions internal/provider/attribution_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func (c *ClientTest) CreateAttributionGroup(attributionGroup AttributionGroup) (
}
log.Println(strings.NewReader(string(rb)))

req, err := http.NewRequest("POST", fmt.Sprintf("%s/analytics/v1/attributiongroups?customerContext=%s", c.HostURL, c.Auth.CustomerContext), strings.NewReader(string(rb)))
urlRequestBase := fmt.Sprintf("%s/analytics/v1/attributiongroups", c.HostURL)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)

req, err := http.NewRequest("POST", urlRequestContext, strings.NewReader(string(rb)))
log.Println("URL:")
log.Println(req.URL)
if err != nil {
Expand Down Expand Up @@ -48,7 +51,9 @@ func (c *ClientTest) UpdateAttributionGroup(attributionGroupID string, attributi
if err != nil {
return nil, err
}
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/analytics/v1/attributiongroups/%s?customerContext=%s", c.HostURL, attributionGroupID, c.Auth.CustomerContext), strings.NewReader(string(rb)))
urlRequestBase := fmt.Sprintf("%s/analytics/v1/attributiongroups/%s", c.HostURL, attributionGroupID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("PATCH", urlRequestContext, strings.NewReader(string(rb)))
if err != nil {
return nil, err
}
Expand All @@ -65,7 +70,9 @@ func (c *ClientTest) UpdateAttributionGroup(attributionGroupID string, attributi
}

func (c *ClientTest) DeleteAttributionGroup(attributionGroupID string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/analytics/v1/attributiongroups/%s?customerContext=%s", c.HostURL, attributionGroupID, c.Auth.CustomerContext), nil)
urlRequestBase := fmt.Sprintf("%s/analytics/v1/attributiongroups/%s", c.HostURL, attributionGroupID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("DELETE", urlRequestContext, nil)
if err != nil {
return err
}
Expand All @@ -80,7 +87,9 @@ func (c *ClientTest) DeleteAttributionGroup(attributionGroupID string) error {

// GetAttributionGroup - Returns a specifc attribution
func (c *ClientTest) GetAttributionGroup(attributionGroupID string) (*AttributionGroup, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/analytics/v1/attributiongroups/%s?customerContext=%s", c.HostURL, attributionGroupID, c.Auth.CustomerContext), nil)
urlRequestBase := fmt.Sprintf("%s/analytics/v1/attributiongroups/%s", c.HostURL, attributionGroupID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("GET", urlRequestContext, nil)
if err != nil {
return nil, err
}
Expand Down
17 changes: 12 additions & 5 deletions internal/provider/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ func (c *ClientTest) CreateBudget(budget Budget) (*Budget, error) {
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/analytics/v1/budgets?customerContext=%s", c.HostURL, c.Auth.CustomerContext), strings.NewReader(string(rb)))
urlRequestBase := fmt.Sprintf("%s/analytics/v1/budgets", c.HostURL)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("POST", urlRequestContext, strings.NewReader(string(rb)))
log.Println("URL----------------")
log.Println(req.URL)
if err != nil {
Expand Down Expand Up @@ -47,7 +48,9 @@ func (c *ClientTest) UpdateBudget(budgetID string, budget Budget) (*Budget, erro
if err != nil {
return nil, err
}
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/analytics/v1/budgets/%s?customerContext=%s", c.HostURL, budgetID, c.Auth.CustomerContext), strings.NewReader(string(rb)))
urlRequestBase := fmt.Sprintf("%s/analytics/v1/budgets/%s", c.HostURL, budgetID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("PATCH",urlRequestContext, strings.NewReader(string(rb)))
if err != nil {
return nil, err
}
Expand All @@ -72,7 +75,9 @@ func (c *ClientTest) UpdateBudget(budgetID string, budget Budget) (*Budget, erro
}

func (c *ClientTest) DeleteBudget(budgetID string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/analytics/v1/budgets/%s?customerContext=%s", c.HostURL, budgetID, c.Auth.CustomerContext), nil)
urlRequestBase := fmt.Sprintf("%s/analytics/v1/budgets/%s", c.HostURL, budgetID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("DELETE", urlRequestContext, nil)
if err != nil {
return err
}
Expand All @@ -87,7 +92,9 @@ func (c *ClientTest) DeleteBudget(budgetID string) error {

// GetBudget - Returns a specifc budget
func (c *ClientTest) GetBudget(orderID string) (*Budget, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/analytics/v1/budgets/%s?customerContext=%s", c.HostURL, orderID, c.Auth.CustomerContext), nil)
urlRequestBase := fmt.Sprintf("%s/analytics/v1/budgets/%s", c.HostURL, orderID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("GET", urlRequestContext, nil)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/budget_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func (r *budgetResource) Delete(ctx context.Context, req resource.DeleteRequest,
err := r.client.DeleteBudget(state.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error Deleting DoiT Attribution",
"Error Deleting DoiT Budget",
"Could not delete budge, unexpected error: "+err.Error(),
)
return
Expand Down
9 changes: 9 additions & 0 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net/http"
"strings"
"time"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -144,3 +145,11 @@ func (c *ClientTest) doRequest(req *http.Request) ([]byte, error) {

return body, err
}

func addContextToURL(context, url string) (urlContext string) {
urlContext = url
if len(strings.TrimSpace(context)) != 0 {
urlContext = fmt.Sprintf("url?customerContext=%s", context)
}
return urlContext
}
19 changes: 0 additions & 19 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,6 @@ func (p *doitProvider) Configure(ctx context.Context, req provider.ConfigureRequ
)
}

if config.CustomerContext.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("CustomerContext"),
"Unknown DoiT API CustomerContext",
"-The provider cannot create the DoiT API client as there is an unknown configuration value for the DoiT API customerContext. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the DOIT_CUSTOMER_CONTEXT environment variable.",
)
}

if config.DoiTAPITOken.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("doiTAPITOken"),
Expand Down Expand Up @@ -179,16 +170,6 @@ func (p *doitProvider) Configure(ctx context.Context, req provider.ConfigureRequ
host = HostURL
}

if customerContext == "" {
resp.Diagnostics.AddAttributeError(
path.Root("customerContext"),
"Missing DoiT Customer Context",
"The provider cannot create the DoiT API client as there is a missing or empty value for the DoiT API customer Context. "+
"Set the CustomerContext value in the configuration or use the DOIT_CUSTOMER_CONTEXT environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}

if doiTAPIToken == "" {
resp.Diagnostics.AddAttributeError(
path.Root("doiTAPIToken"),
Expand Down
17 changes: 13 additions & 4 deletions internal/provider/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func (c *ClientTest) CreateReport(report Report) (*Report, error) {
log.Print("Report body----------------")
log.Println(string(rb))

req, err := http.NewRequest("POST", fmt.Sprintf("%s/analytics/v1/reports?customerContext=%s", c.HostURL, c.Auth.CustomerContext), strings.NewReader(string(rb)))
urlRequestBase := fmt.Sprintf("%s/analytics/v1/reports", c.HostURL)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("POST", urlRequestContext, strings.NewReader(string(rb)))
log.Println("URL----------------")
log.Println(req.URL)
if err != nil {
Expand Down Expand Up @@ -53,7 +55,9 @@ func (c *ClientTest) UpdateReport(reportID string, report Report) (*Report, erro
}
log.Print("Report body----------------")
log.Println(string(rb))
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/analytics/v1/reports/%s?customerContext=%s", c.HostURL, reportID, c.Auth.CustomerContext), strings.NewReader(string(rb)))
urlRequestBase := fmt.Sprintf("%s/analytics/v1/reports/%s", c.HostURL, reportID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("PATCH", urlRequestContext, strings.NewReader(string(rb)))
if err != nil {
return nil, err
}
Expand All @@ -75,7 +79,9 @@ func (c *ClientTest) UpdateReport(reportID string, report Report) (*Report, erro
}

func (c *ClientTest) DeleteReport(reportID string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/analytics/v1/reports/%s?customerContext=%s", c.HostURL, reportID, c.Auth.CustomerContext), nil)
urlRequestBase := fmt.Sprintf("%s/analytics/v1/reports/%s", c.HostURL, reportID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("DELETE", urlRequestContext, nil)
if err != nil {
return err
}
Expand All @@ -91,7 +97,10 @@ func (c *ClientTest) DeleteReport(reportID string) error {

// GetReport - Returns a specifc report
func (c *ClientTest) GetReport(orderID string) (*Report, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/analytics/v1/reports/%s/config?customerContext=%s", c.HostURL, orderID, c.Auth.CustomerContext), nil)

urlRequestBase := fmt.Sprintf("%s/analytics/v1/reports/%s/config", c.HostURL, orderID)
urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase)
req, err := http.NewRequest("GET", urlRequestContext, nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4c52381

Please sign in to comment.