Class: Fog::AWS::ECS::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/ecs.rb,
lib/fog/aws/requests/ecs/run_task.rb,
lib/fog/aws/requests/ecs/stop_task.rb,
lib/fog/aws/requests/ecs/list_tasks.rb,
lib/fog/aws/requests/ecs/start_task.rb,
lib/fog/aws/requests/ecs/list_clusters.rb,
lib/fog/aws/requests/ecs/list_services.rb,
lib/fog/aws/requests/ecs/create_cluster.rb,
lib/fog/aws/requests/ecs/create_service.rb,
lib/fog/aws/requests/ecs/delete_cluster.rb,
lib/fog/aws/requests/ecs/delete_service.rb,
lib/fog/aws/requests/ecs/describe_tasks.rb,
lib/fog/aws/requests/ecs/update_service.rb,
lib/fog/aws/requests/ecs/describe_clusters.rb,
lib/fog/aws/requests/ecs/describe_services.rb,
lib/fog/aws/requests/ecs/list_task_definitions.rb,
lib/fog/aws/requests/ecs/describe_task_definition.rb,
lib/fog/aws/requests/ecs/list_container_instances.rb,
lib/fog/aws/requests/ecs/register_task_definition.rb,
lib/fog/aws/requests/ecs/deregister_task_definition.rb,
lib/fog/aws/requests/ecs/describe_container_instances.rb,
lib/fog/aws/requests/ecs/deregister_container_instance.rb,
lib/fog/aws/requests/ecs/list_task_definition_families.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



162
163
164
165
166
167
168
169
# File 'lib/fog/aws/ecs.rb', line 162

def initialize(options={})
  @use_iam_profile = options[:use_iam_profile]
  @region          = options[:region] || 'us-east-1'

  Fog::AWS.validate_region!(@region)

  setup_credentials(options)
end

Instance Attribute Details

#regionObject

Returns the value of attribute region.



160
161
162
# File 'lib/fog/aws/ecs.rb', line 160

def region
  @region
end

Class Method Details

.dataObject



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fog/aws/ecs.rb', line 142

def self.data
  @data ||= Hash.new do |hash, region|
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :clusters => [],
        :task_definitions => [],
        :services => [],
        :container_instances => [],
        :tasks => []
      }
    end
  end
end

.resetObject



156
157
158
# File 'lib/fog/aws/ecs.rb', line 156

def self.reset
  @data = nil
end

Instance Method Details

#create_cluster(params = {}) ⇒ Object



24
25
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
# File 'lib/fog/aws/requests/ecs/create_cluster.rb', line 24

