jcf 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +26 -0
  4. data/CHANGELOG.md +43 -0
  5. data/CODE_OF_CONDUCT.md +84 -0
  6. data/Guardfile +21 -0
  7. data/LICENSE.txt +20 -0
  8. data/README.md +120 -0
  9. data/Rakefile +167 -0
  10. data/cucumber.yml +1 -0
  11. data/exe/jcf +21 -0
  12. data/lib/.DS_Store +0 -0
  13. data/lib/jcf/.DS_Store +0 -0
  14. data/lib/jcf/aws/cloudwatch.rb +90 -0
  15. data/lib/jcf/cf/.DS_Store +0 -0
  16. data/lib/jcf/cf/base.rb +113 -0
  17. data/lib/jcf/cf/metric.rb +39 -0
  18. data/lib/jcf/cf/organization.rb +10 -0
  19. data/lib/jcf/cf/quota.rb +11 -0
  20. data/lib/jcf/cf/relationships.rb +88 -0
  21. data/lib/jcf/cf/service_broker.rb +10 -0
  22. data/lib/jcf/cf/service_instance.rb +10 -0
  23. data/lib/jcf/cf/service_offering.rb +10 -0
  24. data/lib/jcf/cf/service_plan.rb +10 -0
  25. data/lib/jcf/cf/space.rb +10 -0
  26. data/lib/jcf/cf/user.rb +37 -0
  27. data/lib/jcf/cf.rb +29 -0
  28. data/lib/jcf/cli/command.rb +35 -0
  29. data/lib/jcf/cli/commands/.DS_Store +0 -0
  30. data/lib/jcf/cli/commands/cf/metrics.rb +156 -0
  31. data/lib/jcf/cli/commands/cf/organizations.rb +24 -0
  32. data/lib/jcf/cli/commands/cf/quota.rb +26 -0
  33. data/lib/jcf/cli/commands/cf/service_brokers.rb +30 -0
  34. data/lib/jcf/cli/commands/cf/service_instances.rb +37 -0
  35. data/lib/jcf/cli/commands/cf/service_offerings.rb +26 -0
  36. data/lib/jcf/cli/commands/cf/service_plans.rb +30 -0
  37. data/lib/jcf/cli/commands/cf/spaces.rb +26 -0
  38. data/lib/jcf/cli/commands/cf/users.rb +24 -0
  39. data/lib/jcf/cli/commands/version.rb +16 -0
  40. data/lib/jcf/cli/commands.rb +21 -0
  41. data/lib/jcf/cli/errors.rb +17 -0
  42. data/lib/jcf/cli/output_formatters/csv.rb +35 -0
  43. data/lib/jcf/cli/output_formatters/json.rb +19 -0
  44. data/lib/jcf/cli/output_formatters/text.rb +39 -0
  45. data/lib/jcf/cli/output_formatters.rb +26 -0
  46. data/lib/jcf/cli.rb +53 -0
  47. data/lib/jcf/version.rb +5 -0
  48. data/lib/jcf.rb +9 -0
  49. data/sig/jcf.rbs +4 -0
  50. metadata +327 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1dc40224058bf16a626b143c430840d71b08e7c40c2075cc4bb37792fd2c578c
