Skip to content

Commit

Permalink
Add CIDR and Nameservers support in network (#200)
Browse files Browse the repository at this point in the history
* Add CIDR and Nameserver support in network

* update examples

* _v4 suffix
  • Loading branch information
uzaxirr committed Mar 29, 2024
1 parent cd13982 commit 887ffbe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
42 changes: 37 additions & 5 deletions civo/network/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ func ResourceNetwork() *schema.Resource {
Computed: true,
Description: "The region of the network",
},
"cidr_v4": {
Type: schema.TypeString,
Optional: true,
Description: "The CIDR block for the network",
},
"nameservers_v4": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of nameservers for the network",
},
// Computed resource
"name": {
Type: schema.TypeString,
Expand All @@ -56,13 +69,19 @@ func ResourceNetwork() *schema.Resource {
func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
// overwrite the region if is defined in the datasource
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

log.Printf("[INFO] creating the new network %s", d.Get("label").(string))
network, err := apiClient.NewNetwork(d.Get("label").(string))
configs := civogo.NetworkConfig{
Label: d.Get("label").(string),
CIDRv4: d.Get("cidr_v4").(string),
Region: apiClient.Region,
NameserversV4: expandStringList(d.Get("nameservers_v4")),
}
network, err := apiClient.CreateNetwork(configs)
if err != nil {
return diag.Errorf("[ERR] failed to create a new network: %s", err)
}
Expand All @@ -76,7 +95,7 @@ func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, m interf
func resourceNetworkRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
// overwrite the region if is defined in the datasource
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}
Expand Down Expand Up @@ -111,7 +130,7 @@ func resourceNetworkRead(_ context.Context, d *schema.ResourceData, m interface{
func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
// overwrite the region if is defined in the datasource
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}
Expand All @@ -131,7 +150,7 @@ func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interf
func resourceNetworkDelete(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
// overwrite the region if is defined in the datasource
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}
Expand Down Expand Up @@ -168,3 +187,16 @@ func resourceNetworkDelete(_ context.Context, d *schema.ResourceData, m interfac

return nil
}

func expandStringList(input interface{}) []string {
var result []string

if inputList, ok := input.([]interface{}); ok {
for _, item := range inputList {
if str, ok := item.(string); ok {
result = append(result, str)
}
}
}
return result
}
2 changes: 2 additions & 0 deletions examples/data-sources/civo_network/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
data "civo_network" "test" {
label = "test-network"
region = "LON1"
cidr_v4 = "10.0.0.0/24"
nameservers_v4 = ["8.8.8.8", "8.8.4.4", "1.1.1.1"]
}

0 comments on commit 887ffbe

Please sign in to comment.