Class: Fog::AWS::EFS::Mock

Inherits:
Object
  • Object
show all
Includes:
CredentialFetcher::ConnectionMethods
Defined in:
lib/fog/aws/efs.rb,
lib/fog/aws/requests/efs/create_file_system.rb,
lib/fog/aws/requests/efs/delete_file_system.rb,
lib/fog/aws/requests/efs/create_mount_target.rb,
lib/fog/aws/requests/efs/delete_mount_target.rb,
lib/fog/aws/requests/efs/describe_file_systems.rb,
lib/fog/aws/requests/efs/describe_mount_targets.rb,
lib/fog/aws/requests/efs/modify_mount_target_security_groups.rb,
lib/fog/aws/requests/efs/describe_mount_target_security_groups.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CredentialFetcher::ConnectionMethods

#refresh_credentials_if_expired

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



60
61
62
63
64
# File 'lib/fog/aws/efs.rb', line 60

def initialize(options={})
  @region                = options[:region] || "us-east-1"
  @aws_access_key_id     = options[:aws_access_key_id]
  @aws_secret_access_key = options[:aws_secret_access_key]
end

Instance Attribute Details

#regionObject

Returns the value of attribute region.



58
59
60
# File 'lib/fog/aws/efs.rb', line 58

def region
  @region
end

Class Method Details

.dataObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/aws/efs.rb', line 34

def self.data
  @data ||= Hash.new do |hash, region|
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :file_systems    => {},
        :mount_targets   => {},
        :security_groups => {}
      }
    end
  end
end

.resetObject



46
47
48
# File 'lib/fog/aws/efs.rb', line 46

def self.reset
  @data = nil
end

Instance Method Details

#create_file_system(creation_token, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fog/aws/requests/efs/create_file_system.rb', line 35

def create_file_system(creation_token, options={})
  response = Excon::Response.new

  id = "fs-#{Fog::Mock.random_letters(8)}"
  file_system = {
    "OwnerId"              => Fog::AWS::Mock.owner_id,
    "CreationToken"        => creation_token,
    "PerformanceMode"      => options[:performance_mode] || "generalPurpose",
    "Encrypted"            => options[:encrypted] || false,
    "FileSystemId"         => id,
    "CreationTime"         => Time.now.to_i.to_f,
    "LifeCycleState"       => "creating",
    "NumberOfMountTargets" => 0,
    "SizeInBytes"          => {
      "Value"     => 1024,
      "Timestamp" => Time.now.to_i.to_f
    }
  }
  file_system[:kms_key_id] = options[:kms_key_id] if options.key?(:kms_key_id)

  self.data[:file_systems][id] = file_system
  response.body = file_system
  response.status = 201
  response
end

#create_mount_target(file_system_id, subnet_id, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fog/aws/requests/efs/create_mount_target.rb', line 26

def create_mount_target(file_system_id, subnet_id, options={})
  response               = Excon::Response.new
  default_security_group = mock_compute.data[:security_groups].find do |_, sg|
    sg['groupDescription'] == 'default_elb security group'
  end
  security_groups        = options["SecurityGroups"] || [default_security_group.first]

  unless file_system = self.data[:file_systems][file_system_id]
    raise Fog::AWS::EFS::NotFound.new("invalid file system ID: #{file_system_id}")
  end

  unless file_system["LifeCycleState"] == 'available'
    # this error doesn't include a message for some reason
    raise Fog::AWS::EFS::IncorrectFileSystemLifeCycleState.new("")
  end

  unless subnet = mock_compute.subnets.get(subnet_id)
    raise Fog::AWS::EFS::InvalidSubnet.new("invalid subnet ID: #{subnet_id}")
  end

  security_groups.each do |group_id|
    unless mock_compute.data[:security_groups][group_id]
      raise Fog::AWS::EFS::NotFound.new("invalid security group ID: #{group_id}")
    end
  end

  id = "fsmt-#{Fog::Mock.random_letters(8)}"

  mount_target = {
    'MountTargetId'      => id,
    'FileSystemId'       => file_system_id,
    'IpAddress'          => Fog::AWS::Mock.ip_address,
    'OwnerId'            => Fog::AWS::Mock.owner_id,
    'LifeCycleState'     => 'creating',
    'NetworkInterfaceId' => "eni-#{Fog::Mock.random_hex(8)}",
    'SubnetId'           => subnet.identity,
  }

  self.data[:mount_targets][id]   = mount_target
  self.data[:security_groups][id] = security_groups

  response.body = mount_target
  response
