forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for classic reserved capacity and instance
- Loading branch information
Showing
9 changed files
with
808 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.