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 region parameter to logs-routing resources and data sources #5634

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions examples/ibm-logs-routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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"
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
19 changes: 17 additions & 2 deletions examples/ibm-logs-routing/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ 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"
Expand All @@ -16,7 +17,7 @@ resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
}
}
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"
Expand All @@ -25,6 +26,20 @@ resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
}
}

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 = 80
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
Expand Down
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.67.0
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
30 changes: 24 additions & 6 deletions ibm/service/logsrouting/resource_ibm_logs-router_tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ func TestAccIBMLogsRouterTenantBasic(t *testing.T) {
),
},
resource.TestStep{
ResourceName: "ibm_logs_router_tenant.logs_router_tenant_instance",
ImportState: true,
ImportStateVerify: true,
ResourceName: "ibm_logs_router_tenant.logs_router_tenant_instance",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources["ibm_logs_router_tenant.logs_router_tenant_instance"]
if !ok {
return "", fmt.Errorf("Not found: %s", "ibm_logs_router_tenant.logs_router_tenant_instance")
}
return fmt.Sprintf("%s/%s", rs.Primary.ID, rs.Primary.Attributes["region"]), nil
},
ImportStateVerifyIgnore: []string{"targets.0.parameters.0.access_credential", "targets.1.parameters.0.access_credential"},
},
},
Expand Down Expand Up @@ -92,9 +99,16 @@ func TestAccIBMLogsRouterTenantAllArgs(t *testing.T) {
),
},
resource.TestStep{
ResourceName: "ibm_logs_router_tenant.logs_router_tenant_instance",
ImportState: true,
ImportStateVerify: true,
ResourceName: "ibm_logs_router_tenant.logs_router_tenant_instance",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources["ibm_logs_router_tenant.logs_router_tenant_instance"]
if !ok {
return "", fmt.Errorf("Not found: %s", "ibm_logs_router_tenant.logs_router_tenant_instance")
}
return fmt.Sprintf("%s/%s", rs.Primary.ID, rs.Primary.Attributes["region"]), nil
},
ImportStateVerifyIgnore: []string{"targets.0.parameters.0.access_credential", "targets.1.parameters.0.access_credential"},
},
},
Expand All @@ -105,6 +119,7 @@ func testAccCheckIBMLogsRouterTenantConfigBasic(name string, crn string, host st
return fmt.Sprintf(`
resource "ibm_logs_router_tenant" "logs_router_tenant_instance" {
name = "%s"
region = "br-sao"
targets {
log_sink_crn = "%s"
name = "my-log-sink"
Expand All @@ -122,6 +137,7 @@ func testAccCheckIBMLogsRouterTenantConfigAllArgs(name string, target0Name strin
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 = "%s"
Expand Down Expand Up @@ -151,6 +167,7 @@ func testAccCheckIBMLogsRouterTenantExists(n string, obj ibmcloudlogsroutingv0.T

tenantId := strfmt.UUID(rs.Primary.ID)
getTenantDetailOptions.SetTenantID(&tenantId)
getTenantDetailOptions.SetRegion(rs.Primary.Attributes["region"])

tenant, _, err := ibmCloudLogsRoutingClient.GetTenantDetail(getTenantDetailOptions)
if err != nil {
Expand All @@ -176,6 +193,7 @@ func testAccCheckIBMLogsRouterTenantDestroy(s *terraform.State) error {

tenantId := strfmt.UUID(rs.Primary.ID)
getTenantDetailOptions.SetTenantID(&tenantId)
getTenantDetailOptions.SetRegion(rs.Primary.Attributes["region"])

// Try to find the key
_, response, err := ibmCloudLogsRoutingClient.GetTenantDetail(getTenantDetailOptions)
Expand Down
Loading
Loading