Skip to content

Commit 8283deb

Browse files
committed
fix(network): make type Optional+Computed, default from cidr in CustomizeDiff, normalize L2 in Read; plus errcheck fix
- Make type field Optional+Computed to prevent drift when config omits it - Default type from cidr presence in CustomizeDiff (cidr present = L3, absent = L2) - Normalize L2 networks in Read by clearing cidr/gateway to prevent plan drift - Fix unchecked errors in resource_cloudstack_configuration.go - Update validation tests to reflect new schema behavior This ensures backward compatibility - existing configs without explicit type will continue to work without modification.
1 parent 89fd3c1 commit 8283deb

3 files changed

Lines changed: 39 additions & 21 deletions

File tree

cloudstack/resource_cloudstack_configuration.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ func resourceCloudStackConfigurationCreate(d *schema.ResourceData, meta interfac
163163
d.SetId(v.(string))
164164
}
165165

166-
resourceCloudStackConfigurationUpdate(d, meta)
167-
168-
return nil
169-
166+
return resourceCloudStackConfigurationUpdate(d, meta)
170167
}
171168

172169
func resourceCloudStackConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
@@ -201,9 +198,7 @@ func resourceCloudStackConfigurationUpdate(d *schema.ResourceData, meta interfac
201198
return err
202199
}
203200

204-
resourceCloudStackConfigurationRead(d, meta)
205-
206-
return nil
201+
return resourceCloudStackConfigurationRead(d, meta)
207202
}
208203

209204
func resourceCloudStackConfigurationDelete(d *schema.ResourceData, meta interface{}) error {

cloudstack/resource_cloudstack_network.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func resourceCloudStackNetwork() *schema.Resource {
7777
"type": {
7878
Type: schema.TypeString,
7979
Optional: true,
80-
Default: "L3",
80+
Computed: true,
8181
ForceNew: true,
8282
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
8383
v := val.(string)
@@ -175,17 +175,35 @@ func resourceCloudStackNetwork() *schema.Resource {
175175
}
176176

177177
func resourceCloudStackNetworkCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
178-
networkType := d.Get("type").(string)
178+
var t string
179+
if v, ok := d.GetOk("type"); ok {
180+
t = v.(string)
181+
}
179182
cidr := d.Get("cidr").(string)
180183

181-
// For L3 networks, cidr is required
182-
if networkType == "L3" && cidr == "" {
183-
return fmt.Errorf("cidr is required when type is L3")
184+
// Default type if user didn't set it
185+
if t == "" {
186+
if cidr != "" {
187+
if err := d.SetNew("type", "L3"); err != nil {
188+
return err
189+
}
190+
t = "L3"
191+
} else {
192+
if err := d.SetNew("type", "L2"); err != nil {
193+
return err
194+
}
195+
t = "L2"
196+
}
184197
}
185198

186-
// For L2 networks, cidr should not be provided
187-
if networkType == "L2" && cidr != "" {
188-
return fmt.Errorf("cidr should not be provided when type is L2")
199+
// Enforce combinations; also clear stray cidr for L2 to avoid drift
200+
if t == "L3" && cidr == "" {
201+
return fmt.Errorf("cidr is required when type is L3")
202+
}
203+
if t == "L2" && cidr != "" {
204+
if err := d.SetNew("cidr", ""); err != nil {
205+
return err
206+
}
189207
}
190208

191209
return nil
@@ -342,16 +360,18 @@ func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) err
342360

343361
d.Set("name", n.Name)
344362
d.Set("display_text", n.Displaytext)
345-
d.Set("cidr", n.Cidr)
346-
d.Set("gateway", n.Gateway)
347363
d.Set("network_domain", n.Networkdomain)
348364
d.Set("vpc_id", n.Vpcid)
349365

350-
// Determine network type based on CIDR presence
351-
if n.Cidr == "" {
366+
// Normalize: different ACS versions omit/empty fields
367+
if strings.TrimSpace(n.Cidr) == "" {
352368
d.Set("type", "L2")
369+
d.Set("cidr", "")
370+
d.Set("gateway", "")
353371
} else {
354372
d.Set("type", "L3")
373+
d.Set("cidr", n.Cidr)
374+
d.Set("gateway", n.Gateway)
355375
}
356376

357377
if n.Aclid == "" {

cloudstack/resource_cloudstack_network_validation_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ func TestResourceCloudStackNetworkSchema(t *testing.T) {
4242
if typeField.Optional != true {
4343
t.Error("Type field should be optional")
4444
}
45-
if typeField.Default != "L3" {
46-
t.Errorf("Type field default should be 'L3', got: %v", typeField.Default)
45+
if typeField.Computed != true {
46+
t.Error("Type field should be computed")
47+
}
48+
if typeField.Default != nil {
49+
t.Errorf("Type field should not have a default (it's computed), got: %v", typeField.Default)
4750
}
4851
}
4952
})

0 commit comments

Comments
 (0)