-
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.
- Remove validator from resource_instance.go to utils.go
- Add resource_network.go to handler the networks in civo Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
89aa863
commit 47fc803
Showing
6 changed files
with
142 additions
and
18 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
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
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
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
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,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 | ||
} |
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,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 | ||
} |