-
Notifications
You must be signed in to change notification settings - Fork 674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Resource] [Datasource] Power add VSN functionality to datasources an… #5854
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
73 changes: 73 additions & 0 deletions
73
ibm/service/power/data_source_ibm_pi_virtual_serial_number.go
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,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
37
ibm/service/power/data_source_ibm_pi_virtual_serial_number_test.go
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,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
96
ibm/service/power/data_source_ibm_pi_virtual_serial_numbers.go
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,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 virtual serial numbers 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 | ||
} |
37 changes: 37 additions & 0 deletions
37
ibm/service/power/data_source_ibm_pi_virtual_serial_numbers_test.go
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,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) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this attr looks like redundant as it is same as
Arg_InstanceID
isn't?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be only when
Arg_InstanceID
is supplied. However, it is needed whenever the argument is not supplied as it will be a list of virtual serial numbers with different values for InstanceID.