-
Notifications
You must be signed in to change notification settings - Fork 63
Declarative storage network IP range #299
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
Open
bddvlpr
wants to merge
1
commit into
apache:main
Choose a base branch
from
bddvlpr:feat/storage-network-range
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
169 changes: 169 additions & 0 deletions
169
cloudstack/resource_cloudstack_storage_network_ip_range.go
This file contains hidden or 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,169 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
|
|
||
| package cloudstack | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
|
|
||
| "github.com/apache/cloudstack-go/v2/cloudstack" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func resourceCloudStackStorageNetworkIpRange() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Create: resourceCloudStackStorageNetworkIpRangeCreate, | ||
| Read: resourceCloudStackStorageNetworkIpRangeRead, | ||
| Update: resourceCloudStackStorageNetworkIpRangeUpdate, | ||
| Delete: resourceCloudStackStorageNetworkIpRangeDelete, | ||
| Importer: &schema.ResourceImporter{ | ||
| State: schema.ImportStatePassthrough, | ||
| }, | ||
| Schema: map[string]*schema.Schema{ | ||
| "gateway": { | ||
| Description: "the gateway for the storage network IP range", | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| }, | ||
| "netmask": { | ||
| Description: "the netmask for the storage network IP range", | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "pod_id": { | ||
| Description: "the Pod ID for the storage network IP range", | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| }, | ||
| "start_ip": { | ||
| Description: "the beginning IP address in the storage network IP range", | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "end_ip": { | ||
| Description: "the ending IP address in the storage network IP range", | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Computed: true, | ||
| }, | ||
| "vlan": { | ||
| Description: "the optional VLAN of the storage network IP range", | ||
| Type: schema.TypeInt, | ||
| Optional: true, | ||
| }, | ||
| "network_id": { | ||
| Description: "the network id of the storage network IP range", | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func resourceCloudStackStorageNetworkIpRangeCreate(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| p := cs.Network.NewCreateStorageNetworkIpRangeParams( | ||
| d.Get("gateway").(string), | ||
| d.Get("netmask").(string), | ||
| d.Get("pod_id").(string), | ||
| d.Get("start_ip").(string), | ||
| ) | ||
|
|
||
| if v, ok := d.GetOk("end_ip"); ok { | ||
| p.SetEndip(v.(string)) | ||
| } | ||
| if v, ok := d.GetOk("vlan"); ok { | ||
| p.SetVlan(v.(int)) | ||
| } | ||
|
|
||
| r, err := cs.Network.CreateStorageNetworkIpRange(p) | ||
| if err != nil { | ||
| return fmt.Errorf("Error creating storage network IP range: %s", err) | ||
| } | ||
|
|
||
| d.SetId(r.Id) | ||
|
|
||
| return resourceCloudStackStorageNetworkIpRangeRead(d, meta) | ||
| } | ||
|
|
||
| func resourceCloudStackStorageNetworkIpRangeRead(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| r, count, err := cs.Network.GetStorageNetworkIpRangeByID(d.Id()) | ||
| if err != nil { | ||
| if count == 0 { | ||
| log.Printf("[DEBUG] Storage network IP range %s does no longer exist", d.Id()) | ||
| d.SetId("") | ||
| return nil | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| d.Set("gateway", r.Gateway) | ||
| d.Set("netmask", r.Netmask) | ||
| d.Set("pod_id", r.Podid) | ||
| d.Set("start_ip", r.Startip) | ||
| d.Set("end_ip", r.Endip) | ||
| d.Set("vlan", r.Vlan) | ||
| d.Set("network_id", r.Networkid) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceCloudStackStorageNetworkIpRangeUpdate(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| p := cs.Network.NewUpdateStorageNetworkIpRangeParams(d.Id()) | ||
|
|
||
| if v, ok := d.GetOk("netmask"); ok { | ||
| p.SetNetmask(v.(string)) | ||
| } | ||
| if v, ok := d.GetOk("start_ip"); ok { | ||
| p.SetStartip(v.(string)) | ||
| } | ||
| if v, ok := d.GetOk("end_ip"); ok { | ||
| p.SetEndip(v.(string)) | ||
| } | ||
| if v, ok := d.GetOk("vlan"); ok { | ||
| p.SetVlan(v.(int)) | ||
| } | ||
|
|
||
| _, err := cs.Network.UpdateStorageNetworkIpRange(p) | ||
| if err != nil { | ||
| return fmt.Errorf("Error updating storage network IP range: %s", err) | ||
| } | ||
|
|
||
| return resourceCloudStackStorageNetworkIpRangeRead(d, meta) | ||
| } | ||
|
|
||
| func resourceCloudStackStorageNetworkIpRangeDelete(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| _, err := cs.Network.DeleteStorageNetworkIpRange(cs.Network.NewDeleteStorageNetworkIpRangeParams(d.Id())) | ||
| if err != nil { | ||
| return fmt.Errorf("Error deleting storage network IP range: %s", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
154 changes: 154 additions & 0 deletions
154
cloudstack/resource_cloudstack_storage_network_ip_range_test.go
This file contains hidden or 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,154 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
|
|
||
| package cloudstack | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccCloudStackStorageNetworkIpRange_basic(t *testing.T) { | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { testAccPreCheck(t) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: testAccCloudStackStorageNetworkIpRange_basic, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "gateway", "10.1.1.1"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "netmask", "255.255.255.0"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "start_ip", "10.1.1.2"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "end_ip", "10.1.1.10"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "vlan", "100"), | ||
| ), | ||
| }, | ||
| { | ||
| Config: testAccCloudStackStorageNetworkIpRange_update, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "gateway", "10.1.1.1"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "netmask", "255.255.255.0"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "start_ip", "10.1.1.20"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "end_ip", "10.1.1.30"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "vlan", "100"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAccCloudStackStorageNetworkIpRange_noVlan(t *testing.T) { | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { testAccPreCheck(t) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: testAccCloudStackStorageNetworkIpRange_noVlan, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "start_ip", "10.1.1.2"), | ||
| resource.TestCheckResourceAttr("cloudstack_storage_network_ip_range.test", "end_ip", "10.1.1.10"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| const testAccCloudStackStorageNetworkIpRange_basic = ` | ||
| resource "cloudstack_zone" "test" { | ||
| name = "acctest" | ||
| dns1 = "8.8.8.8" | ||
| dns2 = "8.8.8.8" | ||
| internal_dns1 = "8.8.4.4" | ||
| internal_dns2 = "8.8.4.4" | ||
| network_type = "Advanced" | ||
| domain = "cloudstack.apache.org" | ||
| } | ||
| resource "cloudstack_pod" "test" { | ||
| name = "acctestpod" | ||
| allocation_state = "Enabled" | ||
| zone_id = cloudstack_zone.test.id | ||
| gateway = "10.0.0.1" | ||
| netmask = "255.255.255.0" | ||
| start_ip = "10.0.0.2" | ||
| } | ||
| resource "cloudstack_storage_network_ip_range" "test" { | ||
| pod_id = cloudstack_pod.test.id | ||
| gateway = "10.1.1.1" | ||
| netmask = "255.255.255.0" | ||
| start_ip = "10.1.1.2" | ||
| end_ip = "10.1.1.10" | ||
| vlan = 100 | ||
| } | ||
| ` | ||
|
|
||
| const testAccCloudStackStorageNetworkIpRange_update = ` | ||
| resource "cloudstack_zone" "test" { | ||
| name = "acctest" | ||
| dns1 = "8.8.8.8" | ||
| dns2 = "8.8.8.8" | ||
| internal_dns1 = "8.8.4.4" | ||
| internal_dns2 = "8.8.4.4" | ||
| network_type = "Advanced" | ||
| domain = "cloudstack.apache.org" | ||
| } | ||
| resource "cloudstack_pod" "test" { | ||
| name = "acctestpod" | ||
| allocation_state = "Enabled" | ||
| zone_id = cloudstack_zone.test.id | ||
| gateway = "10.0.0.1" | ||
| netmask = "255.255.255.0" | ||
| start_ip = "10.0.0.2" | ||
| } | ||
| resource "cloudstack_storage_network_ip_range" "test" { | ||
| pod_id = cloudstack_pod.test.id | ||
| gateway = "10.1.1.1" | ||
| netmask = "255.255.255.0" | ||
| start_ip = "10.1.1.20" | ||
| end_ip = "10.1.1.30" | ||
| vlan = 100 | ||
| } | ||
| ` | ||
|
|
||
| const testAccCloudStackStorageNetworkIpRange_noVlan = ` | ||
| resource "cloudstack_zone" "test" { | ||
| name = "acctest" | ||
| dns1 = "8.8.8.8" | ||
| dns2 = "8.8.8.8" | ||
| internal_dns1 = "8.8.4.4" | ||
| internal_dns2 = "8.8.4.4" | ||
| network_type = "Advanced" | ||
| domain = "cloudstack.apache.org" | ||
| } | ||
| resource "cloudstack_pod" "test" { | ||
| name = "acctestpod" | ||
| allocation_state = "Enabled" | ||
| zone_id = cloudstack_zone.test.id | ||
| gateway = "10.0.0.1" | ||
| netmask = "255.255.255.0" | ||
| start_ip = "10.0.0.2" | ||
| } | ||
| resource "cloudstack_storage_network_ip_range" "test" { | ||
| pod_id = cloudstack_pod.test.id | ||
| gateway = "10.1.1.1" | ||
| netmask = "255.255.255.0" | ||
| start_ip = "10.1.1.2" | ||
| end_ip = "10.1.1.10" | ||
| } | ||
| ` |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.