Skip to content

Commit

Permalink
[Resource] [Datasource] Power add VSN functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Kita committed Nov 22, 2024
1 parent 7f0333b commit f12ea95
Show file tree
Hide file tree
Showing 21 changed files with 1,237 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.5
require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20240926024252-81b3928fd062
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113
github.com/IBM-Cloud/power-go-client v1.8.3
github.com/IBM-Cloud/power-go-client v1.9.0-beta8
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
github.com/IBM/appconfiguration-go-admin-sdk v0.3.0
github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/IBM-Cloud/bluemix-go v0.0.0-20240926024252-81b3928fd062/go.mod h1:/7h
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113 h1:f2Erqfea1dKpaTFagTJM6W/wnD3JGq/Vn9URh8nuRwk=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.5.3/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs=
github.com/IBM-Cloud/power-go-client v1.8.3 h1:QsBuIS6KvKsiEpe0yiHYKhWgXlqkcJ7XqFHtATj8Yh4=
github.com/IBM-Cloud/power-go-client v1.8.3/go.mod h1:UDyXeIKEp6r7yWUXYu3r0ZnFSlNZ2YeQTHwM2Tmlgv0=
github.com/IBM-Cloud/power-go-client v1.9.0-beta8 h1:wLRzZw/jOg8mGfGFCXutKrymZHwGaih/EpZrHTSp8Ko=
github.com/IBM-Cloud/power-go-client v1.9.0-beta8/go.mod h1:UDyXeIKEp6r7yWUXYu3r0ZnFSlNZ2YeQTHwM2Tmlgv0=
github.com/IBM-Cloud/softlayer-go v1.0.5-tf h1:koUAyF9b6X78lLLruGYPSOmrfY2YcGYKOj/Ug9nbKNw=
github.com/IBM-Cloud/softlayer-go v1.0.5-tf/go.mod h1:6HepcfAXROz0Rf63krk5hPZyHT6qyx2MNvYyHof7ik4=
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca h1:crniVcf+YcmgF03NmmfonXwSQ73oJF+IohFYBwknMxs=
Expand Down
7 changes: 7 additions & 0 deletions ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ var (
Pi_spp_placement_group_id string
Pi_storage_connection string
Pi_target_storage_tier string
Pi_virtual_serial_number string
Pi_volume_clone_task_id string
Pi_volume_group_id string
Pi_volume_group_name string
Expand Down Expand Up @@ -1289,6 +1290,12 @@ func init() {
fmt.Println("[INFO] Set the environment variable PI_VOLUME_CLONE_TASK_ID for testing Pi_volume_clone_task_id resource else it is set to default value 'terraform-test-volume-clone-task-id'")
}

Pi_virtual_serial_number = os.Getenv("PI_VIRTUAL_SERIAL_NUMBER")
if Pi_volume_clone_task_id == "" {
Pi_volume_clone_task_id = "terraform_test_power"
fmt.Println("[INFO] Set the environment variable PI_VIRTUAL_SERIAL_NUMBER for testing ibm_pi_virtual_serial_number data source else it is set to default value 'terraform-test-power'")
}

Pi_resource_group_id = os.Getenv("PI_RESOURCE_GROUP_ID")
if Pi_resource_group_id == "" {
Pi_resource_group_id = ""
Expand Down
3 changes: 3 additions & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ func Provider() *schema.Provider {
"ibm_pi_storage_types_capacity": power.DataSourceIBMPIStorageTypesCapacity(),
"ibm_pi_system_pools": power.DataSourceIBMPISystemPools(),
"ibm_pi_tenant": power.DataSourceIBMPITenant(),
"ibm_pi_virtual_serial_number": power.DataSourceIBMPIVirtualSerialNumber(),
"ibm_pi_virtual_serial_numbers": power.DataSourceIBMPIVirtualSerialNumbers(),
"ibm_pi_volume_clone": power.DataSourceIBMPIVolumeClone(),
"ibm_pi_volume_flash_copy_mappings": power.DataSourceIBMPIVolumeFlashCopyMappings(),
"ibm_pi_volume_group_details": power.DataSourceIBMPIVolumeGroupDetails(),
Expand Down Expand Up @@ -1342,6 +1344,7 @@ func Provider() *schema.Provider {
"ibm_pi_shared_processor_pool": power.ResourceIBMPISharedProcessorPool(),
"ibm_pi_snapshot": power.ResourceIBMPISnapshot(),
"ibm_pi_spp_placement_group": power.ResourceIBMPISPPPlacementGroup(),
"ibm_pi_virtual_serial_number": power.ResourceIBMPIVirtualSerialNumber(),
"ibm_pi_volume_attach": power.ResourceIBMPIVolumeAttach(),
"ibm_pi_volume_clone": power.ResourceIBMPIVolumeClone(),
"ibm_pi_volume_group_action": power.ResourceIBMPIVolumeGroupAction(),
Expand Down
22 changes: 22 additions & 0 deletions ibm/service/power/data_source_ibm_pi_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,25 @@ func DataSourceIBMPIInstance() *schema.Resource {
Description: "The virtual cores that are assigned to the instance.",
Type: schema.TypeInt,
},
Attr_VirtualSerialNumber: {
Computed: true,
Description: "Virtual Serial Number information",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_Description: {
Computed: true,
Description: "Description of the Virtual Serial Number",
Type: schema.TypeString,
},
Attr_Serial: {
Computed: true,
Description: "Virtual serial number.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
Attr_Volumes: {
Computed: true,
Description: "List of volume IDs that are attached to the instance.",
Expand Down Expand Up @@ -311,6 +330,9 @@ func dataSourceIBMPIInstancesRead(ctx context.Context, d *schema.ResourceData, m
if powervmdata.Fault != nil {
d.Set(Attr_Fault, flattenPvmInstanceFault(powervmdata.Fault))
}
if powervmdata.VirtualSerialNumber != nil {
d.Set(Attr_VirtualSerialNumber, flattenVirtualSerialNumberToList(powervmdata.VirtualSerialNumber))
}

return nil
}
23 changes: 23 additions & 0 deletions ibm/service/power/data_source_ibm_pi_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ func DataSourceIBMPIInstances() *schema.Resource {
Description: "The virtual cores that are assigned to the instance.",
Type: schema.TypeInt,
},
Attr_VirtualSerialNumber: {
Computed: true,
Description: "Virtual Serial Number information",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_Description: {
Computed: true,
Description: "Description of the Virtual Serial Number",
Type: schema.TypeString,
},
Attr_Serial: {
Computed: true,
Description: "Virtual serial number.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
},
},
Type: schema.TypeList,
Expand Down Expand Up @@ -290,6 +309,10 @@ func flattenPvmInstances(list []*models.PVMInstanceReference, meta interface{})
l[Attr_Fault] = flattenPvmInstanceFault(i.Fault)
}

if i.VirtualSerialNumber != nil {
l[Attr_VirtualSerialNumber] = flattenVirtualSerialNumberToList(i.VirtualSerialNumber)
}

result = append(result, l)
}
return result
Expand Down
73 changes: 73 additions & 0 deletions ibm/service/power/data_source_ibm_pi_virtual_serial_number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

import (
"context"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// Datasource to get a virtual serial number in a power instance
func DataSourceIBMPIVirtualSerialNumber() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIVirtualSerialNumberRead,
Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_Serial: {
Description: "Virtual serial number.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Attributes
Attr_Description: {
Computed: true,
Description: "Description of virtual serial number.",
Type: schema.TypeString,
},
Attr_InstanceID: {
Computed: true,
Description: "ID of PVM instance virtual serial number is attached to.",
Type: schema.TypeString,
},
},
}
}

func dataSourceIBMPIVirtualSerialNumberRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
client := instance.NewIBMPIVSNClient(ctx, sess, cloudInstanceID)

vsnInput := d.Get(Arg_Serial).(string)
virtualSerialNumberData, err := client.Get(vsnInput)
if err != nil {
return diag.FromErr(err)
}

id := *virtualSerialNumberData.Serial
d.SetId(id)
d.Set(Attr_Description, virtualSerialNumberData.Description)
if virtualSerialNumberData.PvmInstanceID != nil {
d.Set(Attr_InstanceID, virtualSerialNumberData.PvmInstanceID)
}

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

package power_test

import (
"fmt"
"testing"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"

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

func TestAccIBMPIVirtualSerialNumber(t *testing.T) {
vsnData := "data.ibm_pi_virtual_serial_number.testacc_virtual_serial_number"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPIVirtualSerialNumberConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(vsnData, "id"),
),
},
},
})
}

func testAccCheckIBMPIVirtualSerialNumberConfig() string {
return fmt.Sprintf(`
data "ibm_pi_virtual_serial_number" "testacc_virtual_serial_number" {
pi_cloud_instance_id = "%s"
pi_serial = "%s"
}`, acc.Pi_cloud_instance_id, acc.Pi_virtual_serial_number)
}
96 changes: 96 additions & 0 deletions ibm/service/power/data_source_ibm_pi_virtual_serial_numbers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

import (
"context"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// Datasource to list Cloud Connections in a power instance
func DataSourceIBMPIVirtualSerialNumbers() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIVirtualSerialNumbersRead,
Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_InstanceID: {
Description: "ID of PVM instance to get virtual serial number attached to.",
Optional: true,
Type: schema.TypeString,
},

// Attributes
Attr_VirtualSerialNumbers: {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_Description: {
Computed: true,
Description: "Description of virtual serial number.",
Type: schema.TypeString,
},
Attr_InstanceID: {
Computed: true,
Description: "ID of PVM instance virtual serial number is attached to.",
Type: schema.TypeString,
},
Attr_Serial: {
Computed: true,
Description: "Virtual Serial Number.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
},
}
}

func dataSourceIBMPIVirtualSerialNumbersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)

client := instance.NewIBMPIVSNClient(ctx, sess, cloudInstanceID)

var pvmInstanceID string
if instanceID, ok := d.GetOk(Arg_InstanceID); ok {
pvmInstanceID = instanceID.(string)
}

vsns, err := client.GetAll(&pvmInstanceID)
if err != nil {
return diag.FromErr(err)
}

vsnMapList := make([]map[string]interface{}, 0)
for _, vsn := range vsns {
v := make(map[string]interface{})
v[Attr_Description] = vsn.Description
v[Attr_InstanceID] = vsn.PvmInstanceID
v[Attr_Serial] = vsn.Serial
vsnMapList = append(vsnMapList, v)
}

var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)
d.Set(Attr_VirtualSerialNumbers, vsnMapList)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power_test

import (
"fmt"
"testing"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"

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

func TestAccIBMPIVirtualSerialNumbers(t *testing.T) {
vsnData := "data.ibm_pi_virtual_serial_numbers.testacc_virtual_serial_numbers"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPIVirtualSerialNumbersConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(vsnData, "id"),
resource.TestCheckResourceAttrSet(vsnData, "virtual_serial_numbers.#"),
),
},
},
})
}

func testAccCheckIBMPIVirtualSerialNumbersConfig() string {
return fmt.Sprintf(`
data "ibm_pi_virtual_serial_numbers" "testacc_virtual_serial_numbers" {
pi_cloud_instance_id = "%s"
}`, acc.Pi_cloud_instance_id)
}
Loading

0 comments on commit f12ea95

Please sign in to comment.