-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit for the terraform provider for civo
- Now can create a instances Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
d9e6c92
commit e018fda
Showing
6 changed files
with
286 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}, | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |