Copilot commented on code in PR #327: URL: https://github.com/apache/cloudstack-terraform-provider/pull/327#discussion_r3911762316
########## cloudstack/resource_cloudstack_system_service_offering.go: ########## @@ -0,0 +1,352 @@ +// +// 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 ( + "context" + "fmt" + "log" + "strings" + + "github.com/apache/cloudstack-go/v2/cloudstack" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func resourceCloudStackSystemServiceOffering() *schema.Resource { + return &schema.Resource{ + Create: resourceCloudStackSystemServiceOfferingCreate, + Read: resourceCloudStackSystemServiceOfferingRead, + Update: resourceCloudStackSystemServiceOfferingUpdate, + Delete: resourceCloudStackSystemServiceOfferingDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + CustomizeDiff: resourceCloudStackSystemServiceOfferingCustomizeDiff, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "display_text": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "system_vm_type": { + Description: "The system VM type that uses this offering", + Type: schema.TypeString, + Required: true, + ForceNew: true, + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{ + "domainrouter", + "consoleproxy", + "secondarystoragevm", + }, true), + }, + "cpu_number": { + Description: "Number of CPU cores", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "cpu_speed": { + Description: "Speed of each CPU core in MHz", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(0), + }, + "memory": { + Description: "Memory reserved by the system VM in MB", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(32), + }, + "storage_type": { + Description: "The storage type of the offering", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: "shared", + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{"local", "shared"}, true), + }, + "network_rate": { + Description: "Network rate in Mbps; valid only for domain router offerings", + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "offer_ha": { + Description: "Whether the system offering supports HA", + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "limit_cpu_use": { + Description: "Whether CPU usage is limited to the offering's committed resources", + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "host_tags": { + Description: "Host tags associated with the offering", + Type: schema.TypeString, + Optional: true, + }, + "storage_tags": { + Description: "Storage tags associated with the offering", + Type: schema.TypeString, + Optional: true, + }, + "domain_ids": { + Description: "IDs of the domains that can use the offering; omit for a public offering", + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringIsNotEmpty, + }, + Set: schema.HashString, + }, + }, + } +} + +func resourceCloudStackSystemServiceOfferingCustomizeDiff(_ context.Context, d *schema.ResourceDiff, _ interface{}) error { + _, hasNetworkRate := d.GetOk("network_rate") + return validateSystemServiceOfferingConfiguration( + d.Get("system_vm_type").(string), + hasNetworkRate, + d.Get("storage_type").(string), + d.Get("offer_ha").(bool), + ) +} + +func validateSystemServiceOfferingConfiguration(systemVMType string, hasNetworkRate bool, storageType string, offerHA bool) error { + if hasNetworkRate && systemVMType != "domainrouter" { + return fmt.Errorf("network_rate can only be set when system_vm_type is domainrouter") + } + + if offerHA && storageType == "local" { + return fmt.Errorf("offer_ha cannot be enabled when storage_type is local") + } + + return nil +} + +func resourceCloudStackSystemServiceOfferingCreate(d *schema.ResourceData, meta interface{}) error { + cs := meta.(*cloudstack.CloudStackClient) + name := d.Get("name").(string) + p := cs.ServiceOffering.NewCreateServiceOfferingParams(d.Get("display_text").(string), name) + + p.SetIssystem(true) + p.SetSystemvmtype(d.Get("system_vm_type").(string)) + p.SetCpunumber(d.Get("cpu_number").(int)) + p.SetCpuspeed(d.Get("cpu_speed").(int)) + p.SetMemory(d.Get("memory").(int)) + p.SetCustomized(false) + p.SetStoragetype(d.Get("storage_type").(string)) + p.SetOfferha(d.Get("offer_ha").(bool)) + p.SetLimitcpuuse(d.Get("limit_cpu_use").(bool)) + + if v, ok := d.GetOk("network_rate"); ok { + p.SetNetworkrate(v.(int)) + } + if v, ok := d.GetOk("host_tags"); ok { + p.SetHosttags(v.(string)) + } + if v, ok := d.GetOk("storage_tags"); ok { + p.SetTags(v.(string)) + } + if domainIDs := expandSystemServiceOfferingDomainIDs(d.Get("domain_ids")); len(domainIDs) > 0 { + p.SetDomainid(domainIDs) Review Comment: `expandSystemServiceOfferingDomainIDs` returns a `[]string`, but `domainid` is treated elsewhere as a comma-separated string (`flattenSystemServiceOfferingDomainIDs(domainID string)` and `Update` uses `strings.Join(...)`). This is either a compile-time type mismatch (if `SetDomainid` expects `string`) or an inconsistent encoding that can break domain scoping. Align `Create` with `Update/Read` by using the same representation (e.g., comma-separated string) and the correct setter signature. ########## cloudstack/resource_cloudstack_system_service_offering.go: ########## @@ -0,0 +1,352 @@ +// +// 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 ( + "context" + "fmt" + "log" + "strings" + + "github.com/apache/cloudstack-go/v2/cloudstack" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func resourceCloudStackSystemServiceOffering() *schema.Resource { + return &schema.Resource{ + Create: resourceCloudStackSystemServiceOfferingCreate, + Read: resourceCloudStackSystemServiceOfferingRead, + Update: resourceCloudStackSystemServiceOfferingUpdate, + Delete: resourceCloudStackSystemServiceOfferingDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + CustomizeDiff: resourceCloudStackSystemServiceOfferingCustomizeDiff, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "display_text": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "system_vm_type": { + Description: "The system VM type that uses this offering", + Type: schema.TypeString, + Required: true, + ForceNew: true, + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{ + "domainrouter", + "consoleproxy", + "secondarystoragevm", + }, true), + }, + "cpu_number": { + Description: "Number of CPU cores", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "cpu_speed": { + Description: "Speed of each CPU core in MHz", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(0), + }, + "memory": { + Description: "Memory reserved by the system VM in MB", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(32), + }, + "storage_type": { + Description: "The storage type of the offering", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: "shared", + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{"local", "shared"}, true), + }, + "network_rate": { + Description: "Network rate in Mbps; valid only for domain router offerings", + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "offer_ha": { + Description: "Whether the system offering supports HA", + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "limit_cpu_use": { + Description: "Whether CPU usage is limited to the offering's committed resources", + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "host_tags": { + Description: "Host tags associated with the offering", + Type: schema.TypeString, + Optional: true, + }, + "storage_tags": { + Description: "Storage tags associated with the offering", + Type: schema.TypeString, + Optional: true, + }, + "domain_ids": { + Description: "IDs of the domains that can use the offering; omit for a public offering", + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringIsNotEmpty, + }, + Set: schema.HashString, + }, + }, + } +} + +func resourceCloudStackSystemServiceOfferingCustomizeDiff(_ context.Context, d *schema.ResourceDiff, _ interface{}) error { + _, hasNetworkRate := d.GetOk("network_rate") + return validateSystemServiceOfferingConfiguration( + d.Get("system_vm_type").(string), + hasNetworkRate, + d.Get("storage_type").(string), + d.Get("offer_ha").(bool), + ) +} + +func validateSystemServiceOfferingConfiguration(systemVMType string, hasNetworkRate bool, storageType string, offerHA bool) error { + if hasNetworkRate && systemVMType != "domainrouter" { + return fmt.Errorf("network_rate can only be set when system_vm_type is domainrouter") + } + + if offerHA && storageType == "local" { + return fmt.Errorf("offer_ha cannot be enabled when storage_type is local") + } + + return nil +} + +func resourceCloudStackSystemServiceOfferingCreate(d *schema.ResourceData, meta interface{}) error { + cs := meta.(*cloudstack.CloudStackClient) + name := d.Get("name").(string) + p := cs.ServiceOffering.NewCreateServiceOfferingParams(d.Get("display_text").(string), name) + + p.SetIssystem(true) + p.SetSystemvmtype(d.Get("system_vm_type").(string)) + p.SetCpunumber(d.Get("cpu_number").(int)) + p.SetCpuspeed(d.Get("cpu_speed").(int)) + p.SetMemory(d.Get("memory").(int)) + p.SetCustomized(false) + p.SetStoragetype(d.Get("storage_type").(string)) + p.SetOfferha(d.Get("offer_ha").(bool)) + p.SetLimitcpuuse(d.Get("limit_cpu_use").(bool)) + + if v, ok := d.GetOk("network_rate"); ok { + p.SetNetworkrate(v.(int)) + } + if v, ok := d.GetOk("host_tags"); ok { + p.SetHosttags(v.(string)) + } + if v, ok := d.GetOk("storage_tags"); ok { + p.SetTags(v.(string)) Review Comment: `storage_tags` is being set via `p.SetTags(...)`, but the rest of this resource treats it as *storage tags* (`Read` uses `offering.Storagetags` and `Update` uses `p.SetStoragetags`). This mismatch likely results in the API receiving the wrong parameter and/or Terraform state not reflecting what was configured. Use the storage-tags-specific setter consistently for create/update/read. ########## cloudstack/resource_cloudstack_system_service_offering.go: ########## @@ -0,0 +1,352 @@ +// +// 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 ( + "context" + "fmt" + "log" + "strings" + + "github.com/apache/cloudstack-go/v2/cloudstack" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func resourceCloudStackSystemServiceOffering() *schema.Resource { + return &schema.Resource{ + Create: resourceCloudStackSystemServiceOfferingCreate, + Read: resourceCloudStackSystemServiceOfferingRead, + Update: resourceCloudStackSystemServiceOfferingUpdate, + Delete: resourceCloudStackSystemServiceOfferingDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + CustomizeDiff: resourceCloudStackSystemServiceOfferingCustomizeDiff, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "display_text": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "system_vm_type": { + Description: "The system VM type that uses this offering", + Type: schema.TypeString, + Required: true, + ForceNew: true, + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{ + "domainrouter", + "consoleproxy", + "secondarystoragevm", + }, true), + }, + "cpu_number": { + Description: "Number of CPU cores", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "cpu_speed": { + Description: "Speed of each CPU core in MHz", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(0), Review Comment: `cpu_speed` is required, but validation permits `0`. A 0 MHz CPU speed is typically invalid and likely to be rejected by the API. Consider tightening validation to `IntAtLeast(1)` (or whatever minimum CloudStack enforces) to fail fast and provide a clearer Terraform-side error. ########## cloudstack/resource_cloudstack_system_service_offering.go: ########## @@ -0,0 +1,352 @@ +// +// 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 ( + "context" + "fmt" + "log" + "strings" + + "github.com/apache/cloudstack-go/v2/cloudstack" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func resourceCloudStackSystemServiceOffering() *schema.Resource { + return &schema.Resource{ + Create: resourceCloudStackSystemServiceOfferingCreate, + Read: resourceCloudStackSystemServiceOfferingRead, + Update: resourceCloudStackSystemServiceOfferingUpdate, + Delete: resourceCloudStackSystemServiceOfferingDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + CustomizeDiff: resourceCloudStackSystemServiceOfferingCustomizeDiff, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "display_text": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "system_vm_type": { + Description: "The system VM type that uses this offering", + Type: schema.TypeString, + Required: true, + ForceNew: true, + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{ + "domainrouter", + "consoleproxy", + "secondarystoragevm", + }, true), + }, + "cpu_number": { + Description: "Number of CPU cores", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "cpu_speed": { + Description: "Speed of each CPU core in MHz", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(0), + }, + "memory": { + Description: "Memory reserved by the system VM in MB", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(32), + }, + "storage_type": { + Description: "The storage type of the offering", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: "shared", + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{"local", "shared"}, true), + }, + "network_rate": { + Description: "Network rate in Mbps; valid only for domain router offerings", + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "offer_ha": { + Description: "Whether the system offering supports HA", + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "limit_cpu_use": { + Description: "Whether CPU usage is limited to the offering's committed resources", + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "host_tags": { + Description: "Host tags associated with the offering", + Type: schema.TypeString, + Optional: true, + }, + "storage_tags": { + Description: "Storage tags associated with the offering", + Type: schema.TypeString, + Optional: true, + }, + "domain_ids": { + Description: "IDs of the domains that can use the offering; omit for a public offering", + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringIsNotEmpty, + }, + Set: schema.HashString, + }, + }, + } +} + +func resourceCloudStackSystemServiceOfferingCustomizeDiff(_ context.Context, d *schema.ResourceDiff, _ interface{}) error { + _, hasNetworkRate := d.GetOk("network_rate") + return validateSystemServiceOfferingConfiguration( + d.Get("system_vm_type").(string), + hasNetworkRate, + d.Get("storage_type").(string), + d.Get("offer_ha").(bool), + ) +} Review Comment: The PR introduces custom diff-time validation (e.g., rejecting `network_rate` unless `system_vm_type == domainrouter`, and rejecting `offer_ha` with `storage_type == local`), but the acceptance test does not exercise these failure paths. Adding at least one negative test step for each validation would prevent regressions and confirm users get the expected Terraform error behavior. ########## cloudstack/resource_cloudstack_system_service_offering.go: ########## @@ -0,0 +1,352 @@ +// +// 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 ( + "context" + "fmt" + "log" + "strings" + + "github.com/apache/cloudstack-go/v2/cloudstack" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func resourceCloudStackSystemServiceOffering() *schema.Resource { + return &schema.Resource{ + Create: resourceCloudStackSystemServiceOfferingCreate, + Read: resourceCloudStackSystemServiceOfferingRead, + Update: resourceCloudStackSystemServiceOfferingUpdate, + Delete: resourceCloudStackSystemServiceOfferingDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + CustomizeDiff: resourceCloudStackSystemServiceOfferingCustomizeDiff, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "display_text": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "system_vm_type": { + Description: "The system VM type that uses this offering", + Type: schema.TypeString, + Required: true, + ForceNew: true, + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{ + "domainrouter", + "consoleproxy", + "secondarystoragevm", + }, true), + }, + "cpu_number": { + Description: "Number of CPU cores", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "cpu_speed": { + Description: "Speed of each CPU core in MHz", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(0), + }, + "memory": { + Description: "Memory reserved by the system VM in MB", + Type: schema.TypeInt, + Required: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(32), + }, + "storage_type": { + Description: "The storage type of the offering", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: "shared", + StateFunc: func(v interface{}) string { + return strings.ToLower(v.(string)) + }, + ValidateFunc: validation.StringInSlice([]string{"local", "shared"}, true), + }, + "network_rate": { + Description: "Network rate in Mbps; valid only for domain router offerings", + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "offer_ha": { + Description: "Whether the system offering supports HA", + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "limit_cpu_use": { + Description: "Whether CPU usage is limited to the offering's committed resources", + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "host_tags": { + Description: "Host tags associated with the offering", + Type: schema.TypeString, + Optional: true, + }, + "storage_tags": { + Description: "Storage tags associated with the offering", + Type: schema.TypeString, + Optional: true, + }, + "domain_ids": { + Description: "IDs of the domains that can use the offering; omit for a public offering", + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringIsNotEmpty, + }, + Set: schema.HashString, + }, + }, + } +} + +func resourceCloudStackSystemServiceOfferingCustomizeDiff(_ context.Context, d *schema.ResourceDiff, _ interface{}) error { + _, hasNetworkRate := d.GetOk("network_rate") + return validateSystemServiceOfferingConfiguration( + d.Get("system_vm_type").(string), + hasNetworkRate, + d.Get("storage_type").(string), + d.Get("offer_ha").(bool), + ) +} + +func validateSystemServiceOfferingConfiguration(systemVMType string, hasNetworkRate bool, storageType string, offerHA bool) error { + if hasNetworkRate && systemVMType != "domainrouter" { + return fmt.Errorf("network_rate can only be set when system_vm_type is domainrouter") + } + + if offerHA && storageType == "local" { + return fmt.Errorf("offer_ha cannot be enabled when storage_type is local") + } + + return nil +} + +func resourceCloudStackSystemServiceOfferingCreate(d *schema.ResourceData, meta interface{}) error { + cs := meta.(*cloudstack.CloudStackClient) + name := d.Get("name").(string) + p := cs.ServiceOffering.NewCreateServiceOfferingParams(d.Get("display_text").(string), name) + + p.SetIssystem(true) + p.SetSystemvmtype(d.Get("system_vm_type").(string)) + p.SetCpunumber(d.Get("cpu_number").(int)) + p.SetCpuspeed(d.Get("cpu_speed").(int)) + p.SetMemory(d.Get("memory").(int)) + p.SetCustomized(false) + p.SetStoragetype(d.Get("storage_type").(string)) + p.SetOfferha(d.Get("offer_ha").(bool)) + p.SetLimitcpuuse(d.Get("limit_cpu_use").(bool)) + + if v, ok := d.GetOk("network_rate"); ok { + p.SetNetworkrate(v.(int)) + } + if v, ok := d.GetOk("host_tags"); ok { + p.SetHosttags(v.(string)) + } + if v, ok := d.GetOk("storage_tags"); ok { + p.SetTags(v.(string)) + } + if domainIDs := expandSystemServiceOfferingDomainIDs(d.Get("domain_ids")); len(domainIDs) > 0 { + p.SetDomainid(domainIDs) + } + + log.Printf("[DEBUG] Creating System Service Offering %s", name) + offering, err := cs.ServiceOffering.CreateServiceOffering(p) + if err != nil { + return fmt.Errorf("error creating System Service Offering %s: %s", name, err) + } + + d.SetId(offering.Id) + log.Printf("[DEBUG] System Service Offering %s successfully created", name) + + return resourceCloudStackSystemServiceOfferingRead(d, meta) +} + +func resourceCloudStackSystemServiceOfferingRead(d *schema.ResourceData, meta interface{}) error { + cs := meta.(*cloudstack.CloudStackClient) + log.Printf("[DEBUG] Retrieving System Service Offering %s", d.Id()) + + offering, count, err := getSystemServiceOfferingByID(cs, d.Id()) + if err != nil { + return err + } + if count == 0 { + log.Printf("[DEBUG] System Service Offering %s no longer exists", d.Id()) + d.SetId("") + return nil + } + + fields := map[string]interface{}{ + "name": offering.Name, + "display_text": offering.Displaytext, + "system_vm_type": strings.ToLower(offering.Systemvmtype), + "cpu_number": offering.Cpunumber, + "cpu_speed": offering.Cpuspeed, + "memory": offering.Memory, + "storage_type": strings.ToLower(offering.Storagetype), + "network_rate": offering.Networkrate, + "offer_ha": offering.Offerha, + "limit_cpu_use": offering.Limitcpuuse, + "host_tags": offering.Hosttags, + "storage_tags": offering.Storagetags, + "domain_ids": flattenSystemServiceOfferingDomainIDs(offering.Domainid), + } + + for key, value := range fields { + if err := d.Set(key, value); err != nil { + return fmt.Errorf("error setting %s for System Service Offering %s: %s", key, d.Id(), err) + } + } + + return nil +} + +func resourceCloudStackSystemServiceOfferingUpdate(d *schema.ResourceData, meta interface{}) error { + cs := meta.(*cloudstack.CloudStackClient) + p := cs.ServiceOffering.NewUpdateServiceOfferingParams(d.Id()) + hasChanges := false + + if d.HasChange("name") { + p.SetName(d.Get("name").(string)) + hasChanges = true + } + if d.HasChange("display_text") { + p.SetDisplaytext(d.Get("display_text").(string)) + hasChanges = true + } + if d.HasChange("host_tags") { + p.SetHosttags(d.Get("host_tags").(string)) + hasChanges = true + } + if d.HasChange("storage_tags") { + p.SetStoragetags(d.Get("storage_tags").(string)) + hasChanges = true + } + if d.HasChange("domain_ids") { + domainIDs := expandSystemServiceOfferingDomainIDs(d.Get("domain_ids")) + if len(domainIDs) == 0 { + p.SetDomainid("public") + } else { + p.SetDomainid(strings.Join(domainIDs, ",")) + } + hasChanges = true + } Review Comment: `domain_ids` is a `TypeSet`, so `set.List()` ordering is not stable. Joining unsorted values can lead to non-deterministic `domainid` strings across plans/applies (and potentially noisy diffs if the API normalizes ordering). Consider sorting `domainIDs` before joining to keep API requests deterministic. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
