apigee_cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2abec78aa81785c59bad5ee3701a8ef516a5534c
4
+ data.tar.gz: 601c222a8621f1f758b161b7b58fb062c1d22109
5
+ SHA512:
6
+ metadata.gz: df99dee88e32dd2b020dfa9d049335bbbaa3d6bda30757772799b7186a69add283b91e89b474b255762bcf2e13d09f944f329e9e0398beccdb561d81205953a0
7
+ data.tar.gz: 0646b46581aa48fabfd29e070da357450d2677573ce9c8e24f40d2a3feb0b83e4420bbfbde9012b04744a2968c4027b1ba6852edfd684d50bb5aa766dfd561d0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in apigee_cli.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Darby Frey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # ApigeeCli
2
+
3
+ An API Wrapper and CLI for Apigee
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'apigee_cli'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install apigee_cli
18
+
19
+ ## Commands
20
+
21
+ ### Top level commands
22
+
23
+ Commands:
24
+ apigee apigeetool [COMMAND] # Run a command using the apigeetool Node.js module
25
+ apigee config [COMMAND] # Run a command within the context of an app configuration
26
+ apigee help [COMMAND] # Describe available commands or one specific command
27
+ apigee resource [COMMAND] # Run a command within the context of a resource
28
+ apigee settings # Show the current apigeerc settings
29
+ apigee version # Shows the Apigee CLI version number
30
+
31
+ ### To see sublevel commands, you can run:
32
+
33
+ apigee help apigeetool
34
+ apigee help config
35
+ apigee help resource
36
+
37
+ ### Usage
38
+
39
+ A. Deployment (leverages apigeetool Node.js module)
40
+
41
+ #### apigee apigeetool listdeployments [DEFAULT]
42
+
43
+ # by environment
44
+ $ apigee apigeetool listdeployments -e=ENVIRONMENT
45
+
46
+ # by proxy name
47
+ $ apigee apigeetool listdeployments -n PROXY_NAME
48
+
49
+ #### apigee apigeetool deploy
50
+
51
+ $ apigee apigeetool deploy -e=ENVIRONMENT -n PROXY_NAME -d ROOT_DIR_OF_PROXY -V
52
+
53
+ #### apigee apigeetool nodedeploy
54
+
55
+ $ apigee apigeetool nodedeploy -e=ENVIRONMENT -n NODE_PROXY_NAME -d ROOT_DIR_OF_NODE_PROXY -m MAIN_JS_FILE -b BASE_PATH -v secure
56
+
57
+ #### apigee apigeetool undeploy
58
+
59
+ $ apigee apigeetool undeploy -e=ENVIRONMENT -n PROXY_NAME -D
60
+
61
+ #### apigee apigeetool fetchproxy
62
+
63
+ $ apigee apigeetool fetchproxy -n PROXY_NAME -r REVISION_NUMBER
64
+
65
+ #### apigee apigeetool delete
66
+
67
+ NOTE: This deletes all revisions of PROXY_NAME. It is an error to delete a proxy that still has deployed revisions. Revisions must be undeployed using "undeploy" before this command may be used.
68
+
69
+ $ apigee apigeetool delete -n PROXY_NAME
70
+
71
+ #### apigee apigeetool getlogs [from a Node app]
72
+
73
+ $ apigee apigeetool getlogs -e=ENVIRONMENT -n NODE_PROXY_NAME
74
+
75
+
76
+ B. Configuration Settings on Apigee Server
77
+
78
+ #### apigee config list [DEFAULT]
79
+
80
+ # defaults
81
+ -e=test|--environment=test
82
+
83
+ # List configs for default environment of test
84
+ $ apigee config list
85
+
86
+ # List config for a particular config_name
87
+ $ apigee config list --config_name=configuration_one
88
+
89
+ #### apigee config push
90
+
91
+ # defaults
92
+ -e=test|--environment=test
93
+ --config_name=configuration
94
+
95
+ # Create config key-value map
96
+ $ apigee config push --config_name=new_config
97
+
98
+ # Update config key-value pair
99
+ $ apigee config push key_one=value_one
100
+
101
+ # Overwrite existing config key-value pair
102
+ $ apigee config push key_one=changed_value_one --overwrite=true
103
+
104
+ #### apigee config delete
105
+
106
+ # defaults
107
+ -e=test|--environment=test
108
+ --config_name=configuration
109
+
110
+ # Delete config key-value pair (default config_name is configuration)
111
+ $ apigee config delete --entry_name=key_one
112
+
113
+ # Delete config for that config_name
114
+ $ apigee config delete --config_name=configuration_one
115
+
116
+
117
+ C. Resource Files on Apigee Server
118
+
119
+ #### apigee resource list [DEFAULT]
120
+
121
+ # List resource files for organization
122
+ $ apigee resource list
123
+
124
+ # Get resource file with resource_name
125
+ $ apigee resource list --name=testing.js
126
+
127
+ #### apigee resource upload
128
+
129
+ # Upload resource files from resource_folder
130
+ $ apigee resource upload --folder=jsc
131
+
132
+ #### apigee resource delete
133
+
134
+ # Delete resource file of resource_name
135
+ $ apigee resource delete --name=testing.js
136
+
137
+ ## Contributing
138
+
139
+ 1. Fork it ( http://github.com/<my-github-username>/apigee_cli/fork )
140
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
141
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
142
+ 4. Push to the branch (`git push origin my-new-feature`)
143
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apigee_cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "apigee_cli"
8
+ spec.version = ApigeeCli::VERSION
9
+ spec.authors = ["Darby Frey"]
10
+ spec.email = ["darbyfrey@gmail.com"]
11
+ spec.summary = %q{A CLI for Apigee}
12
+ spec.description = %q{A CLI for Apigee}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+
22
+ spec.add_dependency 'httparty', '~> 0.13'
23
+ spec.add_dependency 'faraday', '~> 0.9.1'
24
+ spec.add_dependency 'thor', '~> 0.19'
25
+ spec.add_dependency 'hashie', '~> 3.3'
26
+ spec.add_dependency 'git', '~> 1.2'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.5'
29
+ spec.add_development_dependency 'rake', '~> 10.4'
30
+ spec.add_development_dependency 'pry', '~> 0.10.1'
31
+ spec.add_development_dependency 'rspec', '~> 3.1'
32
+ spec.add_development_dependency 'rack-test', '~> 0.6.3'
33
+ spec.add_development_dependency 'webmock', '~> 1.21.0'
34
+ end
data/bin/apigee ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'apigee_cli'
4
+
5
+ require 'thor'
6
+ require 'apigee_cli/cli/thor_cli'
7
+ require 'apigee_cli/cli/app_config'
8
+ require 'apigee_cli/cli/apigee_tool'
9
+ require 'apigee_cli/cli/resource'
10
+ require 'apigee_cli/cli'
11
+
12
+ ApigeeCli::Cli.start
@@ -0,0 +1,61 @@
1
+ require 'faraday'
2
+
3
+ module ApigeeCli
4
+ class Base
5
+ attr_accessor :org, :environment
6
+
7
+ def initialize(environment = nil)
8
+ @username = ApigeeCli.configuration.username
9
+ @password = ApigeeCli.configuration.password
10
+ @org = ApigeeCli.configuration.org
11
+ @environment = environment || ApigeeCli.configuration.environment
12
+ end
13
+
14
+ def get(url, params = {})
15
+ conn = Faraday.new(url: url)
16
+ conn.basic_auth(@username, @password)
17
+ conn.get do |request|
18
+ request.params = params
19
+ end
20
+ end
21
+
22
+ def upload_file(url, file)
23
+ conn = Faraday.new(url: url)
24
+ conn.basic_auth(@username, @password)
25
+ conn.post do |request|
26
+ request.headers['Content-Type'] = "application/octet-stream"
27
+ request.headers['Content-Length'] = File.size(file).to_s
28
+ request.body = Faraday::UploadIO.new(file, 'text/plain')
29
+ end
30
+ end
31
+
32
+ def post(url, body)
33
+ conn = Faraday.new(url: url)
34
+ conn.basic_auth(@username, @password)
35
+ conn.post do |request|
36
+ request.headers['Content-Type'] = "application/json"
37
+ request.body = body.to_json
38
+ end
39
+ end
40
+
41
+ def put(url, body)
42
+ conn = Faraday.new(url: url)
43
+ conn.basic_auth(@username, @password)
44
+ conn.put do |request|
45
+ request.headers['Content-Type'] = "application/json"
46
+ request.body = body.to_json
47
+ end
48
+ end
49
+
50
+ def delete(url)
51
+ conn = Faraday.new(url: url)
52
+ conn.basic_auth(@username, @password)
53
+ conn.delete
54
+ end
55
+
56
+ def response_error(response)
57
+ raise "Response Error: #{response.status} #{response.body}"
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,55 @@
1
+ class ApigeeTool < ThorCli
2
+ namespace 'apigeetool'
3
+ default_task :listdeployments
4
+
5
+ no_commands do
6
+ def load_config
7
+ @username = ApigeeCli.configuration.username
8
+ @password = ApigeeCli.configuration.password
9
+ @org = ApigeeCli.configuration.org
10
+ @env = environment
11
+ end
12
+ end
13
+
14
+ desc 'deploy', 'Deploy a proxy'
15
+ def deploy(*args)
16
+ load_config
17
+ say `apigeetool deployproxy -u #{@username} -p #{@password} -o #{@org} -e #{@env} #{args.join(' ')}`
18
+ end
19
+
20
+ desc 'nodedeploy', 'Deploy a node app'
21
+ def nodedeploy(*args)
22
+ load_config
23
+ say `apigeetool deploynodeapp -u #{@username} -p #{@password} -o #{@org} -e #{@env} #{args.join(' ')}`
24
+ end
25
+
26
+ desc 'undeploy', 'Undeploy a proxy or node app'
27
+ def undeploy(*args)
28
+ load_config
29
+ say `apigeetool undeploy -u #{@username} -p #{@password} -o #{@org} -e #{@env} #{args.join(' ')}`
30
+ end
31
+
32
+ desc 'listdeployments', 'List all the deployments for a given environment'
33
+ def listdeployments(*args)
34
+ load_config
35
+ say `apigeetool listdeployments -u #{@username} -p #{@password} -o #{@org} -e #{@env} #{args.join(' ')}`
36
+ end
37
+
38
+ desc 'fetchproxy', 'Download a proxy as a zip file'
39
+ def fetchproxy(*args)
40
+ load_config
41
+ say `apigeetool fetchproxy -u #{@username} -p #{@password} -o #{@org} -e #{@env} #{args.join(' ')}`
42
+ end
43
+
44
+ desc 'delete', 'Delete a proxy or node app'
45
+ def deleteproxy(*args)
46
+ load_config
47
+ say `apigeetool delete -u #{@username} -p #{@password} -o #{@org} -e #{@env} #{args.join(' ')}`
48
+ end
49
+
50
+ desc 'getlogs', 'Retrieve the last set of logs from a Node app'
51
+ def getlogs(*args)
52
+ load_config
53
+ say `apigeetool getlogs -u #{@username} -p #{@password} -o #{@org} -e #{@env} #{args.join(' ')}`
54
+ end
55
+ end
@@ -0,0 +1,147 @@
1
+ require 'apigee_cli/cli/thor_cli'
2
+
3
+ class AppConfig < ThorCli
4
+ namespace 'config'
5
+ default_task :list
6
+
7
+ desc 'list', 'Pulls down keyvaluemaps from Apigee server'
8
+ option :config_name, type: :string
9
+ def list
10
+ config_name = options[:config_name]
11
+ config_set = ApigeeCli::ConfigSet.new(environment)
12
+
13
+ if config_name
14
+ pull_config(config_set, config_name)
15
+ else
16
+ pull_list(config_set)
17
+ end
18
+ end
19
+
20
+ desc 'push', 'Push up keyvaluemaps for [config_name] to Apigee server'
21
+ option :config_name, type: :string, default: ApigeeCli::ConfigSet::DEFAULT_CONFIG_NAME
22
+ option :overwrite, type: :boolean, default: false
23
+ def push(*entries)
24
+ config_name = options[:config_name]
25
+ overwrite = options[:overwrite] || false
26
+
27
+ config_set = ApigeeCli::ConfigSet.new(environment)
28
+
29
+ data = populate_data(entries)
30
+ result, changed_keys = config_set.add_config(config_name, data, overwrite)
31
+
32
+ if result == :error
33
+ say "Error [#{changed_keys.first}] pushing config for [#{config_name}] to [#{environment}] environment"
34
+ exit
35
+ end
36
+
37
+ if result == :new
38
+ say "Creating new config for [#{config_name}] in [#{environment}] environment"
39
+ elsif result == :existing
40
+ say "Adding new keys #{changed_keys} to config [#{config_name}] in [#{environment}] environment"
41
+ elsif result == :overwritten
42
+ say "Overwriting existing config [#{config_name}] in [#{environment}] environment"
43
+ end
44
+
45
+ updated_config = config_set.read_config(config_name)[ApigeeCli::ConfigSet::ENTRY_KEY]
46
+
47
+ render_config(config_name, updated_config, changed_keys)
48
+ end
49
+
50
+ desc 'delete', 'Delete keyvaluemaps for [config_name] from Apigee server'
51
+ option :config_name, type: :string, default: ApigeeCli::ConfigSet::DEFAULT_CONFIG_NAME
52
+ option :entry_name, type: :string
53
+ def delete
54
+ config_name = options[:config_name]
55
+ entry_name = options[:entry_name]
56
+
57
+ config_set = ApigeeCli::ConfigSet.new(environment)
58
+
59
+ pull_list(config_set)
60
+
61
+ if entry_name
62
+ confirm = yes? "Are you sure you want to delete #{entry_name} from #{config_name} in #{environment} environment? [y/n]"
63
+ exit if !confirm
64
+
65
+ remove_entry(config_set, config_name, entry_name)
66
+ else
67
+ confirm = yes? "Are you sure you want to delete #{config_name} from #{environment} environment? [y/n]"
68
+ exit if !confirm
69
+
70
+ remove_config(config_set, config_name)
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def pull_list(config_set)
77
+ begin
78
+ response = Hashie::Mash.new(config_set.list_configs)
79
+ render_list(response[ApigeeCli::ConfigSet::MAP_KEY])
80
+ rescue RuntimeError => e
81
+ render_error(e)
82
+ end
83
+ end
84
+
85
+ def pull_config(config_set, config_name)
86
+ begin
87
+ response = Hashie::Mash.new(config_set.read_config(config_name))
88
+ render_config(config_name, response[ApigeeCli::ConfigSet::ENTRY_KEY])
89
+ rescue RuntimeError => e
90
+ render_error(e)
91
+ end
92
+ end
93
+
94
+ def remove_config(config_set, config_name)
95
+ begin
96
+ response = Hashie::Mash.new(config_set.remove_config(config_name))
97
+ say "Config [#{config_name}] has been deleted from [#{environment}] environment", :red
98
+ rescue RuntimeError => e
99
+ render_error(e)
100
+ end
101
+ end
102
+
103
+ def remove_entry(config_set, config_name, entry_name)
104
+ begin
105
+ response = Hashie::Mash.new(config_set.remove_entry(config_name, entry_name))
106
+ say "Entry [#{entry_name}] has been deleted from [#{config_name}] in [#{environment}] environment", :red
107
+ rescue RuntimeError => e
108
+ render_error(e)
109
+ end
110
+ end
111
+
112
+ def populate_data(entries)
113
+ entries.each_with_object([]) do |entry, data|
114
+ name = entry.split("=", 2).first
115
+ value = entry.split("=", 2).last
116
+
117
+ data << Hashie::Mash.new({ name: name, value: value })
118
+ end
119
+ end
120
+
121
+ def render_list(configs)
122
+ configs.each do |config|
123
+ render_config(config['name'], config[ApigeeCli::ConfigSet::ENTRY_KEY])
124
+ end
125
+ end
126
+
127
+ def render_config(config_name, entries, highlight = [])
128
+ say "Environment: #{environment}, Config Name: #{config_name}", :blue
129
+ entries.each do |entry|
130
+ name = entry['name']
131
+ color = highlight.include?(name) ? :yellow : :green
132
+
133
+ render_entry(entry, color)
134
+ end
135
+ end
136
+
137
+ def render_entry(entry, color = :green)
138
+ name = entry['name']
139
+ value = entry['value']
140
+
141
+ say "\s\s#{name}: #{value}", color
142
+ end
143
+
144
+ def render_error(error)
145
+ say error.to_s, :red
146
+ end
147
+ end
@@ -0,0 +1,83 @@
1
+ require 'apigee_cli/cli/thor_cli'
2
+
3
+ class Resource < ThorCli
4
+ namespace 'resource'
5
+ default_task :list
6
+
7
+ desc 'list', 'List resource files'
8
+ option :name, type: :string
9
+ def list
10
+ name = options[:name]
11
+
12
+ resource = ApigeeCli::ResourceFile.new(environment)
13
+
14
+ if name
15
+ response = resource.read(name, ApigeeCli::ResourceFile::DEFAULT_RESOURCE_TYPE)
16
+ say response
17
+ else
18
+ pull_list(resource)
19
+ end
20
+ end
21
+
22
+ desc 'upload', 'Upload resource files'
23
+ option :folder, type: :string, required: true
24
+ def upload
25
+ folder = options[:folder]
26
+
27
+ files = Dir.entries(folder).select{ |f| f =~ /.js$/ }
28
+
29
+ resource = ApigeeCli::ResourceFile.new(environment)
30
+
31
+ files.each do |file|
32
+ result = resource.upload file, ApigeeCli::ResourceFile::DEFAULT_RESOURCE_TYPE, "#{folder}/#{file}"
33
+ if result == :overwritten
34
+ say "Overwriting current resource for #{file}", :green
35
+ elsif result == :new_file
36
+ say "Creating resource for #{file}", :green
37
+ end
38
+ end
39
+ end
40
+
41
+ desc 'delete', 'Delete resource file'
42
+ option :name, type: :string, required: true
43
+ def delete
44
+ name = options[:name]
45
+
46
+ resource = ApigeeCli::ResourceFile.new(environment)
47
+
48
+ confirm = yes? "Are you sure you want to delete #{name} from #{org}? [y/n]"
49
+
50
+ if confirm
51
+ begin
52
+ say "Deleting current resource for #{name}", :red
53
+ resource.remove(name, ApigeeCli::ResourceFile::DEFAULT_RESOURCE_TYPE)
54
+ rescue RuntimeError => e
55
+ render_error(e)
56
+ exit
57
+ end
58
+ else
59
+ exit
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def pull_list(resource)
66
+ response = Hashie::Mash.new(resource.all)
67
+ render_list(response[ApigeeCli::ResourceFile::RESOURCE_FILE_KEY])
68
+ end
69
+
70
+ def render_list(resource_files)
71
+ say "Resource files for #{org}", :blue
72
+ resource_files.each do |resource_file|
73
+ name = resource_file['name']
74
+ type = resource_file['type']
75
+
76
+ say " #{type} file - #{name}", :green
77
+ end
78
+ end
79
+
80
+ def render_error(error)
81
+ say error.to_s, :red
82
+ end
83
+ end
@@ -0,0 +1,15 @@
1
+ require 'thor'
2
+
3
+ class ThorCli < Thor
4
+ class_option :environment, aliases: [:env, :e]
5
+
6
+ no_commands do
7
+ def environment
8
+ options[:environment] || 'test'
9
+ end
10
+
11
+ def org
12
+ options[:org] || 'bellycard'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ require 'thor'
2
+
3
+ module ApigeeCli
4
+ class Cli < Thor
5
+ desc "version", "Shows the Apigee CLI version number"
6
+ def version
7
+ say ApigeeCli::VERSION
8
+ end
9
+
10
+ desc 'settings', 'Show the current apigeerc settings'
11
+ def settings
12
+ puts ApigeeCli.configuration.apigeerc_config.to_yaml
13
+ end
14
+
15
+ desc 'config [COMMAND]', 'Run a command within the context of an app configuration'
16
+ subcommand 'config', ::AppConfig
17
+
18
+ desc 'apigeetool [COMMAND]', 'Run a command using the apigeetool Node.js module'
19
+ subcommand 'apigeetool', ::ApigeeTool
20
+
21
+ desc 'resource [COMMAND]', 'Run a command within the context of a resource'
22
+ subcommand 'resource', ::Resource
23
+ end
24
+ end