Skip to content

Commit f86128d

Browse files
committed
feat: fetch fresh bucket metadata to display detailed IP filter information with error handling
1 parent 832736b commit f86128d

1 file changed

Lines changed: 28 additions & 21 deletions

File tree

storage/listBucketIpFilters.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,36 @@ function main(projectId = 'my-project-id') {
3232
const [buckets] = await storage.getBuckets();
3333

3434
for (const bucket of buckets) {
35-
const ipFilter = bucket.metadata?.ipFilter;
36-
if (ipFilter) {
37-
console.log(`${bucket.name}: IP Filter Mode - ${ipFilter.mode}`);
35+
if (bucket.metadata?.ipFilter) {
36+
try {
37+
const [metadata] = await storage.bucket(bucket.name).getMetadata();
38+
const ipFilter = metadata.ipFilter;
39+
console.log(`${bucket.name}: IP Filter Mode - ${ipFilter.mode}`);
3840

39-
const publicNetworkSource = ipFilter.publicNetworkSource;
40-
if (publicNetworkSource && publicNetworkSource.allowedIpCidrRanges) {
41-
console.log(' Public Network Allowed IP Ranges:');
42-
publicNetworkSource.allowedIpCidrRanges.forEach(range => {
43-
console.log(` - ${range}`);
44-
});
45-
}
41+
const publicNetworkSource = ipFilter.publicNetworkSource;
42+
if (publicNetworkSource && publicNetworkSource.allowedIpCidrRanges) {
43+
console.log(' Public Network Allowed IP Ranges:');
44+
publicNetworkSource.allowedIpCidrRanges.forEach(range => {
45+
console.log(` - ${range}`);
46+
});
47+
}
4648

47-
const vpcNetworkSources = ipFilter.vpcNetworkSources;
48-
if (vpcNetworkSources && vpcNetworkSources.length > 0) {
49-
console.log(' VPC Network Sources:');
50-
vpcNetworkSources.forEach(source => {
51-
console.log(` - Network: ${source.network}`);
52-
if (source.allowedIpCidrRanges) {
53-
source.allowedIpCidrRanges.forEach(range => {
54-
console.log(` - ${range}`);
55-
});
56-
}
57-
});
49+
const vpcNetworkSources = ipFilter.vpcNetworkSources;
50+
if (vpcNetworkSources && vpcNetworkSources.length > 0) {
51+
console.log(' VPC Network Sources:');
52+
vpcNetworkSources.forEach(source => {
53+
console.log(` - Network: ${source.network}`);
54+
if (source.allowedIpCidrRanges) {
55+
source.allowedIpCidrRanges.forEach(range => {
56+
console.log(` - ${range}`);
57+
});
58+
}
59+
});
60+
}
61+
} catch (err) {
62+
console.log(
63+
`${bucket.name}: Error fetching IP filter - ${err.message}`
64+
);
5865
}
5966
}
6067
}

0 commit comments

Comments
 (0)