Class: ApigeeCli::ConfigSet

Inherits:
Base
  • Object
show all
Defined in:
lib/apigee_cli/config_set.rb

Constant Summary collapse

MAP_KEY =
'keyValueMap'
ENTRY_KEY =
'entry'
DEFAULT_CONFIG_NAME =
'configuration'

Instance Attribute Summary

Attributes inherited from Base

#environment, #org

Instance Method Summary collapse

Methods inherited from Base

#delete, #get, #initialize, #post, #put, #response_error, #upload_file

Constructor Details

This class inherits a constructor from ApigeeCli::Base

Instance Method Details

#add_config(config_name, data, overwrite) ⇒ Object



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
# File 'lib/apigee_cli/config_set.rb', line 59

def add_config(config_name, data, overwrite)
  changed_keys = []

  begin
    response = Hashie::Mash.new(read_config(config_name))
    if overwrite
      result = :overwritten
    else
      orig_keys = response[ENTRY_KEY].map(&:name)
      data.reject! { |pair| orig_keys.include?(pair['name']) }

      result = :existing
    end

    update_config(config_name, data)
  rescue RuntimeError => e
    if e.message.include?('keyvaluemap_doesnt_exist')
      result = :new
      write_config(config_name, data)
    else
      changed_keys = [e.to_s]
      result = :error
    end
  end

  changed_keys = data.map(&:name)
  [result, changed_keys]
end

#base_urlObject



8
9
10
# File 'lib/apigee_cli/config_set.rb', line 8

def base_url
  "https://api.enterprise.apigee.com/v1/o/#{org}/environments/#{environment}/keyvaluemaps"
end

#list_configsObject



12
13
14
15
16
17
18
19
20
# File 'lib/apigee_cli/config_set.rb', line 12

def list_configs
  # We need the expand: true option to get an expanded view of the KeyValueMaps
  response = get(base_url, expand: true)
  if response.status != 200
    response_error(response)
  else
    JSON.parse(response.body)
  end
end

#read_config(config_name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/apigee_cli/config_set.rb', line 22

def read_config(config_name)
  url = [base_url, config_name].join('/')
  response = get(url)
  if response.status != 200
    response_error(response)
  else
    JSON.parse(response.body)
  end
end

#remove_config(config_name) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/apigee_cli/config_set.rb', line 88

def remove_config(config_name)
  url = [base_url, config_name].join('/')
  response = delete(url)
  if response.status != 200
    response_error(response)
  else
    JSON.parse(response.body)
  end
end

#remove_entry(config_name, entry_name) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/apigee_cli/config_set.rb', line 98

def remove_entry(config_name, entry_name)
  url = [base_url, config_name, 'entries', entry_name].join('/')

  response = delete(url)
  if response.status != 200
    response_error(response)
  else
    JSON.parse(response.body)
  end
end

#update_config(config_name, data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/apigee_cli/config_set.rb', line 45

def update_config(config_name, data)
  url = [base_url, config_name].join('/')
  body = {
    name: config_name,
    entry: data
  }
  response = put(url, body)
  if response.status != 200
    response_error(response)
  else
    JSON.parse(response.body)
  end
end

#write_config(config_name, data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/apigee_cli/config_set.rb', line 32

def write_config(config_name, data)
  body = {
    name: config_name,
    entry: data
  }
  response = post(base_url, body)
  if response.status != 201
    response_error(response)
  else
    JSON.parse(response.body)
  end
end