Skip to content

Commit

Permalink
provider/fastly: Add Fastly Provider, ServiceV1 resource
Browse files Browse the repository at this point in the history
  • Loading branch information
catsby committed Mar 23, 2016
1 parent 2cc8ade commit 2ad37bb
Show file tree
Hide file tree
Showing 12 changed files with 1,374 additions and 0 deletions.
12 changes: 12 additions & 0 deletions builtin/bins/provider-fastly/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/hashicorp/terraform/builtin/providers/fastly"
"github.com/hashicorp/terraform/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: fastly.Provider,
})
}
1 change: 1 addition & 0 deletions builtin/bins/provider-fastly/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
31 changes: 31 additions & 0 deletions builtin/providers/fastly/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fastly

import (
"fmt"

gofastly "github.com/sethvargo/go-fastly"
)

type Config struct {
ApiKey string
}

type FastlyClient struct {
conn *gofastly.Client
}

func (c *Config) Client() (interface{}, error) {
var client FastlyClient

if c.ApiKey == "" {
return nil, fmt.Errorf("[Err] No API key for Fastly")
}

fconn, err := gofastly.NewClient(c.ApiKey)
if err != nil {
return nil, err
}

client.conn = fconn
return &client, nil
}
34 changes: 34 additions & 0 deletions builtin/providers/fastly/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fastly

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"api_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"FASTLY_API_KEY",
}, nil),
Description: "Fastly API Key from https://app.fastly.com/#account",
},
},
ResourcesMap: map[string]*schema.Resource{
"fastly_service_v1": resourceServiceV1(),
},

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
ApiKey: d.Get("api_key").(string),
}
return config.Client()
}
35 changes: 35 additions & 0 deletions builtin/providers/fastly/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fastly

import (
"os"
"testing"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"fastly": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("FASTLY_API_KEY"); v == "" {
t.Fatal("FASTLY_API_KEY must be set for acceptance tests")
}
}
Loading

0 comments on commit 2ad37bb

Please sign in to comment.