Skip to content

Commit

Permalink
First commit for the terraform provider for civo
Browse files Browse the repository at this point in the history
- Now can create a instances

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jan 17, 2020
1 parent d9e6c92 commit e018fda
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 235 deletions.
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/civo/terraform-provider-civo

require github.com/hashicorp/terraform v0.12.0-rc1
require (
github.com/aws/aws-sdk-go v1.25.39
github.com/civo/civogo v0.0.0-20200116211328-a6d635388fa4
github.com/hashicorp/terraform-plugin-sdk v1.3.0
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d // indirect
)

go 1.13
374 changes: 154 additions & 220 deletions go.sum

Large diffs are not rendered by default.

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

import (
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
"github.com/civo/terraform-provider-civo/provider"
"github.com/hashicorp/terraform-plugin-sdk/plugin"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return Provider()
return provider.Provider()
},
})
}
11 changes: 0 additions & 11 deletions provider.go

This file was deleted.

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

import (
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"token": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("CIVO_TOKEN", ""),
},
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
},
ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
token := d.Get("token").(string)
client, _ := civogo.NewClient(token)
return client, nil
}
91 changes: 91 additions & 0 deletions provider/resource_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package provider

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"regexp"
)

func validateName(v interface{}, k string) (ws []string, es []error) {
var errs []error
var warns []string
value, ok := v.(string)
if !ok {
errs = append(errs, fmt.Errorf("Expected name to be string"))
return warns, errs
}
whiteSpace := regexp.MustCompile(`\s+`)
if whiteSpace.Match([]byte(value)) {
errs = append(errs, fmt.Errorf("name cannot contain whitespace. Got %s", value))
return warns, errs
}
return warns, errs
}

func resourceInstance() *schema.Resource {
fmt.Print()
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the resource, also acts as it's unique ID",
ForceNew: true,
ValidateFunc: validateName,
},
"description": {
Type: schema.TypeString,
Required: true,
Description: "A description of an item",
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Description: "An optional list of tags, represented as a key, value pair",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
Create: resourceInstanceCreate,
Read: resourceInstanceRead,
Update: resourceInstanceUpdate,
Delete: resourceInstanceDelete,
//Exists: resourceExistsItem,
//Importer: &schema.ResourceImporter{
// State: schema.ImportStatePassthrough,
//},
}
}

func resourceInstanceCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

config, err := apiClient.NewInstanceConfig()
if err != nil {
fmt.Errorf("failed to create a new config: %s", err)
return err
}

config.Hostname = d.Get("name").(string)

_, err = apiClient.CreateInstance(config)
if err != nil {
fmt.Errorf("failed to create instance: %s", err)
return err
}

d.SetId(config.Hostname)
return nil
}

func resourceInstanceRead(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceInstanceUpdate(d *schema.ResourceData, m interface{}) error {
return resourceInstanceRead(d, m)
}

func resourceInstanceDelete(d *schema.ResourceData, m interface{}) error {
return nil
}

0 comments on commit e018fda

Please sign in to comment.