Skip to content
Open
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
24 changes: 17 additions & 7 deletions ibm/service/vpc/data_source_ibm_is_virtual_endpoint_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
package vpc

import (
"context"
"fmt"
"log"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceIBMISEndpointGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISEndpointGatewayRead,
Importer: &schema.ResourceImporter{},
ReadContext: dataSourceIBMISEndpointGatewayRead,
Importer: &schema.ResourceImporter{},

Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayName: {
Expand Down Expand Up @@ -176,10 +178,12 @@ func DataSourceIBMISEndpointGateway() *schema.Resource {
}

func dataSourceIBMISEndpointGatewayRead(
d *schema.ResourceData, meta interface{}) error {
context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return err
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_is_virtual_endpoint_gateway", "read", "initialize-client")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

name := d.Get(isVirtualEndpointGatewayName).(string)
Expand All @@ -189,12 +193,18 @@ func dataSourceIBMISEndpointGatewayRead(

results, response, err := sess.ListEndpointGateways(options)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching endpoint gateways %s\n%s", err, response)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ListEndpointGateways failed: %s\n%s", err.Error(), response), "(Data) ibm_is_virtual_endpoint_gateway", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
}
allrecs := results.EndpointGateways

if len(allrecs) == 0 {
return fmt.Errorf("[ERROR] No Virtual Endpoints Gateway found with given name %s", name)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ListEndpointGateways failed: %s", err.Error()), "(Data) ibm_is_virtual_endpoint_gateway", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
result := allrecs[0]
d.SetId(*result.ID)
Expand All @@ -205,7 +215,7 @@ func dataSourceIBMISEndpointGatewayRead(
d.Set(isVirtualEndpointGatewayCreatedAt, result.CreatedAt.String())
d.Set(isVirtualEndpointGatewayLifecycleState, result.LifecycleState)
if err := d.Set(isVirtualEndpointGatewayLifecycleReasons, resourceEGWFlattenLifecycleReasons(result.LifecycleReasons)); err != nil {
return fmt.Errorf("[ERROR] Error setting lifecycle_reasons: %s", err)
return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_is_virtual_endpoint_gateway", "read", "lifecycle_reasons-to-map").GetDiag()
}
d.Set(isVirtualEndpointGatewayResourceType, result.ResourceType)
d.Set(isVirtualEndpointGatewayIPs, flattenIPs(result.Ips))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
package vpc

import (
"context"
"fmt"
"log"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceIBMISEndpointGatewayIPs() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISEndpointGatewayIPsRead,
Importer: &schema.ResourceImporter{},
ReadContext: dataSourceIBMISEndpointGatewayIPsRead,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayID: {
Type: schema.TypeString,
Expand Down Expand Up @@ -87,10 +90,12 @@ func DataSourceIBMISEndpointGatewayIPs() *schema.Resource {
}
}

func dataSourceIBMISEndpointGatewayIPsRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceIBMISEndpointGatewayIPsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return err
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_is_virtual_endpoint_gateways_ips", "read", "initialize-client")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
gatewayID := d.Get(isVirtualEndpointGatewayID).(string)

Expand All @@ -103,7 +108,9 @@ func dataSourceIBMISEndpointGatewayIPsRead(d *schema.ResourceData, meta interfac
}
result, response, err := sess.ListEndpointGatewayIps(options)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching endpoint gateway ips %s\n%s", err, response)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("[ERROR] Error fetching endpoint gateway ips %s\n%s", err, response), "(Data) ibm_is_virtual_endpoint_gateways_ips", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
start = flex.GetNext(result.Next)
allrecs = append(allrecs, result.Ips...)
Expand Down
17 changes: 11 additions & 6 deletions ibm/service/vpc/data_source_ibm_is_virtual_endpoint_gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
package vpc

import (
"context"
"fmt"
"log"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -20,8 +22,8 @@ const (

func DataSourceIBMISEndpointGateways() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISEndpointGatewaysRead,
Importer: &schema.ResourceImporter{},
ReadContext: dataSourceIBMISEndpointGatewaysRead,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"resource_group": {
Type: schema.TypeString,
Expand Down Expand Up @@ -198,12 +200,13 @@ func DataSourceIBMISEndpointGateways() *schema.Resource {
}
}

func dataSourceIBMISEndpointGatewaysRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceIBMISEndpointGatewaysRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return err
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_is_virtual_endpoint_gateways", "read", "initialize-client")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

start := ""
allrecs := []vpcv1.EndpointGateway{}
options := sess.NewListEndpointGatewaysOptions()
Expand All @@ -222,7 +225,9 @@ func dataSourceIBMISEndpointGatewaysRead(d *schema.ResourceData, meta interface{
}
result, response, err := sess.ListEndpointGateways(options)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching endpoint gateways %s\n%s", err, response)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("[ERROR] Error fetching endpoint gateways %s\n%s", err, response), "(Data) ibm_is_virtual_endpoint_gateways", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
start = flex.GetNext(result.Next)
allrecs = append(allrecs, result.EndpointGateways...)
Expand Down
84 changes: 52 additions & 32 deletions ibm/service/vpc/resource_ibm_is_virtual_endpoint_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -51,12 +52,12 @@ func ResourceIBMISEndpointGateway() *schema.Resource {
targetNameFmt := fmt.Sprintf("%s.0.%s", isVirtualEndpointGatewayTarget, isVirtualEndpointGatewayTargetName)
targetCRNFmt := fmt.Sprintf("%s.0.%s", isVirtualEndpointGatewayTarget, isVirtualEndpointGatewayTargetCRN)
return &schema.Resource{
Create: resourceIBMisVirtualEndpointGatewayCreate,
Read: resourceIBMisVirtualEndpointGatewayRead,
Update: resourceIBMisVirtualEndpointGatewayUpdate,
Delete: resourceIBMisVirtualEndpointGatewayDelete,
Exists: resourceIBMisVirtualEndpointGatewayExists,
Importer: &schema.ResourceImporter{},
CreateContext: resourceIBMisVirtualEndpointGatewayCreate,
ReadContext: resourceIBMisVirtualEndpointGatewayRead,
UpdateContext: resourceIBMisVirtualEndpointGatewayUpdate,
DeleteContext: resourceIBMisVirtualEndpointGatewayDelete,
Exists: resourceIBMisVirtualEndpointGatewayExists,
Importer: &schema.ResourceImporter{},

CustomizeDiff: customdiff.All(
customdiff.Sequence(
Expand Down Expand Up @@ -308,10 +309,12 @@ func ResourceIBMISEndpointGatewayValidator() *validate.ResourceValidator {
return &ibmEndpointGatewayResourceValidator
}

func resourceIBMisVirtualEndpointGatewayCreate(d *schema.ResourceData, meta interface{}) error {
func resourceIBMisVirtualEndpointGatewayCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return err
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "create", "initialize-client")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

name := d.Get(isVirtualEndpointGatewayName).(string)
Expand Down Expand Up @@ -377,8 +380,9 @@ func resourceIBMisVirtualEndpointGatewayCreate(d *schema.ResourceData, meta inte
}
endpointGateway, response, err := sess.CreateEndpointGateway(opt)
if err != nil {
log.Printf("Create Endpoint Gateway failed: %v", response)
return fmt.Errorf("[ERROR] Create Endpoint Gateway failed %s\n%s", err, response)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("[ERROR] Create Endpoint Gateway failed %s\n%s", err, response), "ibm_is_virtual_endpoint_gateway", "create")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

d.SetId(*endpointGateway.ID)
Expand All @@ -392,7 +396,9 @@ func resourceIBMisVirtualEndpointGatewayCreate(d *schema.ResourceData, meta inte
endpointGateway, response, err := sess.GetEndpointGateway(opt)
if err != nil {
log.Printf("Get Endpoint Gateway failed: %v", response)
return fmt.Errorf("[ERROR] Get Endpoint Gateway failed %s\n%s", err, response)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetEndpointGateway failed: %s", err.Error()), "ibm_is_virtual_endpoint_gateway", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
if len(endpointGateway.LifecycleReasons) > 0 {
if endpointGateway.LifecycleReasons[0].Code != nil && strings.Compare(*endpointGateway.LifecycleReasons[0].Code, "access_pending") == 0 {
Expand All @@ -401,10 +407,10 @@ func resourceIBMisVirtualEndpointGatewayCreate(d *schema.ResourceData, meta inte
}
}
if !isAccessPending {
return err
return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "create", "access-pending").GetDiag()
}
} else {
return err
return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "create", "not-private-path-service-gateway").GetDiag()
}
}

Expand All @@ -427,13 +433,15 @@ func resourceIBMisVirtualEndpointGatewayCreate(d *schema.ResourceData, meta inte
}
}

return resourceIBMisVirtualEndpointGatewayRead(d, meta)
return resourceIBMisVirtualEndpointGatewayRead(context, d, meta)
}

func resourceIBMisVirtualEndpointGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceIBMisVirtualEndpointGatewayUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return err
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "update", "initialize-client")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

// create option
Expand All @@ -451,7 +459,7 @@ func resourceIBMisVirtualEndpointGatewayUpdate(d *schema.ResourceData, meta inte
_, response, err := sess.UpdateEndpointGateway(opt)
if err != nil {
log.Printf("Update Endpoint Gateway failed: %v", response)
return fmt.Errorf("Update Endpoint Gateway failed : %s\n%s", err, response)
return flex.TerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "update").GetDiag()
}
id := d.Id()
var remove, add []string
Expand All @@ -468,11 +476,13 @@ func resourceIBMisVirtualEndpointGatewayUpdate(d *schema.ResourceData, meta inte
createSecurityGroupTargetBindingOptions.ID = &id
_, response, err := sess.CreateSecurityGroupTargetBinding(createSecurityGroupTargetBindingOptions)
if err != nil {
return fmt.Errorf("Error while creating Security Group Target Binding %s\n%s", err, response)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error while creating Security Group Target Binding %s\n%s", err, response), "ibm_is_virtual_endpoint_gateway", "update")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
_, err = isWaitForVirtualEndpointGatewayAvailable(sess, d.Id(), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
return flex.TerraformErrorf(err, fmt.Sprintf("isWaitForVirtualEndpointGatewayAvailable failed: %s", err.Error()), "ibm_is_virtual_endpoint_gateway", "update").GetDiag()
}
}
}
Expand All @@ -487,16 +497,18 @@ func resourceIBMisVirtualEndpointGatewayUpdate(d *schema.ResourceData, meta inte
if response != nil && response.StatusCode == 404 {
continue
}
return fmt.Errorf("Error Getting Security Group Target for this endpoint gateway (%s): %s\n%s", sgId, err, response)
return flex.TerraformErrorf(err, fmt.Sprintf("Error Getting Security Group Target for this endpoint gateway (%s): %s\n%s", sgId, err, response), "ibm_is_virtual_endpoint_gateway", "update").GetDiag()

}
deleteSecurityGroupTargetBindingOptions := sess.NewDeleteSecurityGroupTargetBindingOptions(sgId, id)
response, err = sess.DeleteSecurityGroupTargetBinding(deleteSecurityGroupTargetBindingOptions)
if err != nil {
return fmt.Errorf("Error Deleting Security Group Target for this endpoint gateway : %s\n%s", err, response)
return flex.TerraformErrorf(err, fmt.Sprintf("Error Deleting Security Group Target for this endpoint gateway : %s\n%s", err, response), "ibm_is_virtual_endpoint_gateway", "update").GetDiag()

}
_, err = isWaitForVirtualEndpointGatewayAvailable(sess, d.Id(), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
return flex.TerraformErrorf(err, fmt.Sprintf("isWaitForVirtualEndpointGatewayAvailable failed: %s", err.Error()), "ibm_is_virtual_endpoint_gateway", "update").GetDiag()
}
}
}
Expand All @@ -506,14 +518,16 @@ func resourceIBMisVirtualEndpointGatewayUpdate(d *schema.ResourceData, meta inte
opt := sess.NewGetEndpointGatewayOptions(d.Id())
endpointGateway, response, err := sess.GetEndpointGateway(opt)
if err != nil {
return fmt.Errorf("[ERROR] Error getting VPE: %s\n%s", err, response)
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("[ERROR] Error getting VPE: %s\n%s", err, response), "ibm_is_virtual_endpoint_gateway", "update", "get-vpe").GetDiag()

}
if d.HasChange(isVirtualEndpointGatewayTags) {
oldList, newList := d.GetChange(isVirtualEndpointGatewayTags)
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, *endpointGateway.CRN, "", isUserTagType)
if err != nil {
log.Printf(
"Error on update of VPE (%s) tags: %s", d.Id(), err)

}
}

Expand All @@ -526,13 +540,15 @@ func resourceIBMisVirtualEndpointGatewayUpdate(d *schema.ResourceData, meta inte
}
}
}
return resourceIBMisVirtualEndpointGatewayRead(d, meta)
return resourceIBMisVirtualEndpointGatewayRead(context, d, meta)
}

func resourceIBMisVirtualEndpointGatewayRead(d *schema.ResourceData, meta interface{}) error {
func resourceIBMisVirtualEndpointGatewayRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return err
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "read", "initialize-client")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
// read option
opt := sess.NewGetEndpointGatewayOptions(d.Id())
Expand All @@ -543,14 +559,14 @@ func resourceIBMisVirtualEndpointGatewayRead(d *schema.ResourceData, meta interf
return nil
}
log.Printf("Get Endpoint Gateway failed: %v", response)
return fmt.Errorf("[ERROR] Get Endpoint Gateway failed %s\n%s", err, response)
return flex.TerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "read").GetDiag()
}
d.Set(isVirtualEndpointGatewayName, endpointGateway.Name)
d.Set(isVirtualEndpointGatewayHealthState, endpointGateway.HealthState)
d.Set(isVirtualEndpointGatewayCreatedAt, endpointGateway.CreatedAt.String())
d.Set(isVirtualEndpointGatewayLifecycleState, endpointGateway.LifecycleState)
if err := d.Set(isVirtualEndpointGatewayLifecycleReasons, resourceEGWFlattenLifecycleReasons(endpointGateway.LifecycleReasons)); err != nil {
return fmt.Errorf("[ERROR] Error setting lifecycle_reasons: %s", err)
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("[ERROR] Error setting lifecycle_reasons: %s", err), "ibm_is_virtual_endpoint_gateway", "read", "set-lifecycle-reasons").GetDiag()
}
d.Set(isVirtualEndpointGatewayAllowDnsResolutionBinding, endpointGateway.AllowDnsResolutionBinding)
d.Set(isVirtualEndpointGatewayResourceType, endpointGateway.ResourceType)
Expand Down Expand Up @@ -644,21 +660,25 @@ func isVirtualEndpointGatewayRefreshFunc(sess *vpcv1.VpcV1, endPointGatewayId st
}
}

func resourceIBMisVirtualEndpointGatewayDelete(d *schema.ResourceData, meta interface{}) error {
func resourceIBMisVirtualEndpointGatewayDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return err
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "delete", "initialize-client")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

opt := sess.NewDeleteEndpointGatewayOptions(d.Id())
response, err := sess.DeleteEndpointGateway(opt)
if err != nil {
log.Printf("Delete Endpoint Gateway failed: %v", response)
return fmt.Errorf("Delete Endpoint Gateway failed : %s\n%s", err, response)
return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_is_virtual_endpoint_gateway", "delete", "sep-id-parts").GetDiag()
}
_, err = isWaitForEGWDelete(sess, d, d.Id())
if err != nil {
return err
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("DeleteEndpointGateway failed: %s", err.Error()), "ibm_is_virtual_endpoint_gateway", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
return nil
}
Expand Down
Loading
Loading