Skip to content

Commit e3b4353

Browse files
author
Sebastian Schürmann
committed
refactor: policy generation and cleanup
1 parent 3aa7f1d commit e3b4353

File tree

1 file changed

+43
-59
lines changed

1 file changed

+43
-59
lines changed

typescript/quicksight/lib/quicksight-example-stack.ts

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ import {logicalColumns} from './logical-columns';
88
import {physicalColumns} from './physical-columns';
99

1010
export class QuicksightExampleStack extends Stack {
11-
// location of the manifest json file in the s3 bucket.
12-
// Used by quicksight to discover the csv files.
11+
/**
12+
* location of the manifest json file in the s3 bucket.
13+
* Used by quicksight to discover the csv files.
14+
* */
1315
public static MANIFEST_KEY = 'manifests/manifest.json';
1416
/**
15-
* foo bar
17+
* Name of the datasource in quicksight
1618
*/
1719
public static QUICKSIGHT_DATASOURCE_NAME = 's3DataSourceExample';
20+
/**
21+
* By default, Amazon QuickSight uses a role named aws-quicksight-service-role-v0.
22+
* @see https://docs.aws.amazon.com/lake-formation/latest/dg/qs-integ-lf.html
23+
*/
24+
public static QUICKSIGHT_SERVICE_ROLE = 'aws-quicksight-service-role-v0';
25+
1826

1927
constructor(scope: Construct, id: string, props?: StackProps) {
2028
super(scope, id, props);
@@ -92,14 +100,34 @@ export class QuicksightExampleStack extends Stack {
92100
}
93101
];
94102

95-
// this service role is created automatically when you set up your quicksight account
96-
const quicksightServiceRole = 'aws-quicksight-service-role-v0';
97-
// allow quicksight to access the bucket
98-
const managedPolicy = this.createManagedPolicyForQuicksight(
99-
'quicksightExamplePolicy',
100-
'quicksightExamplePolicy',
101-
bucket.bucketName,
102-
[ quicksightServiceRole ]);
103+
const policyName = 'quicksightExamplePolicy'
104+
const managedPolicy = new CfnManagedPolicy(
105+
this,
106+
policyName,
107+
{
108+
managedPolicyName: policyName,
109+
policyDocument: {
110+
'Statement': [
111+
{
112+
'Effect': 'Allow',
113+
'Action': ['s3:ListAllMyBuckets'],
114+
'Resource': ['arn:aws:s3:::*']
115+
},
116+
{
117+
'Effect': 'Allow',
118+
'Action': ['s3:*'],
119+
'Resource': [
120+
`arn:aws:s3:::${bucket.bucketName}`,
121+
`arn:aws:s3:::${bucket.bucketName}/*`
122+
]
123+
}
124+
],
125+
'Version': '2012-10-17'
126+
},
127+
roles: [ QuicksightExampleStack.QUICKSIGHT_SERVICE_ROLE ]
128+
}
129+
);
130+
103131

104132
const quicksightS3DataSource = new CfnDataSource(
105133
this,
@@ -150,63 +178,19 @@ export class QuicksightExampleStack extends Stack {
150178
}
151179
}
152180
}
153-
181+
const datasetName = 'quicksightExampleDataset';
154182
new CfnDataSet(
155183
this,
156-
'quicksightExampleDataset',
184+
datasetName,
157185
{
158186
awsAccountId: this.account,
159187
physicalTableMap: {[QuicksightExampleStack.QUICKSIGHT_DATASOURCE_NAME]: physicalTableProperties},
160188
logicalTableMap: {[QuicksightExampleStack.QUICKSIGHT_DATASOURCE_NAME]: logicalTableProperties},
161-
name: 'quicksightExampleDataset',
162-
dataSetId: 'quicksightExampleDataset',
189+
name: datasetName,
190+
dataSetId: datasetName,
163191
permissions: quicksightDatasetPermissions,
164192
importMode: 'SPICE'
165193
}
166194
);
167195
}
168-
169-
// Creates a very simple manifest JSON for the QuickSight S3 data source.
170-
public static createS3Manifest(s3BucketName: string): object {
171-
return {
172-
fileLocations: [
173-
{
174-
URIPrefixes: [`s3://${s3BucketName}`]
175-
},
176-
],
177-
globalUploadSettings: {
178-
format: 'CSV',
179-
delimiter: ',',
180-
}
181-
};
182-
}
183-
184-
createManagedPolicyForQuicksight(idManagedPolicy: string, namePolicy: string, bucketName: string, quicksightRoles: string[]): CfnManagedPolicy {
185-
return new CfnManagedPolicy(
186-
this,
187-
idManagedPolicy,
188-
{
189-
managedPolicyName: namePolicy,
190-
policyDocument: {
191-
'Statement': [
192-
{
193-
'Effect': 'Allow',
194-
'Action': ['s3:ListAllMyBuckets'],
195-
'Resource': ['arn:aws:s3:::*']
196-
},
197-
{
198-
'Effect': 'Allow',
199-
'Action': ['s3:*'],
200-
'Resource': [
201-
`arn:aws:s3:::${bucketName}`,
202-
`arn:aws:s3:::${bucketName}/*`
203-
]
204-
}
205-
],
206-
'Version': '2012-10-17'
207-
},
208-
roles: quicksightRoles
209-
}
210-
);
211-
}
212196
}

0 commit comments

Comments
 (0)