Skip to content

Commit

Permalink
Merge pull request #8 from ewilde/ew/openfaas-sdk
Browse files Browse the repository at this point in the history
Move to official OpenFaaS proxy & client
  • Loading branch information
ewilde authored Feb 29, 2020
2 parents 7982675 + 6b76f2c commit 21a3939
Show file tree
Hide file tree
Showing 62 changed files with 3,240 additions and 269 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Full documentation, see: https://openfaas-tfe.edwardwilde.com/docs/providers/ope

```hcl
resource "openfaas_function" "function_test" {
name = "test-function"
image = "functions/alpine:latest"
f_process = "sha512sum"
labels {
name = "test-function"
image = "functions/alpine:latest"
f_process = "sha512sum"
labels = {
Group = "London"
Environment = "Test"
}
annotations {
annotations = {
CreatedDate = "Mon Sep 3 07:15:55 BST 2018"
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/authenticated/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ git clone https://github.com/openfaas/faas && \

```bash
cat <<EOF >>terraform.tfvars
provider_password = "72b97dd9abe096b91478df91b6549f2998aee27dbdde9a4bbec4182801d6c398"
openfaas_provider_password = "72b97dd9abe096b91478df91b6549f2998aee27dbdde9a4bbec4182801d6c398"
EOF
```
3. `terraform apply`
Expand Down
3 changes: 2 additions & 1 deletion examples/authenticated/database.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
resource "postgresql_database" "function_db" {
name = "function_db"
}
}

17 changes: 9 additions & 8 deletions examples/authenticated/function.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
resource "openfaas_function" "function_test" {
name = "test-function"
image = "functions/alpine:latest"
f_process = "env"
labels {
name = "test-function"
image = "functions/alpine:latest"
f_process = "env"
labels = {
Group = "London"
Environment = "Test"
}
Expand All @@ -12,11 +12,12 @@ resource "openfaas_function" "function_test" {
cpu = "100m"
}

env_vars {
database_name = "${postgresql_database.function_db.name}"
env_vars = {
database_name = postgresql_database.function_db.name
}

annotations {
annotations = {
CreatedDate = "Mon Sep 3 07:15:55 BST 2018"
}
}
}

12 changes: 6 additions & 6 deletions examples/authenticated/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
provider "openfaas" {
version = "~> 0.1"
uri = "http://localhost:8080"
user_name = "admin"
password = "${var.openfaas_provider_password}"
password = var.openfaas_provider_password
}

provider "postgresql" {
host = "localhost"
username = "${var.database_username}"
password = "${var.database_password}"
sslmode = "disable"
host = "localhost"
username = var.database_username
password = var.database_password
sslmode = "disable"
}

12 changes: 9 additions & 3 deletions examples/authenticated/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
variable "openfaas_provider_password" {}
variable "database_username" {}
variable "database_password" {}
variable "openfaas_provider_password" {
}

variable "database_username" {
}

variable "database_password" {
}

4 changes: 4 additions & 0 deletions examples/authenticated/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}
1 change: 0 additions & 1 deletion examples/unauthenticated/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
provider "openfaas" {
version = "~> 0.1"
uri = "http://localhost:8080"
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/ewilde/terraform-provider-openfaas
go 1.13

require (
github.com/drone/envsubst v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/oklog/run v1.1.0 // indirect
github.com/openfaas/faas v0.0.0-20180822191204-91b6c244941c
github.com/openfaas/faas-cli v0.0.0-20180829141537-b7ba9a60dc2c
github.com/openfaas/faas v0.0.0-20180822191204-91b6c244941c // indirect
github.com/openfaas/faas-cli v0.0.0-20200226083118-b0a70a3f4f20
github.com/openfaas/faas-provider v0.15.0
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735 // indirect
github.com/viveksyngh/faas-cli v0.0.0-20180831214523-1fe7ecc51fb5
)
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/drone/envsubst v1.0.2 h1:dpYLMAspQHW0a8dZpLRKe9jCNvIGZPhCPrycZzIHdqo=
github.com/drone/envsubst v1.0.2/go.mod h1:bkZbnc/2vh1M12Ecn7EYScpI4YGYU0etwLJICOWi8Z0=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -71,6 +73,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/hashicorp/errwrap v0.0.0-20180715044906-d6c0cd880357/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -169,8 +173,10 @@ github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/openfaas/faas v0.0.0-20180822191204-91b6c244941c h1:+MrLQNzu91zUS7vl8wFeBMkNqZgX+VuS9f01jXgJc78=
github.com/openfaas/faas v0.0.0-20180822191204-91b6c244941c/go.mod h1:E0m2rLup0Vvxg53BKxGgaYAGcZa3Xl+vvL7vSi5yQ14=
github.com/openfaas/faas-cli v0.0.0-20180829141537-b7ba9a60dc2c h1:BF+OhyexBSqsYUjZc8YX/kP87X6FGOuTIxaLwgHUIco=
github.com/openfaas/faas-cli v0.0.0-20180829141537-b7ba9a60dc2c/go.mod h1:u/KO+e43wkagC0lqM1eaqNEWEBdg08Q1ugP/idj39MM=
github.com/openfaas/faas-cli v0.0.0-20200226083118-b0a70a3f4f20 h1:v0Yuo8+U2U7vamYrZmXN0MudPdP07fOVJLJ5OXd1q6Q=
github.com/openfaas/faas-cli v0.0.0-20200226083118-b0a70a3f4f20/go.mod h1:u/KO+e43wkagC0lqM1eaqNEWEBdg08Q1ugP/idj39MM=
github.com/openfaas/faas-provider v0.15.0 h1:3x5ma90FL7AqP4NOD6f03AY24y3xBeVF6xGLUx6Xrlc=
github.com/openfaas/faas-provider v0.15.0/go.mod h1:8Fagi2UeMfL+gZAqZWSMQg86i+w1+hBOKtwKRP5sLFI=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -192,8 +198,6 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/viveksyngh/faas-cli v0.0.0-20180831214523-1fe7ecc51fb5 h1:gmJ5Lf4awVArqDtmdJ4UnG+q0yYg9xLBXLTIHIDwj1I=
github.com/viveksyngh/faas-cli v0.0.0-20180831214523-1fe7ecc51fb5/go.mod h1:hjz01B9wMGspSpFAhplBEjwCTaUkkAHVWqxfvb2Z+tU=
github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvcciqcxEHac4CYj89xI=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
Expand All @@ -204,6 +208,8 @@ github.com/zclconf/go-cty-yaml v1.0.1/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgK
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.uber.org/goleak v0.10.0 h1:G3eWbSNIskeRqtsN/1uI5B+eP73y3JUuBsv9AZjehb4=
go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo=
Expand Down
8 changes: 4 additions & 4 deletions openfaas/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package openfaas

import "github.com/openfaas/faas-cli/proxy"

type Config struct {
TLSInsecure bool
GatewayURI string
GatewayUserName string
GatewayPassword string
Client *proxy.Client
FunctionNamespace string
}
4 changes: 2 additions & 2 deletions openfaas/data_source_openfaas_function.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package openfaas

import (
"context"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/viveksyngh/faas-cli/proxy"
)

func dataSourceOpenFaaSFunction() *schema.Resource {
Expand Down Expand Up @@ -45,7 +45,7 @@ func dataSourceOpenFaaSFunctionRead(d *schema.ResourceData, meta interface{}) er
config := meta.(Config)

log.Printf("[DEBUG] Reading function Balancer: %s", name)
function, err := proxy.GetFunctionInfo(config.GatewayURI, name, config.TLSInsecure)
function, err := config.Client.GetFunctionInfo(context.Background(), name, "")
if err != nil {
return fmt.Errorf("error retrieving function: %s", err)
}
Expand Down
107 changes: 98 additions & 9 deletions openfaas/provider.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package openfaas

import (
"crypto/tls"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"log"
"net"
"net/http"
"time"

openfaas_config "github.com/openfaas/faas-cli/config"
"github.com/openfaas/faas-cli/config"
"github.com/openfaas/faas-cli/proxy"
)

var (
defaultTimeout = 60 * time.Second
)

// Provider returns a terraform.ResourceProvider.
Expand Down Expand Up @@ -54,16 +63,96 @@ func Provider() terraform.ResourceProvider {

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
log.Printf("[DEBUG] configuring provider")
config := Config{
GatewayURI: d.Get("uri").(string),
TLSInsecure: d.Get("tls_insecure").(bool),
GatewayUserName: d.Get("user_name").(string),
GatewayPassword: d.Get("password").(string),

gatewayURI := d.Get("uri").(string)
auth := newCLIAuth("", gatewayURI)
insecure := d.Get("tls_insecure").(bool)
transport := GetDefaultCLITransport(insecure, &defaultTimeout)
client := proxy.NewClient(auth, gatewayURI, transport, &defaultTimeout)

providerConfig := Config{
Client: client,
}

return providerConfig, nil
}

func newCLIAuth(token string, gateway string) proxy.ClientAuth {
authConfig, _ := config.LookupAuthConfig(gateway)

var (
username string
password string
bearerToken string
)

if authConfig.Auth == config.BasicAuthType {
username, password, _ = config.DecodeAuth(authConfig.Token)

return &BasicAuth{
username: username,
password: password,
}

}

// User specified token gets priority
if len(token) > 0 {
bearerToken = token
} else {
bearerToken = authConfig.Token
}

return &BearerToken{
token: bearerToken,
}
}

type BasicAuth struct {
username string
password string
}

if config.GatewayUserName != "" && config.GatewayPassword != "" {
openfaas_config.UpdateAuthConfig(config.GatewayURI, config.GatewayUserName, config.GatewayPassword)
func (auth *BasicAuth) Set(req *http.Request) error {
if auth.username == "" {
return nil
}

return config, nil
req.SetBasicAuth(auth.username, auth.password)
return nil
}

type BearerToken struct {
token string
}

func (c *BearerToken) Set(req *http.Request) error {
req.Header.Set("Authorization", "Bearer "+c.token)
return nil
}

func GetDefaultCLITransport(tlsInsecure bool, timeout *time.Duration) *http.Transport {
if timeout != nil || tlsInsecure {
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: false,
}

if timeout != nil {
tr.DialContext = (&net.Dialer{
Timeout: *timeout,
}).DialContext

tr.IdleConnTimeout = 120 * time.Millisecond
tr.ExpectContinueTimeout = 1500 * time.Millisecond
}

if tlsInsecure {
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: tlsInsecure}
}
tr.DisableKeepAlives = false

return tr
}
return nil
}
Loading

0 comments on commit 21a3939

Please sign in to comment.