Skip to content

Commit

Permalink
delete alredy applied lifecycle rules from backup bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
var77 committed Nov 15, 2024
1 parent 7bbdeca commit 99d1bb3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/hosting/gcp_apis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ def resize_vm_disk(zone, disk_source, storage_size_gib)
wait_for_operation(zone, data["id"])
end

def list_objects(bucket, pattern)
def list_objects(bucket, pattern, query = {matchGlob: pattern, delimiter: "/"})
connection = Excon.new("https://storage.googleapis.com", headers: @host[:headers])
query = {matchGlob: pattern, delimiter: "/"}

response = connection.get(path: "/storage/v1/b/#{bucket}/o", query: query, expects: [200, 400])
Hosting::GcpApis.check_errors(response)
Expand Down Expand Up @@ -500,6 +499,22 @@ def add_delete_lifecycle_rule(bucket, prefix)
}
}

# Delete rules that are already applied
if data["lifecycle"]
existing_rules = data["lifecycle"]["rule"]

existing_rules.each do |rule|
rule_prefixes = rule.dig("condition", "matchesPrefix") || []
applied_prefixes = rule_prefixes.select do |rule_prefix|
list_objects(bucket, rule_prefix, {prefix: rule_prefix, maxResults: 1}).any?
end

if applied_prefixes.empty?
data["lifecycle"]["rule"].delete(rule)
end
end
end

if data.empty?
data = {"lifecycle" => {"rule" => [lifecycle_rule]}}
else
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/hosting/gcp_apis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,18 @@
.with(body: JSON.dump({"lifecycle" => {"rule" => [{"action" => {"type" => "Delete"}, "condition" => {age: 0, matchesPrefix: ["test-ubid2"]}}, {"action" => {"type" => "Delete"}, "condition" => {age: 0, matchesPrefix: ["test-ubid"]}}]}}))
.to_return(status: 200, body: JSON.dump({}), headers: {"Content-Type" => "application/json"})
api = described_class.new
expect(api).to receive(:list_objects).and_return([1]).at_least(:once)
expect { api.add_delete_lifecycle_rule("test", "test-ubid") }.not_to raise_error
end

it "adds lifecycle rule and deletes already applied rules" do
stub_request(:post, "https://oauth2.googleapis.com/token").to_return(status: 200, body: JSON.dump({}), headers: {"Content-Type" => "application/json"})
stub_request(:get, "https://storage.googleapis.com/storage/v1/b/test?fields=lifecycle").to_return(status: 200, body: JSON.dump({"lifecycle" => {"rule" => [{"action" => {"type" => "Delete"}, "condition" => {age: 0, matchesPrefix: ["test-ubid2"]}}]}}), headers: {"Content-Type" => "application/json"})
stub_request(:patch, "https://storage.googleapis.com/storage/v1/b/test?fields=lifecycle")
.with(body: JSON.dump({"lifecycle" => {"rule" => [{"action" => {"type" => "Delete"}, "condition" => {age: 0, matchesPrefix: ["test-ubid"]}}]}}))
.to_return(status: 200, body: JSON.dump({}), headers: {"Content-Type" => "application/json"})
api = described_class.new
expect(api).to receive(:list_objects).and_return([]).at_least(:once)
expect { api.add_delete_lifecycle_rule("test", "test-ubid") }.not_to raise_error
end
end
Expand Down

0 comments on commit 99d1bb3

Please sign in to comment.