Skip to content

Commit

Permalink
Add ability to destroy a namespace and its contents (#653)
Browse files Browse the repository at this point in the history
Co-authored-by: Bess Sadler <bess@users.noreply.github.com>
Co-authored-by: Carolyn Cole <carolyncole@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 23, 2024
1 parent 4de0389 commit 87783db
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ public
doc

# Ignore test created to not get accidentally added to commit
project_create_8.txt
project_create_8.txt

# Ignore .yardoc
.yardoc/
15 changes: 10 additions & 5 deletions app/models/mediaflux/http/namespace_describe_request.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# frozen_string_literal: true
module Mediaflux
module Http
# Describes a namespace
# @example
# namespace = Mediaflux::Http::NamespaceDescribeRequest.new(session_token: session_id).metadata
# => {:id=>"1", :path=>"/", :name=>"", :description=>"", :store=>"data"}
# @example
# namespace = Mediaflux::Http::NamespaceDescribeRequest.new(session_token: session_id, path: "/td-test-001/tigerdataNS").metadata
# => {:id=>"1182", :path=>"/td-test-001/tigerdataNS", :name=>"tigerdataNS", :description=>"TigerData client app root namespace", :store=>"db"}
class NamespaceDescribeRequest < Request
attr_reader :path, :id

# Constructor
# @param session_token [String] the API token for the authenticated session
# @param name [String] Name of the Asset
# @param collection [Boolean] create a collection asset if true
# @param namespace [String] Optional Parent namespace for the asset to be created in
# @param pid [Integer] Optional Parent id for the asset to be created in
# @param path [String] path of the asset to be described
# @param id [Integer] TODO: Define what this is and how to use it.
def initialize(session_token:, path: nil, id: nil)
super(session_token: session_token)
@path = path
@id = id
end

# Specifies the Mediaflux service to use when creating assets
# Specifies the Mediaflux service to use
# @return [String]
def self.service
"asset.namespace.describe"
Expand Down
44 changes: 44 additions & 0 deletions app/models/mediaflux/http/namespace_destroy_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true
module Mediaflux
module Http
# Destroy a MediaFlux namespace and everything in it
# @example
# Mediaflux::Http::NamespaceDestroyRequest.new(session_token: session_id, namespace: "/td-test-001/tigerdataNS/Banana1NS").destroy
# => true
class NamespaceDestroyRequest < Request
attr_reader :description, :namespace, :store

# Constructor
# @param session_token [String] the API token for the authenticated session
# @param namespace [String] name of namespace to be destroyed
def initialize(session_token:, namespace:)
super(session_token: session_token)
@namespace = namespace
end

def destroy
resolve
if error?
response_error
end
end

# Specifies the Mediaflux service to use when destroying namespaces
# @return [String]
def self.service
"asset.namespace.hard.destroy"
end

private

def build_http_request_body(name:)
super do |xml|
xml.args do
xml.namespace @namespace
xml.atomic true
end
end
end
end
end
end
11 changes: 6 additions & 5 deletions app/models/mediaflux/http/namespace_list_request.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# frozen_string_literal: true
module Mediaflux
module Http
# List all of the namespaces that are inside of a given namespace
# @example
# namespace_list = Mediaflux::Http::NamespaceListRequest.new(session_token: session_id, parent_namespace: "/td-test-001/tigerdataNS").namespaces
# => [{:id=>"1264", :name=>"Avocado1NS"}, {:id=>"1282", :name=>"Banana1NS"}]
class NamespaceListRequest < Request
attr_reader :parent_namespace

# Constructor
# @param session_token [String] the API token for the authenticated session
# @param name [String] Name of the Asset
# @param collection [Boolean] create a collection asset if true
# @param namespace [String] Optional Parent namespace for the asset to be created in
# @param pid [Integer] Optional Parent id for the asset to be created in
# @param session_token [String] the API token for the authenticated session, same as session_id
# @param parent_namespace [String] Parent namespace for the query
def initialize(session_token:, parent_namespace:)
super(session_token: session_token)
@parent_namespace = parent_namespace
Expand Down
1 change: 0 additions & 1 deletion app/models/mediaflux/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def resolved?
# @return [Nokogiri::XML::Document]
def response_xml
resolve unless resolved?

Rails.logger.debug(response_body)
@response_xml ||= Nokogiri::XML.parse(response_body)
Rails.logger.debug(@response_xml)
Expand Down
8 changes: 8 additions & 0 deletions app/models/project_mediaflux.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# frozen_string_literal: true

# Take an instance of Project and adds it to MediaFlux
class ProjectMediaflux
# Create a project in MediaFlux
#
# @param project [Project] the project that needs to be added to MediaFlux
# @param session_id [] the session id for the user who is currently authenticated to MediaFlux
# @param xml_namespace []
# @return [String] The id of the project that got created
def self.create!(project:, session_id:, xml_namespace: nil)
store_name = Store.default(session_id: session_id).name

Expand Down
25 changes: 25 additions & 0 deletions spec/models/mediaflux/http/namespace_destroy_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe Mediaflux::Http::NamespaceDestroyRequest, type: :model, connect_to_mediaflux: true do
let(:valid_project) { FactoryBot.create(:project_with_dynamic_directory, project_id: "10.34770/tbd") }
let(:namespace) { "#{valid_project.directory}NS".strip.gsub(/[^A-Za-z\d]/, "-") }
let(:sponsor_user) { FactoryBot.create(:project_sponsor) }
let(:session_id) { sponsor_user.mediaflux_session }
it "deletes a namespace and everything inside of it" do
valid_project
mediaflux_id = ProjectMediaflux.create!(project: valid_project, session_id: session_id)
expect(mediaflux_id).not_to be_nil
namespace_list = Mediaflux::Http::NamespaceListRequest.new(session_token: session_id, parent_namespace: "/td-test-001/tigerdataNS").namespaces
namespace_names = namespace_list.map { |a| a[:name] }
expect(namespace_names).to include(namespace)

# Destroy the namespace of the project and everything in it
described_class.new(session_token: session_id, namespace: "/td-test-001/tigerdataNS/#{namespace}").destroy
namespace_list = Mediaflux::Http::NamespaceListRequest.new(session_token: session_id, parent_namespace: "/td-test-001/tigerdataNS").namespaces
namespace_names = namespace_list.map { |a| a[:name] }
expect(namespace_names).not_to include(namespace)
response = described_class.new(session_token: session_id, namespace: "/td-test-001/tigerdataNS/#{namespace}").destroy
expect(response[:message]).to include("The namespace '/td-test-001/tigerdataNS/#{namespace}' does not exist or is not accessible")
end
end

0 comments on commit 87783db

Please sign in to comment.