def test_filters_and_extras_with_region_specified(self): argv = ["foo.j2", "ec2", "avengers-west-2", "bar=mars", "--whee", "xyzzy"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, "foo.j2") self.assertEqual(ns, "ec2") self.assertEqual(region, "avengers-west-2") self.assertEqual(len(filter_by), 1) self.assertEqual(len(extras), 2)
def test_namespace_can_use_cloudwatch_syntax(self): args = [ 'foo.yaml.j2', 'AWS/EC2', ] templ, ns, region, filter_by, token = plumbum.interpret_options(args) self.assertEqual(templ, 'foo.yaml.j2') self.assertEqual(ns, 'ec2')
def test_minimal(self): argv = ['foo.j2', 'ec2'] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, 'foo.j2') self.assertEqual(ns, 'ec2') self.assertEqual(region, plumbum.DEFAULT_REGION) self.assertFalse(len(filter_by)) self.assertFalse(len(extras))
def test_minimal(self): argv = ["foo.j2", "ec2"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, "foo.j2") self.assertEqual(ns, "ec2") self.assertEqual(region, plumbum.DEFAULT_REGION) self.assertFalse(len(filter_by)) self.assertFalse(len(extras))
def test_filters_and_extras_found(self): argv = ["foo.j2", "ec2", "bar=mars", "--whee", "xyzzy"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, "foo.j2") self.assertEqual(ns, "ec2") self.assertEqual(region, plumbum.DEFAULT_REGION) self.assertEqual(len(filter_by), 1) self.assertEqual(len(extras), 2)
def test_filters_and_extras_found(self): argv = ['foo.j2', 'ec2', 'bar=mars', '--whee', 'xyzzy'] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, 'foo.j2') self.assertEqual(ns, 'ec2') self.assertEqual(region, plumbum.DEFAULT_REGION) self.assertEqual(len(filter_by), 1) self.assertEqual(len(extras), 2)
def test_all_args(self): args = ["-r", "us-west-2", "-f", "instance-type=c3.large", "foo.yaml.j2", "ec2"] templ, ns, region, filter_by, token = plumbum.interpret_options(args) self.assertEqual(region, "us-west-2") self.assertEqual(ns, "ec2") self.assertEqual(templ, "foo.yaml.j2") self.assertEqual(filter_by, {"instance-type": "c3.large"})
def test_namespace_can_use_cloudwatch_syntax(self): args = [ 'AWS/EC2', 'foo.yaml.j2' ] templ, ns, region, filter_by, token = plumbum.interpret_options(args) self.assertEqual(templ, 'foo.yaml.j2') self.assertEqual(ns, 'ec2')
def test_no_template(self, mock_exit): """ Test that if the namespace and template are not passed, we get the correct failure/exit. """ args = ["-f", "instance-type=c3.large", "foo.yaml.j2"] templ, ns, region, filter_by, token = plumbum.interpret_options(args) self.assertEqual(ns, None) self.assertEqual(region, plumbum.DEFAULT_REGION) self.assertEqual(filter_by, {"instance-type": "c3.large"})
def test_filters_and_extras_with_region_specified(self): argv = [ 'foo.j2', 'ec2', 'avengers-west-2', 'bar=mars', '--whee', 'xyzzy' ] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, 'foo.j2') self.assertEqual(ns, 'ec2') self.assertEqual(region, 'avengers-west-2') self.assertEqual(len(filter_by), 1) self.assertEqual(len(extras), 2)
def test_all_args(self): args = [ '-r', 'non-legal-region', 'ec2', '-f', 'instance-type=c3.large', 'foo.yaml.j2' ] templ, ns, region, filter_by, token = plumbum.interpret_options(args) self.assertEqual(region, 'non-legal-region') self.assertEqual(ns, 'ec2') self.assertEqual(templ, 'foo.yaml.j2') self.assertEqual(filter_by, 'instance-type=c3.large')
def test_no_template(self, mock_exit): """ Test that if the namespace and template are not passed, we get the correct failure/exit. """ args = [ 'ec2', '-f', 'instance-type=c3.large', ] templ, ns, region, filter_by, token = plumbum.interpret_options(args) self.assertEqual(templ, None) self.assertEqual(region, plumbum.DEFAULT_REGION) self.assertEqual(filter_by, 'instance-type=c3.large')
def test_region_can_be_specified(self): argv = ["foo.j2", "ec2", "avengers-west-2"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, "foo.j2") self.assertEqual(ns, "ec2") self.assertEqual(region, "avengers-west-2") self.assertFalse(len(filter_by)) self.assertFalse(len(extras)) # more regions argv = ["foo.j2", "ec2", "us-east-1"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(region, "us-east-1") argv = ["foo.j2", "ec2", "cn-north-1"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(region, "cn-north-1") argv = ["foo.j2", "ec2", "ap-northeast-1"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(region, "ap-northeast-1") argv = ["foo.j2", "ec2", "us-gov-west-1"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(region, "us-gov-west-1")
def test_region_can_be_specified(self): argv = ['foo.j2', 'ec2', 'avengers-west-2'] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, 'foo.j2') self.assertEqual(ns, 'ec2') self.assertEqual(region, 'avengers-west-2') self.assertFalse(len(filter_by)) self.assertFalse(len(extras)) # more regions argv = ['foo.j2', 'ec2', 'us-east-1'] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(region, 'us-east-1') argv = ['foo.j2', 'ec2', 'cn-north-1'] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(region, 'cn-north-1') argv = ['foo.j2', 'ec2', 'ap-northeast-1'] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(region, 'ap-northeast-1') argv = ['foo.j2', 'ec2', 'us-gov-west-1'] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(region, 'us-gov-west-1')
def test_no_template(self, mock_exit): """ Test that if the namespace and template are not passed, we get the correct failure/exit. """ args = [ '-f', 'instance-type=c3.large', 'foo.yaml.j2', ] templ, ns, region, filter_by, token = plumbum.interpret_options(args) self.assertEqual(ns, None) self.assertEqual(region, plumbum.DEFAULT_REGION) self.assertEqual(filter_by, {u'instance-type': u'c3.large'})
def test_all_args(self): args = [ '-r', 'non-legal-region', '-f', 'instance-type=c3.large', 'foo.yaml.j2', 'ec2', ] templ, ns, region, filter_by, token = plumbum.interpret_options(args) self.assertEqual(region, 'non-legal-region') self.assertEqual(ns, 'ec2') self.assertEqual(templ, 'foo.yaml.j2') self.assertEqual(filter_by, {u'instance-type': u'c3.large'})
def main(): template_file, namespace, region, filters, cli_tokens = interpret_options() # get the template first so this can fail before making a network request jinja_template = get_jinja_template(template_file) # this is the ugly hack part: we'll require the caller to pass in a specific CLI 'option' # to achieve the desired effect of getting the specified beanstalk environment resources # TODO: refactor plumbum into a class style, so an argument for the environment name can replace this if 'environment_name' not in filters: raise CliArgsException( 'in {}, you must pass at least one environment_name=something filter'.format( os.path.basename(__file__) ) ) # check the namespace, this script only works for 'beanstall' if namespace != 'beanstalk': raise CliArgsException( "The only valid namespace for {} is 'beanstalk'".format(os.path.basename(__file__)) ) resources = list_beanstalk(region, filters) base_tokens = { 'filters': filters, 'region': region, # Use for Auth config section if needed 'resources': resources, 'environment_name': filters['environment_name'], } tempfile = NamedTemporaryFile() tempfile.write(jinja_template.render(get_template_tokens(base_tokens=base_tokens, cli_tokens=cli_tokens))) tempfile.flush() # TODO: not hardcoding the Perdiod and Count requies a refactor of leabutt.py into a class with a config object. leadbutt(tempfile.name, {'Period': 1, 'Count': 5}, verbose=False) tempfile.close()
def test_trivial_case(self): argv = [] with self.assertRaises(IndexError): plumbum.interpret_options(argv)
def test_namespace_can_use_cloudwatch_syntax(self): argv = ["foo.j2", "AWS/EC2"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, "foo.j2") self.assertEqual(ns, "ec2")
def test_dynamodb_namespace(self): argv = ["foo.j2", "dynamodb", "us-east-1"] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, "foo.j2") self.assertEqual(ns, "dynamodb") self.assertEqual(region, "us-east-1")
def test_dynamodb_namespace(self): argv = ['foo.j2', 'dynamodb', 'us-east-1'] templ, ns, region, filter_by, extras = plumbum.interpret_options(argv) self.assertEqual(templ, 'foo.j2') self.assertEqual(ns, 'dynamodb') self.assertEqual(region, 'us-east-1')