Skip to content

Commit

Permalink
currentModel behaves differently in local and when deployed (#211)
Browse files Browse the repository at this point in the history
* Fix real world issue not found in testing

* Updates to fix time offset as well
  • Loading branch information
kddejong authored May 25, 2023
1 parent 49b1049 commit 5f70b10
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 23 deletions.
19 changes: 12 additions & 7 deletions resources/Time_Offset/cmd/resource/resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resource

import (
"encoding/json"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -47,9 +48,10 @@ func Create(req handler.Request, _ *Model, currentModel *Model) (handler.Progres
// Save the unique identifier in SSM if it exists its a duplicate
ssmParameter := buildSsmParameterString(currentModel)
svc := ssm.New(req.Session)
jsonData, _ := json.Marshal(currentModel)
_, err = svc.PutParameter(&ssm.PutParameterInput{
Name: &ssmParameter,
Value: &id,
Value: aws.String(string(jsonData)),
Overwrite: aws.Bool(false),
Tier: aws.String("Standard"),
Type: aws.String("String"),
Expand Down Expand Up @@ -77,7 +79,7 @@ func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
// See if the SSM parameter exists to determine if the resource exists
ssmParameter := buildSsmParameterString(currentModel)
svc := ssm.New(req.Session)
_, err := svc.GetParameter(&ssm.GetParameterInput{
p, err := svc.GetParameter(&ssm.GetParameterInput{
Name: &ssmParameter,
})
if err != nil {
Expand All @@ -88,8 +90,14 @@ func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
}, nil
}

// determine the offset
t, err := updateTimeWithOffset(currentModel)
err = json.Unmarshal([]byte(*p.Parameter.Value), currentModel)
if err != nil {
return handler.ProgressEvent{
OperationStatus: handler.Failed,
HandlerErrorCode: cloudformation.HandlerErrorCodeInternalFailure,
Message: "Error decoding SSM parameter value",
}, nil
}
if err != nil {
return handler.ProgressEvent{
OperationStatus: handler.Failed,
Expand All @@ -99,9 +107,6 @@ func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
}, nil
}

// convert a timestamp into the model so we have all the attributes
timeToModel(currentModel, t)

response := handler.ProgressEvent{
OperationStatus: handler.Success,
Message: "Read complete",
Expand Down
14 changes: 8 additions & 6 deletions resources/Time_Offset/test/integ.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Resources:
Time:
Type: AwsCommunity::Time::Offset
Properties:
Time: "2023-03-31T18:51:56Z"
OffsetDays: 1

Time:
Type: AwsCommunity::Time::Offset
Properties:
Time: "2023-03-31T18:51:56Z"
OffsetDays: 1
Outputs:
Time:
Value: !GetAtt Time.Utc
19 changes: 19 additions & 0 deletions resources/Time_Static/cmd/resource/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions resources/Time_Static/cmd/resource/resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resource

import (
"encoding/json"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -36,13 +37,15 @@ func Create(req handler.Request, _ *Model, currentModel *Model) (handler.Progres
// Save the unique identifier in SSM if it exists its a duplicate
ssmParameter := buildSsmParameterString(currentModel)
svc := ssm.New(req.Session)
jsonData, _ := json.Marshal(currentModel)
_, err := svc.PutParameter(&ssm.PutParameterInput{
Name: &ssmParameter,
Value: &id,
Value: aws.String(string(jsonData)),
Overwrite: aws.Bool(false),
Tier: aws.String("Standard"),
Type: aws.String("String"),
})

if err != nil {
return handler.ProgressEvent{
OperationStatus: handler.Failed,
Expand All @@ -66,9 +69,10 @@ func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
// See if the SSM parameter exists to determine if the resource exists
ssmParameter := buildSsmParameterString(currentModel)
svc := ssm.New(req.Session)
_, err := svc.GetParameter(&ssm.GetParameterInput{
p, err := svc.GetParameter(&ssm.GetParameterInput{
Name: &ssmParameter,
})

if err != nil {
return handler.ProgressEvent{
OperationStatus: handler.Failed,
Expand All @@ -78,7 +82,14 @@ func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
}

// convert a timestamp into the model so we have all the attributes
timeToModel(currentModel)
err = json.Unmarshal([]byte(*p.Parameter.Value), currentModel)
if err != nil {
return handler.ProgressEvent{
OperationStatus: handler.Failed,
HandlerErrorCode: cloudformation.HandlerErrorCodeInternalFailure,
Message: "Error decoding SSM parameter value",
}, nil
}

response := handler.ProgressEvent{
OperationStatus: handler.Success,
Expand Down
2 changes: 1 addition & 1 deletion resources/Time_Static/makebuild
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
.PHONY: build
build:
cfn generate
env GOOS=linux go build -ldflags="-s -w" -tags="$(TAGS)" -o bin/handler cmd/main.go
env GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -tags="$(TAGS)" -o bin/handler cmd/main.go
2 changes: 1 addition & 1 deletion resources/Time_Static/resource-role-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Resources:
- "ssm:DeleteParameter"
- "ssm:GetParameter"
- "ssm:PutParameter"
Resource: !Sub "arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/CloudFormation/AwsCommunity/Time/Static/*"
Resource: "*"
Outputs:
ExecutionRoleArn:
Value:
Expand Down
2 changes: 1 addition & 1 deletion resources/Time_Static/resource-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Resources:
- "ssm:DeleteParameter"
- "ssm:GetParameter"
- "ssm:PutParameter"
Resource: !Sub "arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/CloudFormation/AwsCommunity/Time/Static/*"
Resource: "*"
Outputs:
ExecutionRoleArn:
Value:
Expand Down
11 changes: 7 additions & 4 deletions resources/Time_Static/test/integ.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Resources:
Time:
Type: AwsCommunity::Time::Static
Properties:
Time: "2023-03-31T18:51:56Z"
Time:
Type: AwsCommunity::Time::Static
Properties:
Time: "2023-03-31T18:51:56Z"
Outputs:
Time:
Value: !GetAtt Time.Utc

0 comments on commit 5f70b10

Please sign in to comment.