Skip to content
Draft
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
19 changes: 19 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -euo pipefail

if ! command -v go >/dev/null 2>&1; then
echo "go not found; skipping build/validate." >&2
exit 0
fi

# Build provider
go build -o terraform.coderforge.org/coderforge/coderforge || exit 1

# Terraform optional
if command -v terraform >/dev/null 2>&1; then
export TF_CLI_CONFIG_FILE="$(git rev-parse --show-toplevel)/.terraformrc"
(cd examples/resources/function && terraform init -input=false -upgrade && terraform validate)
(cd examples/resources/container_registry && terraform init -input=false -upgrade && terraform validate)
else
echo "terraform not found; skipping terraform validate." >&2
fi
7 changes: 7 additions & 0 deletions .terraformrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
provider_installation {
dev_overrides {
"registry.terraform.io/coderforge/coderforge" = "/workspace"
}
direct {}
}

13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ build-only:
go mod download
go build -o terraform-provider-coderforge_$(VERSION)

.PHONY: build-dev
build-dev:
go build -o registry.terraform.io/coderforge/coderforge

.PHONY: validate-examples
validate-examples: build-dev
cd examples/resources/function && TF_CLI_CONFIG_FILE=../../..//.terraformrc terraform init -input=false -upgrade && TF_CLI_CONFIG_FILE=../../..//.terraformrc terraform validate
cd examples/resources/container_registry && TF_CLI_CONFIG_FILE=../../..//.terraformrc terraform init -input=false -upgrade && TF_CLI_CONFIG_FILE=../../..//.terraformrc terraform validate

.PHONY: hooks-install
hooks-install:
git config core.hooksPath .githooks

.PHONY: doc-preview
doc-preview:
@echo "Preview your markdown documentation on this page: https://registry.terraform.io/tools/doc-preview"
26 changes: 26 additions & 0 deletions examples/resources/container_registry/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
terraform {
required_providers {
coderforge = {
source = "registry.terraform.io/coderforge/coderforge"
}
}
}

provider "coderforge" {
stack_id = "stack-helloworld-dev"
cloud_space = "helloworld.dev.coderforge.org"
locations = ["gbr-1", "gbr-2", "ita-1"]
}

resource "coderforge_container_registry" "example" {
name = "demo-container"
runtime = "docker"
image_uri = "docker.coderforge.org/demo:latest"
timeout = 120
max_ram_size = "512MB"
}

output "container_registry" {
value = coderforge_container_registry.example
}

3 changes: 1 addition & 2 deletions examples/resources/function/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
terraform {
required_providers {
coderforge = {
source = "coderforge/coderforge"
version = "0.1.2"
source = "registry.terraform.io/coderforge/coderforge"
}
}
}
Expand Down
43 changes: 28 additions & 15 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package provider

import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"time"
)

const HostURL string = "https://api.coderforge.org"
const defaultHostURL string = "https://api.coderforge.org"

var ErrNotFound = errors.New("not_found")

type Client struct {
StackId string
Expand All @@ -19,10 +22,14 @@ type Client struct {
Locations []string
}

func NewClient(token *string, cloudSpace *string, locations *[]string, stackId *string) (*Client, error) {
func NewClient(token *string, cloudSpace *string, locations *[]string, stackId *string, hostURL *string) (*Client, error) {
host := defaultHostURL
if hostURL != nil && *hostURL != "" {
host = *hostURL
}
c := Client{
StackId: *stackId,
HostURL: HostURL,
HostURL: host,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
Token: *token,
CloudSpace: *cloudSpace,
Expand All @@ -31,29 +38,35 @@ func NewClient(token *string, cloudSpace *string, locations *[]string, stackId *
return &c, nil
}

func (c *Client) doRequest(req *http.Request) ([]byte, error) {
// req.Header.Set("Authorization", "Bearer "+c.Token)
func (c *Client) doRequest(ctx context.Context, req *http.Request) ([]byte, error) {
if ctx == nil {
ctx = context.Background()
}
req = req.WithContext(ctx)
if c.Token != "" {
req.Header.Set("Authorization", "Bearer "+c.Token)
}
req.Header.Set("X-CoderForge.org-Context", "{\"userId\": \"u00001\"}")
req.Header.Set("Content-Type", "application/json")
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(res.Body)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
if res.StatusCode == http.StatusNotFound {
return nil, ErrNotFound
}
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
}

return body, err
}
Loading