diff --git a/CHANGELOG.md b/CHANGELOG.md index f5a756df..cd971b65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ _Please add entries here for your pull requests that are not yet released._ ### Added - Added `--clean` option to `run:detached` command to delete workload when disconnecting. [PR 129](https://github.com/shakacode/heroku-to-control-plane/pull/129) by [Rafael Gomes](https://github.com/rafaelgomesxyz). +- Added `--domain` option to `maintenance`, `maintenance:on` and `maintenance:off` commands. [PR 131](https://github.com/shakacode/heroku-to-control-plane/pull/131) by [Rafael Gomes](https://github.com/rafaelgomesxyz). +- Added `default_domain` config to specify domain for `maintenance`, `maintenance:on` and `maintenance:off` commands. [PR 131](https://github.com/shakacode/heroku-to-control-plane/pull/131) by [Rafael Gomes](https://github.com/rafaelgomesxyz). ### Changed diff --git a/README.md b/README.md index 15b98a1c..e46c31f5 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,10 @@ apps: # This is relative to the `.controlplane/` directory. release_script: release_script + # default_domain is used for commands that require a domain + # including `maintenance`, `maintenance:on`, `maintenance:off`. + default_domain: domain.com + my-app-other: <<: *common diff --git a/examples/controlplane.yml b/examples/controlplane.yml index 50dfd4c3..c3568045 100644 --- a/examples/controlplane.yml +++ b/examples/controlplane.yml @@ -101,6 +101,10 @@ apps: # This is relative to the `.controlplane/` directory. release_script: release_script + # default_domain is used for commands that require a domain + # including `maintenance`, `maintenance:on`, `maintenance:off`. + default_domain: domain.com + my-app-other: <<: *common diff --git a/lib/command/base.rb b/lib/command/base.rb index 0cb45090..6d802837 100644 --- a/lib/command/base.rb +++ b/lib/command/base.rb @@ -125,6 +125,18 @@ def self.location_option(required: false) } end + def self.domain_option(required: false) + { + name: :domain, + params: { + banner: "DOMAIN_NAME", + desc: "Domain name", + type: :string, + required: required + } + } + end + def self.upstream_token_option(required: false) { name: :upstream_token, diff --git a/lib/command/maintenance.rb b/lib/command/maintenance.rb index b4ef3081..514dadd1 100644 --- a/lib/command/maintenance.rb +++ b/lib/command/maintenance.rb @@ -4,7 +4,8 @@ module Command class Maintenance < Base NAME = "maintenance" OPTIONS = [ - app_option(required: true) + app_option(required: true), + domain_option ].freeze DESCRIPTION = "Checks if maintenance mode is on or off for an app" LONG_DESCRIPTION = <<~DESC @@ -20,7 +21,11 @@ def call # rubocop:disable Metrics/MethodLength one_off_workload = config[:one_off_workload] maintenance_workload = config.current[:maintenance_workload] || "maintenance" - domain_data = cp.find_domain_for([one_off_workload, maintenance_workload]) + domain_data = if config.domain + cp.fetch_domain(config.domain) + else + cp.find_domain_for([one_off_workload, maintenance_workload]) + end unless domain_data raise "Can't find domain. " \ "Maintenance mode is only supported for domains that use path based routing mode " \ diff --git a/lib/command/maintenance_off.rb b/lib/command/maintenance_off.rb index 0cd06ea4..03d0c86a 100644 --- a/lib/command/maintenance_off.rb +++ b/lib/command/maintenance_off.rb @@ -4,7 +4,8 @@ module Command class MaintenanceOff < Base NAME = "maintenance:off" OPTIONS = [ - app_option(required: true) + app_option(required: true), + domain_option ].freeze DESCRIPTION = "Disables maintenance mode for an app" LONG_DESCRIPTION = <<~DESC @@ -18,7 +19,11 @@ def call # rubocop:disable Metrics/MethodLength one_off_workload = config[:one_off_workload] maintenance_workload = config.current[:maintenance_workload] || "maintenance" - domain_data = cp.find_domain_for([one_off_workload, maintenance_workload]) + domain_data = if config.domain + cp.fetch_domain(config.domain) + else + cp.find_domain_for([one_off_workload, maintenance_workload]) + end unless domain_data raise "Can't find domain. " \ "Maintenance mode is only supported for domains that use path based routing mode " \ diff --git a/lib/command/maintenance_on.rb b/lib/command/maintenance_on.rb index aa25b8cf..ba734b33 100644 --- a/lib/command/maintenance_on.rb +++ b/lib/command/maintenance_on.rb @@ -4,7 +4,8 @@ module Command class MaintenanceOn < Base NAME = "maintenance:on" OPTIONS = [ - app_option(required: true) + app_option(required: true), + domain_option ].freeze DESCRIPTION = "Enables maintenance mode for an app" LONG_DESCRIPTION = <<~DESC @@ -18,7 +19,11 @@ def call # rubocop:disable Metrics/MethodLength one_off_workload = config[:one_off_workload] maintenance_workload = config.current[:maintenance_workload] || "maintenance" - domain_data = cp.find_domain_for([one_off_workload, maintenance_workload]) + domain_data = if config.domain + cp.fetch_domain(config.domain) + else + cp.find_domain_for([one_off_workload, maintenance_workload]) + end unless domain_data raise "Can't find domain. " \ "Maintenance mode is only supported for domains that use path based routing mode " \ diff --git a/lib/core/config.rb b/lib/core/config.rb index 13e10f45..fec91235 100644 --- a/lib/core/config.rb +++ b/lib/core/config.rb @@ -38,6 +38,10 @@ def location @location ||= load_location_from_options || load_location_from_env || load_location_from_file end + def domain + @domain ||= load_domain_from_options || load_domain_from_file + end + def [](key) ensure_current_config! @@ -253,6 +257,16 @@ def load_location_from_file strip_str_and_validate(current.fetch(:default_location)) end + def load_domain_from_options + strip_str_and_validate(options[:domain]) + end + + def load_domain_from_file + return unless current&.key?(:default_domain) + + strip_str_and_validate(current.fetch(:default_domain)) + end + def warn_deprecated_options(app_options) deprecated_option_keys = new_option_keys.select { |old_key| app_options.key?(old_key) } return if deprecated_option_keys.empty? diff --git a/lib/core/controlplane.rb b/lib/core/controlplane.rb index 09a5a43b..0f73ae5b 100644 --- a/lib/core/controlplane.rb +++ b/lib/core/controlplane.rb @@ -265,10 +265,18 @@ def find_domain_for(workloads) route = find_domain_route(domain_data) next false if route.nil? - workloads.any? { |workload| route["workloadLink"].split("/").last == workload } + workloads.any? { |workload| route["workloadLink"].match?(%r{/org/#{org}/gvc/#{gvc}/workload/#{workload}}) } end end + def fetch_domain(domain) + domain_data = api.fetch_domain(org: org, domain: domain) + route = find_domain_route(domain_data) + return nil if route.nil? + + domain_data + end + def get_domain_workload(data) route = find_domain_route(data) route["workloadLink"].split("/").last diff --git a/lib/core/controlplane_api.rb b/lib/core/controlplane_api.rb index 3ea941e3..18029b6b 100644 --- a/lib/core/controlplane_api.rb +++ b/lib/core/controlplane_api.rb @@ -94,6 +94,10 @@ def delete_volumeset(org:, gvc:, volumeset:) api_json("/org/#{org}/gvc/#{gvc}/volumeset/#{volumeset}", method: :delete) end + def fetch_domain(org:, domain:) + api_json("/org/#{org}/domain/#{domain}", method: :get) + end + def list_domains(org:) api_json("/org/#{org}/domain", method: :get) end