class ApigeeCli::ConfigSet

Constants

DEFAULT_CONFIG_NAME
ENTRY_KEY
MAP_KEY

Public Instance Methods

add_config(config_name, data, overwrite) click to toggle source
# 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_url() click to toggle source
# 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_configs() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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