Skip to content

Commit

Permalink
Add region parameter to logs-routing resources and data sources (#5634)
Browse files Browse the repository at this point in the history
* Added region parameter to logs-routing resources and data sources

Signed-off-by: Ian Reyes <ianre@ibm.com>

* Fixed examples for logs-routing resources

---------

Signed-off-by: Ian Reyes <ianre@ibm.com>
Co-authored-by: Ian Reyes <ianre@ibm.com>
  • Loading branch information
ianre and ianree committed Sep 12, 2024
1 parent cf6b849 commit 290b912
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 34 deletions.
11 changes: 8 additions & 3 deletions examples/ibm-logs-routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ Run `terraform destroy` when you don't need these resources.
```hcl
resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
name = var.logs_router_tenant_name
region = "us-east"
targets {
log_sink_crn = "crn:v1:bluemix:public:logdna:eu-de:a/7246b8fa0a174a71899f5affa4f18d78:3517d2ed-9429-af34-ad52-34278391cbc8::"
name = "my-log-sink"
parameters {
host = "www.example.com"
port = 1
port = 443
access_credential = "new-credential"
}
}
Expand All @@ -46,6 +47,7 @@ resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
|------|-------------|------|---------|
| ibmcloud\_api\_key | IBM Cloud API key | `string` | true |
| name | The name for this tenant. The name is regionally unique across all tenants in the account. | `string` | true |
| region | The region to onboard this tenant. | `string` | true |
| targets | List of targets. | `list()` | true |
| targets.log_sink_crn | CRN of the Mezmo or Cloud Logs instance to sends logs to | `string` | true |
| targets.name | The name for this target. The name is regionally unique for this tenant. | `string` | true |
Expand All @@ -70,14 +72,16 @@ resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
```hcl
data "ibm_logs_router_tenants" "logs_router_tenants_instance" {
name = var.logs_router_tenants_name
region = "us-east"
}
```

#### Inputs

| Name | Description | Type | Required |
|------|-------------|------|---------|
| name | Optional: The name of a tenant. | `string` | true |
| name | The name of a tenant. | `string` | true |
| region | The region to query the tenant. | `string` | true |

#### Outputs

Expand All @@ -90,7 +94,7 @@ data "ibm_logs_router_tenants" "logs_router_tenants_instance" {
```hcl
data "ibm_logs_router_targets" "logs_router_targets_instance" {
tenant_id = var.logs_router_targets_tenant_id
name = var.logs_router_targets_name
region = "us-east"
}
```

Expand All @@ -99,6 +103,7 @@ data "ibm_logs_router_targets" "logs_router_targets_instance" {
| Name | Description | Type | Required |
|------|-------------|------|---------|
| tenant_id | The instance ID of the tenant. | `` | true |
| region | The region where the tenant for this target exists, | `string` | true |
| name | Optional: Name of the tenant target. | `string` | false |

#### Outputs
Expand Down
29 changes: 23 additions & 6 deletions examples/ibm-logs-routing/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,48 @@ provider "ibm" {
// Provision logs_router_tenant resource instance
resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
name = var.logs_router_tenant_name
region = "us-east"
targets {
log_sink_crn = "crn:v1:bluemix:public:logdna:eu-de:a/7246b8fa0a174a71899f5affa4f18d78:3517d2ed-9429-af34-ad52-34278391cbc8::"
log_sink_crn = "crn:v1:bluemix:public:logdna:us-east:a/7246b8fa0a174a71899f5affa4f18d78:3517d2ed-9429-af34-ad52-34278391cbc8::"
name = "my-logdna-target"
parameters {
host = "www.example-1.com"
port = 80
port = 443
access_credential = "new-cred"
}
}
targets {
log_sink_crn = "crn:v1:bluemix:public:logs:eu-de:a/7246b8fa0a174a71899f5affa4f18d78:3517d2ed-9429-af34-ad52-34278391cbc8::"
log_sink_crn = "crn:v1:bluemix:public:logs:us-east:a/7246b8fa0a174a71899f5affa4f18d78:3517d2ed-9429-af34-ad52-34278391cbc8::"
name = "my-cloud-logs-target"
parameters {
host = "www.example-2.com"
port = 80
port = 443
}
}
}

resource "ibm_logs_router_tenant" "logs_router_tenant_instance_eu_de" {
name = "eu-de-tenant"
region = "eu-de"
targets {
log_sink_crn = "crn:v1:bluemix:public:logdna:eu-de:a/7246b8fa0a174a71899f5affa4f18d78:3517d2ed-9429-af34-ad52-34278391cbc8::"
name = "my-logdna-target"
parameters {
host = "www.example-1.com"
port = 443
access_credential = "new-cred"
}
}
}

// Create logs_router_tenants data source
data "ibm_logs_router_tenants" "logs_router_tenants_instance" {
name = ibm_logs_router_tenant.logs_router_tenant_instance_both.name
name = ibm_logs_router_tenant.logs_router_tenant_instance.name
region = ibm_logs_router_tenant.logs_router_tenant_instance.region
}

// Create logs_router_targets data source
data "ibm_logs_router_targets" "logs_router_targets_instance" {
tenant_id = ibm_logs_router_tenant.logs_router_tenant_instance_both.id
tenant_id = ibm_logs_router_tenant.logs_router_tenant_instance.id
region = ibm_logs_router_tenant.logs_router_tenant_instance.region
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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.3.0
github.com/IBM/logs-router-go-sdk v1.0.3
github.com/IBM/logs-router-go-sdk v1.0.5
github.com/IBM/mqcloud-go-sdk v0.1.0
github.com/IBM/networking-go-sdk v0.49.0
github.com/IBM/platform-services-go-sdk v0.68.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ github.com/IBM/keyprotect-go-client v0.15.1 h1:m4qzqF5zOumRxKZ8s7vtK7A/UV/D278L8
github.com/IBM/keyprotect-go-client v0.15.1/go.mod h1:asXtHwL/4uCHA221Vd/7SkXEi2pcRHDzPyyksc1DthE=
github.com/IBM/logs-go-sdk v0.3.0 h1:FHzTCCMyp9DvQGXgkppzcOPywC4ggt7x8xu0MR5h8xI=
github.com/IBM/logs-go-sdk v0.3.0/go.mod h1:yv/GCXC4/p+MZEeXl4xjZAOMvDAVRwu61WyHZFKFXQM=
github.com/IBM/logs-router-go-sdk v1.0.3 h1:VO64OpANNouxS/0kvUeBpENKWxYx3TYnoNzW8OycMb0=
github.com/IBM/logs-router-go-sdk v1.0.3/go.mod h1:tCN2vFgu5xG0ob9iJcxi5M4bJ6mWmu3nhmRPnvlwev0=
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/mqcloud-go-sdk v0.1.0 h1:fWt4uisg5GbbsfNmAxx5/6c5gQIPM+VrEsTtnimELeA=
github.com/IBM/mqcloud-go-sdk v0.1.0/go.mod h1:LesMQlKHXvdks4jqQLZH7HfATY5lvTzHuwQU5+y7b2g=
github.com/IBM/networking-go-sdk v0.49.0 h1:lPS34u3C0JVrbxH+Ulua76Nwl6Frv8BEfq6LRkyvOv0=
Expand Down
10 changes: 10 additions & 0 deletions ibm/service/logsrouting/data_source_ibm_logs-router_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func DataSourceIBMLogsRouterTargets() *schema.Resource {
Optional: true,
Description: "Optional: Name of the tenant target.",
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The region where the tenant for these targets exist.",
},
"targets": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -121,6 +127,10 @@ func dataSourceIBMLogsRouterTargetsRead(context context.Context, d *schema.Resou
listTenantTargetsOptions.SetName(d.Get("name").(string))
}

if _, ok := d.GetOk("region"); ok {
listTenantTargetsOptions.SetRegion(d.Get("region").(string))
}

targetTypeCollection, _, err := ibmCloudLogsRoutingClient.ListTenantTargetsWithContext(context, listTenantTargetsOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ListTenantTargetsWithContext failed: %s", err.Error()), "(Data) ibm_logs_router_targets", "read")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func testAccCheckIBMLogsRouterTargetsDataSourceConfigBasic(tenantName string) st
return fmt.Sprintf(`
resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
name = "%s"
region = "br-sao"
targets {
log_sink_crn = "crn:v1:bluemix:public:logdna:eu-de:a/3516b8fa0a174a71899f5affa4f18d78:3517d2ed-9429-af34-ad52-34278391cbc8::"
name = "my-log-sink"
Expand All @@ -58,6 +59,7 @@ func testAccCheckIBMLogsRouterTargetsDataSourceConfigBasic(tenantName string) st
data "ibm_logs_router_targets" "logs_router_targets_instance" {
tenant_id = ibm_logs_router_tenant.logs_router_tenant_instance.id
region = ibm_logs_router_tenant.logs_router_tenant_instance.region
}
`, tenantName, acc.IngestionKey)
}
Expand Down
10 changes: 10 additions & 0 deletions ibm/service/logsrouting/data_source_ibm_logs-router_tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func DataSourceIBMLogsRouterTenants() *schema.Resource {
Required: true,
Description: "Optional: The name of a tenant.",
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The region where the tenants exist.",
},
"tenants": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -152,6 +158,10 @@ func dataSourceIBMLogsRouterTenantsRead(context context.Context, d *schema.Resou
listTenantsOptions.SetName(d.Get("name").(string))
}

if _, ok := d.GetOk("region"); ok {
listTenantsOptions.SetRegion(d.Get("region").(string))
}

tenantCollection, _, err := ibmCloudLogsRoutingClient.ListTenantsWithContext(context, listTenantsOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ListTenantsWithContext failed: %s", err.Error()), "(Data) ibm_logs_router_tenants", "read")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func testAccCheckIBMLogsRouterTenantsDataSourceConfigBasic(tenantName string) st
return fmt.Sprintf(`
resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
name = "%s"
region = "br-sao"
targets {
log_sink_crn = "crn:v1:bluemix:public:logdna:eu-de:a/3516b8fa0a174a71899f5affa4f18d78:3517d2ed-9429-af34-ad52-34278391cbc8::"
name = "my-log-sink"
Expand All @@ -57,6 +58,7 @@ func testAccCheckIBMLogsRouterTenantsDataSourceConfigBasic(tenantName string) st
data "ibm_logs_router_tenants" "logs_router_tenants_instance" {
name = ibm_logs_router_tenant.logs_router_tenant_instance.name
region = ibm_logs_router_tenant.logs_router_tenant_instance.region
}
`, tenantName, acc.IngestionKey)
}
Expand Down
45 changes: 42 additions & 3 deletions ibm/service/logsrouting/resource_ibm_logs-router_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ func ResourceIBMLogsRouterTenant() *schema.Resource {
ReadContext: resourceIBMLogsRouterTenantRead,
UpdateContext: resourceIBMLogsRouterTenantUpdate,
DeleteContext: resourceIBMLogsRouterTenantDelete,
Importer: &schema.ResourceImporter{},
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) (result []*schema.ResourceData, err error) {
ID := d.Id()
parts := strings.Split(ID, "/")
if len(parts) < 2 {
return nil, fmt.Errorf("Invalid import format: please specify 'tenant_id/region'")
}
tenantID := parts[0]
region := parts[1]
d.SetId(tenantID)
if err := d.Set("region", region); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Expand All @@ -39,9 +54,16 @@ func ResourceIBMLogsRouterTenant() *schema.Resource {
ValidateFunc: validate.InvokeValidator("ibm_logs_router_tenant", "name"),
Description: "The name for this tenant. The name is regionally unique across all tenants in the account.",
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.InvokeValidator("ibm_logs_router_tenant", "region"),
Description: "The region where this tenant exists.",
},
"targets": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Required: true,
MinItems: 1,
Description: "List of targets",
Elem: &schema.Resource{
Expand Down Expand Up @@ -86,7 +108,7 @@ func ResourceIBMLogsRouterTenant() *schema.Resource {
"parameters": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Required: true,
Description: "List of properties returned from a successful list operation for a log-sink of type IBM Log Analysis (logdna).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -148,6 +170,15 @@ func ResourceIBMLogsRouterTenantValidator() *validate.ResourceValidator {
MinValueLength: 1,
MaxValueLength: 35,
},
validate.ValidateSchema{
Identifier: "region",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^[a-zA-Z0-9 -._:]+$`,
MinValueLength: 3,
MaxValueLength: 1000,
},
)

resourceValidator := validate.ResourceValidator{ResourceName: "ibm_logs_router_tenant", Schema: validateSchema}
Expand All @@ -166,6 +197,7 @@ func resourceIBMLogsRouterTenantCreate(context context.Context, d *schema.Resour
createTenantOptions := &ibmcloudlogsroutingv0.CreateTenantOptions{}

createTenantOptions.SetName(d.Get("name").(string))
createTenantOptions.SetRegion(d.Get("region").(string))
var targets []ibmcloudlogsroutingv0.TargetTypePrototypeIntf
for _, v := range d.Get("targets").([]interface{}) {
value := v.(map[string]interface{})
Expand Down Expand Up @@ -201,6 +233,7 @@ func resourceIBMLogsRouterTenantRead(context context.Context, d *schema.Resource

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

tenant, response, err := ibmCloudLogsRoutingClient.GetTenantDetailWithContext(context, getTenantDetailOptions)
if err != nil {
Expand Down Expand Up @@ -317,6 +350,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour

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

hasChange := false
hasChangeTarget0 := false
Expand All @@ -336,6 +370,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
target0ID := strfmt.UUID(d.Get("targets.0.id").(string))
updateTarget0Options.SetTenantID(&tenantId)
updateTarget0Options.SetTargetID(&target0ID)
updateTarget0Options.SetRegion(d.Get("region").(string))

patchValsTarget0 := &ibmcloudlogsroutingv0.TargetTypePatch{}
if d.HasChange("targets.0.tenant_id") {
Expand Down Expand Up @@ -367,6 +402,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
target1ID := strfmt.UUID(d.Get("targets.1.id").(string))
updateTarget1Options.SetTenantID(&tenantId)
updateTarget1Options.SetTargetID(&target1ID)
updateTarget1Options.SetRegion(d.Get("region").(string))

bodyModelMap := map[string]interface{}{}
createTarget1Options := &ibmcloudlogsroutingv0.CreateTargetOptions{}
Expand All @@ -383,6 +419,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
bodyModelMap["parameters"] = d.Get("targets.1.parameters")
}
createTarget1Options.SetTenantID(&tenantId)
createTarget1Options.SetRegion(d.Get("region").(string))
convertedModel, err := ResourceIBMLogsRouterTargetMapToTargetTypePrototypeTargetTypeLogDNAPrototype(bodyModelMap)
if err != nil {
return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_logs_router_target", "create", "parse-request-body").GetDiag()
Expand Down Expand Up @@ -497,6 +534,7 @@ func resourceIBMLogsRouterTenantUpdate(context context.Context, d *schema.Resour
deleteTargetOptions := &ibmcloudlogsroutingv0.DeleteTargetOptions{}
deleteTargetOptions.SetTenantID(&tenantId)
deleteTargetOptions.SetTargetID(&target1ID)
deleteTargetOptions.SetRegion(d.Get("region").(string))
_, err = ibmCloudLogsRoutingClient.DeleteTargetWithContext(context, deleteTargetOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("DeleteTargetWithContext failed: %s", err.Error()), "ibm_logs_router_target", "delete")
Expand Down Expand Up @@ -567,6 +605,7 @@ func resourceIBMLogsRouterTenantDelete(context context.Context, d *schema.Resour

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

_, err = ibmCloudLogsRoutingClient.DeleteTenantWithContext(context, deleteTenantOptions)
if err != nil {
Expand Down
Loading

0 comments on commit 290b912

Please sign in to comment.