Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-Order] Setup order implementation #36

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ terraform-provider-dominos

# VSCode
.vscode

# Randon
_*.*
data
NOTES.md
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ build: *.go

run:
clear
rm -rf .terraform.lock.hcl
rm -rf .terraform.lock.hcl terraform.tfstat*
make build
terraform init -plugin-dir .terraform.d/plugins/
TF_LOG=INFO terraform plan
TF_LOG=INFO terraform apply -auto-approve
rm -rf .terraform.lock.hcl terraform.tfstat*

watch:
while true; do \
inotifywait -e modify,create,delete -r internal/provider/*.go && make run; \
inotifywait -e modify,create,delete -r internal/provider/*.go && make run; \
done

localSetup:
Expand Down
7 changes: 0 additions & 7 deletions examples/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ provider "dominos" {
last_name = "Name"
email_address = "my@name.com"
phone_number = "15555555555"

credit_card = {
number = 123456789101112
cvv = 1314
date = "15/16"
postal_code = "18192"
}
}

data "dominos_address" "addr" {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.18
require (
github.com/hashicorp/terraform-plugin-docs v0.10.1
github.com/hashicorp/terraform-plugin-framework v0.11.1
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/mitchellh/mapstructure v1.5.0
)

require (
Expand All @@ -29,7 +31,6 @@ require (
github.com/hashicorp/terraform-exec v0.16.1 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
Expand Down
39 changes: 2 additions & 37 deletions go.sum

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions internal/provider/data_source_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -113,15 +112,16 @@ func (d dataSourceAddress) Read(ctx context.Context, req datasource.ReadRequest,
}
url_json, err := json.Marshal(urlobj)
if err != nil {
log.Fatalf("Cannot unmarshall urlobj")

resp.Diagnostics.AddError("Cannot unmarshall urlobj", fmt.Sprintf("%s", err))
return
}

data.URLObject = types.String{Value: string(url_json)}

api_json, err := json.Marshal(apiobj)
if err != nil {
log.Fatalf("Cannot unmarshall apiobj")
resp.Diagnostics.AddError("Cannot unmarshall apiobj", fmt.Sprintf("%s", err))
return
}

data.APIObject = types.String{Value: string(api_json)}
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/data_source_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -97,9 +96,10 @@ func (d dataSourceMenu) Read(ctx context.Context, req datasource.ReadRequest, re

var client = &http.Client{Timeout: 10 * time.Second}

menuitems, err := getAllMenuItems(fmt.Sprintf("https://order.dominos.com/power/store/%d/menu?lang=en&structured=true", data.StoreID.Value), client)
menuitems, err := getAllMenuItems(fmt.Sprintf("https://order.dominos.ca/power/store/%d/menu?lang=en&structured=true", data.StoreID.Value), client)
if err != nil {
log.Fatalf("Cannot get all menu items: %v", err)
resp.Diagnostics.AddError("Cannot get all menu items", fmt.Sprintf("%s", err))
return
}

for i := range menuitems {
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/data_source_menu_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -105,9 +104,10 @@ func (d dataSourceMenuItem) Read(ctx context.Context, req datasource.ReadRequest
}

var client = &http.Client{Timeout: 10 * time.Second}
menuitems, err := getAllMenuItems(fmt.Sprintf("https://order.dominos.com/power/store/%d/menu?lang=en&structured=true", data.StoreID.Value), client)
menuitems, err := getAllMenuItems(fmt.Sprintf("https://order.dominos.ca/power/store/%d/menu?lang=en&structured=true", data.StoreID.Value), client)
if err != nil {
log.Fatalf("Cannot get all menu items: %v", err)
resp.Diagnostics.AddError("Cannot get all menu items", fmt.Sprintf("%s", err))
return
}

queries := data.QueryString
Expand Down
186 changes: 186 additions & 0 deletions internal/provider/data_source_pizza.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package provider

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mitchellh/mapstructure"
)

// Ensure provider defined types fully satisfy framework interfaces
var _ provider.DataSourceType = dataSourcePizzaType{}
var _ datasource.DataSource = dataSourcePizza{}

type dataSourcePizzaType struct{}

var itemOptionsAttributes = tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
"portion": {
Description: "Choose:",
Optional: true,
Type: types.StringType,
},
"weight": {
Description: "Choose:",
Optional: true,
Type: types.StringType,
},
})

func (t dataSourcePizzaType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Description: `
Provided a Dominos address, this data source returns the store_id of the closest Dominos store, and, in case it's useful to you somehow, the delivery_minutes, an integer showing the estimated minutes until your pizza will be delivered.
`,
Attributes: map[string]tfsdk.Attribute{
"size": {
Description: "",
Type: types.StringType,
Required: true,
},
"crust": {
Description: "",
Type: types.StringType,
Required: true,
},
"options": {
Description: "",
Optional: true,
Attributes: tfsdk.SingleNestedAttributes(pizzaOptionsAttributes()),
},
"pizza_json": {
Description: "The json for the pizza Product.",
Type: types.StringType,
Computed: true,
},
"quantity": {
Description: "",
Type: types.NumberType,
Optional: true,
},
},
}, nil
}

func (t dataSourcePizzaType) NewDataSource(ctx context.Context, in provider.Provider) (datasource.DataSource, diag.Diagnostics) {
provider, diags := convertProviderType(in)

return dataSourcePizza{
provider: provider,
}, diags
}

type dataSourcePizzaData struct {
Size types.String `tfsdk:"size"`
Crust types.String `tfsdk:"crust"`
Options types.Object `tfsdk:"options"`
PizzaJson types.String `tfsdk:"pizza_json"`
Quantity types.Number `tfsdk:"quantity"`
}

type dataSourcePizza struct {
provider dominosProvider
}

func (d dataSourcePizza) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data dataSourcePizzaData
var pizzaJson Product

diags := req.Config.Get(ctx, &data)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

// Pizza Code
// TODO: Validate crust is offered
pizzaJson.Code = data.Size.Value + data.Crust.Value
if data.Quantity.Null {
pizzaJson.Qty = 1
} else {
qty, _ := data.Quantity.Value.Int64()
pizzaJson.Qty = int(qty)
}

if !data.Options.IsNull() {
var tmp1 TFPizzaOption
mapTest := make(map[string]Option)

for optionName, optionVal := range data.Options.Attrs {
pizzaOptionMap := make(map[string]string)
var key string
var weight string

optionValStr := strings.ReplaceAll(optionVal.String(), "<null>", "null")

if optionVal.IsNull() {
continue
}

err := json.Unmarshal([]byte(optionValStr), &tmp1)
if err != nil {
resp.Diagnostics.AddError("Cannot unmarshall Stuff", fmt.Sprintf("%s", err))
return
}

if tmp1.Portion != nil {
switch *tmp1.Portion {
case "left":
key = "left"
case "all":
key = "all"
case "right":
key = "right"
default:
resp.Diagnostics.AddError("Portion not valid:", fmt.Sprintf("%s", *tmp1.Weight))
return
}
} else {
key = "all"
}

if tmp1.Weight != nil {
switch *tmp1.Weight {
case "light":
weight = "0.5"
case "normal":
weight = "1"
case "extra":
weight = "1.5"
default:
resp.Diagnostics.AddError("Weight not valid:", fmt.Sprintf("%s", *tmp1.Weight))
return
}
} else {
weight = "1"
}

pizzaOptionMap[key] = weight
var pizzaOption Option
mapstructure.Decode(pizzaOptionMap, &pizzaOption)

mapTest[optionName] = pizzaOption
}

mapstructure.Decode(mapTest, &pizzaJson.Options)
}

out, _ := json.Marshal(pizzaJson)
output := string(out)

tflog.Info(ctx, string(out))

data.PizzaJson = types.String{Value: output}

diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
12 changes: 7 additions & 5 deletions internal/provider/data_source_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -81,16 +80,19 @@ func (d dataSourceStore) Read(ctx context.Context, req datasource.ReadRequest, r
address_url_obj := make(map[string]string)
err := json.Unmarshal([]byte(data.AddressURLObj.Value), &address_url_obj)
if err != nil {
log.Fatalf("Cannot unmarshall address_url_obj")
resp.Diagnostics.AddError("Cannot unmarshall address_url_obj", fmt.Sprintf("%s", err))
return
}
line1 := url.QueryEscape(address_url_obj["line1"])
line2 := url.QueryEscape(address_url_obj["line2"])
stores, err := getStores(fmt.Sprintf("https://order.dominos.com/power/store-locator?s=%s&c=%s&s=Delivery", line1, line2), client)
stores, err := getStores(fmt.Sprintf("https://order.dominos.ca/power/store-locator?s=%s&c=%s&s=Delivery", line1, line2), client)
if err != nil {
log.Fatalf("Cannot get stores: %v", err)
resp.Diagnostics.AddError("Cannot get stores", fmt.Sprintf("%s", err))
return
}
if len(stores) == 0 {
log.Fatalf("No stores near the address %#v", address_url_obj)
resp.Diagnostics.AddError("No stores near the address", fmt.Sprintf("%s", err))
return
}
storeID, _ := strconv.ParseInt(stores[0].StoreID, 10, 64)
data.StoreID = types.Int64{Value: storeID}
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/data_source_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"

Expand Down Expand Up @@ -71,9 +70,10 @@ func (d dataSourceTracking) Read(ctx context.Context, req datasource.ReadRequest

var client = &http.Client{Timeout: 10 * time.Second}

_, err := getTrackingApiObject(fmt.Sprintf("https://trkweb.dominos.com/orderstorage/GetTrackerData?StoreID=%d&OrderKey=%d", data.StoreID.Value, data.OrderID.Value), client)
_, err := getTrackingApiObject(fmt.Sprintf("https://trkweb.dominos.ca/orderstorage/GetTrackerData?StoreID=%d&OrderKey=%d", data.StoreID.Value, data.OrderID.Value), client)
if err != nil {
log.Fatalf("Cannot get tracking api object: %v", err)
resp.Diagnostics.AddError("Cannot get tracking api object", fmt.Sprintf("%s", err))
return
}

diags = resp.State.Set(ctx, &data)
Expand Down
Loading