Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CIDR and Nameservers support in network #200

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
}
Loading