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

Feature/aws custom log source #35354

Merged
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 .changelog/35354.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_securitylake_custom_log_source
```
16 changes: 8 additions & 8 deletions internal/service/bedrock/custom_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (r *customModelResource) Schema(ctx context.Context, request resource.Schem
}

func (r *customModelResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var data resourceCustomModelData
var data customModelResourceModel
response.Diagnostics.Append(request.Plan.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -331,7 +331,7 @@ func (r *customModelResource) Create(ctx context.Context, request resource.Creat
}

func (r *customModelResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
var data resourceCustomModelData
var data customModelResourceModel
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -402,7 +402,7 @@ func (r *customModelResource) Read(ctx context.Context, request resource.ReadReq
return
}

var dataFromGetCustomModel resourceCustomModelData
var dataFromGetCustomModel customModelResourceModel
response.Diagnostics.Append(fwflex.Flatten(ctx, outputGM, &dataFromGetCustomModel)...)
if response.Diagnostics.HasError() {
return
Expand All @@ -417,7 +417,7 @@ func (r *customModelResource) Read(ctx context.Context, request resource.ReadReq
}

func (r *customModelResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
var old, new resourceCustomModelData
var old, new customModelResourceModel
response.Diagnostics.Append(request.State.Get(ctx, &old)...)
if response.Diagnostics.HasError() {
return
Expand All @@ -438,7 +438,7 @@ func (r *customModelResource) Update(ctx context.Context, request resource.Updat
}

func (r *customModelResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
var data resourceCustomModelData
var data customModelResourceModel
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -616,7 +616,7 @@ func waitModelCustomizationJobStopped(ctx context.Context, conn *bedrock.Client,
return nil, err
}

type resourceCustomModelData struct {
type customModelResourceModel struct {
BaseModelIdentifier fwtypes.ARN `tfsdk:"base_model_identifier"`
CustomModelARN types.String `tfsdk:"custom_model_arn"`
CustomModelKmsKeyID fwtypes.ARN `tfsdk:"custom_model_kms_key_id"`
Expand All @@ -639,13 +639,13 @@ type resourceCustomModelData struct {
VPCConfig fwtypes.ListNestedObjectValueOf[customModelVPCConfigModel] `tfsdk:"vpc_config"`
}

func (data *resourceCustomModelData) InitFromID() error {
func (data *customModelResourceModel) InitFromID() error {
data.JobARN = data.ID

return nil
}

func (data *resourceCustomModelData) setID() {
func (data *customModelResourceModel) setID() {
data.ID = data.JobARN
}

Expand Down
2 changes: 1 addition & 1 deletion internal/service/bedrock/custom_model_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (d *customModelDataSource) Read(ctx context.Context, request datasource.Rea
}

// Some fields are only available in GetModelCustomizationJobOutput.
var dataFromGetModelCustomizationJob resourceCustomModelData
var dataFromGetModelCustomizationJob customModelResourceModel
response.Diagnostics.Append(fwflex.Flatten(ctx, outputGJ, &dataFromGetModelCustomizationJob)...)
if response.Diagnostics.HasError() {
return
Expand Down
119 changes: 55 additions & 64 deletions internal/service/securitylake/aws_log_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,61 @@ package securitylake

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/securitylake"
awstypes "github.com/aws/aws-sdk-go-v2/service/securitylake/types"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkResource(name="AWS Log Source")
func newAWSLogSourceResource(_ context.Context) (resource.ResourceWithConfigure, error) {
func newAWSLogSourceResource(context.Context) (resource.ResourceWithConfigure, error) {
r := &awsLogSourceResource{}

return r, nil
}

const (
ResNameAWSLogSource = "AWS Log Source"
)

type awsLogSourceResource struct {
framework.ResourceWithConfigure
framework.WithNoUpdate
framework.WithImportByID
}

func (r *awsLogSourceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = "aws_securitylake_aws_log_source"
func (r *awsLogSourceResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = "aws_securitylake_aws_log_source"
}

func (r *awsLogSourceResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
func (r *awsLogSourceResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrID: framework.IDAttribute(),
},
Blocks: map[string]schema.Block{
"source": schema.ListNestedBlock{
CustomType: fwtypes.NewListNestedObjectTypeOf[awsLogSourceSourceModel](ctx),
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
},
Validators: []validator.List{
listvalidator.IsRequired(),
listvalidator.SizeAtLeast(1),
listvalidator.SizeAtMost(1),
},
Expand Down Expand Up @@ -99,108 +102,100 @@ func (r *awsLogSourceResource) Schema(ctx context.Context, req resource.SchemaRe
}
}

func (r *awsLogSourceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
conn := r.Meta().SecurityLakeClient(ctx)

func (r *awsLogSourceResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var data awsLogSourceResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
response.Diagnostics.Append(request.Plan.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().SecurityLakeClient(ctx)

input := &securitylake.CreateAwsLogSourceInput{}
resp.Diagnostics.Append(flex.Expand(ctx, data, input)...)
if resp.Diagnostics.HasError() {
response.Diagnostics.Append(fwflex.Expand(ctx, data, input)...)
if response.Diagnostics.HasError() {
return
}

_, err := conn.CreateAwsLogSource(ctx, input)

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.SecurityLake, create.ErrActionCreating, ResNameAWSLogSource, data.ID.ValueString(), err),
err.Error(),
)
response.Diagnostics.AddError("creating Security Lake AWS Log Source", err.Error())

return
}

// Set values for unknowns.
data.ID = flex.StringValueToFramework(ctx, input.Sources[0].SourceName)
data.ID = fwflex.StringValueToFramework(ctx, input.Sources[0].SourceName)

logSource, err := findAWSLogSourceBySourceName(ctx, conn, awstypes.AwsLogSourceName(data.ID.ValueString()))

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.SecurityLake, create.ErrActionReading, ResNameAWSLogSource, data.ID.String(), err),
err.Error(),
)
response.Diagnostics.AddError(fmt.Sprintf("reading Security Lake AWS Log Source (%s)", data.ID.ValueString()), err.Error())

return
}

sourceData, diags := data.Source.ToPtr(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

sourceData.Accounts.SetValue = flex.FlattenFrameworkStringValueSet(ctx, logSource.Accounts)
sourceData.SourceVersion = flex.StringToFramework(ctx, logSource.SourceVersion)
sourceData.Accounts.SetValue = fwflex.FlattenFrameworkStringValueSet(ctx, logSource.Accounts)
sourceData.SourceVersion = fwflex.StringToFramework(ctx, logSource.SourceVersion)

resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
response.Diagnostics.Append(response.State.Set(ctx, data)...)
}

func (r *awsLogSourceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
conn := r.Meta().SecurityLakeClient(ctx)

func (r *awsLogSourceResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
var data awsLogSourceResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().SecurityLakeClient(ctx)

logSource, err := findAWSLogSourceBySourceName(ctx, conn, awstypes.AwsLogSourceName(data.ID.ValueString()))

if tfresource.NotFound(err) {
resp.State.RemoveResource(ctx)
response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err))
response.State.RemoveResource(ctx)

return
}

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.SecurityLake, create.ErrActionReading, ResNameAWSLogSource, data.ID.String(), err),
err.Error(),
)
response.Diagnostics.AddError(fmt.Sprintf("reading Security Lake AWS Log Source (%s)", data.ID.ValueString()), err.Error())

return
}

// We can't use AutoFlEx with the top-level resource model because the API structure uses Go interfaces.
var sourceData awsLogSourceSourceModel
resp.Diagnostics.Append(flex.Flatten(ctx, logSource, &sourceData)...)
if resp.Diagnostics.HasError() {
response.Diagnostics.Append(fwflex.Flatten(ctx, logSource, &sourceData)...)
if response.Diagnostics.HasError() {
return
}

data.Source = fwtypes.NewListNestedObjectValueOfPtr(ctx, &sourceData)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

func (r *awsLogSourceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// NoOP.
}

func (r *awsLogSourceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
conn := r.Meta().SecurityLakeClient(ctx)

func (r *awsLogSourceResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
var data awsLogSourceResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().SecurityLakeClient(ctx)

input := &securitylake.DeleteAwsLogSourceInput{}
resp.Diagnostics.Append(flex.Expand(ctx, data, input)...)
if resp.Diagnostics.HasError() {
response.Diagnostics.Append(fwflex.Expand(ctx, data, input)...)
if response.Diagnostics.HasError() {
return
}

Expand All @@ -209,10 +204,8 @@ func (r *awsLogSourceResource) Delete(ctx context.Context, req resource.DeleteRe
logSource, err := findAWSLogSourceBySourceName(ctx, conn, awstypes.AwsLogSourceName(data.ID.ValueString()))

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.SecurityLake, create.ErrActionReading, ResNameAWSLogSource, data.ID.String(), err),
err.Error(),
)
response.Diagnostics.AddError(fmt.Sprintf("reading Security Lake AWS Log Source (%s)", data.ID.ValueString()), err.Error())

return
}

Expand All @@ -226,10 +219,8 @@ func (r *awsLogSourceResource) Delete(ctx context.Context, req resource.DeleteRe
}

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.SecurityLake, create.ErrActionDeleting, ResNameAWSLogSource, data.ID.String(), err),
err.Error(),
)
response.Diagnostics.AddError(fmt.Sprintf("deleting Security Lake AWS Log Source (%s)", data.ID.ValueString()), err.Error())

return
}
}
Expand Down
Loading
Loading