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

Add Custom Endpoints to Logs Routing Service #5961

Merged
merged 2 commits into from
Feb 4, 2025
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
3 changes: 3 additions & 0 deletions examples/ibm-logs-routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ data "ibm_logs_router_targets" "logs_router_targets_instance" {

### The Logs Routing URL can be set in endpoints.json

You can declare the service endpoints in a JSON file and either reference this file in your provider block by using the `endpoints_file_path` argument, or export the path to your file with the `IBMCLOUD_ENDPOINTS_FILE_PATH` or `IC_ENDPOINTS_FILE_PATH` environment variable.
To use the provided endpoints file, set the visibility to either `public` or `pivate` by using the `IC_VISIBILITY` or `IBMCLOUD_VISIBILITY` environment variable, or by setting the `visibility` field in your provider block.

**Example**:

```json
Expand Down
9 changes: 8 additions & 1 deletion examples/ibm-logs-routing/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
region = "us-south"
region = "us-east"
}

provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
region = "eu-de"
alias = "provider-eu-de"
}

// Provision logs_router_tenant resource instance
Expand All @@ -27,6 +33,7 @@ resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
}

resource "ibm_logs_router_tenant" "logs_router_tenant_instance_eu_de" {
provider = ibm.provider-eu-de
name = "eu-de-tenant"
region = "eu-de"
targets {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/IBM/ibm-hpcs-uko-sdk v0.0.20-beta
github.com/IBM/keyprotect-go-client v0.15.1
github.com/IBM/logs-go-sdk v0.4.0
github.com/IBM/logs-router-go-sdk v1.0.5
github.com/IBM/logs-router-go-sdk v1.0.7
github.com/IBM/mqcloud-go-sdk v0.2.0
github.com/IBM/networking-go-sdk v0.51.1
github.com/IBM/platform-services-go-sdk v0.73.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ github.com/IBM/logs-go-sdk v0.4.0 h1:CyUjm19EUtcJjf4mxsj6Rc7gkZDT8JEY5rLUIz8Eoag
github.com/IBM/logs-go-sdk v0.4.0/go.mod h1:yv/GCXC4/p+MZEeXl4xjZAOMvDAVRwu61WyHZFKFXQM=
github.com/IBM/logs-router-go-sdk v1.0.5 h1:r0kC1+HfmSeQCD6zQTUp4PDI/zp4Ueo1Zo19ipHuNlw=
github.com/IBM/logs-router-go-sdk v1.0.5/go.mod h1:tCN2vFgu5xG0ob9iJcxi5M4bJ6mWmu3nhmRPnvlwev0=
github.com/IBM/logs-router-go-sdk v1.0.7 h1:uQjQAAcQdo3XvhY6MC7HakhZaXIUsGfUmKj2d5vkjnY=
github.com/IBM/logs-router-go-sdk v1.0.7/go.mod h1:tCN2vFgu5xG0ob9iJcxi5M4bJ6mWmu3nhmRPnvlwev0=
github.com/IBM/mqcloud-go-sdk v0.2.0 h1:QOWk8ZGk0QfIL0MOGTKzNdM3Qe0Hk+ifAFtNSFQo5HU=
github.com/IBM/mqcloud-go-sdk v0.2.0/go.mod h1:VZQKMtqmcdXKhmLhLiPuS/UHMs/5yo2tA/nD83cQt9E=
github.com/IBM/networking-go-sdk v0.51.1 h1:xRlDFxSMejMmJ7JPFZ9cflL86uJpbqUzuL/1D/pP7/g=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func dataSourceIBMLogsRouterTargetsRead(context context.Context, d *schema.Resou
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
ibmCloudLogsRoutingClient, _, err = updateClientURLWithEndpoint(ibmCloudLogsRoutingClient, d)

listTenantTargetsOptions := &ibmcloudlogsroutingv0.ListTenantTargetsOptions{}

Expand All @@ -131,7 +132,7 @@ func dataSourceIBMLogsRouterTargetsRead(context context.Context, d *schema.Resou
listTenantTargetsOptions.SetRegion(d.Get("region").(string))
}

targetTypeCollection, _, err := ibmCloudLogsRoutingClient.ListTenantTargetsWithContext(context, listTenantTargetsOptions)
targetTypeCollection, _, err := ibmCloudLogsRoutingClient.ListTenantTargetsWithContextEndpoint(context, listTenantTargetsOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ListTenantTargetsWithContext failed: %s", err.Error()), "(Data) ibm_logs_router_targets", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func dataSourceIBMLogsRouterTenantsRead(context context.Context, d *schema.Resou
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
ibmCloudLogsRoutingClient, _, err = updateClientURLWithEndpoint(ibmCloudLogsRoutingClient, d)

listTenantsOptions := &ibmcloudlogsroutingv0.ListTenantsOptions{}

Expand All @@ -162,7 +163,7 @@ func dataSourceIBMLogsRouterTenantsRead(context context.Context, d *schema.Resou
listTenantsOptions.SetRegion(d.Get("region").(string))
}

tenantCollection, _, err := ibmCloudLogsRoutingClient.ListTenantsWithContext(context, listTenantsOptions)
tenantCollection, _, err := ibmCloudLogsRoutingClient.ListTenantsWithContextEndpoint(context, listTenantsOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ListTenantsWithContext failed: %s", err.Error()), "(Data) ibm_logs_router_tenants", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
Expand Down
28 changes: 16 additions & 12 deletions ibm/service/logsrouting/resource_ibm_logs-router_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func resourceIBMLogsRouterTenantCreate(context context.Context, d *schema.Resour
return tfErr.GetDiag()
}

ibmCloudLogsRoutingClient, _, err = updateClientURLWithEndpoint(ibmCloudLogsRoutingClient, d)
createTenantOptions := &ibmcloudlogsroutingv0.CreateTenantOptions{}

createTenantOptions.SetName(d.Get("name").(string))
Expand All @@ -209,7 +210,7 @@ func resourceIBMLogsRouterTenantCreate(context context.Context, d *schema.Resour
}
createTenantOptions.SetTargets(targets)

tenant, _, err := ibmCloudLogsRoutingClient.CreateTenantWithContext(context, createTenantOptions)
tenant, _, err := ibmCloudLogsRoutingClient.CreateTenantWithContextEndpoint(context, createTenantOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("CreateTenantWithContext failed: %s", err.Error()), "ibm_logs_router_tenant", "create")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
Expand All @@ -229,13 +230,14 @@ func resourceIBMLogsRouterTenantRead(context context.Context, d *schema.Resource
return tfErr.GetDiag()
}

ibmCloudLogsRoutingClient, _, err = updateClientURLWithEndpoint(ibmCloudLogsRoutingClient, d)
getTenantDetailOptions := &ibmcloudlogsroutingv0.GetTenantDetailOptions{}

tenantId := strfmt.UUID(d.Id())
getTenantDetailOptions.SetTenantID(&tenantId)
getTenantDetailOptions.SetRegion(d.Get("region").(string))

tenant, response, err := ibmCloudLogsRoutingClient.GetTenantDetailWithContext(context, getTenantDetailOptions)
tenant, response, err := ibmCloudLogsRoutingClient.GetTenantDetailWithContextEndpoint(context, getTenantDetailOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
Expand Down Expand Up @@ -345,6 +347,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
ibmCloudLogsRoutingClient, _, err = updateClientURLWithEndpoint(ibmCloudLogsRoutingClient, d)

updateTenantOptions := &ibmcloudlogsroutingv0.UpdateTenantOptions{}

Expand Down Expand Up @@ -462,7 +465,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
updateTenantOptions.TenantPatch["name"] = nil
}

_, _, err = ibmCloudLogsRoutingClient.UpdateTenantWithContext(context, updateTenantOptions)
_, _, err = ibmCloudLogsRoutingClient.UpdateTenantWithContextEndpoint(context, updateTenantOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("UpdateTenantWithContext failed: %s", err.Error()), "ibm_logs_router_tenant", "update")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
Expand Down Expand Up @@ -493,9 +496,9 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
_, newCRN := d.GetChange("targets.0.log_sink_crn")
if crn, ok := newCRN.(string); ok {
if strings.Contains(crn, ":logs:") {
_, _, err = ibmCloudLogsRoutingClient.UpdateLogsTargetWithContext(context, updateTarget0Options)
_, _, err = ibmCloudLogsRoutingClient.UpdateLogsTargetWithContextEndpoint(context, updateTarget0Options)
} else {
_, _, err = ibmCloudLogsRoutingClient.UpdateTargetWithContext(context, updateTarget0Options)
_, _, err = ibmCloudLogsRoutingClient.UpdateTargetWithContextEndpoint(context, updateTarget0Options)
}
}

Expand All @@ -507,7 +510,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
}

if target1Create {
_, _, err := ibmCloudLogsRoutingClient.CreateTargetWithContext(context, createTarget1Options)
_, _, err := ibmCloudLogsRoutingClient.CreateTargetWithContextEndpoint(context, createTarget1Options)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("CreateTargetWithContext failed: %s", err.Error()), "ibm_logs_router_target", "create")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
Expand Down Expand Up @@ -535,7 +538,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
deleteTargetOptions.SetTenantID(&tenantId)
deleteTargetOptions.SetTargetID(&target1ID)
deleteTargetOptions.SetRegion(d.Get("region").(string))
_, err = ibmCloudLogsRoutingClient.DeleteTargetWithContext(context, deleteTargetOptions)
_, err = ibmCloudLogsRoutingClient.DeleteTargetWithContextEndpoint(context, deleteTargetOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("DeleteTargetWithContext failed: %s", err.Error()), "ibm_logs_router_target", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
Expand All @@ -546,9 +549,9 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
_, newCRN := d.GetChange("targets.1.log_sink_crn")
if crn, ok := newCRN.(string); ok {
if strings.Contains(crn, ":logs:") {
_, _, err = ibmCloudLogsRoutingClient.UpdateLogsTargetWithContext(context, updateTarget1Options)
_, _, err = ibmCloudLogsRoutingClient.UpdateLogsTargetWithContextEndpoint(context, updateTarget1Options)
} else {
_, _, err = ibmCloudLogsRoutingClient.UpdateTargetWithContext(context, updateTarget1Options)
_, _, err = ibmCloudLogsRoutingClient.UpdateTargetWithContextEndpoint(context, updateTarget1Options)
}
}
if err != nil {
Expand Down Expand Up @@ -578,9 +581,9 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
_, newCRN := d.GetChange("targets.0.log_sink_crn")
if crn, ok := newCRN.(string); ok {
if strings.Contains(crn, ":logs:") {
_, _, err = ibmCloudLogsRoutingClient.UpdateLogsTargetWithContext(context, updateTarget0Options)
_, _, err = ibmCloudLogsRoutingClient.UpdateLogsTargetWithContextEndpoint(context, updateTarget0Options)
} else {
_, _, err = ibmCloudLogsRoutingClient.UpdateTargetWithContext(context, updateTarget0Options)
_, _, err = ibmCloudLogsRoutingClient.UpdateTargetWithContextEndpoint(context, updateTarget0Options)
}
}
if err != nil {
Expand All @@ -601,13 +604,14 @@ func resourceIBMLogsRouterTenantDelete(context context.Context, d *schema.Resour
return tfErr.GetDiag()
}

ibmCloudLogsRoutingClient, _, err = updateClientURLWithEndpoint(ibmCloudLogsRoutingClient, d)
deleteTenantOptions := &ibmcloudlogsroutingv0.DeleteTenantOptions{}

tenantId := strfmt.UUID(d.Id())
deleteTenantOptions.SetTenantID(&tenantId)
deleteTenantOptions.SetRegion(d.Get("region").(string))

_, err = ibmCloudLogsRoutingClient.DeleteTenantWithContext(context, deleteTenantOptions)
_, err = ibmCloudLogsRoutingClient.DeleteTenantWithContextEndpoint(context, deleteTenantOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("DeleteTenantWithContext failed: %s", err.Error()), "ibm_logs_router_tenant", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
Expand Down
43 changes: 43 additions & 0 deletions ibm/service/logsrouting/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package logsrouting

import (
"log"
"strings"

"github.com/IBM/logs-router-go-sdk/ibmcloudlogsroutingv0"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Clones the logs routing client and sets the correct URL. Public, private, or custom
func updateClientURLWithEndpoint(logsRoutingClient *ibmcloudlogsroutingv0.IBMCloudLogsRoutingV0, d *schema.ResourceData) (*ibmcloudlogsroutingv0.IBMCloudLogsRoutingV0, string, error) {

region := d.Get("region").(string)
originalConfigServiceURL := logsRoutingClient.GetServiceURL()
newServiceURL := replaceRegion(originalConfigServiceURL, region)

newClient := &ibmcloudlogsroutingv0.IBMCloudLogsRoutingV0{
Service: logsRoutingClient.Service.Clone(),
}

log.Printf("Constructing client with new service URL %s", newServiceURL)

newClient.Service.SetServiceURL(newServiceURL)

return newClient, region, nil
}

// Function to replace the region in the URL
func replaceRegion(url, region string) string {
// Split the URL by "." to isolate the relevant parts
parts := strings.Split(url, ".")

// Check if the URL contains 'private'
if len(parts) > 1 && parts[1] == "private" {
parts[2] = region
} else {
parts[1] = region
}

// Join the parts back into a complete URL
return strings.Join(parts, ".")
}
Loading