Sha256: 844145dd3dd57cb032c6bd35bdf10fa4799f947dbf72fb540b69656764272b03

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module ThreeScaleToolbox
  module Commands
    module ImportCommand
      module OpenAPI
        class CreateActiveDocsStep
          include Step

          def call
            active_doc = {
              name: api_spec.title,
              system_name: activedocs_system_name,
              service_id: service.id,
              body: resource.to_json,
              description: api_spec.description
            }

            res = threescale_client.create_activedocs(active_doc)
            # Make operation indempotent
            if (errors = res['errors'])
              raise ThreeScaleToolbox::Error, "ActiveDocs has not been created. #{errors}" \
                unless system_name_already_taken_error? errors

              # if activedocs system_name exists, ignore error, update activedocs
              puts 'Activedocs exists, update!'
              update_res = threescale_client.update_activedocs(find_activedocs_id, active_doc)
              raise ThreeScaleToolbox::Error, "ActiveDocs has not been updated. #{update_res['errors']}" unless update_res['errors'].nil?
            end
          end

          private

          def activedocs_system_name
            @activedocs_system_name ||= service.show_service['system_name']
          end

          def find_activedocs_id
            activedocs = get_current_service_activedocs
            raise ThreeScaleToolbox::Error, "Could not find activedocs with system_name: #{activedocs_system_name}" if activedocs.empty?

            activedocs.dig(0, 'id')
          end

          def get_current_service_activedocs
            threescale_client.list_activedocs.select do |activedoc|
              activedoc['system_name'] == activedocs_system_name
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
3scale_toolbox-0.7.0 lib/3scale_toolbox/commands/import_command/openapi/create_activedocs_step.rb