def create_cluster(params={})
  response = Excon::Response.new
  response.status = 200

  params.has_key?('clusterName') || params['clusterName'] = 'default'

  owner_id = Fog::AWS::Mock.owner_id
  cluster_name = params['clusterName']
  cluster_path = "cluster/#{cluster_name}"
  cluster_arn = Fog::AWS::Mock.arn('ecs', owner_id, cluster_path, region)
  cluster = {}

  search_cluster_result = self.data[:clusters].select { |c| c['clusterName'].eql?(cluster_name) }
  if search_cluster_result.empty?
    cluster = {
      'clusterName'                       => cluster_name,
      'clusterArn'                        => cluster_arn,
      'status'                            => 'ACTIVE',
      'registeredContainerInstancesCount' => 0,
      'runningTasksCount'                 => 0,
      'pendingTasksCount'                 => 0
    }
    self.data[:clusters] << cluster
  else
    cluster = search_cluster_result.first
  end

  response.body = {
    'CreateClusterResult' => {
      'cluster' => cluster
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#create_service(params = {}) ⇒ Object

Raises:

  • (e)


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fog/aws/requests/ecs/create_service.rb', line 33

def create_service(params={})
  response = Excon::Response.new
  response.status = 200

  e = Fog::AWS::ECS::Error
  msg = 'ClientException => desiredCount cannot be empty.'
  raise e, msg unless desired_count = params['desiredCount']
  msg = 'ClientException => serviceName cannot be empty.'
  raise e unless service_name = params['serviceName']
  msg = 'ClientException => taskDefinition cannot be empty.'
  raise e unless task_definition = params['taskDefinition']

  owner_id = Fog::AWS::Mock.owner_id

  service_path = "service/#{service_name}"
  service_arn = Fog::AWS::Mock.arn('ecs', owner_id, service_path, region)

  cluster = params['cluster'] || 'default'
  if !cluster.match(/^arn:aws:ecs:.+:.+:cluster\/(.+)$/)
    cluster_path = "cluster/#{cluster}"
    cluster_arn = Fog::AWS::Mock.arn('ecs', owner_id, cluster_path, region)
  else
    cluster_arn = cluster
  end

  if params['role']
    role = params['role'] if params['role']
    if !role.match(/^arn:aws:iam:.*:.*:role\/(.+)$/)
      role_path = "role/#{role}"
      role_arn = Fog::AWS::Mock.arn('iam', owner_id, role_path, region)
    else
      role_arn = role
    end
  end

  if !task_definition.match(/^arn:aws:ecs:.+:.+:task-definition\/.+$/)
    task_def_path = "task-definition\/#{task_definition}"
    task_def_arn = Fog::AWS::Mock.arn('ecs', owner_id, task_def_path, region)
  else
    task_def_arn = task_definition
  end

  load_balancers = params['loadBalancers'] || []

  service = {
    'events'         => [],
    'serviceName'    => service_name,
    'serviceArn'     => service_arn,
    'taskDefinition' => task_def_arn,
    'clusterArn'     => cluster_arn,
    'status'         => 'ACTIVE',
    'roleArn'        => role_arn,
    'loadBalancers'  => [*load_balancers],
    'deployments'    => [],
    'desiredCount'   => desired_count,
    'pendingCount'   => 0,
    'runningCount'   => 0
  }

  service['deployments'] << {
    'updatedAt'      => Time.now.utc,
    'id'             => "ecs-svc/#{Fog::Mock.random_numbers(19)}",
    'taskDefinition' => task_def_arn,
    'status'         => 'PRIMARY',
    'desiredCount'   => desired_count,
    'createdAt'      => Time.now.utc,
    'pendingCount'   => 0,
    'runningCount'   => 0
  }

  self.data[:services] << service

  response.body = {
    'CreateServiceResult' => {
      'service' => service,
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#dataObject



171
172
173
# File 'lib/fog/aws/ecs.rb', line 171

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

#delete_cluster(params = {}) ⇒ Object



24
25
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
# File 'lib/fog/aws/requests/ecs/delete_cluster.rb', line 24

def delete_cluster(params={})
  response = Excon::Response.new
  response.status = 200

  cluster_id = params.delete('cluster')

  if !cluster_id
    message = 'ClientException => Cluster can not be blank.'
    raise Fog::AWS::ECS::Error, message
  end

  if match = cluster_id.match(/^arn:aws:ecs:.+:\d{1,12}:cluster\/(.+)$/)
    i = self.data[:clusters].index { |c| c['clusterArn'].eql?(cluster_id) }
  else
    i = self.data[:clusters].index { |c| c['clusterName'].eql?(cluster_id) }
  end

  if i
    cluster = self.data[:clusters].delete_at(i)
  else
    raise Fog::AWS::ECS::NotFound, 'Cluster not found.'
  end

  cluster['status'] = 'INACTIVE'
  response.body = {
    'DeleteClusterResult' => {
      'cluster' => cluster
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#delete_service(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


25
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
# File 'lib/fog/aws/requests/ecs/delete_service.rb', line 25

def delete_service(params={})
  response = Excon::Response.new
  response.status = 200

  service_id = params.delete('service')
  msg = 'ClientException => Service cannot be empty.'
  raise Fog::AWS::ECS::Error, msg unless service_id

  owner_id = Fog::AWS::Mock.owner_id

  cluster = params.delete('cluster') || 'default'
  if !cluster.match(/^arn:aws:ecs:.+:.+:cluster\/(.+)$/)
    cluster_path = "cluster/#{cluster}"
    cluster_arn = Fog::AWS::Mock.arn('ecs', owner_id, cluster_path, region)
  else
    cluster_arn = cluster
  end

  if match = service_id.match(/^arn:aws:ecs:.+:\d{1,12}:service\/(.+)$/)
    i = self.data[:services].index do |s|
      s['clusterArn'].eql?(cluster_arn) && s['serviceArn'].eql?(service_id)
    end
  else
    i = self.data[:services].index do |s|
      s['clusterArn'].eql?(cluster_arn) && s['serviceName'].eql?(service_id)
    end
  end

  msg = "ServiceNotFoundException => Service not found."
  raise Fog::AWS::ECS::Error, msg unless i

  service = self.data[:services][i]
  self.data[:services].delete_at(i)

  response.body = {
    'DeleteServiceResult' => {
      'service' => service
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#deregister_container_instance(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


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
# File 'lib/fog/aws/requests/ecs/deregister_container_instance.rb', line 26

def deregister_container_instance(params={})
  response = Excon::Response.new
  response.status = 200

  instance_id = params.delete('containerInstance')
  instance_error = "ClientException => Container instance can not be blank."
  raise Fog::AWS::ECS::Error, instance_error unless instance_id

  if match = instance_id.match(/^arn:aws:ecs:.+:\d{1,12}:container-instance\/(.+)$/)
    i = self.data[:container_instances].index do |inst|
      inst['containerInstanceArn'].eql?(instance_id)
    end
  else
    i = self.data[:container_instances].index do |inst|
      inst['containerInstanceArn'].match(/#{instance_id}$/)
    end
  end

  msg = "ClientException => Referenced container instance #{instance_id} not found."
  raise Fog::AWS::ECS::Error, msg unless i

  instance = self.data[:container_instances][i]
  self.data[:container_instances].delete_at(i)

  response.body = {
    'DeregisterContainerInstanceResult' => {
      'containerInstance' => instance
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#deregister_task_definition(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


24
25
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
# File 'lib/fog/aws/requests/ecs/deregister_task_definition.rb', line 24

def deregister_task_definition(params={})
  response = Excon::Response.new
  response.status = 200

  taskdef_error = "ClientException => Task Definition can not be blank."
  raise Fog::AWS::ECS::Error, taskdef_error unless params['taskDefinition']

  task_def_name = params['taskDefinition']

  case task_def_name
  when /^arn:aws:ecs:.+:\d{1,12}:task-definition\/(.+:.+)$/
    i = self.data[:task_definitions].index { |t| t['taskDefinitionArn'].eql?(task_def_name) }
  when /^(.+:.+)$/
    i = self.data[:task_definitions].index { |t| t['taskDefinitionArn'].match(/task-definition\/#{task_def_name}$/) }
  else
    raise Fog::AWS::ECS::Error, 'Invalid task definition'
  end

  raise Fog::AWS::ECS::NotFound, 'Task definition not found.' unless i
  task_definition = self.data[:task_definitions].delete_at(i)

  response.body = {
    'DeregisterTaskDefinitionResult' => {
      'taskDefinition' => task_definition
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#describe_clusters(params = {}) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/aws/requests/ecs/describe_clusters.rb', line 29

def describe_clusters(params={})
  response = Excon::Response.new
  response.status = 200

  members = params.delete('clusters')
  members = 'default' unless members
  clusters = []
  failures = []

  [*members].each do |c|
    if match = c.match(/^arn:aws:ecs:.+:\d{1,12}:cluster\/(.+)$/)
      result = self.data[:clusters].select { |cl| cl['clusterArn'].eql?(c) }
    else
      result = self.data[:clusters].select { |cl| cl['clusterName'].eql?(c) }
    end
    if result.empty?
      cluster_name = match[1] if match
      cluster_name = c        unless match
      failures << { 'name' => cluster_name }
    else
      clusters << result.first
    end
  end

  owner_id = Fog::AWS::Mock.owner_id

  failures.map! do |f|
    {
      'arn' => Fog::AWS::Mock.arn('ecs', owner_id, "cluster/#{f['name']}", region),
      'reason' => 'MISSING'
    }
  end
  clusters.map! do |c|
    {
      'clusterName' => c['clusterName'],
      'clusterArn'  => c['clusterArn'],
      'status'      => c['status']
    }
  end

  response.body = {
    'DescribeClustersResult' => {
      'failures' => failures,
      'clusters' => clusters
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#describe_container_instances(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


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
# File 'lib/fog/aws/requests/ecs/describe_container_instances.rb', line 30

def describe_container_instances(params={})
  response = Excon::Response.new
  response.status = 200

  cluster = params.delete('cluster') || 'default'

  instances_id = params.delete('containerInstances')
  msg = 'ClientException => Container instance cannot be empty.'
  raise Fog::AWS::ECS::Error, msg unless instances_id

  result = []
  [*instances_id].each do |inst|
    if match = inst.match(/^arn:aws:ecs:.+:\d{1,12}:container-instance\/(.+)$/)
      result = self.data[:container_instances].select { |i| i['containerInstanceArn'].eql?(inst) }
    else
      result = self.data[:container_instances].select { |i| i['containerInstanceArn'].match(/#{inst}$/) }
    end
  end

  instances = result
  response.body = {
    'DescribeContainerInstancesResult' => {
      'containerInstances' => instances,
      'failures'           => []
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#describe_services(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


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
70
# File 'lib/fog/aws/requests/ecs/describe_services.rb', line 30

def describe_services(params={})
  response = Excon::Response.new
  response.status = 200

  cluster = params.delete('cluster') || 'default'
  services = params.delete('services')
  msg = 'InvalidParameterException => Services cannot be empty.'
  raise Fog::AWS::ECS::Error, msg unless services

  owner_id = Fog::AWS::Mock.owner_id

  if !cluster.match(/^arn:aws:ecs:.+:.+:cluster\/(.+)$/)
    cluster_path = "cluster/#{cluster}"
    cluster_arn = Fog::AWS::Mock.arn('ecs', owner_id, cluster_path, region)
  else
    cluster_arn = cluster
  end

  result = []
  ([*services].select { |s| s.match(/^arn:/) }).each do |ds|
    result.concat(self.data[:services].select do |sv|
      sv['serviceArn'].eql?(ds) && sv['clusterArn'].eql?(cluster_arn)
    end)
  end
  ([*services].select { |s| !s.match(/^arn:/) }).each do |ds|
    result.concat(self.data[:services].select do |sv|
      sv['serviceName'].eql?(ds) && sv['clusterArn'].eql?(cluster_arn)
    end)
  end

  response.body = {
    'DescribeServicesResult' => {
      'services' => result,
      'failures' => []
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#describe_task_definition(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


24
25
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
# File 'lib/fog/aws/requests/ecs/describe_task_definition.rb', line 24

def describe_task_definition(params={})
  response = Excon::Response.new
  response.status = 200

  taskdef_error = "ClientException => Task Definition can not be blank."
  raise Fog::AWS::ECS::Error, taskdef_error unless params['taskDefinition']

  task_def_name = params['taskDefinition']

  case task_def_name
  when /^arn:aws:ecs:.+:\d{1,12}:task-definition\/(.+:.+)$/
    result = self.data[:task_definitions].select { |t| t['taskDefinitionArn'].eql?(task_def_name) }
  when /^(.+:.+)$/
    result = self.data[:task_definitions].select { |t| t['taskDefinitionArn'].match(/task-definition\/#{task_def_name}/) }
  else
    result = self.data[:task_definitions].select { |t| t['family'].eql?(task_def_name) }
    if !result.empty?
      result = [] << (result.max_by { |t| t['revision'] })
    end
  end

  if result.empty?
    raise Fog::AWS::ECS::Error, 'ClientException => Unable to describe task definition.'
  end

  task_definition = result.first

  response.body = {
    'DescribeTaskDefinitionResult' => {
      'taskDefinition' => task_definition
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#describe_tasks(params = {}) ⇒ Object



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
# File 'lib/fog/aws/requests/ecs/describe_tasks.rb', line 30

def describe_tasks(params={})
  response = Excon::Response.new
  response.status = 200

  unless tasks = params.delete('tasks')
    msg = 'InvalidParameterException => Tasks cannot be empty.'
    raise Fog::AWS::ECS::Error, msg
  end

  cluster = params.delete('cluster') || 'default'

  result = []
  [*tasks].each do |tid|
    if match = tid.match(/^arn:aws:ecs:.+:\d{1,12}:task\/(.+)$/)
      result = self.data[:tasks].select { |t| t['taskArn'].eql?(tid) }
    else
      result = self.data[:tasks].select { |t| t['taskArn'].match(/#{tid}$/) }
    end
  end

  tasks = result
  response.body = {
    'DescribeTasksResult' => {
      'failures' => [],
      'tasks' => tasks
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#list_clusters(params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/aws/requests/ecs/list_clusters.rb', line 26

def list_clusters(params={})
  response = Excon::Response.new
  response.status = 200

  cluster_arns = self.data[:clusters].map { |c| c['clusterArn'] }

  response.body = {
    'ListClustersResult' => {
      'clusterArns' => cluster_arns
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#list_container_instances(params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/aws/requests/ecs/list_container_instances.rb', line 27

def list_container_instances(params={})
  response = Excon::Response.new
  response.status = 200

  instance_arns = self.data[:container_instances].map { |i| i['containerInstanceArn'] }

  response.body = {
    'ListContainerInstancesResult' => {
      'containerInstanceArns' => instance_arns
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#list_services(params = {}) ⇒ Object



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
# File 'lib/fog/aws/requests/ecs/list_services.rb', line 27

def list_services(params={})
  response = Excon::Response.new
  response.status = 200

  owner_id = Fog::AWS::Mock.owner_id

  cluster = params.delete('cluster') || 'default'
  if !cluster.match(/^arn:aws:ecs:.+:.+:cluster\/(.+)$/)
    cluster_path = "cluster/#{cluster}"
    cluster_arn = Fog::AWS::Mock.arn('ecs', owner_id, cluster_path, region)
  else
    cluster_arn = cluster
  end

  result = self.data[:services].select do |s|
    s['clusterArn'].eql?(cluster_arn)
  end
  service_arns = result.map { |s| s['serviceArn'] }

  response.body = {
    'ListServicesResult' => {
      'serviceArns' => service_arns
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#list_task_definition_families(params = {}) ⇒ Object



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
# File 'lib/fog/aws/requests/ecs/list_task_definition_families.rb', line 27

def list_task_definition_families(params={})
  response = Excon::Response.new
  response.status = 200

  family_prefix = params['familyPrefix']

  if family_prefix
    result = self.data[:task_definitions].select do |t|
      t['family'].match(/^#{family_prefix}/)
    end
  else
    result = self.data[:task_definitions].dup
  end
  result.map! { |t| t['family'] }
  result.uniq!

  response.body = {
    'ListTaskDefinitionFamiliesResult' => {
      'families' => result
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#list_task_definitions(params = {}) ⇒ Object



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
# File 'lib/fog/aws/requests/ecs/list_task_definitions.rb', line 27

def list_task_definitions(params={})
  if %w(
    familyPrefix
    maxResults
    nextToken
    ).any? { |k| params.has_key?(k) }
    Fog::Logger.warning("list_task_definitions filters are not yet mocked [light_black](#{caller.first})[/]")
    Fog::Mock.not_implemented
  end

  response = Excon::Response.new
  response.status = 200

  taskdef_arns = self.data[:task_definitions].map { |c| c['taskDefinitionArn'] }

  response.body = {
    'ListTaskDefinitionsResult' => {
      'taskDefinitionArns' => taskdef_arns
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#list_tasks(params = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/aws/requests/ecs/list_tasks.rb', line 31

def list_tasks(params={})
  response = Excon::Response.new
  response.status = 200

  task_arns = self.data[:tasks].map { |t| t['taskArn'] }

  response.body = {
    'ListTasksResult' => {
      'taskArns' => task_arns
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#register_task_definition(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


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
# File 'lib/fog/aws/requests/ecs/register_task_definition.rb', line 30

def register_task_definition(params={})
  response = Excon::Response.new
  response.status = 200

  family_error = 'ClientException => Family can not be blank.'
  container_error = 'ClientException => Container list cannot be empty.'
  raise Fog::AWS::ECS::Error, family_error    unless params['family']
  raise Fog::AWS::ECS::Error, container_error unless params['containerDefinitions']

  owner_id = Fog::AWS::Mock.owner_id
  taskdef_name = params['family']
  taskdef_rev = (1..9).to_a.shuffle.first
  taskdef_path = "task-definition/#{taskdef_name}:#{taskdef_rev}"
  taskdef_arn = Fog::AWS::Mock.arn('ecs', owner_id, taskdef_path, region)

  task_definition = {
    'revision'             => taskdef_rev,
    'taskDefinitionArn'    => taskdef_arn,
    'family'               => params['family'],
    'containerDefinitions' => params['containerDefinitions']
  }
  task_definition['volumes'] = params['volumes'] if params['volumes']

  self.data[:task_definitions] << task_definition

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'RegisterTaskDefinitionResult' => {
      'taskDefinition' => task_definition
    }
  }
  response
end

#reset_dataObject



175
176
177
# File 'lib/fog/aws/ecs.rb', line 175

def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end

#run_task(params = {}) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fog/aws/requests/ecs/run_task.rb', line 39

def run_task(params={})
  response = Excon::Response.new
  response.status = 200

  unless task_def_id = params.delete('taskDefinition')
    msg = 'ClientException => TaskDefinition cannot be empty.'
    raise Fog::AWS::ECS::Error, msg
  end

  begin
    result = describe_task_definition('taskDefinition' => task_def_id).body
    task_def = result["DescribeTaskDefinitionResult"]["taskDefinition"]
    task_def_arn = task_def["taskDefinitionArn"]
  rescue Fog::AWS::ECS::Error => e
    msg = 'ClientException => TaskDefinition not found.'
    raise Fog::AWS::ECS::Error, msg
  end

  if %w(count overrides).any? { |k| params.has_key?(k) }
    Fog::Logger.warning("you used parameters not mocked yet [light_black](#{caller.first})[/]")
    Fog::Mock.not_implemented
  end

  cluster_id = params.delete('cluster') || 'default'
  cluster_arn = nil
  owner_id = Fog::AWS::Mock.owner_id

  if cluster_id.match(/^arn:aws:ecs:.+:\d{1,12}:cluster\/(.+)$/)
    cluster_arn = cluster_id
  else
    cluster_path = "cluster/#{cluster_id}"
    cluster_arn = Fog::AWS::Mock.arn('ecs', owner_id, cluster_path, region)
  end

  task_path = "task/#{UUID.uuid}"
  task_arn = Fog::AWS::Mock.arn('ecs', owner_id, task_path, region)
  instance_path = "container-instance/#{UUID.uuid}"
  container_instance_arn = Fog::AWS::Mock.arn('ecs', owner_id, instance_path, region)

  containers = []
  task_def["containerDefinitions"].each do |c|
    container_path = "container/#{UUID.uuid}"
    containers << {
      'name'         => c['name'],
      'taskArn'      => task_arn,
      'lastStatus'   => 'PENDING',
      'containerArn' => Fog::AWS::Mock.arn('ecs', owner_id, container_path, region)
    }
  end

  task = {
    'clusterArn'           => cluster_arn,
    'desiredStatus'        => 'RUNNING',
    'taskDefinitionArn'    => task_def_arn,
    'lastStatus'           => 'PENDING',
    'taskArn'              => task_arn,
    'containerInstanceArn' => container_instance_arn,
    'containers'           => containers
  }
  self.data[:tasks] << task

  response.body = {
    'RunTaskResult' => {
      'failures' => [],
      'tasks' => [] << task
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#setup_credentials(options) ⇒ Object



179
180
181
# File 'lib/fog/aws/ecs.rb', line 179

def setup_credentials(options)
  @aws_access_key_id = options[:aws_access_key_id]
end

#start_task(params = {}) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fog/aws/requests/ecs/start_task.rb', line 43

def start_task(params={})
  response = Excon::Response.new
  response.status = 200

  unless task_def_id = params.delete('taskDefinition')
    msg = 'ClientException => TaskDefinition cannot be empty.'
    raise Fog::AWS::ECS::Error, msg
  end

  unless instances_id = params.delete('containerInstances')
    msg = 'ClientException => Container instances cannot be empty.'
    raise Fog::AWS::ECS::Error, msg
  end

  begin
    result = describe_task_definition('taskDefinition' => task_def_id).body
    task_def = result["DescribeTaskDefinitionResult"]["taskDefinition"]
    task_def_arn = task_def["taskDefinitionArn"]
  rescue Fog::AWS::ECS::Error => e
    msg = 'ClientException => TaskDefinition not found.'
    raise Fog::AWS::ECS::Error, msg
  end

  if %w(startedBy overrides).any? { |k| params.has_key?(k) }
    Fog::Logger.warning("you used parameters not mocked yet [light_black](#{caller.first})[/]")
    Fog::Mock.not_implemented
  end

  cluster_id = params.delete('cluster') || 'default'
  cluster_arn = nil
  owner_id = Fog::AWS::Mock.owner_id

  if cluster_id.match(/^arn:aws:ecs:.+:\d{1,12}:cluster\/(.+)$/)
    cluster_arn = cluster_id
  else
    cluster_path = "cluster/#{cluster_id}"
    cluster_arn = Fog::AWS::Mock.arn('ecs', owner_id, cluster_path, region)
  end

  task_path = "task/#{UUID.uuid}"
  task_arn = Fog::AWS::Mock.arn('ecs', owner_id, task_path, region)
  instance_path = "container-instance/#{UUID.uuid}"

  instance_id = [*instances_id].first
  if instance_id.match(/^arn:aws:ecs:.+:\d{1,12}:container-instance\/(.+)$/)
    container_instance_arn = instance_id
  else
    instance_path = "container-instance/#{instance_id}"
    container_instance_arn = Fog::AWS::Mock.arn('ecs', owner_id, instance_path, region)
  end

  containers = []
  task_def["containerDefinitions"].each do |c|
    container_path = "container/#{UUID.uuid}"
    containers << {
      'name'         => c['name'],
      'taskArn'      => task_arn,
      'lastStatus'   => 'PENDING',
      'containerArn' => Fog::AWS::Mock.arn('ecs', owner_id, container_path, region)
    }
  end

  task = {
    'clusterArn'           => cluster_arn,
    'desiredStatus'        => 'RUNNING',
    'taskDefinitionArn'    => task_def_arn,
    'lastStatus'           => 'PENDING',
    'taskArn'              => task_arn,
    'containerInstanceArn' => container_instance_arn,
    'containers'           => containers
  }
  self.data[:tasks] << task

  response.body = {
    'StartTaskResult' => {
      'failures' => [],
      'tasks' => [] << task
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#stop_task(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


25
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
# File 'lib/fog/aws/requests/ecs/stop_task.rb', line 25

def stop_task(params={})
  response = Excon::Response.new
  response.status = 200

  unless task_id = params.delete('task')
    msg = "InvalidParameterException => Task can not be blank."
    raise Fog::AWS::ECS::Error, msg
  end

  if cluster = params.delete('cluster')
    Fog::Logger.warning("you used parameters not mocked yet [light_black](#{caller.first})[/]")
  end

  if match = task_id.match(/^arn:aws:ecs:.+:\d{1,12}:task\/(.+)$/)
    i = self.data[:tasks].index { |t| t['taskArn'].eql?(task_id) }
  else
    i = self.data[:tasks].index { |t| t['taskArn'].match(/#{task_id}$/) }
  end

  msg = "ClientException => The referenced task was not found."
  raise Fog::AWS::ECS::Error, msg unless i

  task = self.data[:tasks][i]
  task['desiredStatus'] = 'STOPPED'
  self.data[:tasks].delete_at(i)

  response.body = {
    'StopTaskResult' => {
      'task' => task
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end

#update_service(params = {}) ⇒ Object

Raises:

  • (Fog::AWS::ECS::Error)


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
70
71
72
73
74
75
76
77
# File 'lib/fog/aws/requests/ecs/update_service.rb', line 27

def update_service(params={})
  response = Excon::Response.new
  response.status = 200

  service_id = params.delete('service')
  msg = 'ClientException => Service cannot be empty.'
  raise Fog::AWS::ECS::Error, msg unless service_id

  owner_id = Fog::AWS::Mock.owner_id

  cluster = params.delete('cluster') || 'default'
  if !cluster.match(/^arn:aws:ecs:.+:.+:cluster\/(.+)$/)
    cluster_path = "cluster/#{cluster}"
    cluster_arn = Fog::AWS::Mock.arn('ecs', owner_id, cluster_path, region)
  else
    cluster_arn = cluster
  end

  if match = service_id.match(/^arn:aws:ecs:.+:\d{1,12}:service\/(.+)$/)
    i = self.data[:services].index do |s|
      s['clusterArn'].eql?(cluster_arn) && s['serviceArn'].eql?(service_id)
    end
  else
    i = self.data[:services].index do |s|
      s['clusterArn'].eql?(cluster_arn) && s['serviceName'].eql?(service_id)
    end
  end

  msg = "ServiceNotFoundException => Service not found."
  raise Fog::AWS::ECS::Error, msg unless i

  service = self.data[:services][i]

  if desired_count = params.delete('desiredCount')
    # ignore
  end

  if task_definition = params.delete('taskDefinition')
    service['taskDefinition'] = task_definition
  end

  response.body = {
    'UpdateServiceResult' => {
      'service' => service
    },
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }
  response
end