Skip to content

Commit

Permalink
Merged in TER-233-project-structure (pull request #2)
Browse files Browse the repository at this point in the history
TER-233 provider configuration

Approved-by: Patrick Decat <[email protected]>
Approved-by: Jordan Caussat <[email protected]>
Approved-by: Jérôme Respaut <[email protected]>
  • Loading branch information
Jordan Caussat authored and Shr3ps committed Mar 21, 2018
2 parents e5a830f + 7597c98 commit 4c6d612
Show file tree
Hide file tree
Showing 1,405 changed files with 434,466 additions and 193 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ignored/
/build/terraform*
bin/**
**/*.tfvars
3 changes: 0 additions & 3 deletions Dockerfile

This file was deleted.

41 changes: 41 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
SOURCEDIR=.
SOURCES := $(shell find $(SOURCEDIR) -name '*.go' | grep -v vendor)
TEST?=./...
BIN_FOLDER=bin/
BINARY=$(BIN_FOLDER)terraform-provider-ghost
VERSION=1.0.0
BUILD_TIME=`date +%FT%T%z`

default: $(BINARY)

$(BINARY): $(SOURCES)
go build ${LDFLAGS} -o ${BINARY} main.go

install: fmt
go install

fmt:
gofmt -w $(SOURCES)

test:
go test $(TEST) -timeout=30s -parallel=4

testacc:
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m

vet:
@echo "go vet ."
@go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \
echo ""; \
echo "Vet found suspicious constructs. Please check the reported constructs"; \
echo "and fix them if necessary before submitting the code for review."; \
exit 1; \
fi

vendor-status:
@govendor status

clean:
$(RM) ${BINARY}

.PHONY: install fmt test testacc vet vendor-status clean
102 changes: 65 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,72 @@
# Terraform provider that manages Ghost apps #
Terraform Provider that manages Ghost apps
==========================================

TF example:
Requirements
------------

- [Terraform](https://www.terraform.io/downloads.html) 0.10.x
- [Go](https://golang.org/doc/install) 1.9 (to build the provider plugin)

Bulding The Provider
--------------------
Clone repository to: `$GOPATH/src/cloud-deploy.io/terraform-provider-ghost`

Using Go:
```sh
$ go get -d cloud-deploy.io/terraform-provider-ghost
```

Using git:
```sh
$ mkdir -p $GOPATH/src/cloud-deploy.io; cd $GOPATH/src/cloud-deploy.io
$ git clone [email protected]:morea/terraform-provider-ghost.git
```

Enter the repository and build the provider
```sh
$ cd $GOPATH/src/cloud-deploy.io/terraform-provider-ghost
make
```

Using the provider
----------------------
If you're building the provider, follow the instructions to [install it as a plugin.](https://www.terraform.io/docs/plugins/basics.html#installing-a-plugin) After placing it into your plugins directory, run `terraform init` to initialize it.

An example is available in the examples directory.

Developing the Provider
---------------------------

If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.9+ is *required*). You'll also need to correctly setup a [GOPATH](http://golang.org/doc/code.html#GOPATH), as well as adding `$GOPATH/bin` to your `$PATH`.

To compile the provider, run `make install`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.

In order to test the provider, you can simply run `make test`.

In order to run the full suite of Acceptance tests, run `make testacc`.

*Note:* Acceptance tests create real resources. For the tests to pass, you'll need to setup a ghost instance (can be local), and set the following environment variables:

```sh
$ export GHOST_USER=myuser
$ export GHOST_PASSWORD=mypwd
$ export GHOST_ENDPOINT=http://localhost

$ make testacc
```

TF example:
-----------
```
provider "ghost" {
user = "admin"
password = "mypass"
endpoint = "https://demo.ghost.morea.fr"
}
resource "ghost_app" "wordpress" {
resource "ghost_app" "test" {
name = "wordpress"
env = "prod"
env = "dev"
role = "webfront"
region = "eu-west-1"
Expand Down Expand Up @@ -41,48 +96,21 @@ resource "ghost_app" "wordpress" {
name = ""
}
module = {
modules = [{
name = "symfony2"
pre_deploy = "ZXhpdCAx"
path = "/var/www"
scope = "code"
git_repo = "https://github.com/KnpLabs/KnpIpsum.git"
}
}]
feature = {
features = [{
version = "5.4"
name = "php5"
}
feature = {
},
{
version = "2.2"
name = "apache2"
}
}
```

# Building and testing in a Dockerized environment #

The dockerized environment can be invoked through docker-compose. It mounts an external volume named _terraform-provider-ghost-root_user_ in which you can personalize the root session.

Hint: in the _terraform-provider-ghost-root_user_ volume, you can import your personal SSH keys and add a .gitconfig file with a content like this:
```
[url "[email protected]:"]
insteadOf = https://bitbucket.org/
```
To allow go to load git dependencies from private repositories using your SSH key.

To use the environment, just run the command:
`docker-compose run --rm [test|build]-env` in the root directory of this project.

# Running terraform in a Dockerized environment #

To run the provider after building, just run the command:
`docker-compose run --rm terraform [plan|<terraform command>]` in the root directory of this project

the _sources/build_ directory is mounted directly in the container and should be linked by a _.terraformrc_ file in the _terraform-provider-ghost-root_user_ volume, example:
```
providers {
ghost = "/terraform-providers/ghost/terraform-provider-ghost"
}]
}
```
14 changes: 14 additions & 0 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
image: golang:1.10

pipelines:
default:
- step:
script:
- PACKAGE_PATH="${GOPATH}/src/cloud-deploy.io/${BITBUCKET_REPO_SLUG}"
- mkdir -pv "${PACKAGE_PATH}"
- tar -cO --exclude-vcs --exclude=bitbucket-pipelines.yml . | tar -xv -C "${PACKAGE_PATH}"
- cd "${PACKAGE_PATH}"
- go get github.com/kardianos/govendor
- make test
- make vendor-status
- make vet
34 changes: 0 additions & 34 deletions docker-compose.yml

This file was deleted.

1 change: 1 addition & 0 deletions examples/inputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
variable "password" {}
55 changes: 55 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
provider "ghost" {
user = "demo"
password = "${var.password}"
endpoint = "https://demo.ghost.morea.fr"
}

resource "ghost_app" "test" {
name = "wordpress"
env = "dev"
role = "webfront"

region = "eu-west-1"
instance_type = "t2.micro"
vpc_id = "vpc-3f1eb65a"

log_notifications = [
"[email protected]",
]

build_infos = {
subnet_id = "subnet-a7e849fe"
ssh_username = "admin"
source_ami = "ami-03ce4474"
}

environment_infos = {
instance_profile = "iam.ec2.demo"
key_name = "ghost-demo"
root_block_device = {}
optional_volumes = []
subnet_ids = ["subnet-a7e849fe"]
security_groups = ["sg-6814f60c"]
}

autoscale = {
name = ""
}

modules = [{
name = "symfony2"
pre_deploy = "ZXhpdCAx"
path = "/var/www"
scope = "code"
git_repo = "https://github.com/KnpLabs/KnpIpsum.git"
}]

features = [{
version = "5.4"
name = "php5"
},
{
version = "2.2"
name = "apache2"
}]
}
34 changes: 34 additions & 0 deletions ghost/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ghost

import (
"fmt"
"log"
"net/url"

"cloud-deploy.io/go-st"
)

// Config defines the configuration options for the Ghost client
type Config struct {
User string
Password string
URL string
}

// Client returns a new Ghost client
func (c *Config) Client() (*ghost.Client, error) {
if c.Password == "" || c.User == "" || c.URL == "" {
return nil, fmt.Errorf(`At least 1 ghost parameter is empty: Username: %s,
Password, URL: %s`, c.User, c.URL)
}

if _, err := url.ParseRequestURI(c.URL); err != nil {
return nil, fmt.Errorf("Invalid endpoint URL")
}

client := ghost.NewClient(c.URL, c.User, c.Password)

log.Printf("[INFO] Ghost client configured: %s %s", c.User, c.URL)

return client, nil
}
44 changes: 44 additions & 0 deletions ghost/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ghost

import (
"testing"
)

// Test config with empty parameters
func TestConfigEmptyParameters(t *testing.T) {
config := Config{
User: "",
Password: "",
URL: "",
}

if _, err := config.Client(); err == nil {
t.Fatalf("expected error, but got nil")
}
}

// Test config with invalid url
func TestConfigInvalidUrl(t *testing.T) {
config := Config{
User: "myuser",
Password: "mypwd",
URL: "invalid.url",
}

if _, err := config.Client(); err == nil {
t.Fatalf("expected error, but got nil")
}
}

// Test config with valid parameters
func TestConfigValidParameters(t *testing.T) {
config := Config{
User: "myuser",
Password: "mypwd",
URL: "https://www.valid.url",
}

if _, err := config.Client(); err != nil {
t.Fatalf("expected no error, but got %s", err)
}
}
7 changes: 4 additions & 3 deletions sources/provider/provider.go → ghost/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func Provider() terraform.ResourceProvider {

func providerConfigure(data *schema.ResourceData) (interface{}, error) {
config := Config{
User: data.Get("user").(string),
Password: data.Get("password").(string),
URL: data.Get("endpoint").(string),
User: data.Get("user").(string),
Password: data.Get("password").(string),
URL: data.Get("endpoint").(string),
}
log.Println("[INFO] Initializing Ghost client")

return config.Client()
}
Loading

0 comments on commit 4c6d612

Please sign in to comment.