Skip to content

Commit

Permalink
intial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Winkler authored and Scott Winkler committed Dec 21, 2020
0 parents commit b69bf17
Show file tree
Hide file tree
Showing 13 changed files with 949 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Go
on: [push, pull_request]
jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.12
uses: actions/setup-go@v1
with:
go-version: 1.12
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Test
run: make test

- name: Build
run: make build
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
*.dll
*.exe
.DS_Store
example.tf
terraform.tfplan
terraform.tfstate
bin/
modules-dev/
pkg/
vendor/*/
website/.vagrant
website/build
website/node_modules
.vagrant/
*.backup
./*.tfstate
.terraform/
*.log
*.bak
*~
.*.swp
.idea
src/
.vscode
.vscode/
terraform-provider-petstore
dist/*
.terraform.lock.hcl
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.DEFAULT_GOAL := build
OS := $(shell go env GOOS)
ARCH := $(shell go env GOARCH)
PLUGIN_PATH := ${HOME}/.terraform.d/plugins/${OS}_${ARCH}
PLUGIN_NAME := terraform-provider-petstore
DIST_PATH := dist/${OS}_${ARCH}
GO_PACKAGES := $(shell go list ./... | grep -v /vendor/)
GO_FILES := $(shell find . -type f -name '*.go')


.PHONY: all
all: clean test build

.PHONY: test
test: test-all

.PHONY: test-all
test-all:
@TF_ACC=1 go test -v -race $(GO_PACKAGES)

${DIST_PATH}/${PLUGIN_NAME}: ${GO_FILES}
mkdir -p $(DIST_PATH); \
go build -o $(DIST_PATH)/${PLUGIN_NAME}

.PHONY: build
build: ${DIST_PATH}/${PLUGIN_NAME}

.PHONY: install
install: build
mkdir -p $(PLUGIN_PATH); \
rm -rf $(PLUGIN_PATH)/${PLUGIN_NAME}; \
install -m 0755 $(DIST_PATH)/${PLUGIN_NAME} $(PLUGIN_PATH)/${PLUGIN_NAME}

.PHONY: clean
clean:
rm -rf ${DIST_PATH}/*

.PHONY: update
update:
go get
18 changes: 18 additions & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
petstore = {
source = "hashicorp/petstore"
version = "~> 1.0"
}
}
}

provider "petstore" {
address = "https://xfc42dh782.execute-api.us-west-2.amazonaws.com/v1"
}

resource "petstore_pet" "my_pet" {
name = "princess"
species = "cat"
age = 3
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/terraform-in-action/terraform-provider-petstore

go 1.14

require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.0
github.com/terraform-in-action/go-petstore v0.1.1
)
532 changes: 532 additions & 0 deletions go.sum

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/terraform-in-action/terraform-provider-petstore/petstore"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: petstore.Provider})
}
47 changes: 47 additions & 0 deletions petstore/datasource_ps_pet_ids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package petstore

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
sdk "github.com/terraform-in-action/go-petstore"
)

func dataSourcePSPets() *schema.Resource {
return &schema.Resource{
Read: resourcePSPetRead,

Schema: map[string]*schema.Schema{
"names": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
},
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourcePSPetsRead(d *schema.ResourceData, meta interface{}) error {
names := make(map[string]bool)
for _, name := range d.Get("names").([]interface{}) {
names[name.(string)] = true
}

conn := meta.(*sdk.Client)
petList, err := conn.Pets.List(sdk.PetListOptions{})
if err != nil {
return err
}

var ids []string
for _, pet := range petList.Items {
if names["*"] || names[pet.Name] {
ids = append(ids, pet.ID)
}
}
d.Set("ids", ids)
return nil
}
36 changes: 36 additions & 0 deletions petstore/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package petstore

import (
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
sdk "github.com/terraform-in-action/go-petstore"
)

// Provider returns a *schema.Provider.
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PETSTORE_ADDRESS", nil),
},
},

ResourcesMap: map[string]*schema.Resource{
"petstore_pet": resourcePSPet(),
},

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
hostname, _ := d.Get("address").(string)
address, _ := url.Parse(hostname)
cfg := &sdk.Config{
Address: address.String(),
}
return sdk.NewClient(cfg)
}
40 changes: 40 additions & 0 deletions petstore/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package petstore

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

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

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

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

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

func testAccPreCheck(t *testing.T) {
if diags := Provider().Configure(context.Background(), &terraform.ResourceConfig{}); diags.HasError() {
for _, d := range diags {
if d.Severity == diag.Error {
t.Fatalf("err: %s", d.Summary)
}
}
}
}
95 changes: 95 additions & 0 deletions petstore/resource_ps_pet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package petstore

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
sdk "github.com/terraform-in-action/go-petstore"
)

func resourcePSPet() *schema.Resource {
return &schema.Resource{
Create: resourcePSPetCreate,
Read: resourcePSPetRead,
Update: resourcePSPetUpdate,
Delete: resourcePSPetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"species": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"age": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(int)
if v < 0 {
errs = append(errs, fmt.Errorf("%q must be greater than 0, got: %d", key, v))
}
return
},
},
},
}
}

func resourcePSPetCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*sdk.Client)
options := sdk.PetCreateOptions{
Name: d.Get("name").(string),
Species: d.Get("species").(string),
Age: d.Get("age").(int),
}

pet, err := conn.Pets.Create(options)
if err != nil {
return err
}

d.SetId(pet.ID)
return resourcePSPetRead(d, meta)
}

func resourcePSPetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*sdk.Client)
pet, err := conn.Pets.Read(d.Id())
if err != nil {
return err
}
d.Set("name", pet.Name)
d.Set("species", pet.Species)
d.Set("age", pet.Age)
return nil
}

func resourcePSPetUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*sdk.Client)
options := sdk.PetUpdateOptions{}
if d.HasChange("name") {
options.Name = d.Get("name").(string)
}
if d.HasChange("age") {
options.Age = d.Get("age").(int)
}
conn.Pets.Update(d.Id(), options)
return resourcePSPetRead(d, meta)
}

func resourcePSPetDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*sdk.Client)
err := conn.Pets.Delete(d.Id())
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit b69bf17

Please sign in to comment.