-
Notifications
You must be signed in to change notification settings - Fork 3
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
2 changed files
with
93 additions
and
0 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 |
---|---|---|
|
@@ -8,3 +8,5 @@ plan.tfplan | |
# IntelliJ IDEA project files | ||
.idea | ||
*.iprg | ||
|
||
.DS_Store |
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,91 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hashicorp/boundary-plugin-aws/internal/credential" | ||
"github.com/hashicorp/boundary-plugin-aws/plugin/service/storage" | ||
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/storagebuckets" | ||
pb "github.com/hashicorp/boundary/sdk/pbs/plugin" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
const ( | ||
localFileName = "testFileForBondary" | ||
localFileSize = 10 | ||
) | ||
|
||
func main() { | ||
region := flag.String("region", "us-east-1", "AWS Region") | ||
bucket := flag.String("bucket", "test-bucket", "AWS Bucket") | ||
arn := flag.String("arn", "", "AWS Role ARN") | ||
access := flag.String("access", "", "AWS Access Key ID") | ||
secret := flag.String("secret", "", "AWS Secret Access Key") | ||
flag.Parse() | ||
|
||
p := new(storage.StoragePlugin) | ||
ctx := context.Background() | ||
|
||
var reqSecrets *structpb.Struct | ||
if *access != "" { | ||
if *secret == "" { | ||
panic("secret is required when accessId is provided") | ||
} | ||
var err error | ||
reqSecrets, err = structpb.NewStruct(map[string]any{ | ||
credential.ConstAccessKeyId: *access, | ||
credential.ConstSecretAccessKey: *secret, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} else if *arn == "" { | ||
panic("arn is required when accessId is not provided") | ||
} | ||
|
||
reqAttrs, err := structpb.NewStruct(map[string]any{ | ||
credential.ConstRegion: *region, | ||
credential.ConstDisableCredentialRotation: true, | ||
credential.ConstRoleArn: *arn, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
f, err := os.Create(localFileName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
_, err = io.Copy(f, io.LimitReader(rand.Reader, localFileSize)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer os.Remove(localFileName) | ||
|
||
// TODO: put loop here | ||
objectKey := uuid.New().String() | ||
putObjReq := &pb.PutObjectRequest{ | ||
Bucket: &storagebuckets.StorageBucket{ | ||
BucketName: *bucket, | ||
Attributes: reqAttrs, | ||
Secrets: reqSecrets, | ||
}, | ||
Key: objectKey, | ||
Path: localFileName, | ||
} | ||
|
||
_, err = p.PutObject(ctx, putObjReq) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println("Done") | ||
} |