-
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.
- Loading branch information
Showing
6 changed files
with
92 additions
and
99 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
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
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,54 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"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/aws" | ||
) | ||
|
||
func WithDynamoDBLogin(region string) DatabaseOption { | ||
return func(t *Database) { | ||
cfg, err := config.LoadDefaultConfig(context.TODO()) | ||
cfg.Region = region | ||
if err != nil { | ||
fmt.Printf("Cannot log into DB: %s", err.Error()) | ||
t.Client = nil | ||
return | ||
} | ||
client := dynamodb.NewFromConfig(cfg) | ||
if client == nil { | ||
fmt.Printf("Cannot log into DB: %s", err.Error()) | ||
t.Client = nil | ||
return | ||
} | ||
t.Client = client | ||
} | ||
} | ||
|
||
func dynamoDBPutItem(client *dynamodb.Client, item TableRecord, table string) error { | ||
_, err := client.DescribeTable( | ||
context.TODO(), &dynamodb.DescribeTableInput{TableName: aws.String(table)}) | ||
if err != nil { | ||
return fmt.Errorf("table error: %s", err.Error()) | ||
} | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
dbItem, err := attributevalue.MarshalMap(item) | ||
|
||
if err != nil { | ||
return fmt.Errorf("cannot marshal item into dynamoDBFormat") | ||
} | ||
_, err = client.PutItem(context.TODO(), &dynamodb.PutItemInput{ | ||
TableName: aws.String(table), Item: dbItem}) | ||
if err != nil { | ||
log.Printf("Couldn't add item to table. Here's why: %v\n", err) | ||
} | ||
return nil | ||
} |
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,13 @@ | ||
package aws | ||
|
||
type CertItem struct { | ||
PrivateKey []byte `dynamodbav:"PrivateKey"` | ||
Cert []byte `dynamodbav:"Cert"` | ||
} | ||
|
||
type TableRecord struct { | ||
CaCert CertItem `dynamodbav:"Ca"` | ||
CeCert CertItem `dynamodbav:"Ce"` | ||
Name string `dynamodbav:"Name"` | ||
CreationDate string `dynamodbav:"CreationDate"` | ||
} |
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