diff --git a/ibm/resource_ibm_compute_bare_metal.go b/ibm/resource_ibm_compute_bare_metal.go index 5736662ea8..68b12c735a 100644 --- a/ibm/resource_ibm_compute_bare_metal.go +++ b/ibm/resource_ibm_compute_bare_metal.go @@ -1028,47 +1028,9 @@ func getMonthlyBareMetalOrder(d *schema.ResourceData, meta interface{}) (datatyp if err != nil { return datatypes.Container_Product_Order{}, err } + var order datatypes.Container_Product_Order - monitoring, err := getItemPriceId(items, "monitoring", "MONITORING_HOST_PING") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - if d.Get("tcp_monitoring").(bool) { - monitoring, err = getItemPriceId(items, "monitoring", "MONITORING_HOST_PING_AND_TCP_SERVICE") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - } - - // Other common default options - priIpAddress, err := getItemPriceId(items, "pri_ip_addresses", "1_IP_ADDRESS") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - remoteManagement, err := getItemPriceId(items, "remote_management", "REBOOT_KVM_OVER_IP") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - vpnManagement, err := getItemPriceId(items, "vpn_management", "UNLIMITED_SSL_VPN_USERS_1_PPTP_VPN_USER_PER_ACCOUNT") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - - notification, err := getItemPriceId(items, "notification", "NOTIFICATION_EMAIL_AND_TICKET") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - response, err := getItemPriceId(items, "response", "AUTOMATED_NOTIFICATION") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - vulnerabilityScanner, err := getItemPriceId(items, "vulnerability_scanner", "NESSUS_VULNERABILITY_ASSESSMENT_REPORTING") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - - // Define an order object using basic paramters. - order := datatypes.Container_Product_Order{ + order = datatypes.Container_Product_Order{ Quantity: sl.Int(1), Hardware: []datatypes.Hardware{{ Hostname: sl.String(d.Get("hostname").(string)), @@ -1082,16 +1044,20 @@ func getMonthlyBareMetalOrder(d *schema.ResourceData, meta interface{}) (datatyp os, ram, portSpeed, - priIpAddress, - remoteManagement, - vpnManagement, - monitoring, - notification, - response, - vulnerabilityScanner, }, } + if d.Get("tcp_monitoring").(bool) { + monitoring, err := getItemPriceId(items, "monitoring", "MONITORING_HOST_PING_AND_TCP_SERVICE") + if err != nil { + return datatypes.Container_Product_Order{}, err + } + order.Prices = append(order.Prices, monitoring) + + } + + order = addCommomDefaultPrices(d, meta, order, items) + // Add optional price ids. // Add public bandwidth privateNetworkOnly := d.Get("private_network_only").(bool) @@ -1117,20 +1083,14 @@ func getMonthlyBareMetalOrder(d *schema.ResourceData, meta interface{}) (datatyp } } - // Add storage_groups for RAID configuration - diskController, err := getItemPriceId(items, "disk_controller", "DISK_CONTROLLER_NONRAID") - if err != nil { - return datatypes.Container_Product_Order{}, err - } - if _, ok := d.GetOk("storage_groups"); ok { order.StorageGroups = getStorageGroupsFromResourceData(d) - diskController, err = getItemPriceId(items, "disk_controller", "DISK_CONTROLLER_RAID") + diskController, err := getItemPriceId(items, "disk_controller", "DISK_CONTROLLER_RAID") if err != nil { return datatypes.Container_Product_Order{}, err } + order.Prices = append(order.Prices, diskController) } - order.Prices = append(order.Prices, diskController) err = setMonthlyHourlyCommonOrder(d, items, &order) if err != nil { @@ -1424,3 +1384,78 @@ func applyOnce(k, o, n string, d *schema.ResourceData) bool { } return true } + +func addCommomDefaultPrices(d *schema.ResourceData, meta interface{}, order datatypes.Container_Product_Order, items []datatypes.Product_Item) datatypes.Container_Product_Order { + + if !d.Get("tcp_monitoring").(bool) { + monExists, moniotring := getCommonItemPriceID(items, "monitoring", "MONITORING_HOST_PING") + + if monExists { + order.Prices = append(order.Prices, moniotring) + } + + } + + priExists, priIPAddress := getCommonItemPriceID(items, "pri_ip_addresses", "1_IP_ADDRESS") + if priExists { + order.Prices = append(order.Prices, priIPAddress) + } + + remotExists, remoteManagement := getCommonItemPriceID(items, "remote_management", "REBOOT_KVM_OVER_IP") + if remotExists { + order.Prices = append(order.Prices, remoteManagement) + } + + vpnExists, vpnManagement := getCommonItemPriceID(items, "vpn_management", "UNLIMITED_SSL_VPN_USERS_1_PPTP_VPN_USER_PER_ACCOUNT") + if vpnExists { + order.Prices = append(order.Prices, vpnManagement) + } + + notificationExists, notification := getCommonItemPriceID(items, "notification", "NOTIFICATION_EMAIL_AND_TICKET") + if notificationExists { + order.Prices = append(order.Prices, notification) + } + + resExists, response := getCommonItemPriceID(items, "response", "AUTOMATED_NOTIFICATION") + if resExists { + order.Prices = append(order.Prices, response) + } + + vulExists, vulnerabilityScanner := getCommonItemPriceID(items, "vulnerability_scanner", "NESSUS_VULNERABILITY_ASSESSMENT_REPORTING") + if vulExists { + order.Prices = append(order.Prices, vulnerabilityScanner) + } + + if _, ok := d.GetOk("storage_groups"); !ok { + diskExists, diskController := getCommonItemPriceID(items, "disk_controller", "DISK_CONTROLLER_NONRAID") + if diskExists { + order.Prices = append(order.Prices, diskController) + } + + } + + return order +} + +// Returns a common default sprice from an item list. +// Example usage : getItemPriceId(items, 'server', 'INTEL_XEON_2690_2_60') +func getCommonItemPriceID(items []datatypes.Product_Item, categoryCode string, keyName string) (bool, datatypes.Product_Item_Price) { + availableItems := "" + for _, item := range items { + for _, itemCategory := range item.Categories { + if *itemCategory.CategoryCode == categoryCode { + availableItems = availableItems + *item.KeyName + " ( " + *item.Description + " ) , " + if *item.KeyName == keyName { + for _, price := range item.Prices { + for _, category := range price.Categories { + if *category.CategoryCode == categoryCode && price.LocationGroupId == nil { + return true, datatypes.Product_Item_Price{Id: price.Id} + } + } + } + } + } + } + } + return false, datatypes.Product_Item_Price{} +} diff --git a/ibm/resource_ibm_compute_bare_metal_test.go b/ibm/resource_ibm_compute_bare_metal_test.go index 96fbb5fb48..2689d7af6f 100644 --- a/ibm/resource_ibm_compute_bare_metal_test.go +++ b/ibm/resource_ibm_compute_bare_metal_test.go @@ -305,6 +305,29 @@ func TestAccSoftLayerBareMetalCustom_with_gpus(t *testing.T) { }) } +func TestAccSoftLayerBareMetalCustom_with_monitoring_none(t *testing.T) { + var bareMetal datatypes.Hardware + hostname := acctest.RandString(14) + domain := "bm.custom.tfuat.com" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckIBMComputeBareMetalDestroy, + Steps: []resource.TestStep{ + { + Config: testBareMetalCustomConfigWithMonitoringNone(hostname, domain), + Destroy: false, + Check: resource.ComposeTestCheckFunc( + testAccCheckIBMComputeBareMetalExists("ibm_compute_bare_metal.bm-custom", &bareMetal), + resource.TestCheckResourceAttr( + "ibm_compute_bare_metal.bm-custom", "memory", "128"), + ), + }, + }, + }) +} + func testAccCheckIBMComputeBareMetalDestroy(s *terraform.State) error { service := services.GetHardwareService(testAccProvider.Meta().(ClientSession).SoftLayerSession()) @@ -543,3 +566,22 @@ resource "ibm_compute_bare_metal" "terraform-acceptance-test-1" { } `, hostname) } + +func testBareMetalCustomConfigWithMonitoringNone(hostname, domain string) string { + return fmt.Sprintf(` + resource "ibm_compute_bare_metal" "bm-custom" { + package_key_name = "BI_S1_NW128_VMWARE_VIRTUALIZATION" + process_key_name = "INTEL_XEON_2690_2_60" + memory = 128 + os_key_name = "OS_VMWARE_SERVER_VIRTUALIZATION_6_5" + hostname = "%s" + domain = "%s" + datacenter = "sao01" + disk_key_names = ["HARD_DRIVE_1_2_TB_SSD_10_DWPD"] + hourly_billing = false + public_bandwidth = 500 + network_speed = 1000 + unbonded_network = true + } +`, hostname, domain) +}