Skip to content

Commit

Permalink
- Remove validator from resource_instance.go to utils.go
Browse files Browse the repository at this point in the history
- Add resource_network.go to handler the networks in civo

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jan 22, 2020
1 parent 89aa863 commit 47fc803
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/civo/terraform-provider-civo

require (
github.com/aws/aws-sdk-go v1.25.39
github.com/civo/civogo v0.0.0-20200116211328-a6d635388fa4
github.com/civo/civogo v0.0.0-20200121103424-a69bef1d9ea8
github.com/hashicorp/terraform-plugin-sdk v1.3.0
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2m
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
github.com/civo/civogo v0.0.0-20200116211328-a6d635388fa4 h1:bRX4UokNiGQxrB2uP/M3O2vYOSi4WmQNqgd1ZozZkfk=
github.com/civo/civogo v0.0.0-20200116211328-a6d635388fa4/go.mod h1:QwPxaDertZ7sxu3Fdibu/JdXBJV3RqYZrh76YEbGLKg=
github.com/civo/civogo v0.0.0-20200121103424-a69bef1d9ea8 h1:v6uezjqyqgivBSCcPH9R9oC1XgyRK11WoHYYPX//x9Y=
github.com/civo/civogo v0.0.0-20200121103424-a69bef1d9ea8/go.mod h1:QwPxaDertZ7sxu3Fdibu/JdXBJV3RqYZrh76YEbGLKg=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Provider() terraform.ResourceProvider {
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
"civo_network": resourceNetwork(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
17 changes: 0 additions & 17 deletions provider/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"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{
Expand Down
116 changes: 116 additions & 0 deletions provider/resource_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package provider

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

func resourceNetwork() *schema.Resource {
fmt.Print()
return &schema.Resource{
Schema: map[string]*schema.Schema{
"label": {
Type: schema.TypeString,
Required: true,
Description: "Name for the network",
ValidateFunc: validateName,
},
// Computed resource
"name": {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
"default": {
Type: schema.TypeBool,
Computed: true,
},
"cidr": {
Type: schema.TypeString,
Computed: true,
},
},
Create: resourceNetworkCreate,
Read: resourceNetworkRead,
Update: resourceNetworkUpdate,
Delete: resourceNetworkDelete,
//Exists: resourceExistsItem,
//Importer: &schema.ResourceImporter{
// State: schema.ImportStatePassthrough,
//},
}
}

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

config := &civogo.NetworkConfig{Label: d.Get("label").(string)}

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

d.SetId(network.ID)

return resourceNetworkRead(d, m)
}

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

CurrentNetwork := civogo.Network{}

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

for _, net := range resp {
if net.ID == d.Id() {
CurrentNetwork = net
}
}

d.Set("name", CurrentNetwork.Name)
d.Set("region", CurrentNetwork.Region)
d.Set("default", CurrentNetwork.Default)
d.Set("cidr", CurrentNetwork.CIDR)
d.Set("label", CurrentNetwork.Label)

return nil
}

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

if d.HasChange("label") {
config := &civogo.NetworkConfig{Label: d.Get("label").(string)}

//_, err := apiClient.RenameNetwork(config, d.Id())
_, err := apiClient.RenameNetwork(config)
if err != nil {
log.Printf("[WARN] An error occurred while rename the network (%s)", d.Id())
}

return resourceNetworkRead(d, m)
}

return resourceNetworkRead(d, m)
}

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

_, err := apiClient.DeleteNetwork(d.Id())
if err != nil {
log.Printf("[INFO] Civo network (%s) was delete", d.Id())
}
return nil
}
22 changes: 22 additions & 0 deletions provider/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package provider

import (
"fmt"
"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
}

0 comments on commit 47fc803

Please sign in to comment.