From f067936f250bed7f38b2b6576bce93912be62a5d Mon Sep 17 00:00:00 2001 From: Bitor Tonixa Balenca Date: Tue, 19 Dec 2023 23:31:45 +0000 Subject: [PATCH] Added go backend for testing purposes --- .gitignore | 2 ++ api/backend.go | 41 +++++++++++++++++++++++++++ example/receitas.tf | 8 +++++- internal/provider/receita_resource.go | 31 +++++++++++--------- 4 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 api/backend.go diff --git a/.gitignore b/.gitignore index fe060a8..16bf837 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .terraform.lock.hcl .terraform/ *.tfstate +receita-provider +backend diff --git a/api/backend.go b/api/backend.go new file mode 100644 index 0000000..15a55ac --- /dev/null +++ b/api/backend.go @@ -0,0 +1,41 @@ +package main + +import ( + "errors" + "fmt" + "io" + "net/http" + "os" +) + +func handleCreate(w http.ResponseWriter, r *http.Request) { + fmt.Printf("got /create request\n") + io.WriteString(w, "Resource created!\n") +} + +func handleUpdate(w http.ResponseWriter, r *http.Request) { + fmt.Printf("got /update request\n") + io.WriteString(w, "Resource updated!\n") +} + +func handleDelete(w http.ResponseWriter, r *http.Request) { + fmt.Printf("got /delete request\n") + io.WriteString(w, "Resource deleted\n") +} + +func main() { + + mux := http.NewServeMux() + mux.HandleFunc("/create", handleCreate) + mux.HandleFunc("/update", handleUpdate) + mux.HandleFunc("/delete", handleDelete) + + err := http.ListenAndServe(":9999", mux) + + if errors.Is(err, http.ErrServerClosed) { + fmt.Printf("server closed\n") + } else if err != nil { + fmt.Printf("error starting server: %s\n", err) + os.Exit(1) + } +} diff --git a/example/receitas.tf b/example/receitas.tf index b69fb0c..f853fba 100644 --- a/example/receitas.tf +++ b/example/receitas.tf @@ -11,6 +11,12 @@ provider "receita" { } resource "receita_receita" "receita_one" { - name = "Bola de carne" + name = "Bola de carne" + #name = "Batata frita" author = "Maria Bacalhau" } + +resource "receita_receita" "receita_two" { + name = "Bacalhau com Todos" + author = "Antonio Mariscada" +} diff --git a/internal/provider/receita_resource.go b/internal/provider/receita_resource.go index 23e836d..34ef4ff 100644 --- a/internal/provider/receita_resource.go +++ b/internal/provider/receita_resource.go @@ -107,20 +107,11 @@ func (r *ReceitaResource) Create(ctx context.Context, req resource.CreateRequest // Do specific stuff endpoint := r.providerData.Model.Endpoint + hResp, _ := http.Get(endpoint.ValueString() + "/create") - fmt.Println("Before the call") + fmt.Println(hResp.Body) - hResp, err := http.Get(endpoint.ValueString() + "/create") - - fmt.Println("After the call") - - if err != nil { - return - } - - if hResp.Close == false { - fmt.Println("http closed") - } + //End specific stuff //log the creation of the resource in the terraform logging system tflog.Trace(ctx, "created a receita resource") @@ -157,7 +148,13 @@ func (r *ReceitaResource) Update(ctx context.Context, req resource.UpdateRequest return } - //Do specific update stuff + // Do specific stuff + endpoint := r.providerData.Model.Endpoint + hResp, _ := http.Get(endpoint.ValueString() + "/update") + + fmt.Println(hResp.Body) + + //End specific stuff //Finally update the terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -169,6 +166,14 @@ func (r *ReceitaResource) Delete(ctx context.Context, req resource.DeleteRequest // Load tf state into the model resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + // Do specific stuff + endpoint := r.providerData.Model.Endpoint + hResp, _ := http.Get(endpoint.ValueString() + "/delete") + + fmt.Println(hResp.Body) + + //End specific stuff + if resp.Diagnostics.HasError() { return }