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 2 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": {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get it named cidr_v4 like we have in the api? Because In the future we will have cidr_v6

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add cidr_v6 in the future because we assume cidr alone is ipv4

Type: schema.TypeString,
Optional: true,
Description: "The CIDR block for the network",
},
"nameservers": {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get it named nameservers_v4 like we have in the api? Because In the future we will have nameservers_v6

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the cidr, nameserver alone is ipv4 and the other one is ipv6

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").(string),
Region: apiClient.Region,
NameserversV4: expandStringList(d.Get("nameservers")),
}
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 = "10.0.0.0/24"
nameservers = ["8.8.8.8", "8.8.4.4", "1.1.1.1"]
}
Loading