akamai_cloudlet_manager 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: 3097dba680e9840c7433cb9891fffae7c4ec3df4
4
+ data.tar.gz: 992429969fd5c9de85e9e2ddc6e2d468a4f3d68f
5
+ SHA512:
6
+ metadata.gz: d7da5aea3185c4a29ed76642fa56263e0e304e9d352754d93987789ff39bdda4c2615f6c19739499c0dc5bf033209f1c7e8266e55a71490c87e1b17a6c86455b
7
+ data.tar.gz: 03c52fda46f04ba25d38c0a4e6d508488f1489a647d5749d63ba66204c649782dac273f68f936b0e991422d14ac3d7f679ec6a0a277676f43c970e7cb49b912e
data/bin/acm ADDED
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'akamai_cloudlet_manager'
4
+ require 'thor'
5
+ require 'json'
6
+ require 'terminal-table'
7
+
8
+ class AkamaiCloudletManagerCli < Thor
9
+
10
+ desc "get_policy_versions", "Get all versions of a policy"
11
+ long_desc <<-LONGDESC
12
+ Get the policy versions.
13
+ With --policy_id, policy on which version to be cloned
14
+ LONGDESC
15
+ method_option :policy_id, required: true
16
+ def get_policy_versions
17
+ puts "Policy versions:"
18
+ policy_version = AkamaiCloudletManager::PolicyVersion.new({policy_id: options[:policy_id] })
19
+ rows_title = ['Location','Revision','Description']
20
+ rows = JSON.parse(policy_version.versions).collect {|i| [i['location'], i['revisionId'], i['description']]}
21
+ rows.unshift(rows_title)
22
+ puts Terminal::Table.new rows: rows
23
+ end
24
+
25
+ desc "clone_policy_version", "Clones the current policy version"
26
+ long_desc <<-LONGDESC
27
+ Clones the current activated version of a policy. All new updates are performed in cloned version
28
+ With --policy_id, policy on which version to be cloned
29
+ LONGDESC
30
+ method_option :policy_id, required: true
31
+ def clone_policy_version
32
+ policy_version = AkamaiCloudletManager::PolicyVersion.new({policy_id: options[:policy_id] })
33
+ begin
34
+ if !policy_version.versions.empty?
35
+ latest_version = JSON.parse(policy_version.versions).first["version"].to_s
36
+ puts "cloning policy version: #{latest_version}"
37
+ puts policy_version.create(latest_version)
38
+ else
39
+ puts "No policy versions available"
40
+ end
41
+ rescue => err
42
+ puts "cloning failed!"
43
+ puts "Exception: #{err}"
44
+ err
45
+ end
46
+ end
47
+
48
+ desc "update_policy_version", "Updates the policy version"
49
+ long_desc <<-LONGDESC
50
+ Updates the policy version. Policy version can be updated only in draft mode, not when it is activated.
51
+ With --policy, policy on which version to be updated
52
+ With --draft_version, version which needs to be updated, this must be inactive version
53
+ With --file_path, file_path from which rules can be constructed and updated to policy version
54
+ With --rule_name, rule name so that it is visible in akamai control panel
55
+ With --origin_id, origin's id, this can be found from "get_policy_versions" command
56
+ With --rule_type, rule type, e.g. "albMatchRule"
57
+ With --cookie_rules, from which cookie rules can be constructed and updated to policy version, e.g. "abc=xyz"
58
+
59
+ LONGDESC
60
+
61
+ method_option :policy_id, required: true
62
+ method_option :draft_version, required: true
63
+ method_option :file_path, required: true
64
+ method_option :rule_name, required: true
65
+ method_option :origin_id, required: true
66
+ method_option :rule_type, required: false
67
+ method_option :cookie_rules, required: false
68
+ def update_policy_version
69
+ puts "updates the policy version"
70
+ policy_version = AkamaiCloudletManager::PolicyVersion.new({
71
+ policy_id: options[:policy_id],
72
+ version_id: options[:draft_version]
73
+ })
74
+ begin
75
+ puts policy_version.update(options)
76
+ rescue => err
77
+ puts "Exception: #{err.formatted_exception("Policy version update failed!")}"
78
+ err
79
+ end
80
+ end
81
+
82
+ desc "activate_policy_version", "Activate the policy version on a given network(staging/production)"
83
+ long_desc <<-LONGDESC
84
+ Activates the policy version
85
+ With --policy_id, policy on which version to be activated
86
+ With --version, version which needs to be activated
87
+ With --network, network on which policy version needs to be activated, e.g. staging/production
88
+
89
+ LONGDESC
90
+ method_option :policy_id, required: true
91
+ method_option :version, required: true
92
+ method_option :network, required: true, enum: ['staging','production']
93
+ def activate_policy_version
94
+ puts "activates the policy version"
95
+ policy_version = AkamaiCloudletManager::PolicyVersion.new({
96
+ policy_id: options[:policy_id],
97
+ version_id: options[:version]
98
+ })
99
+ begin
100
+ puts options[:network]
101
+ puts options[:policy_id]
102
+ puts options[:version]
103
+ puts policy_version.activate(options[:network])
104
+ rescue => err
105
+ puts "activation failed!"
106
+ puts "Exception: #{err}"
107
+ err
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ AkamaiCloudletManagerCli.start(ARGV)
@@ -0,0 +1,18 @@
1
+ require 'akamai/edgegrid'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ module AkamaiCloudletManager
7
+ class Base
8
+ def initialize(options = {})
9
+ path_to_edgerc = options[:path_to_edgerc] || '~/.edgerc'
10
+ section = options[:section] || 'default'
11
+
12
+ @http_host = Akamai::Edgegrid::HTTP.new(get_host(path_to_edgerc, section), 443)
13
+ @base_uri = URI('https://' + @http_host.host)
14
+
15
+ @http_host.setup_from_edgerc({ section: 'default' })
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ module AkamaiCloudletManager
2
+ class Detail < Base
3
+
4
+ def initialize(options = {})
5
+ @cloudlet_id = options[:cloudlet_id]
6
+ @group_id = options[:group_id]
7
+ super
8
+ end
9
+
10
+ # Get a cloudlet info
11
+ def info
12
+ request = Net::HTTP::Get.new URI.join(@base_uri.to_s, "cloudlets/api/v2/cloudlet-info/#{@cloudlet_id}").to_s
13
+ response = @http_host.request(request)
14
+ # puts response.body
15
+ response.body
16
+ end
17
+
18
+ # Get a list of cloudlets in a group
19
+ def list
20
+ request = Net::HTTP::Get.new URI.join(@base_uri.to_s, "cloudlets/api/v2/cloudlet-info?#{@group_id}").to_s
21
+ response = @http_host.request(request)
22
+ # puts response.body
23
+ response.body
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ module AkamaiCloudletManager
2
+ class Origin < Base
3
+ # List cloudlet Origins
4
+ # @type this is origin type, e.g. APPLICATION_LOAD_BALANCER
5
+ def list(type)
6
+ request = Net::HTTP::Get.new URI.join(@base_uri.to_s, "cloudlets/api/v2/origins?type=#{type}").to_s
7
+ response = @http_host.request(request)
8
+ # puts response.body
9
+ response.body
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module AkamaiCloudletManager
2
+ class Policy < Base
3
+ def initialize(options = {})
4
+ @policy_id = options[:policy_id]
5
+ super
6
+ end
7
+
8
+ # Get Associated Properties for a Policy
9
+ def properties
10
+ request = Net::HTTP::Get.new URI.join(
11
+ @base_uri.to_s,
12
+ "/cloudlets/api/v2/policies/#{@policy_id}/properties"
13
+ ).to_s
14
+ response = @http_host.request(request)
15
+ # puts response.body
16
+ response.body
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,141 @@
1
+ module AkamaiCloudletManager
2
+ class PolicyVersion < Base
3
+ def initialize(options = {})
4
+ @policy_id = options[:policy_id]
5
+ @version_id = options[:version_id]
6
+ super
7
+ end
8
+
9
+ # Get Policy version's rules
10
+ # request = Net::HTTP::Get.new URI.join(@base_uri.to_s, "cloudlets/api/v2/policies/38548/versions/1?omitRules=false&matchRuleFormat=1.0"').to_s
11
+ def rules
12
+ request = Net::HTTP::Get.new URI.join(@base_uri.to_s, "cloudlets/api/v2/policies/#{@policy_id}/versions/#{@version_id}?omitRules=false&matchRuleFormat=1.0").to_s
13
+ response = @http_host.request(request)
14
+ # puts response.body
15
+ response.body
16
+ end
17
+
18
+
19
+ # Get policy versions
20
+ def versions
21
+ request = Net::HTTP::Get.new URI.join(
22
+ @base_uri.to_s,
23
+ "cloudlets/api/v2/policies/#{@policy_id}/versions?includeRules=false&matchRuleFormat=1.0"
24
+ ).to_s
25
+ response = @http_host.request(request)
26
+ # puts response.body
27
+ response.body
28
+ end
29
+
30
+ # Create a policy version
31
+ def create(clone_from_version_id)
32
+ request = Net::HTTP::Post.new(
33
+ URI.join(
34
+ @base_uri.to_s,
35
+ "cloudlets/api/v2/policies/#{@policy_id}/versions?includeRules=false&matchRuleFormat=1.0&cloneVersion=#{clone_from_version_id}"
36
+ ).to_s,
37
+ { 'Content-Type' => 'application/json'}
38
+ )
39
+ response = @http_host.request(request)
40
+ # puts response.body
41
+ response.body
42
+ end
43
+
44
+ # Activate a policy version
45
+ def activate(network)
46
+ request = Net::HTTP::Post.new(
47
+ URI.join(
48
+ @base_uri.to_s,
49
+ "/cloudlets/api/v2/policies/#{@policy_id}/versions/#{@version_id}/activations"
50
+ ).to_s,
51
+ { 'Content-Type' => 'application/json'}
52
+ )
53
+ request.body = {
54
+ "network": network
55
+ }.to_json
56
+ response = @http_host.request(request)
57
+ # puts response.body
58
+ response.body
59
+ end
60
+
61
+ # Update policy version, all rules
62
+ def update(options = {})
63
+ request = Net::HTTP::Put.new(
64
+ URI.join(@base_uri.to_s, "cloudlets/api/v2/policies/#{@policy_id}/versions/#{@version_id}?omitRules=false&matchRuleFormat=1.0").to_s,
65
+ { 'Content-Type' => 'application/json'}
66
+ )
67
+ rules = generate_path_rules(options) + generate_cookie_rules(options)
68
+
69
+ if rules.empty?
70
+ puts "No rules to apply, please check syntax"
71
+ return
72
+ end
73
+
74
+ request.body = {
75
+ matchRules: rules
76
+ }.to_json
77
+ response = @http_host.request(request)
78
+ response.body
79
+ end
80
+
81
+ # All the path rules from one file will be added under same match, space separated
82
+ def generate_path_rules(options={})
83
+ return [] if options[:file_path].empty?
84
+
85
+ options = options.merge(match_operator: 'equals', match_type: 'path')
86
+ counter = 0
87
+ match_value = []
88
+
89
+ file = File.new(options[:file_path], "r")
90
+ while (line = file.gets)
91
+ match_value << line
92
+ counter += 1
93
+ end
94
+ file.close
95
+
96
+ puts "Total rules read from file: #{counter}\n"
97
+
98
+ match_value = match_value.join(' ').gsub(/\n/, '')
99
+
100
+ match_rules(match_value, options)
101
+ rescue => err
102
+ puts "Exception: #{err}"
103
+ err
104
+ end
105
+
106
+ # All the path rules from one file will be added under same match, space separated
107
+ def generate_cookie_rules(options = {})
108
+ return [] if options[:cookie_rules].empty?
109
+
110
+ options = options.merge(match_operator: 'contains', match_type: 'cookie')
111
+
112
+ match_rules(options[:cookie_rules], options)
113
+ rescue => err
114
+ puts "Exception: #{err}"
115
+ err
116
+ end
117
+
118
+ private
119
+
120
+ def match_rules(match_value, options)
121
+ [{
122
+ type: options[:rule_type] || 'albMatchRule',
123
+ id: 0,
124
+ name: options[:rule_name],
125
+ start: 0,
126
+ end: 0,
127
+ matchURL: nil,
128
+ matches: [{
129
+ matchValue: match_value,
130
+ matchOperator: options[:match_operator],
131
+ negate: false,
132
+ caseSensitive: false,
133
+ matchType: options[:match_type]
134
+ }],
135
+ forwardSettings: {
136
+ originId: options[:origin_id]
137
+ }
138
+ }]
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,20 @@
1
+ require 'akamai_cloudlet_manager/base'
2
+ require 'akamai_cloudlet_manager/detail'
3
+ require 'akamai_cloudlet_manager/origin'
4
+ require 'akamai_cloudlet_manager/policy_version'
5
+ require 'akamai_cloudlet_manager/policy'
6
+ require 'extensions/exception'
7
+
8
+ module AkamaiCloudletManager
9
+ def self.root
10
+ File.expand_path '../..', __FILE__
11
+ end
12
+
13
+ def self.lib
14
+ File.join root, 'lib'
15
+ end
16
+
17
+ def self.spec
18
+ File.join root, 'spec'
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: akamai_cloudlet_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gourav Tiwari
8
+ - Ankit Gupta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-11-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: akamai-edgegrid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.6
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: '1.0'
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.6
34
+ - !ruby/object:Gem::Dependency
35
+ name: json
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.2'
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 12.2.1
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '12.2'
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 12.2.1
68
+ - !ruby/object:Gem::Dependency
69
+ name: thor
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 0.20.0
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.20.0
82
+ - !ruby/object:Gem::Dependency
83
+ name: rspec
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.7'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.7'
96
+ - !ruby/object:Gem::Dependency
97
+ name: terminal-table
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.8'
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '1.8'
110
+ description: Akamai Cloudlet Manager manages all the cloudlet api.
111
+ email:
112
+ - gouravtiwari21@gmail.com
113
+ - ankit.gupta8898@gmail.com
114
+ executables:
115
+ - acm
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - bin/acm
120
+ - lib/akamai_cloudlet_manager.rb
121
+ - lib/akamai_cloudlet_manager/base.rb
122
+ - lib/akamai_cloudlet_manager/detail.rb
123
+ - lib/akamai_cloudlet_manager/origin.rb
124
+ - lib/akamai_cloudlet_manager/policy.rb
125
+ - lib/akamai_cloudlet_manager/policy_version.rb
126
+ homepage: https://github.com/gouravtiwari/akamai-cloudlet-manager
127
+ licenses:
128
+ - Apache License
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.6.12
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Akamai Cloudlet Manager
150
+ test_files: []