Skip to content
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

Refactor multiple pi resources with context awareness #3429

Merged
merged 3 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20211109110327-8ef3d89514c8
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20210705152127-41ca00fc9a62
github.com/IBM-Cloud/power-go-client v1.0.79
github.com/IBM-Cloud/power-go-client v1.0.85
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
github.com/IBM/appconfiguration-go-admin-sdk v0.1.0
github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ github.com/IBM-Cloud/bluemix-go v0.0.0-20211109110327-8ef3d89514c8/go.mod h1:q0f
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20210705152127-41ca00fc9a62 h1:MOkcr6qQGk4tY542ZJ1DggVh2WUP72EEyLB79llFVH8=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20210705152127-41ca00fc9a62/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.0.79 h1:k++zgWtuHjq8csmr9VnzczKBRR4ubro7jb8PUfPwADU=
github.com/IBM-Cloud/power-go-client v1.0.79/go.mod h1:YRBsrY+n1+3xMd6HzfG0VATkXZqOQktK5Yvjx9x6ACc=
github.com/IBM-Cloud/power-go-client v1.0.85 h1:5iiYEPNBgSEgTLXcfxXZmyOSKwQO8YbUJz4hBivYOV4=
github.com/IBM-Cloud/power-go-client v1.0.85/go.mod h1:60o7AE2oDi/0CtoXlQhnCbC3o1fSRgaFCJO6DscmokA=
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 Expand Up @@ -71,6 +71,7 @@ github.com/IBM/go-sdk-core/v5 v5.6.3/go.mod h1:tt/B9rxLkRtglE7pvqLuYikgCXaZFL3bt
github.com/IBM/go-sdk-core/v5 v5.6.5/go.mod h1:tt/B9rxLkRtglE7pvqLuYikgCXaZFL3btdruJaoUeek=
github.com/IBM/go-sdk-core/v5 v5.7.0/go.mod h1:+YbdhrjCHC84ls4MeBp+Hj4NZCni+tDAc0XQUqRO9Jc=
github.com/IBM/go-sdk-core/v5 v5.7.2/go.mod h1:+YbdhrjCHC84ls4MeBp+Hj4NZCni+tDAc0XQUqRO9Jc=
github.com/IBM/go-sdk-core/v5 v5.8.0/go.mod h1:+YbdhrjCHC84ls4MeBp+Hj4NZCni+tDAc0XQUqRO9Jc=
github.com/IBM/go-sdk-core/v5 v5.8.2 h1:hbT9jU5o2Gxv0CmPHK8Z1i6CpBVHcVuEajDoXNLXQqU=
github.com/IBM/go-sdk-core/v5 v5.8.2/go.mod h1:axE2JrRq79gIJTjKPBwV6gWHswvVptBjbcvvCPIxARM=
github.com/IBM/ibm-cos-sdk-go v1.3.1/go.mod h1:YLBAYobEA8bD27P7xpMwSQeNQu6W3DNBtBComXrRzRY=
Expand Down
2 changes: 1 addition & 1 deletion ibm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ func (c *Config) ClientSession() (interface{}, error) {
session.apigatewayAPI = apigatewayAPI

// POWER SYSTEMS Service
ibmpisession, err := ibmpisession.New(sess.BluemixSession.Config.IAMAccessToken, c.Region, false, 90000000000, session.bmxUserDetails.userAccount, c.Zone)
ibmpisession, err := ibmpisession.New(sess.BluemixSession.Config.IAMAccessToken, c.Region, false, session.bmxUserDetails.userAccount, c.Zone)
if err != nil {
session.ibmpiConfigErr = err
return nil, err
Expand Down
21 changes: 11 additions & 10 deletions ibm/data_source_ibm_pi_catalog_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package ibm

import (
"context"
"time"

"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"

Expand All @@ -20,7 +22,7 @@ Datasource to get the list of images that are available when a power instance is
func dataSourceIBMPICatalogImages() *schema.Resource {

return &schema.Resource{
Read: dataSourceIBMPICatalogImagesRead,
ReadContext: dataSourceIBMPICatalogImagesRead,
Schema: map[string]*schema.Schema{

helpers.PICloudInstanceId: {
Expand Down Expand Up @@ -112,14 +114,13 @@ func dataSourceIBMPICatalogImages() *schema.Resource {
}
}

func dataSourceIBMPICatalogImagesRead(d *schema.ResourceData, meta interface{}) error {

func dataSourceIBMPICatalogImagesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(ClientSession).IBMPISession()

if err != nil {
return err
return diag.FromErr(err)
}
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
includeSAP := false
if s, ok := d.GetOk("sap"); ok {
includeSAP = s.(bool)
Expand All @@ -128,11 +129,12 @@ func dataSourceIBMPICatalogImagesRead(d *schema.ResourceData, meta interface{})
if v, ok := d.GetOk("vtl"); ok {
includeVTL = v.(bool)
}
imageC := instance.NewIBMPIImageClient(sess, powerinstanceid)
stockImages, err := imageC.GetAllStockImages(powerinstanceid, includeSAP, includeVTL)
imageC := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
stockImages, err := imageC.GetAllStockImages(includeSAP, includeVTL)
if err != nil {
return err
return diag.FromErr(err)
}

images := make([]map[string]interface{}, 0)
for _, i := range stockImages.Images {
image := make(map[string]interface{})
Expand Down Expand Up @@ -187,7 +189,6 @@ func dataSourceIBMPICatalogImagesRead(d *schema.ResourceData, meta interface{})
}
d.SetId(time.Now().UTC().String())
d.Set("images", images)
d.Set(helpers.PICloudInstanceId, powerinstanceid)
return nil

}
6 changes: 3 additions & 3 deletions ibm/data_source_ibm_pi_cloud_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func dataSourceIBMPICloudConnectionRead(ctx context.Context, d *schema.ResourceD

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudConnectionName := d.Get(helpers.PICloudConnectionName).(string)
client := instance.NewIBMPICloudConnectionClient(sess, cloudInstanceID)
client := instance.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID)

// Get API does not work with name for Cloud Connection hence using GetAll (max 2)
// TODO: Uncomment Get call below when avaiable and remove GetAll
Expand All @@ -132,10 +132,10 @@ func dataSourceIBMPICloudConnectionRead(ctx context.Context, d *schema.ResourceD
// log.Printf("[DEBUG] get cloud connection failed %v", err)
// return diag.Errorf(errors.GetCloudConnectionOperationFailed, cloudConnectionName, err)
// }
cloudConnections, err := client.GetAllWithContext(ctx, cloudInstanceID)
cloudConnections, err := client.GetAll()
if err != nil {
log.Printf("[DEBUG] get cloud connections failed %v", err)
return diag.Errorf("failed to perform get cloud connections operation with error %v", err)
return diag.FromErr(err)
}
var cloudConnection *models.CloudConnection
if cloudConnections != nil {
Expand Down
8 changes: 4 additions & 4 deletions ibm/data_source_ibm_pi_cloud_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ func dataSourceIBMPICloudConnectionsRead(ctx context.Context, d *schema.Resource
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
client := st.NewIBMPICloudConnectionClient(sess, cloudInstanceID)
client := st.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID)

cloudConnections, err := client.GetAllWithContext(ctx, cloudInstanceID)
if err != nil || cloudConnections == nil {
cloudConnections, err := client.GetAll()
if err != nil {
log.Printf("[DEBUG] get cloud connections failed %v", err)
return diag.Errorf("failed to perform get cloud connections operation with error %v", err)
return diag.FromErr(err)
}

result := make([]map[string]interface{}, 0, len(cloudConnections.CloudConnections))
Expand Down
25 changes: 10 additions & 15 deletions ibm/data_source_ibm_pi_cloud_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package ibm

import (
"context"

"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"

Expand All @@ -15,7 +18,7 @@ import (
func dataSourceIBMPICloudInstance() *schema.Resource {

return &schema.Resource{
Read: dataSourceIBMPICloudInstanceRead,
ReadContext: dataSourceIBMPICloudInstanceRead,
Schema: map[string]*schema.Schema{
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Expand All @@ -24,15 +27,10 @@ func dataSourceIBMPICloudInstance() *schema.Resource {
},

// Start of Computed Attributes

"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -41,7 +39,6 @@ func dataSourceIBMPICloudInstance() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"capabilities": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -103,20 +100,18 @@ func dataSourceIBMPICloudInstance() *schema.Resource {
}
}

func dataSourceIBMPICloudInstanceRead(d *schema.ResourceData, meta interface{}) error {

func dataSourceIBMPICloudInstanceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(ClientSession).IBMPISession()

if err != nil {
return err
return diag.FromErr(err)
}

powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
cloud_instance := instance.NewIBMPICloudInstanceClient(sess, powerinstanceid)
cloud_instance_data, err := cloud_instance.Get(powerinstanceid)
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)

cloud_instance := instance.NewIBMPICloudInstanceClient(ctx, sess, cloudInstanceID)
cloud_instance_data, err := cloud_instance.Get(cloudInstanceID)
if err != nil {
return err
return diag.FromErr(err)
}

d.SetId(*cloud_instance_data.CloudInstanceID)
Expand Down
10 changes: 4 additions & 6 deletions ibm/data_source_ibm_pi_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

st "github.com/IBM-Cloud/power-go-client/clients/instance"

"github.com/IBM-Cloud/power-go-client/errors"

"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -79,13 +77,13 @@ func dataSourceIBMPIDhcpRead(ctx context.Context, d *schema.ResourceData, meta i
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
dhcpID := d.Get(PIDhcpId).(string)
dhcpID := d.Get(PIDhcpID).(string)

client := st.NewIBMPIDhcpClient(sess, cloudInstanceID)
dhcpServer, err := client.GetWithContext(ctx, dhcpID, cloudInstanceID)
client := st.NewIBMPIDhcpClient(ctx, sess, cloudInstanceID)
dhcpServer, err := client.Get(dhcpID)
if err != nil {
log.Printf("[DEBUG] get DHCP failed %v", err)
return diag.Errorf(errors.GetDhcpOperationFailed, dhcpID, err)
return diag.FromErr(err)
}

d.SetId(*dhcpServer.ID)
Expand Down
6 changes: 3 additions & 3 deletions ibm/data_source_ibm_pi_dhcps.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ func dataSourceIBMPIDhcpServersRead(ctx context.Context, d *schema.ResourceData,

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)

client := st.NewIBMPIDhcpClient(sess, cloudInstanceID)
dhcpServers, err := client.GetAllWithContext(ctx, cloudInstanceID)
client := st.NewIBMPIDhcpClient(ctx, sess, cloudInstanceID)
dhcpServers, err := client.GetAll()
if err != nil {
log.Printf("[DEBUG] get all DHCP failed %v", err)
return diag.Errorf("failed to perform get all DHCP operation with error %v", err)
return diag.FromErr(err)
}

result := make([]map[string]interface{}, 0, len(dhcpServers))
Expand Down
20 changes: 10 additions & 10 deletions ibm/data_source_ibm_pi_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package ibm

import (
"context"

"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

//"fmt"
Expand All @@ -15,7 +18,7 @@ import (
func dataSourceIBMPIImage() *schema.Resource {

return &schema.Resource{
Read: dataSourceIBMPIImagesRead,
ReadContext: dataSourceIBMPIImagesRead,
Schema: map[string]*schema.Schema{

helpers.PIImageName: {
Expand Down Expand Up @@ -66,21 +69,18 @@ func dataSourceIBMPIImage() *schema.Resource {
}
}

func dataSourceIBMPIImagesRead(d *schema.ResourceData, meta interface{}) error {

func dataSourceIBMPIImagesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(ClientSession).IBMPISession()

if err != nil {
return err
return diag.FromErr(err)
}

powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)

imageC := instance.NewIBMPIImageClient(sess, powerinstanceid)
imagedata, err := imageC.Get(d.Get(helpers.PIImageName).(string), powerinstanceid)
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)

imageC := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
imagedata, err := imageC.Get(d.Get(helpers.PIImageName).(string))
if err != nil {
return err
return diag.FromErr(err)
}

d.SetId(*imagedata.ImageID)
Expand Down
23 changes: 11 additions & 12 deletions ibm/data_source_ibm_pi_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package ibm

import (
"context"

"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

//"fmt"
Expand All @@ -21,7 +24,7 @@ Datasource to get the list of images that are available when a power instance is
func dataSourceIBMPIImages() *schema.Resource {

return &schema.Resource{
Read: dataSourceIBMPIImagesAllRead,
ReadContext: dataSourceIBMPIImagesAllRead,
Schema: map[string]*schema.Schema{

helpers.PICloudInstanceId: {
Expand Down Expand Up @@ -72,27 +75,23 @@ func dataSourceIBMPIImages() *schema.Resource {
}
}

func dataSourceIBMPIImagesAllRead(d *schema.ResourceData, meta interface{}) error {

func dataSourceIBMPIImagesAllRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(ClientSession).IBMPISession()

if err != nil {
return err
return diag.FromErr(err)
}

powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)

imageC := instance.NewIBMPIImageClient(sess, powerinstanceid)

imagedata, err := imageC.GetAll(powerinstanceid)
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)

imageC := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
imagedata, err := imageC.GetAll()
if err != nil {
return err
return diag.FromErr(err)
}

var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)
_ = d.Set("image_info", flattenStockImages(imagedata.Images))
d.Set("image_info", flattenStockImages(imagedata.Images))

return nil

Expand Down
4 changes: 2 additions & 2 deletions ibm/data_source_ibm_pi_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ func dataSourceIBMPIInstancesRead(ctx context.Context, d *schema.ResourceData, m
return diag.FromErr(err)
}

powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)

powerC := instance.NewIBMPIInstanceClient(ctx, sess, powerinstanceid)
powerC := instance.NewIBMPIInstanceClient(ctx, sess, cloudInstanceID)
powervmdata, err := powerC.Get(d.Get(helpers.PIInstanceName).(string))

if err != nil {
Expand Down
Loading