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

feat(ga): add a new datasource to get the list of the access logs #6135

Merged
merged 1 commit into from
Jan 6, 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
64 changes: 64 additions & 0 deletions docs/data-sources/ga_access_logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
subcategory: "Global Accelerator (GA)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_ga_access_logs"
description: |-
Use this data source to get the list of the access logs.
---

# huaweicloud_ga_access_logs

Use this data source to get the list of the access logs.

## Example Usage

```hcl
data "huaweicloud_ga_access_logs" "test" {}
```

## Argument Reference

The following arguments are supported:

* `log_id` - (Optional, String) Specifies the ID of the access log.

* `status` - (Optional, String) Specifies the status of the access log.
The valid values are as follows:
+ **ACTIVE**: The resource is running.
+ **PENDING**: The status is to be determined.
+ **ERROR**: Failed to create the resource.
+ **DELETING**: The resource is being deleted.

* `resource_type` - (Optional, String) Specifies the type of the resource to which the access log belongs.
Currently, only **LISTENER** is supported.

* `resource_ids` - (Optional, List) Specifies the ID list of the resource to which the access log belongs.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `logs` - The list of the access logs.

The [logs](#logs_struct) structure is documented below.

<a name="logs_struct"></a>
The `logs` block supports:

* `id` - The ID of the access log.

* `status` - The status of the access log.

* `resource_type` - The type of the resource to which the access log belongs.

* `resource_id` - The ID of the resource to which the access log belongs.

* `log_group_id` - The ID of the log group to which the access log belongs.

* `log_stream_id` - The ID of the log stream to which the access log belongs.

* `created_at` - The creation time of the access log, in RFC3339 format.

* `updated_at` - The latest update time of the access log, in RFC3339 format.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ func Provider() *schema.Provider {
"huaweicloud_fgs_quotas": fgs.DataSourceQuotas(),

"huaweicloud_ga_accelerators": ga.DataSourceAccelerators(),
"huaweicloud_ga_access_logs": ga.DataSourceGaAccessLogs(),
"huaweicloud_ga_address_groups": ga.DataSourceAddressGroups(),
"huaweicloud_ga_availability_zones": ga.DataSourceAvailabilityZones(),
"huaweicloud_ga_endpoint_groups": ga.DataSourceEndpointGroups(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package ga

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourceAccessLogs_basic(t *testing.T) {
var (
name = acceptance.RandomAccResourceNameWithDash()
dataSourceName = "data.huaweicloud_ga_access_logs.test"
dc = acceptance.InitDataSourceCheck(dataSourceName)

byLogId = "data.huaweicloud_ga_access_logs.filter_by_log_id"
dcByLogId = acceptance.InitDataSourceCheck(byLogId)

byStatus = "data.huaweicloud_ga_access_logs.filter_by_status"
dcByStatus = acceptance.InitDataSourceCheck(byStatus)

byResourceType = "data.huaweicloud_ga_access_logs.filter_by_resource_type"
dcByResourceType = acceptance.InitDataSourceCheck(byResourceType)

byResourceIds = "data.huaweicloud_ga_access_logs.filter_by_resource_ids"
dcByResourceIds = acceptance.InitDataSourceCheck(byResourceIds)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceAccessLogs_basic(name),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSourceName, "logs.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "logs.0.id"),
resource.TestCheckResourceAttrSet(dataSourceName, "logs.0.resource_type"),
resource.TestCheckResourceAttrSet(dataSourceName, "logs.0.resource_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "logs.0.log_group_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "logs.0.log_stream_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "logs.0.status"),
resource.TestMatchResourceAttr(dataSourceName, "logs.0.created_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
resource.TestMatchResourceAttr(dataSourceName, "logs.0.updated_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),

dcByLogId.CheckResourceExists(),
resource.TestCheckOutput("log_id_filter_useful", "true"),

dcByStatus.CheckResourceExists(),
resource.TestCheckOutput("status_filter_useful", "true"),

dcByResourceType.CheckResourceExists(),
resource.TestCheckOutput("resource_type_filter_useful", "true"),

dcByResourceIds.CheckResourceExists(),
resource.TestCheckOutput("resource_ids_filter_useful", "true"),
),
},
},
})
}

func testDataSourceAccessLogs_basic(name string) string {
return fmt.Sprintf(`
%s

data "huaweicloud_ga_access_logs" "test" {
depends_on = [huaweicloud_ga_access_log.test]
}

locals {
log_id = data.huaweicloud_ga_access_logs.test.logs[0].id
}

data "huaweicloud_ga_access_logs" "filter_by_log_id" {
log_id = local.log_id
}

output "log_id_filter_useful" {
value = length(data.huaweicloud_ga_access_logs.filter_by_log_id.logs) > 0 && alltrue(
[for v in data.huaweicloud_ga_access_logs.filter_by_log_id.logs[*].id : v == local.log_id]
)
}

locals {
status = data.huaweicloud_ga_access_logs.test.logs[0].status
}

data "huaweicloud_ga_access_logs" "filter_by_status" {
status = local.status
}

output "status_filter_useful" {
value = length(data.huaweicloud_ga_access_logs.filter_by_status.logs) > 0 && alltrue(
[for v in data.huaweicloud_ga_access_logs.filter_by_status.logs[*].status : v == local.status]
)
}

locals {
resource_type = data.huaweicloud_ga_access_logs.test.logs[0].resource_type
}

data "huaweicloud_ga_access_logs" "filter_by_resource_type" {
resource_type = local.resource_type
}

output "resource_type_filter_useful" {
value = length(data.huaweicloud_ga_access_logs.filter_by_resource_type.logs) > 0 && alltrue(
[for v in data.huaweicloud_ga_access_logs.filter_by_resource_type.logs[*].resource_type : v == local.resource_type]
)
}

locals {
resource_ids = [data.huaweicloud_ga_access_logs.test.logs[0].resource_id]
}

data "huaweicloud_ga_access_logs" "filter_by_resource_ids" {
resource_ids = local.resource_ids
}

output "resource_ids_filter_useful" {
value = length(data.huaweicloud_ga_access_logs.filter_by_resource_ids.logs) == 1
}
`, testAccessLog_basic(name))
}
182 changes: 182 additions & 0 deletions huaweicloud/services/ga/data_source_huaweicloud_ga_access_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Generated by PMS #503
package ga

import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func DataSourceGaAccessLogs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGaAccessLogsRead,

Schema: map[string]*schema.Schema{
"log_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the access log.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the status of the access log.`,
},
"resource_type": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the type of the resource to which the access log belongs.`,
},
"resource_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `Specifies the ID list of the resource to which the access log belongs.`,
},
"logs": {
Type: schema.TypeList,
Computed: true,
Description: `The list of the access logs.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the access log.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `The status of the access log.`,
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: `The type of the resource to which the access log belongs.`,
},
"resource_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the resource to which the access log belongs.`,
},
"log_group_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the log group to which the access log belongs.`,
},
"log_stream_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the log stream to which the access log belongs.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `The creation time of the access log, in RFC3339 format.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `The latest update time of the access log, in RFC3339 format.`,
},
},
},
},
},
}
}

type AccessLogsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newAccessLogsDSWrapper(d *schema.ResourceData, meta interface{}) *AccessLogsDSWrapper {
return &AccessLogsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceGaAccessLogsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newAccessLogsDSWrapper(d, meta)
listLogtanksRst, err := wrapper.ListLogtanks()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

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

return nil
}

// @API GA GET /v1/logtanks
func (w *AccessLogsDSWrapper) ListLogtanks() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "ga")
if err != nil {
return nil, err
}

uri := "/v1/logtanks"
params := map[string]any{
"id": w.Get("log_id"),
"status": w.Get("status"),
"resource_ids": w.ListToArray("resource_ids"),
"resource_type": w.Get("resource_type"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
MarkerPager("logtanks", "page_info.next_marker", "marker").
Request().
Result()
}

func (w *AccessLogsDSWrapper) listLogtanksToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("logs", schemas.SliceToList(body.Get("logtanks"),
func(logs gjson.Result) any {
return map[string]any{
"id": logs.Get("id").Value(),
"status": logs.Get("status").Value(),
"resource_type": logs.Get("resource_type").Value(),
"resource_id": logs.Get("resource_id").Value(),
"log_group_id": logs.Get("log_group_id").Value(),
"log_stream_id": logs.Get("log_stream_id").Value(),
"created_at": w.setLogtanksCreatedAt(logs),
"updated_at": w.setLogtanksUpdatedAt(logs),
}
},
)),
)
return mErr.ErrorOrNil()
}

func (*AccessLogsDSWrapper) setLogtanksCreatedAt(data gjson.Result) string {
return utils.FormatTimeStampRFC3339(utils.ConvertTimeStrToNanoTimestamp(data.Get("created_at").String())/1000, false)
}

func (*AccessLogsDSWrapper) setLogtanksUpdatedAt(data gjson.Result) string {
return utils.FormatTimeStampRFC3339(utils.ConvertTimeStrToNanoTimestamp(data.Get("updated_at").String())/1000, false)
}
Loading