Skip to content

samples(Storage): Add samples and tests for Bucket IP filter#3334

Draft
mahendra-google wants to merge 25 commits into
GoogleCloudPlatform:mainfrom
mahendra-google:bucket-ip-filter
Draft

samples(Storage): Add samples and tests for Bucket IP filter#3334
mahendra-google wants to merge 25 commits into
GoogleCloudPlatform:mainfrom
mahendra-google:bucket-ip-filter

Conversation

@mahendra-google
Copy link
Copy Markdown
Contributor

This PR Introduces code samples and integration tests for the full Bucket IP Filter lifecycle.

Operations Covered are :

Create / Update / Delete: Management of IP filter rule sets.

Get / List: Retrieval of configuration metadata.

Disable / Bypass: Logic for pausing filters or configuring administrative overrides.

- Added CreateBucketWithIpFilterSample for bucket security configuration.
- Added ListBucketIpFiltersSample to demonstrate filter retrieval.
- Included integration tests to verify IP filter logic.
- Addressing gemini review feedback on xml docs.
…tructure, and adjust test teardown registration
…ket ip filter.

- Remove redundant bucket cleanup code from tests
@mahendra-google mahendra-google requested review from a team as code owners May 19, 2026 13:52
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented May 19, 2026

Here is the summary of changes.

You are about to add 7 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples. labels May 19, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a suite of samples and tests for managing Google Cloud Storage bucket IP filtering, covering operations such as creation, updates, deletion, and bypass rules. The review feedback identifies several improvement opportunities: replacing hardcoded project IDs in tests and samples with dynamic values to ensure portability, optimizing the deletion logic to prevent unnecessary API calls when an IP range is missing, and adding a project ID parameter to the update sample for consistent resource name construction.

Assert.NotNull(publicRanges);
Assert.Contains(newPublicRange, publicRanges);
var vpcNetwork = updatedBucket.IpFilter.VpcNetworkSources?
.FirstOrDefault(v => v.Network == "projects/storage-sdk-vendor/global/networks/default");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The project ID storage-sdk-vendor is hardcoded in the VPC network path assertion. This will cause the test to fail in any environment where the project ID differs. Use _fixture.ProjectId to make the test portable.

            .FirstOrDefault(v => v.Network == $"projects/{_fixture.ProjectId}/global/networks/default");

Comment on lines +37 to +39
bucket.IpFilter.PublicNetworkSource.AllowedIpCidrRanges.Remove(ipAddress);
storage.UpdateBucket(bucket);
Console.WriteLine($"IP Address ({ipAddress}) from Allowed CIDR Ranges has been deleted from the bucket: {bucketName}");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The code performs a bucket update and logs a success message even if the specified IP address was not found in the allowed ranges. It is better to check the return value of Remove() to avoid redundant API calls and provide accurate feedback to the user.

        if (bucket.IpFilter.PublicNetworkSource.AllowedIpCidrRanges.Remove(ipAddress))
        {
            storage.UpdateBucket(bucket);
            Console.WriteLine($"IP Address ({ipAddress}) from Allowed CIDR Ranges has been deleted from the bucket: {bucketName}");
        }
        else
        {
            Console.WriteLine($"IP Address ({ipAddress}) was not found in the Allowed CIDR Ranges for bucket: {bucketName}");
        }

Comment on lines +31 to +32
public Bucket UpdateBucketIpFilter(string bucketName = "your-unique-bucket-name", string publicRange = "192.0.2.0/24",
string vpcRange = "10.0.0.0/24")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This sample should accept a projectId parameter to construct the VPC network resource name dynamically, rather than using a hardcoded placeholder like your-project. This ensures consistency with other samples in this pull request, such as CreateBucketWithIpFilterSample.

    public Bucket UpdateBucketIpFilter(string projectId = "your-project-id", string bucketName = "your-unique-bucket-name", string publicRange = "192.0.2.0/24",
        string vpcRange = "10.0.0.0/24")

}
else
{
string networkName = "projects/your-project/global/networks/default";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the projectId parameter to construct the network resource name instead of a hardcoded placeholder.

            string networkName = $"projects/{projectId}/global/networks/default";

string newPublicRange = "192.0.2.0/24";
string newVpcRange = "10.0.0.0/24";
_fixture.CreateBucket(bucketName, multiVersion: false, ipFilter: true, registerForDeletion: true);
var updatedBucket = updateSample.UpdateBucketIpFilter(bucketName, newPublicRange, newVpcRange);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Pass the project ID from the fixture to the updated UpdateBucketIpFilter method.

        var updatedBucket = updateSample.UpdateBucketIpFilter(_fixture.ProjectId, bucketName, newPublicRange, newVpcRange);

@Rebel2169
Copy link
Copy Markdown

Rebel2169 commented May 19, 2026 via email

@mahendra-google
Copy link
Copy Markdown
Contributor Author

my buckets are supposed to be private

Thanks for taking the time to review the code!

Could you please point me to the specific line of code where the buckets are private? I want to make sure I get that fixed and —keeping that data private is definitely a priority for our customers.

Appreciate the help!

@mahendra-google mahendra-google marked this pull request as draft May 20, 2026 05:21
@mahendra-google
Copy link
Copy Markdown
Contributor Author

I have moved this PR to Draft mode for now. Based on the updates in issue #422766398 (comment 8), the backend tags for the Bucket IP Filter samples are not fully ready. I will mark this back as ready for review as soon as those tags are fixed.

Updated the start and end region tags for the listbuckets with ip filter
code samples
…ateBucketIpFilter

Updates the UpdateBucketIpFilter method signature to accept projectId and vpcNetwork parameters instead of relying on a hardcoded string. This allows users to pass in actual configuration values and use dynamic string interpolation.

Updates the test accordingly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants