1.1.0
This commit is contained in:
91
internal/service/cloudapi/audit/data_source_audit.go
Normal file
91
internal/service/cloudapi/audit/data_source_audit.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/constants"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/audit/flattens"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/audit/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/audit/schemas"
|
||||
)
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ datasource.DataSource = &dataSourceAudit{}
|
||||
)
|
||||
|
||||
func NewDataSourceAudit() datasource.DataSource {
|
||||
return &dataSourceAudit{}
|
||||
}
|
||||
|
||||
// dataSourceAudit is the data source implementation.
|
||||
type dataSourceAudit struct {
|
||||
client *decort.DecortClient
|
||||
}
|
||||
|
||||
func (d *dataSourceAudit) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
// Read Terraform configuration data into the model
|
||||
var state models.DataSourceAudit
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceAudit: Error get state")
|
||||
return
|
||||
}
|
||||
auditGuid := state.AuditGuid.ValueString()
|
||||
tflog.Info(ctx, "Read dataSourceAudit: got state successfully", map[string]any{"audit_guid": auditGuid})
|
||||
|
||||
// Set timeouts
|
||||
readTimeout, diags := state.Timeouts.Read(ctx, constants.Timeout180s)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceAudit: Error set timeout")
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Read dataSourceAudit: set timeouts successfully", map[string]any{
|
||||
"audit_guid": auditGuid,
|
||||
"readTimeout": readTimeout})
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, readTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Map response body to schema
|
||||
resp.Diagnostics.Append(flattens.AuditDataSource(ctx, &state, d.client)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceAudit: Error flatten")
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceAudit: Error set state")
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "End read dataSourceAudit", map[string]any{"audit_guid": auditGuid})
|
||||
}
|
||||
|
||||
func (d *dataSourceAudit) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: schemas.MakeSchemaDataSourceAudit(),
|
||||
Blocks: map[string]schema.Block{
|
||||
"timeouts": timeouts.Block(ctx),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dataSourceAudit) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_audit"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (d *dataSourceAudit) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
tflog.Info(ctx, "Get Configure dataSourceAudit")
|
||||
d.client = client.DataSource(ctx, &req, resp)
|
||||
tflog.Info(ctx, "Getting Configure dataSourceAudit successfully")
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package flattens
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/audit/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/audit/utilities"
|
||||
)
|
||||
|
||||
func AuditDataSource(ctx context.Context, state *models.DataSourceAudit, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.AuditDataSource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
auditGuid := state.AuditGuid.ValueString()
|
||||
|
||||
recordAudit, diags := utilities.AuditDataSourceCheckPresence(ctx, auditGuid, c)
|
||||
if diags.HasError() {
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.AuditDataSource: before flatten", map[string]any{"audit_guid": auditGuid})
|
||||
|
||||
*state = models.DataSourceAudit{
|
||||
AuditGuid: state.AuditGuid,
|
||||
Timeouts: state.Timeouts,
|
||||
|
||||
Arguments: types.StringValue(recordAudit.Arguments),
|
||||
Call: types.StringValue(recordAudit.Call),
|
||||
GUID: types.StringValue(recordAudit.GUID),
|
||||
ID: types.StringValue(recordAudit.GUID),
|
||||
Kwargs: types.StringValue(recordAudit.Kwargs),
|
||||
RemoteAddr: types.StringValue(recordAudit.RemoteAddr),
|
||||
ResponseTime: types.Float64Value(recordAudit.ResponseTime),
|
||||
Result: types.StringValue(recordAudit.Result),
|
||||
StatusCode: types.Int64Value(int64(recordAudit.StatusCode)),
|
||||
Tags: types.StringValue(recordAudit.Tags),
|
||||
Timestamp: types.Float64Value(recordAudit.Timestamp),
|
||||
TimestampEnd: types.Float64Value(recordAudit.TimestampEnd),
|
||||
User: types.StringValue(recordAudit.User),
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "End flattens.AuditDataSource", map[string]any{"audit_guid": auditGuid})
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
type DataSourceAudit struct {
|
||||
//required field
|
||||
AuditGuid types.String `tfsdk:"audit_guid"`
|
||||
Timeouts timeouts.Value `tfsdk:"timeouts"`
|
||||
|
||||
//response field
|
||||
Arguments types.String `tfsdk:"args"`
|
||||
Call types.String `tfsdk:"call"`
|
||||
GUID types.String `tfsdk:"guid"`
|
||||
ID types.String `tfsdk:"id"`
|
||||
Kwargs types.String `tfsdk:"kwargs"`
|
||||
RemoteAddr types.String `tfsdk:"remote_addr"`
|
||||
ResponseTime types.Float64 `tfsdk:"responsetime"`
|
||||
Result types.String `tfsdk:"result"`
|
||||
StatusCode types.Int64 `tfsdk:"status_code"`
|
||||
Tags types.String `tfsdk:"tags"`
|
||||
Timestamp types.Float64 `tfsdk:"timestamp"`
|
||||
TimestampEnd types.Float64 `tfsdk:"timestamp_end"`
|
||||
User types.String `tfsdk:"user"`
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package schemas
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
)
|
||||
|
||||
func MakeSchemaDataSourceAudit() map[string]schema.Attribute {
|
||||
return map[string]schema.Attribute{
|
||||
"audit_guid": schema.StringAttribute{
|
||||
Required: true,
|
||||
},
|
||||
"args": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"call": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"guid": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"kwargs": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"remote_addr": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"responsetime": schema.Float64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"result": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"status_code": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"tags": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"timestamp": schema.Float64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"timestamp_end": schema.Float64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"user": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"id": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/audit"
|
||||
)
|
||||
|
||||
func AuditDataSourceCheckPresence(ctx context.Context, auditGuid string, c *decort.DecortClient) (*audit.RecordAudit, diag.Diagnostics) {
|
||||
tflog.Info(ctx, fmt.Sprintf("AuditDataSourceCheckPresence: Get info about audit with ID - %v", auditGuid))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
recordAudit, err := c.CloudAPI().Audit().Get(ctx, audit.GetRequest{AuditGuid: auditGuid})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about audit with ID %v", auditGuid), err.Error())
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "AuditDataSourceCheckPresence: response from CloudBroker().Audit().Get", map[string]any{"audit_guid": auditGuid, "response": recordAudit})
|
||||
|
||||
return recordAudit, nil
|
||||
}
|
||||
Reference in New Issue
Block a user