-
Notifications
You must be signed in to change notification settings - Fork 53
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,19 @@ func ResourceNetwork() *schema.Resource { | |
Computed: true, | ||
Description: "The region of the network", | ||
}, | ||
"cidr": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The CIDR block for the network", | ||
}, | ||
"nameservers": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we get it named There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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 | ||
} |
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"] | ||
} |
There was a problem hiding this comment.
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 havecidr_v6
There was a problem hiding this comment.
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 assumecidr
alone is ipv4