-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MLPAB-2691 Use new generation method (see ADR-0006) (#165)
- Loading branch information
Showing
17 changed files
with
955 additions
and
528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
disable-version-string: true | ||
with-expecter: true | ||
inpackage: true | ||
dir: "{{.InterfaceDir}}" | ||
mockname: "mock{{.InterfaceName|firstUpper}}" | ||
outpkg: "{{.PackageName}}" | ||
filename: "mock_{{.InterfaceName}}_test.go" | ||
all: true | ||
packages: | ||
github.com/ministryofjustice/opg-data-lpa-uid/lambda/create-case: |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
github.com/brunoscheufler/aws-ecs-metadata-go v0.0.0-20220812150832-b6b31c6eeeaf/go.mod h1:CeKhh8xSs3WZAc50xABMxu+FlfAAd5PNumo7NfOv7EE= | ||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= | ||
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= | ||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= | ||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= | ||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= | ||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYpldc7gVz2KMQwJ/QYCDIa7XU= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= | ||
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= | ||
go.opentelemetry.io/contrib/detectors/aws/ecs v1.25.0/go.mod h1:SCL97Dnj9dTc7OL9AtTPwHc6GOSfD20pTMEe6uU/4w4= | ||
go.opentelemetry.io/contrib/propagators/aws v1.25.0/go.mod h1:HMRyfyD8oIZLpKSXC0zGmZZTuG4qGo6OtZOEu8IQPJc= | ||
go.opentelemetry.io/otel v1.25.0/go.mod h1:Wa2ds5NOXEMkCmUou1WA7ZBfLTHWIsp034OVD7AO+Vg= | ||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.25.0/go.mod h1:h95q0LBGh7hlAC08X2DhSeyIG02YQ0UyioTCVAqRPmc= | ||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.25.0/go.mod h1:8GlBGcDk8KKi7n+2S4BT/CPZQYH3erLu0/k64r1MYgo= | ||
go.opentelemetry.io/otel/metric v1.25.0/go.mod h1:rkDLUSd2lC5lq2dFNrX9LGAbINP5B7WBkC78RXCpH5s= | ||
go.opentelemetry.io/otel/sdk v1.25.0/go.mod h1:oFgzCM2zdsxKzz6zwpTZYLLQsFwc+K0daArPdIhuxkw= | ||
go.opentelemetry.io/otel/trace v1.25.0/go.mod h1:hCCs70XM/ljO+BeQkyFnbK28SBIJ/Emuha+ccrCRT7I= | ||
go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7ej/RNTae6MdY= | ||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= | ||
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0= | ||
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= | ||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= | ||
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= | ||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw= | ||
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= | ||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | ||
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= | ||
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:5iCWqnniDlqZHrd3neWVTOwvh/v6s3232omMecelax8= | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= | ||
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= | ||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log/slog" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
) | ||
|
||
var ( | ||
ErrMetadataChanged = errors.New("metadata has been changed") | ||
ErrUidExists = errors.New("uid already exists") | ||
) | ||
|
||
type LpaType string | ||
|
||
const ( | ||
LpaTypePersonalWelfare LpaType = "personal-welfare" | ||
LpaTypePropertyAndAffairs LpaType = "property-and-affairs" | ||
) | ||
|
||
type LpaSource string | ||
|
||
const ( | ||
LpaSourceApplicant LpaSource = "APPLICANT" | ||
LpaSourcePhone LpaSource = "PHONE" | ||
) | ||
|
||
type Donor struct { | ||
Name string `json:"name" dynamodbav:"name"` | ||
DateOfBirth string `json:"dob" dynamodbav:"dob"` | ||
Postcode string `json:"postcode" dynamodbav:"postcode"` | ||
} | ||
|
||
type Lpa struct { | ||
UID string `dynamodbav:"uid"` | ||
CreatedAt time.Time `dynamodbav:"created_at"` | ||
Type LpaType `dynamodbav:"type"` | ||
Source LpaSource `dynamodbav:"source"` | ||
Donor Donor `dynamodbav:"donor"` | ||
} | ||
|
||
func (l *Lambda) getMaximum(ctx context.Context) (int, error) { | ||
output, err := l.dynamo.GetItem(ctx, &dynamodb.GetItemInput{ | ||
TableName: aws.String(l.tableName), | ||
Key: map[string]types.AttributeValue{ | ||
"uid": &types.AttributeValueMemberS{Value: "#METADATA"}, | ||
}, | ||
AttributesToGet: []string{"Maximum"}, | ||
ConsistentRead: aws.Bool(true), | ||
}) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if output.Item == nil { | ||
return 0, nil | ||
} | ||
|
||
var max int | ||
if err := attributevalue.Unmarshal(output.Item["Maximum"], &max); err != nil { | ||
return 0, err | ||
} | ||
|
||
return max, nil | ||
} | ||
|
||
func (l *Lambda) insertLpa(ctx context.Context, currentMaximum int, req Request) error { | ||
nextUID := formatUID(currentMaximum + 1) | ||
|
||
lpa := Lpa{ | ||
UID: nextUID, | ||
CreatedAt: l.now(), | ||
Type: req.Type, | ||
Source: req.Source, | ||
Donor: req.Donor, | ||
} | ||
|
||
marshalled, err := attributevalue.MarshalMap(lpa) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
transaction := &dynamodb.TransactWriteItemsInput{ | ||
TransactItems: []types.TransactWriteItem{ | ||
{ | ||
Update: &types.Update{ | ||
TableName: aws.String(l.tableName), | ||
Key: map[string]types.AttributeValue{ | ||
"uid": &types.AttributeValueMemberS{Value: "#METADATA"}, | ||
}, | ||
ConditionExpression: aws.String("Maximum = :currentMaximum"), | ||
UpdateExpression: aws.String("SET Maximum = :nextMaximum"), | ||
ExpressionAttributeValues: map[string]types.AttributeValue{ | ||
":currentMaximum": &types.AttributeValueMemberN{Value: strconv.Itoa(currentMaximum)}, | ||
":nextMaximum": &types.AttributeValueMemberN{Value: strconv.Itoa(currentMaximum + 1)}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Put: &types.Put{ | ||
TableName: aws.String(l.tableName), | ||
Item: marshalled, | ||
ConditionExpression: aws.String("attribute_not_exists(uid)"), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
if currentMaximum == 0 { | ||
l.logger.DebugContext(ctx, "inserting metadata", slog.Any("max", currentMaximum+1)) | ||
transaction.TransactItems[0] = types.TransactWriteItem{ | ||
Put: &types.Put{ | ||
TableName: aws.String(l.tableName), | ||
Item: map[string]types.AttributeValue{ | ||
"uid": &types.AttributeValueMemberS{Value: "#METADATA"}, | ||
"Maximum": &types.AttributeValueMemberN{Value: strconv.Itoa(currentMaximum + 1)}, | ||
}, | ||
ConditionExpression: aws.String("attribute_not_exists(uid)"), | ||
}, | ||
} | ||
} | ||
|
||
_, err = l.dynamo.TransactWriteItems(ctx, transaction) | ||
|
||
var tce *types.TransactionCanceledException | ||
if errors.As(err, &tce) { | ||
if *tce.CancellationReasons[0].Code == "ConditionalCheckFailed" { | ||
return ErrMetadataChanged | ||
} | ||
|
||
if *tce.CancellationReasons[1].Code == "ConditionalCheckFailed" { | ||
return ErrUidExists | ||
} | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.