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

HTTP resource #255

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
59 changes: 59 additions & 0 deletions examples/http/main.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
resource "container" "httpbin" {
image {
name = "kong/httpbin:0.1.0"
}

port {
local = 80
host = 80
}

health_check {
timeout = "30s"

http {
address = "http://localhost/get"
success_codes = [200]
}
}
}

resource "http" "get" {
method = "GET"

url = "http://${resource.container.httpbin.container_name}/get"

headers = {
Accept = "application/json"
}
}

resource "http" "post" {
method = "POST"

url = "http://${resource.container.httpbin.container_name}/post"

payload = jsonencode({
foo = "bar"
})

headers = {
Accept = "application/json"
}
}

output "get_body" {
value = jsondecode(resource.http.get.body)
}

output "get_status" {
value = resource.http.get.status
}

output "post_body" {
value = jsondecode(resource.http.post.body)
}

output "post_status" {
value = resource.http.post.status
}
101 changes: 101 additions & 0 deletions pkg/config/resources/http/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package http

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"

"github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
sdk "github.com/jumppad-labs/plugin-sdk"
)

type Provider struct {
config *HTTP
log logger.Logger
client http.Client
}

func (p *Provider) Init(cfg types.Resource, l sdk.Logger) error {
c, ok := cfg.(*HTTP)
if !ok {
return fmt.Errorf("unable to initialize provider, resource is not of type HTTP")
}

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

p.client = client
p.config = c
p.log = l

return nil
}

func (p *Provider) Create(ctx context.Context) error {
p.log.Info(fmt.Sprintf("Creating %s", p.config.Metadata().Type), "ref", p.config.Metadata().ID)

// If a timeout was specified, set it
if p.config.Timeout != "" {
timeout, err := time.ParseDuration(p.config.Timeout)
if err != nil {
return err
}

p.client.Timeout = timeout
}

var payload io.Reader
if p.config.Method == "POST" {
payload = bytes.NewBuffer([]byte(p.config.Payload))
}

// create a http request
request, err := http.NewRequest(p.config.Method, p.config.URL, payload)
if err != nil {
return err
}

// add headers
for k, v := range p.config.Headers {
request.Header.Add(k, v)
}

// make the request
response, err := p.client.Do(request)
if err != nil {
return err
}

// read the response body
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

// set the outputs
p.config.Status = response.StatusCode
p.config.Body = string(body)

return nil
}

func (p *Provider) Destroy(ctx context.Context, force bool) error {
return nil
}

func (p *Provider) Lookup() ([]string, error) {
return nil, nil
}

func (p *Provider) Refresh(ctx context.Context) error {
return nil
}

func (p *Provider) Changed() (bool, error) {
return false, nil
}
38 changes: 38 additions & 0 deletions pkg/config/resources/http/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package http

import (
"github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/config"
)

const TypeHTTP string = "http"

type HTTP struct {
types.ResourceBase `hcl:",remain"`

Method string `hcl:"method" json:"method"`
URL string `hcl:"url" json:"url"`

Headers map[string]string `hcl:"headers,optional" json:"headers,omitempty"`
Payload string `hcl:"payload,optional" json:"payload,omitempty"`
Timeout string `hcl:"timeout,optional" json:"timeout,omitempty"`

// Output parameters
Status int `hcl:"status,optional" json:"status"`
Body string `hcl:"body,optional" json:"body"`
}

func (t *HTTP) Process() error {
cfg, err := config.LoadState()
if err == nil {
// try and find the resource in the state
r, _ := cfg.FindResource(t.Meta.ID)
if r != nil {
state := r.(*HTTP)
t.Status = state.Status
t.Body = state.Body
}
}

return nil
}
2 changes: 2 additions & 0 deletions pkg/jumppad/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/jumppad-labs/jumppad/pkg/config/resources/docs"
"github.com/jumppad-labs/jumppad/pkg/config/resources/exec"
"github.com/jumppad-labs/jumppad/pkg/config/resources/helm"
"github.com/jumppad-labs/jumppad/pkg/config/resources/http"
"github.com/jumppad-labs/jumppad/pkg/config/resources/ingress"
"github.com/jumppad-labs/jumppad/pkg/config/resources/k8s"
"github.com/jumppad-labs/jumppad/pkg/config/resources/network"
Expand All @@ -39,6 +40,7 @@ func init() {
config.RegisterResource(docs.TypeBook, &docs.Book{}, &null.Provider{})
config.RegisterResource(exec.TypeExec, &exec.Exec{}, &exec.Provider{})
config.RegisterResource(helm.TypeHelm, &helm.Helm{}, &helm.Provider{})
config.RegisterResource(http.TypeHTTP, &http.HTTP{}, &http.Provider{})
config.RegisterResource(ingress.TypeIngress, &ingress.Ingress{}, &ingress.Provider{})
config.RegisterResource(k8s.TypeK8sCluster, &k8s.Cluster{}, &k8s.ClusterProvider{})
config.RegisterResource(k8s.TypeK8sConfig, &k8s.Config{}, &k8s.ConfigProvider{})
Expand Down
Loading