Skip to content

Commit 532da05

Browse files
Use Computed flag for CNI configuration account/domain/project fields
Instead of using GetOk conditionals in the Read function to avoid drift, mark account, domain_id, and project_id as Computed: true in the schema. This is the standard Terraform pattern used throughout the codebase and allows Terraform to properly handle values returned by the API that weren't explicitly set in the configuration. Also fix testAccPreCheckCniSupport to create its own CloudStack client from environment variables instead of relying on testAccProvider.Meta(), which is nil during PreCheck execution. This follows the pattern used in other PreCheck functions like checkCKSEnabled and testAccPreCheckQuotaSupport.
1 parent de4bd12 commit 532da05

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

cloudstack/resource_cloudstack_cni_configuration.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,23 @@ func resourceCloudStackCniConfiguration() *schema.Resource {
5555
"account": {
5656
Type: schema.TypeString,
5757
Optional: true,
58+
Computed: true,
5859
ForceNew: true,
5960
Description: "An optional account for the CNI configuration. Must be used with domain_id.",
6061
},
6162

6263
"domain_id": {
6364
Type: schema.TypeString,
6465
Optional: true,
66+
Computed: true,
6567
ForceNew: true,
6668
Description: "An optional domain ID for the CNI configuration. If the account parameter is used, domain_id must also be used.",
6769
},
6870

6971
"project_id": {
7072
Type: schema.TypeString,
7173
Optional: true,
74+
Computed: true,
7275
ForceNew: true,
7376
Description: "An optional project for the CNI configuration",
7477
},
@@ -188,18 +191,9 @@ func resourceCloudStackCniConfigurationRead(d *schema.ResourceData, meta interfa
188191

189192
d.Set("name", config.CniConfiguration[0].Name)
190193
d.Set("cni_config", config.CniConfiguration[0].Userdata)
191-
192-
// Only set account and domain_id if they were originally provided by the user
193-
// to avoid drift when CloudStack returns default values
194-
if _, ok := d.GetOk("account"); ok {
195-
d.Set("account", config.CniConfiguration[0].Account)
196-
}
197-
if _, ok := d.GetOk("domain_id"); ok {
198-
d.Set("domain_id", config.CniConfiguration[0].Domainid)
199-
}
200-
if _, ok := d.GetOk("project_id"); ok {
201-
d.Set("project_id", config.CniConfiguration[0].Projectid)
202-
}
194+
d.Set("account", config.CniConfiguration[0].Account)
195+
d.Set("domain_id", config.CniConfiguration[0].Domainid)
196+
d.Set("project_id", config.CniConfiguration[0].Projectid)
203197

204198
if config.CniConfiguration[0].Params != "" {
205199
paramsList := strings.Split(config.CniConfiguration[0].Params, ",")

cloudstack/resource_cloudstack_cni_configuration_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package cloudstack
2121

2222
import (
2323
"fmt"
24+
"os"
2425
"testing"
2526

2627
"github.com/apache/cloudstack-go/v2/cloudstack"
@@ -142,11 +143,22 @@ resource "cloudstack_cni_configuration" "foo" {
142143
`
143144

144145
func testAccPreCheckCniSupport(t *testing.T) {
145-
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
146+
cfg := Config{
147+
APIURL: os.Getenv("CLOUDSTACK_API_URL"),
148+
APIKey: os.Getenv("CLOUDSTACK_API_KEY"),
149+
SecretKey: os.Getenv("CLOUDSTACK_SECRET_KEY"),
150+
HTTPGETOnly: true,
151+
Timeout: 60,
152+
}
153+
cs, err := cfg.NewClient()
154+
if err != nil {
155+
t.Skipf("Failed to create CloudStack client: %v", err)
156+
return
157+
}
146158

147159
// Try to list CNI configurations to check if the feature is available
148160
p := cs.Configuration.NewListCniConfigurationParams()
149-
_, err := cs.Configuration.ListCniConfiguration(p)
161+
_, err = cs.Configuration.ListCniConfiguration(p)
150162
if err != nil {
151163
t.Skipf("CNI configuration not supported in this CloudStack version (requires 4.21.0+): %v", err)
152164
}

0 commit comments

Comments
 (0)