Skip to content

Commit

Permalink
Support for classic reserved capacity and instance
Browse files Browse the repository at this point in the history
  • Loading branch information
hkantare committed Sep 28, 2021
1 parent 13f0862 commit a8c07e0
Show file tree
Hide file tree
Showing 9 changed files with 808 additions and 4 deletions.
130 changes: 130 additions & 0 deletions ibm/data_source_ibm_compute_reserved_capacity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package ibm

import (
"fmt"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/softlayer/softlayer-go/datatypes"
"github.com/softlayer/softlayer-go/filter"
"github.com/softlayer/softlayer-go/services"
)

func dataSourceIBMComputeReservedCapacity() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMComputeReservedCapacityRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of reserved instance",
},

"datacenter": {
Type: schema.TypeString,
Computed: true,
Description: "Dataceneter name",
},

"pod": {
Type: schema.TypeString,
Computed: true,
Description: "Pod name",
},

"instances": {
Type: schema.TypeString,
Computed: true,
Description: "no of the instances",
},

"flavor": {
Type: schema.TypeString,
Computed: true,
Description: "flavor of the reserved group",
},

"virtual_guests": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"domain": {
Type: schema.TypeString,
Computed: true,
},
"hostname": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceIBMComputeReservedCapacityRead(d *schema.ResourceData, meta interface{}) error {
sess := meta.(ClientSession).SoftLayerSession()
service := services.GetAccountService(sess)

name := d.Get("name").(string)

groups, err := service.
Filter(filter.Build(filter.Path("reservedCapacityGroups.name").Eq(name))).
Mask("id,name,instancesCount,backendRouter[hostname,datacenter[name]],occupiedInstances[guest[id,domain,hostname]],instances[billingItem[item[keyName]]]").GetReservedCapacityGroups()

if err != nil {
return fmt.Errorf("Error retrieving placement group: %s", err)
}

grps := []datatypes.Virtual_ReservedCapacityGroup{}
for _, g := range groups {
if name == *g.Name {
grps = append(grps, g)

}
}

if len(grps) == 0 {
return fmt.Errorf("No reserved capacity found with name [%s]", name)
}

var grp datatypes.Virtual_ReservedCapacityGroup

grp = grps[0]

d.SetId(fmt.Sprintf("%d", *grp.Id))
d.Set("name", grp.Name)
d.Set("datacenter", grp.BackendRouter.Datacenter.Name)
pod := strings.SplitAfter(*grp.BackendRouter.Hostname, ".")[0]
r, _ := regexp.Compile("[0-9]{2}")
pod = "pod" + r.FindString(pod)
d.Set("pod", pod)
d.Set("instances", grp.ModifyDate)
d.Set("flavor", grp.Instances[0].BillingItem.Item.KeyName)

vgs := make([]map[string]interface{}, len(grp.OccupiedInstances))
for i, vg := range grp.OccupiedInstances {
if vg.Guest != nil {
v := make(map[string]interface{})
v["id"] = *vg.Guest.Id
v["domain"] = *vg.Guest.Domain
v["hostname"] = *vg.Guest.Hostname
vgs[i] = v
}

}
d.Set("virtual_guests", vgs)

return nil
}
60 changes: 60 additions & 0 deletions ibm/data_source_ibm_compute_reserved_capacity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package ibm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccIBMComputeReservedCapacityDataSource_Basic(t *testing.T) {

group1 := fmt.Sprintf("%s%s", "tfuatreservedcapacity", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
//CheckDestroy: testAccCheckIBMComputeReservedCapacityDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckIBMComputeReservedCapacitydsConfig(group1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ibm_compute_reserved_capacity.reservedcapacity", "name", group1),
resource.TestCheckResourceAttr(
"ibm_compute_reserved_capacity.reservedcapacity", "flavor", "B1_4X16_1_YEAR_TERM"),
resource.TestCheckResourceAttr(
"data.ibm_compute_reserved_capacity.reservedcapacityds", "name", group1),
resource.TestCheckResourceAttr(
"data.ibm_compute_reserved_capacity.reservedcapacityds", "flavor", "B1_4X16_1_YEAR_TERM"),
resource.TestCheckResourceAttr(
"data.ibm_compute_reserved_capacity.reservedcapacityds", "datacenter", "dal05"),
resource.TestCheckResourceAttr(
"data.ibm_compute_reserved_capacity.reservedcapacityds", "pod", "pod01"),
resource.TestCheckResourceAttr(
"data.ibm_compute_reserved_capacity.reservedcapacityds", "instances", "1"),
),
},
},
})
}

func testAccCheckIBMComputeReservedCapacitydsConfig(name string) string {
return fmt.Sprintf(`
resource "ibm_compute_reserved_capacity" "reservedcapacity" {
name = "%s"
datacenter = "dal05"
pod = "pod01"
flavor = "B1_4X16_1_YEAR_TERM"
instances = "1"
}
data "ibm_compute_reserved_capacity" "reservedcapacityds" {
name = "${ibm_compute_reserved_capacity.ReservedCapacity.name}"
}
`, name)
}
2 changes: 2 additions & 0 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func Provider() *schema.Provider {
"ibm_compute_bare_metal": dataSourceIBMComputeBareMetal(),
"ibm_compute_image_template": dataSourceIBMComputeImageTemplate(),
"ibm_compute_placement_group": dataSourceIBMComputePlacementGroup(),
"ibm_compute_reserved_capacity": dataSourceIBMComputeReservedCapacity(),
"ibm_compute_ssh_key": dataSourceIBMComputeSSHKey(),
"ibm_compute_vm_instance": dataSourceIBMComputeVmInstance(),
"ibm_container_addons": datasourceIBMContainerAddOns(),
Expand Down Expand Up @@ -536,6 +537,7 @@ func Provider() *schema.Provider {
"ibm_compute_dedicated_host": resourceIBMComputeDedicatedHost(),
"ibm_compute_monitor": resourceIBMComputeMonitor(),
"ibm_compute_placement_group": resourceIBMComputePlacementGroup(),
"ibm_compute_reserved_capacity": resourceIBMComputeReservedCapacity(),
"ibm_compute_provisioning_hook": resourceIBMComputeProvisioningHook(),
"ibm_compute_ssh_key": resourceIBMComputeSSHKey(),
"ibm_compute_ssl_certificate": resourceIBMComputeSSLCertificate(),
Expand Down
3 changes: 3 additions & 0 deletions ibm/resource_ibm_compute_autoscale_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func getModifiedVirtualGuestResource() *schema.Resource {
r := resourceIBMComputeVmInstance()
// wait_time_minutes is only used in virtual_guest resource.
delete(r.Schema, "wait_time_minutes")
delete(r.Schema, "reserved_capacity_id")
delete(r.Schema, "reserved_capacity_name")
delete(r.Schema, "reserved_instance_primary_disk")

for _, elem := range r.Schema {
elem.ForceNew = false
Expand Down
Loading

0 comments on commit a8c07e0

Please sign in to comment.