I'm trying to re-factor current pipeline AWS ECS deployment code for classes that are deployed similarly, only differing in their data. For example,

abstract class EcsService {
  protected String serviceName = null
  protected Integer desiredCount = -1
  ...
}

@Singleton
class AdminEcsService extends EcsService {
  protected String serviceName = "ECS-Admin"
  protected Integer desiredCount = 2
  ...
}

@Singleton
class SamlService extends EcsService {
  protected String serviceName = "ECS-SAML"
  protected Integer desiredCount = 4
  ...
}

I then have a common engine that's able to deploy an AdminEcsService object and a SamlService object. For example (simplified for illustration only)

def deployService() {
  def admin = AdminEcsService.instance
  def saml = SamlService.instance
  deployToAWS(admin)
  deployToAWS(samo)
}

However, I don't really want to create a separate file for each class, i.e., I don't want to do this

EcsService.groovy:
abstract class EcsService {
  protected String serviceName = null
  protected Integer desiredCount = -1
  ...
}

AdminEcsService.groovy:
@Singleton
class AdminEcsService extends EcsService {
  protected String serviceName = "ECS-Admin"
  protected Integer desiredCount = 2
  ...
}

SamlService.groovy:
@Singleton
class SamlService extends EcsService {
  protected String serviceName = "ECS-SAML"
  protected Integer desiredCount = 4
  ...
}

Shared library (pipeline) code doesn't like multiple classes defined in one file that doesn't match the file name.

How can I accomplish this, i.e., still having the classes but all in one file? Maybe this?

class DeployementEngine {
  // Some attributes

  private abstract class EcsService {
  ...
  }

  private class AdminEcsService extends EcsService {
  ...
  }

  private class SamlEcsService extends EcsService {
  ...
  }

  // Some functions
  def deployService() {
    def admin = AdminEcsService.instance
    def saml = SamlService.instance
    deployToAWS(admin)
    deployToAWS(samo)
  }
}

Remember, I'm trying to refactor existing code so I want to change the file structure as little as possible. Any other/better ideas?

0

There are 0 best solutions below