4
+ data.tar.gz: 170d182549a2b12db87edcf56fb6d8303614c7b7a6448ee45fa81b08ea48e9fe
5
+ SHA512:
6
+ metadata.gz: 0616d831613106555a42498d7b578675e72187deac999d92ea460458c3ae345f18b1439429bd354d03e0b9dd8e265c388e7170e6bf21ae344fe13966076f39f7
7
+ data.tar.gz: bf2190ff13ffcfe11195d7bc03f6a5c6b652e9aa3019d2973ef1b4fba6db1abe5c923191444ecae17a3ccb5e1b09b3d0112e60427b40f8438f9151b2c2c53f7c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ ---
2
+ require:
3
+ - rubocop-performance
4
+ - rubocop-rspec
5
+ - rubocop-rake
6
+
7
+ AllCops:
8
+ NewCops: enable
9
+ TargetRubyVersion: 3.0
10
+ Exclude:
11
+ - 'lib/jcf/cli/commands/cf/metrics.rb'
12
+ - 'vendor/bundle/**/*'
13
+
14
+ Style/StringLiterals:
15
+ Enabled: true
16
+ EnforcedStyle: double_quotes
17
+
18
+ Style/StringLiteralsInInterpolation:
19
+ Enabled: true
20
+ EnforcedStyle: double_quotes
21
+
22
+ Style/Documentation:
23
+ Enabled: false
24
+
25
+ Layout/LineLength:
26
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,43 @@
1
+ ## [Unreleased]
2
+ ## [0.0.9] - 2023-10-02
3
+
4
+ - Automatically increase table width for wide tables
5
+
6
+ ## [0.0.8] - 2023-10-02
7
+
8
+ - Add --org filter to service_offerings command
9
+ - Add --org filter to service_plans command
10
+ - Add --org filter to service_instances command
11
+ - Add --space filter to service_instances command
12
+ - Add --org filter to spaces command
13
+
14
+ ## [0.0.7] - 2023-10-02
15
+
16
+ - Add missing gems and tidy up
17
+
18
+ ## [0.0.6] - 2023-10-02
19
+
20
+ - Add missing "english" gem to dependencies
21
+ - Add extra platforms to Gemfile.lock
22
+
23
+ ## [0.0.5] - 2023-10-02
24
+
25
+ - Searching for objects now returns all matches, not just the first
26
+
27
+ ## [0.0.4] - 2023-10-02
28
+
29
+ - Add service_offerings command
30
+
31
+ ## [0.0.3] - 2023-10-02
32
+
33
+ - Relationships output is now more readable
34
+ - Partial lookups for all classes when searching
35
+ ## [0.0.2] - 2023-09-28
36
+
37
+ - Default to text output instead of json
38
+
39
+ ## [0.0.1] - 2023-08-29
40
+
41
+ - Initial release
42
+ - Query RDS metrics
43
+ - Query S3 metrics
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jamie.vandyke@digital.cabinet-office.gov.uk. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Guardfile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # More info at https://github.com/guard/guard#readme
4
+
5
+ directories(%w[exe lib spec])
6
+ .select { |d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist") }
7
+
8
+ guard :rspec, cmd: "bundle exec rspec" do
9
+ require "guard/rspec/dsl"
10
+ dsl = Guard::RSpec::Dsl.new(self)
11
+
12
+ # RSpec files
13
+ rspec = dsl.rspec
14
+ watch(rspec.spec_helper) { rspec.spec_dir }
15
+ watch(rspec.spec_support) { rspec.spec_dir }
16
+ watch(rspec.spec_files)
17
+ watch(%r{^spec/support/fixtures/(.+)\.(json|yml|yaml)$}) { rspec.spec_dir }
18
+
19
+ # Ruby files
20
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
21
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Crown Copyright (Government Digital Service)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ ![tests](https://github.com/fearoffish/paas-org-metric-gathering-gem/actions/workflows/main.yml/badge.svg)
2
+
3
+ # Jcf
4
+
5
+ ## Installation
6
+
7
+ Currently this gem is not available on rubygems.org so you will need to build it yourself. A convenience rake task has been included:
8
+
9
+ ```sh
10
+ rake install
11
+ ```
12
+
13
+ Or you can just build it instead:
14
+
15
+ ```sh
16
+ rake build
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Make sure you're logged into the CloudFoundry you want to query. You can then run the `jcf` command to see the available commands:
22
+
23
+ ```
24
+ $ jcf
25
+ Commands:
26
+ jcf metrics ENV TYPE
27
+ jcf organizations [NAME]
28
+ jcf service_brokers [NAME]
29
+ jcf service_instances [NAME]
30
+ jcf service_offerings [NAME]
31
+ jcf service_plans [NAME]
32
+ jcf spaces [NAME]
33
+ jcf users [NAME]
34
+ jcf version # Print JCF app version
35
+ ```
36
+
37
+ Each command has a `--help` option to show the available options. They also have aliases for the short and long form of the command. For example:
38
+
39
+ ```sh
40
+ jcf organizations
41
+ jcf orgs
42
+ jcf o
43
+
44
+ jcf service_brokers
45
+ jcf sb
46
+ ```
47
+
48
+ ### Examples
49
+
50
+ Ignoring `metrics`, all other types can list all or filter based on a given name.
51
+
52
+ #### List all
53
+
54
+ ```sh
55
+ jcf organizations
56
+ ```
57
+
58
+ #### Filter by name
59
+
60
+ ```sh
61
+ jcf organizations my-org
62
+ ```
63
+
64
+ #### Metrics
65
+
66
+ > **_NOTE:_** Metrics are a little different. They are specific to the gov.uk PaaS. Plans to make this more generic are in the works.
67
+
68
+ You need to specify the environment and the service type you want to query.
69
+
70
+ Query the `production` environment, `rds-broker` service for the organization `my-org`:
71
+
72
+ ```sh
73
+ jcf metrics production rds-broker --org=my-org
74
+ ```
75
+
76
+ Query the `staging` environment, `aws-s3-bucket-broker` service for the organizations `my-org` and `my-org2`:
77
+ ```sh
78
+ jcf metrics staging aws-s3-bucket-broker --org=my-org,my-org2
79
+ ```
80
+
81
+ ### Formatting
82
+
83
+ The default format is a table. You can also format as JSON or CSV. For example:
84
+
85
+ ```sh
86
+ # as json
87
+ jcf organizations --format json
88
+ ```
89
+
90
+ ```sh
91
+ # as csv
92
+ jcf organizations --format csv
93
+ ```
94
+
95
+ When querying metrics, you probably want the output to a file. You can use the `--output` flag for this. Progress will be output to STDERR. For example:
96
+
97
+ ```sh
98
+ jcf metrics production rds-broker \
99
+ --org=my-org \
100
+ --format csv \
101
+ --output rds-broker-metrics.json
102
+ ```
103
+
104
+ ## Development
105
+
106
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107
+
108
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fearoffish/jcf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/fearoffish/paas-org-metric-gathering-gem/blob/main/CODE_OF_CONDUCT.md).
113
+
114
+ ## License
115
+
116
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
117
+
118
+ ## Code of Conduct
119
+
120
+ Everyone interacting in the Jcf project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fearoffish/paas-org-metric-gathering-gem/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+ require "active_support/all"
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+ RuboCop::RakeTask.new
10
+
11
+ task default: %i[spec rubocop]
12
+
13
+ desc "Get all fixtures for an organization: rake get_fixtures[org_name]"
14
+ # rubocop:disable Layout/LineLength,Metrics/BlockLength
15
+ task :get_fixtures, [:org] do |_t, args|
16
+ raise "Provide an organization name" if args[:org].empty?
17
+
18
+ `mkdir -p spec/support/fixtures`
19
+
20
+ json = curl("/v3/organizations?names=#{args[:org]}", debug: ENV.fetch("DEBUG", nil))
21
+ write_fixture("organizations", json)
22
+ org = ActiveSupport::JSON.decode(json).deep_symbolize_keys[:resources].first
23
+ json = curl("/v3/organizations/#{org[:guid]}", debug: ENV.fetch("DEBUG", nil))
24
+ write_fixture("organization", json)
25
+ puts "Found organization: #{org[:name]}"
26
+
27
+ json = curl("/v3/organization_quotas?organization_guids=#{org[:guid]}", debug: ENV.fetch("DEBUG", nil))
28
+ write_fixture("quotas", json)
29
+ quotas = ActiveSupport::JSON.decode(json).deep_symbolize_keys
30
+ json = curl("/v3/organization_quotas/#{quotas[:resources].first[:guid]}", debug: ENV.fetch("DEBUG", nil))
31
+ write_fixture("quota", json)
32
+ puts "Found quotas: #{quotas[:resources].count}"
33
+
34
+ json = curl("/v3/users", debug: ENV.fetch("DEBUG", nil))
35
+ # remove resources with a nil username
36
+ users = ActiveSupport::JSON.decode(json).deep_symbolize_keys
37
+ users[:resources].reject! { |u| u[:username].nil? }
38
+ write_fixture("users", json)
39
+ json = curl("/v3/users/#{users[:resources].first[:guid]}", debug: ENV.fetch("DEBUG", nil))
40
+ write_fixture("user", json)
41
+ puts "Found users: #{users[:resources].count}"
42
+
43
+ # all spaces in our org
44
+ json = curl("/v3/spaces?organization_guids=#{org[:guid]}", debug: ENV.fetch("DEBUG", nil))
45
+ write_fixture("spaces", json)
46
+ spaces = ActiveSupport::JSON.decode(json).deep_symbolize_keys
47
+ json = curl("/v3/spaces/#{spaces[:resources].first[:guid]}", debug: ENV.fetch("DEBUG", nil))
48
+ write_fixture("space", json)
49
+ puts "Found spaces: #{spaces[:resources].count}"
50
+
51
+ # only the rds-broker
52
+ json = curl("/v3/service_brokers?names=rds-broker&per_page=5000", debug: ENV.fetch("DEBUG", nil))
53
+ write_fixture("service_brokers", json)
54
+ service_brokers = ActiveSupport::JSON.decode(json).deep_symbolize_keys
55
+ broker_guids = service_brokers[:resources].collect { |b| b[:guid] }
56
+ puts "Found service_brokers: #{service_brokers[:resources].count}"
57
+ json = curl("/v3/service_brokers/#{service_brokers[:resources].first[:guid]}", debug: ENV.fetch("DEBUG", nil))
58
+ write_fixture("service_broker", json)
59
+
60
+ # all the offerings from the rds-broker
61
+ json = curl("/v3/service_offerings?service_broker_guids=#{broker_guids.join(",")}&per_page=5000", debug: ENV.fetch("DEBUG", nil))
62
+ write_fixture("service_offerings", json)
63
+ service_offerings = ActiveSupport::JSON.decode(json).deep_symbolize_keys
64
+ offering_guids = service_offerings[:resources].collect { |b| b[:guid] }
65
+ puts "Found service_offerings: #{service_offerings[:resources].count}"
66
+ json = curl("/v3/service_offerings/#{service_offerings[:resources].first[:guid]}", debug: ENV.fetch("DEBUG", nil))
67
+ write_fixture("service_offering", json)
68
+
69
+ # all the plans from the rds-broker offerings
70
+ json = curl("/v3/service_plans?service_offering_guids=#{offering_guids.join(",")}&per_page=5000", debug: ENV.fetch("DEBUG", nil))
71
+ write_fixture("service_plans", json)
72
+ service_plans = ActiveSupport::JSON.decode(json).deep_symbolize_keys
73
+ plan_guids = service_plans[:resources].collect { |b| b[:guid] }
74
+ puts "Found service_plans: #{service_plans[:resources].count}"
75
+ json = curl("/v3/service_plans/#{service_plans[:resources].first[:guid]}", debug: ENV.fetch("DEBUG", nil))
76
+ write_fixture("service_plan", json)
77
+
78
+ # all the instances of the rds-broker plans
79
+ json = curl("/v3/service_instances?organization_guids=#{org[:guid]}&service_plan_guids=#{plan_guids.join(",")}&per_page=5000", debug: ENV.fetch("DEBUG", nil))
80
+ write_fixture("service_instances", json)
81
+ service_instances = ActiveSupport::JSON.decode(json).deep_symbolize_keys
82
+ service_instances[:resources].collect { |b| b[:guid] }
83
+ puts "Found service_instances: #{service_instances[:resources].count}"
84
+ json = curl("/v3/service_instances/#{service_instances[:resources].first[:guid]}", debug: ENV.fetch("DEBUG", nil))
85
+ write_fixture("service_instance", json)
86
+
87
+ # now let's sanitize all the names and guids
88
+ # we can just use sed for this
89
+
90
+ # change anything else that looks like a guid to a test-guid using ruby
91
+ Dir.glob("spec/support/fixtures/*.json").each do |f|
92
+ puts "Sanitizing and relinking #{f}..."
93
+
94
+ service_instances[:resources].each_with_index do |instance, i|
95
+ File.write(f, File.read(f).gsub(/#{instance[:guid]}/, "test-service-instance-#{i}-guid"))
96
+ File.write(f, File.read(f).gsub(/#{instance[:name]}/, "test-service-instance-#{i}-name"))
97
+ end
98
+
99
+ service_plans[:resources].each_with_index do |plan, i|
100
+ File.write(f, File.read(f).gsub(/#{plan[:guid]}/, "test-service-plan-#{i}-guid"))
101
+ File.write(f, File.read(f).gsub(/#{plan[:name]}/, "test-service-plan-#{i}-name"))
102
+ end
103
+
104
+ service_offerings[:resources].each_with_index do |offering, i|
105
+ File.write(f, File.read(f).gsub(/#{offering[:guid]}/, "test-service-offering-#{i}-guid"))
106
+ File.write(f, File.read(f).gsub(/#{offering[:name]}/, "test-service-offering-#{i}-name"))
107
+ end
108
+
109
+ service_brokers[:resources].each_with_index do |broker, i|
110
+ File.write(f, File.read(f).gsub(/#{broker[:guid]}/, "test-service-broker-#{i}-guid"))
111
+ File.write(f, File.read(f).gsub(/#{broker[:name]}/, "test-service-broker-#{i}-name"))
112
+ end
113
+
114
+ quotas[:resources].each_with_index do |quota, i|
115
+ File.write(f, File.read(f).gsub(/#{quota[:guid]}/, "test-quota-#{i}-guid"))
116
+ File.write(f, File.read(f).gsub(/#{quota[:name]}/, "test-quota-#{i}-name"))
117
+ end
118
+
119
+ spaces[:resources].each_with_index do |space, i|
120
+ puts "replacing #{space[:guid]} with test-space-#{i}-guid"
121
+ puts "replacing #{space[:name]} with test-space-#{i}-name"
122
+ File.write(f, File.read(f).gsub(/#{space[:guid]}/, "test-space-#{i}-guid"))
123
+ File.write(f, File.read(f).gsub(/#{space[:name]}/, "test-space-#{i}-name"))
124
+ end
125
+
126
+ users[:resources].each_with_index do |user, i|
127
+ puts "replacing #{user[:guid]} with test-user-#{i}-guid"
128
+ puts "replacing #{user[:username]} with test-user-#{i}-username"
129
+ File.write(f, File.read(f).gsub(/#{user[:guid]}/, "test-user-#{i}-guid"))
130
+ # File.write(f, File.read(f).gsub(%r/#{user[:username]}/, "test-user-#{i}-username"))
131
+ # File.write(f, File.read(f).gsub(%r/#{user[:presentation_name]}/, "test-user-#{i}-username"))
132
+ end
133
+
134
+ File.write(f, File.read(f).gsub(/#{org[:guid]}/, "test-organization-0-guid"))
135
+ File.write(f, File.read(f).gsub(/#{org[:name]}/, "test-organization-0-name"))
136
+
137
+ File.write(f, File.read(f).gsub(/[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/, "other-guid"))
138
+ File.write(f, File.read(f).gsub(/([a-z0-9-]+\.)+[a-z]{2,}/, "cf.example.com"))
139
+ end
140
+
141
+ # TODO: validate the files were gsubbed correctly-ish
142
+ # puts "Basic validation..."
143
+ # %w[organization space service_broker service_offering service_plan service_instance].each do |resource|
144
+ # files = File.read(File.join(__dir__, "spec", "support", "fixtures", "#{resource.pluralize}.json"))
145
+ # file = File.read(File.join(__dir__, "spec", "support", "fixtures", "#{resource}.json"))
146
+
147
+ # json = ActiveSupport::JSON.decode(File.read(files)).deep_symbolize_keys
148
+ # json[:resources].each do |resource|
149
+ # json[:name].begin_with? "test-#{resource.underscore.dasherize}"
150
+ # json[:guid].begin_with? "test-#{resource.underscore.dasherize}"
151
+ # end
152
+ # end
153
+
154
+ puts "Done."
155
+ end
156
+ # rubocop:enable Layout/LineLength,Metrics/BlockLength
157
+
158
+ def curl(url, debug: false)
159
+ puts "cf curl \"#{url}\"" if debug
160
+ resp = `cf curl "#{url}" | jq .`
161
+ puts resp if debug
162
+ resp
163
+ end
164
+
165
+ def write_fixture(name, contents)
166
+ File.write("spec/support/fixtures/#{name}.json", contents)
167
+ end
data/cucumber.yml ADDED
@@ -0,0 +1 @@
1
+ default: --publish-quiet -f pretty
data/exe/jcf ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "jcf/cli"
6
+
7
+ cli = Dry::CLI.new(JCF::CLI)
8
+ begin
9
+ cli.call
10
+ rescue JCF::CLI::NotLoggedInError => e
11
+ puts "You are not logged in to Cloud Foundry. Please log in and try again."
12
+ puts e.message
13
+ rescue JCF::CLI::InvalidOptionError => e
14
+ puts "You need to supply options. Use --help to get details."
15
+ puts e.message
16
+ rescue NoMethodError => e
17
+ puts "Unexpected error. Please report this."
18
+ puts
19
+ puts e.message
20
+ puts e.backtrace
21
+ end
data/lib/.DS_Store ADDED
Binary file
data/lib/jcf/.DS_Store ADDED
Binary file
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "aws-sdk-cloudwatch"
4
+ require "aws-sdk-rds"
5
+ require "aws-sdk-s3"
6
+ require "active_support/core_ext/integer/time"
7
+ require "active_support/isolated_execution_state"
8
+ require "filesize"
9
+
10
+ module JCF
11
+ module AWS
12
+ class CloudWatch
13
+ NAMESPACES = {
14
+ rds: "AWS/RDS",
15
+ s3: "AWS/S3"
16
+ }.freeze
17
+ METRICS = {
18
+ free_storage: "FreeStorageSpace",
19
+ allocated_storage: "AllocatedStorage",
20
+ iops: "WriteIOPS",
21
+ cpu: "CPUUtilization"
22
+ }.freeze
23
+
24
+ def storage_free(name:)
25
+ storage_free = rds(name: name, metric: :free_storage)
26
+ storage_free.to_i
27
+ end
28
+
29
+ def storage_allocated(name:)
30
+ rds = Aws::RDS::Client.new
31
+ size = rds
32
+ .describe_db_instances(db_instance_identifier: name)
33
+ .db_instances.first.allocated_storage
34
+ Filesize.from("#{size} GB").to_i
35
+ end
36
+
37
+ def rds_storage_used(name:)
38
+ free = storage_free(name: name)
39
+ allocated = storage_allocated(name: name)
40
+ allocated - free
41
+ end
42
+
43
+ def s3_storage_used(name:)
44
+ s3 = Aws::S3::Client.new
45
+ total_size = 0
46
+ response = s3.list_objects_v2(bucket: name)
47
+ response.contents.each do |object|
48
+ total_size += object.size
49
+ end
50
+ total_size
51
+ end
52
+
53
+ def iops(name:)
54
+ rds(name: name, metric: :iops)
55
+ end
56
+
57
+ def cpu(name:)
58
+ rds(name: name, metric: :cpu)
59
+ end
60
+
61
+ private
62
+
63
+ def rds(name:, metric:)
64
+ dimensions = { name: "DBInstanceIdentifier", value: name }
65
+ aws(namespace: NAMESPACES[:rds], metric: METRICS[metric], dimensions: dimensions)
66
+ end
67
+
68
+ # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
69
+ def aws(namespace:, metric:, dimensions:, start_time: nil, end_time: nil, period: nil, statistic: nil)
70
+ cloudwatch = Aws::CloudWatch::Client.new
71
+ res = cloudwatch.get_metric_statistics({
72
+ namespace: namespace,
73
+ metric_name: metric,
74
+ dimensions: [dimensions],
75
+ start_time: (start_time || 1.day.ago),
76
+ end_time: (end_time || Time.now),
77
+ period: (period || 86_400),
78
+ statistics: [(statistic || "Average")]
79
+ })
80
+
81
+ pp res if ENV["DEBUG"]
82
+
83
+ res.datapoints.first&.average || 0
84
+ rescue Aws::Errors::MissingCredentialsError
85
+ puts "You are not logged in to an AWS shell. 'gds aws <ACCOUNT> -s'"
86
+ end
87
+ # rubocop:enable Metrics/ParameterLists, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
88
+ end
89
+ end
90
+ end
Binary file