bddvlpr commented on code in PR #327: URL: https://github.com/apache/cloudstack-terraform-provider/pull/327#discussion_r3922606075
########## 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: https://github.com/apache/cloudstack/commit/d6a77a72f00d01e82b536c2b9d43c00690ee96fd Tags is correct here; unsure why they split it up further down the line. -- 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]
