Class: OctopusApi

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/octopus_api.rb

Constant Summary collapse

RESOURCE_TYPES =
['Environments','Projects','ProjectGroups','NugetFeeds','LibraryVariableSets','Machines','Lifecycles','Users','Releases','Deployments']

Instance Attribute Summary

Attributes included from API

#api

Instance Method Summary collapse

Methods included from API

#create_api, #parsed_response, #request

Constructor Details

#initializeOctopusApi

Returns a new instance of OctopusApi.



7
8
9
# File 'lib/octopus_api.rb', line 7

def initialize
  create_api(ConfigStore.octopus)
end

Instance Method Details

#check_type(type) ⇒ Object

Raises:

  • (NameError)


52
53
54
# File 'lib/octopus_api.rb', line 52

def check_type(type)
  raise NameError, "Invalid resource type supplied: '#{type}'" unless RESOURCE_TYPES.include? type
end

#create_resource(type, query) ⇒ Object



11
12
13
14
# File 'lib/octopus_api.rb', line 11

def create_resource(type, query)
  check_type type
  request(:post, "/#{type}", query)
end

#get_resource(type) ⇒ Object



21
22
23
24
# File 'lib/octopus_api.rb', line 21

def get_resource(type)
  check_type type
  return request(:get, "/#{type}/all")
end

#get_resource_by_type_and_name(type, name = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/octopus_api.rb', line 31

def get_resource_by_type_and_name(type, name = nil)
  resources = get_resource(type)
  if name && name != ''
    filter = [*name].join("|")

    filtered_resources = resources.select do |resource|
      resource['Name'] =~ /#{filter}/
    end

    if filtered_resources.any?
      Log.info "#{filtered_resources.count} resources found with filter #{filter}"
      filtered_resources
    else
      Log.info "No #{type} found with filter #{filter}"
      return nil
    end
  else
    resources
  end
end

#remove_resource(type, id) ⇒ Object



16
17
18
19
# File 'lib/octopus_api.rb', line 16

def remove_resource(type, id)
  check_type type
  request(:delete, "/#{type}/#{id}")
end

#resource_exists?(type, name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/octopus_api.rb', line 26

def resource_exists?(type, name)
  resource = get_resource_by_type_and_name(type, name)
  return (resource && resource != [])
end