end

#dataObject



50
51
52
# File 'lib/fog/aws/efs.rb', line 50

def data
  self.class.data[@region][@aws_access_key_id]
end

#delete_file_system(id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/aws/requests/efs/delete_file_system.rb', line 23

def delete_file_system(id)
  unless file_system = self.data[:file_systems][id]
    raise Fog::AWS::EFS::NotFound.new("invalid file system ID: #{id}")
  end

  if file_system["NumberOfMountTargets"] > 0
    raise Fog::AWS::EFS::FileSystemInUse.new("")
  end

  self.data[:file_systems].delete(id)

  response = Excon::Response.new
  response.status = 204
  response
end

#delete_mount_target(id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/aws/requests/efs/delete_mount_target.rb', line 23

def delete_mount_target(id)
  response = Excon::Response.new

  unless self.data[:mount_targets][id]
    raise Fog::AWS::EFS::NotFound.new("invalid mount target ID: #{id}")
  end

  self.data[:mount_targets].delete(id)

  response.status = 204
  response
end

#describe_file_systems(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/aws/requests/efs/describe_file_systems.rb', line 35

def describe_file_systems(options={})
  response = Excon::Response.new

  file_systems = if id = options[:id]
                   if fs = self.data[:file_systems][id]
                     [fs]
                   else
                     raise Fog::AWS::EFS::NotFound.new("invalid file system ID: #{id}")
                   end
                 elsif creation_token = options[:creation_token]
                   fs = self.data[:file_systems].values.detect { |file_system| file_system["CreationToken"] == creation_token }
                   [fs]
                 else
                   self.data[:file_systems].values
                 end

  file_systems.each do |file_system|
    file_system['LifeCycleState'] = 'available'
    self.data[:file_systems][file_system['FileSystemId']] = file_system
  end

  response.body = {"FileSystems" => file_systems}
  response
end

#describe_mount_target_security_groups(mount_target_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/aws/requests/efs/describe_mount_target_security_groups.rb', line 21

def describe_mount_target_security_groups(mount_target_id)
  response = Excon::Response.new

  unless self.data[:mount_targets][mount_target_id]
    raise Fog::AWS::EFS::NotFound.new("invalid mount target ID: #{mount_target_id}")
  end

  response.body = {"SecurityGroups" => self.data[:security_groups][mount_target_id]}
  response
end

#describe_mount_targets(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/aws/requests/efs/describe_mount_targets.rb', line 35

def describe_mount_targets(options={})
  response = Excon::Response.new

  mount_targets = if id = options[:id]
                    if mount_target = self.data[:mount_targets][id]
                      [mount_target]
                    else
                      raise Fog::AWS::EFS::NotFound.new("Mount target does not exist.")
                    end
                  elsif file_system_id = options[:file_system_id]
                    self.data[:mount_targets].values.select { |mt| mt["FileSystemId"] == file_system_id }
                  else
                    raise Fog::AWS::EFS::Error.new("file system ID or mount target ID must be specified")
                  end

  mount_targets.each do |mount_target|
    mount_target['LifeCycleState'] = 'available'
    self.data[:mount_targets][mount_target["MountTargetId"]] = mount_target
  end

  response.body = {"MountTargets" => mount_targets}
  response
end

#mock_computeObject



66
67
68
# File 'lib/fog/aws/efs.rb', line 66

def mock_compute
  @mock_compute ||= Fog::AWS::Compute.new(:aws_access_key_id => @aws_access_key_id, :aws_secret_access_key => @aws_secret_access_key, :region => @region)
end

#modify_mount_target_security_groups(id, security_groups) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/aws/requests/efs/modify_mount_target_security_groups.rb', line 16

def modify_mount_target_security_groups(id, security_groups)
  if security_groups.nil? || security_groups.empty?
    raise Fog::AWS::EFS::Error.new("Must provide at least one security group.")
  end

  response = Excon::Response.new

  unless self.data[:mount_targets][id]
    raise Fog::AWS::EFS::NotFound.new("invalid mount target ID: #{id}")
  end

  security_groups.each do |sg|
    raise Fog::AWS::EFS::NotFound.new("invalid security group ID: #{sg}") unless mock_compute.data[:security_groups].values.detect { |sgd| sgd["groupId"] == sg }
  end

  self.data[:security_groups][id] = security_groups

  response.status = 204
  response
end

#resetObject



54
55
56
# File 'lib/fog/aws/efs.rb', line 54

def reset
  self.class.reset
end