Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
moredure committed Sep 26, 2019
1 parent 88c31e5 commit d1ad649
Show file tree
Hide file tree
Showing 7 changed files with 637 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.terraform/
terraform.*
terraform-provider-cloudamqp
.idea/
*.iml
vendor/
crash.log
graph.png
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/scalablespace/terraform-provider-scalablespace

go 1.13

require (
github.com/hashicorp/terraform v0.12.9
github.com/scalablespace/goss v0.0.1
)
483 changes: 483 additions & 0 deletions go.sum

Large diffs are not rendered by default.

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

import (
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return Provider()
},
})
}
13 changes: 13 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
provider "scalablespace" {
apikey = "24870c2a-f8b2-4240-a41b-db98e2d76417"
}

resource "scalablespace_instance" "redis"{
name = "myredis"
plan = "Free"
dc = "do-fra-1"
}

output "myredis" {
value = "${scalablespace_instance.redis.port}"
}
33 changes: 33 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

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

func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"apikey": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("SCALABLESPACE_APIKEY", nil),
Description: "Key used to authentication to the CloudAMQP Customer API",
},
"baseurl": {
Type: schema.TypeString,
Default: "https://api.scalablespace.net",
Optional: true,
Description: "Base URL to CloudAMQP Customer website",
},
},
ResourcesMap: map[string]*schema.Resource{
"scalablespace_instance": resourceInstance(),
},
ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return goss.New(d.Get("baseurl").(string), d.Get("apikey").(string)), nil
}
78 changes: 78 additions & 0 deletions resource_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

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

func resourceInstance() *schema.Resource {
return &schema.Resource{
Create: resourceCreate,
Read: resourceRead,
Update: func(data *schema.ResourceData, i interface{}) error {
return nil
},
Delete: resourceDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the instance",
},
"plan": {
Type: schema.TypeString,
Required: true,
Description: "Name of the plan, valid options are: lemur, tiger, bunny, rabbit, panda, ape, hippo, lion",
},
"dc": {
Type: schema.TypeString,
Required: true,
Description: "Name of the region you want to create your instance in",
},
"port": {
Type: schema.TypeInt,
Computed: true,
Sensitive: true,
Description: "API key for the CloudAMQP instance",
},
},
}
}

func resourceCreate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*goss.API)
keys := []string{"name", "plan", "dc"}
params := make(map[string]interface{})
for _, k := range keys {
if v := d.Get(k); v != nil {
params[k] = v
}
}
data, err := api.CreateInstance(params)
if err != nil {
return err
}
d.SetId(data["id"].(string))
for k, v := range data {
d.Set(k, v)
}
return nil

}

func resourceRead(d *schema.ResourceData, meta interface{}) error {
api := meta.(*goss.API)
data, err := api.ReadInstance(d.Id())
if err != nil {
return err
}
for k, v := range data {
d.Set(k, v)
}
return nil
}

func resourceDelete(d *schema.ResourceData, meta interface{}) error {
api := meta.(*goss.API)
return api.DeleteInstance(d.Id())
}

0 comments on commit d1ad649

Please sign in to comment.