class AwsASGTestCase(unittest.TestCase): def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='dummy-account') self.mock_app_json = self.load_json("test_lib/input/function/app.json") self.mock_env_json = self.load_json("test_lib/input/function/env.json") def create_formatted_application_request(self, app): group_request = ApplicationCreationRequest(app) excluded_group_dict = self.client.exclude_missing( json.loads(group_request.toJSON())) formatted_group_dict = self.client.convert_json( excluded_group_dict, self.client.underscore_to_camel) return formatted_group_dict def create_formatted_environment_request(self, env): group_request = EnvironmentCreationRequest(env) excluded_group_dict = self.client.exclude_missing( json.loads(group_request.toJSON())) formatted_group_dict = self.client.convert_json( excluded_group_dict, self.client.underscore_to_camel) return formatted_group_dict @staticmethod def load_json(input_path): with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), input_path)) as group_json: return json.load(group_json)
def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='dummy-account') self.mock_group_json = self.load_json(path='test_lib/group.json') self.mock_emr = self.load_json('test_lib/emr.json') self.mock_stateful_json = self.load_json( path='test_lib/stateful/import_stateful.json') self.mock_group_response = self.load_json( path='test_lib/group_res.json') self.mock_statful_res = self.load_json( path='test_lib/stateful/import_stateful_res.json') self.mock_get_stateful_import_res = self.load_json( path='test_lib/stateful/get_import_res.json') self.mock_ok_res = self.load_json('test_lib/res_ok.json') self.mock_get_instances_res = self.load_json( 'test_lib/stateful/get_instances_res.json') self.mock_get_group_activity_res = self.load_json( 'test_lib/get_group_activity_res.json') self.mock_get_deployment_status_res = self.load_json( 'test_lib/get_deployment_status_res.json') self.mock_api_call = SimpleNamespace( **self.load_json('test_lib/api_res.json'))
def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='dummy-account') self.mock_ok_res = self.load_json('test_lib/output/res_ok.json') self.mock_api_call = SimpleNamespace( **self.load_json('test_lib/api_res.json'))
def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='act-1234567') self.mock_balancer_json = self.load_json( "test_lib/input/mlb/balancer.json") self.mock_target_set_json = self.load_json( "test_lib/input/mlb/target_set.json") self.mock_target_json = self.load_json( "test_lib/input/mlb/target.json") self.mock_targets_json = self.load_json( "test_lib/input/mlb/targets.json") self.mock_listener_json = self.load_json( "test_lib/input/mlb/listener.json") self.mock_middleware_json = self.load_json( "test_lib/input/mlb/middleware.json") self.mock_routing_rule_json = self.load_json( "test_lib/input/mlb/routing_rule.json")
class AwsElastigroupTestCase(unittest.TestCase): def setUp(self): self.client = SpotinstClient( auth_token='dummy-token', account_id='dummy-account') self.mock_group_json = self.load_group_json() def create_formatted_group_request(self, group): group_request = ElastigroupCreationRequest(group) excluded_group_dict = self.client.exclude_missing( json.loads(group_request.toJSON())) formatted_group_dict = self.client.convert_json( excluded_group_dict, self.client.underscore_to_camel) return formatted_group_dict @staticmethod def load_group_json(): with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_lib/input/group.json')) as group_json: return json.load(group_json)
class SpotinstEMRTestCase(unittest.TestCase): def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='act-1234567') self.mock_emr_json = self.load_emr_json() def create_formatted_emr_request(self, emr): emr_request = EMRCreationRequest(emr) excluded_emr_dict = self.client.exclude_missing( json.loads(emr_request.toJSON())) formatted_emr_dict = self.client.convert_json( excluded_emr_dict, self.client.underscore_to_camel) return formatted_emr_dict @staticmethod def load_emr_json(): with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_lib/input/emr.json')) as emr_json: return json.load(emr_json)
def get_elastigroup(elastigroup_data, **kwargs): """ Returns the first Elastigroup that matches the id provided in the argument object. """ current_span = extract_span_from_kwargs(**kwargs) current_span.set_tag("cloud_account_id", elastigroup_data.account_id) current_span.set_tag("elastigroup_id", elastigroup_data.group_id) current_span.set_tag("elastigroup_name", elastigroup_data.group_name) current_span.set_tag("span.kind", "client") client = SpotinstClient(auth_token=elastigroup_data.access_token, account_id=elastigroup_data.account_id, print_output=False) try: return client.get_elastigroup(elastigroup_data.group_id) except Exception: current_span.set_tag('error', True) current_span.log_kv({'exception': traceback.format_exc()}) logger.exception( 'Failed to get elastigroup {} {} in account {}'.format( elastigroup_data.group_name, elastigroup_data.group_id, elastigroup_data.account_id))
class SpotinstEMRTestCase(unittest.TestCase): def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='act-1234567') self.mock_stateful_json = self.load_stateful_json() def create_formatted_import_stateful(self, stateful_instance): stateful_instance = StatefulImportRequest(stateful_instance) excluded_stateful_dict = self.client.exclude_missing( json.loads(stateful_instance.toJSON())) formatted_stateful_dict = self.client.convert_json( excluded_stateful_dict, self.client.underscore_to_camel) return formatted_stateful_dict @staticmethod def load_stateful_json(): with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_lib/stateful/import_stateful.json') ) as stateful_json: return json.load(stateful_json)
def get_elastigroup_instances(elastigroup_data, **kwargs): """ Returns a list containing the active instances of an Elastigroup. """ current_span = extract_span_from_kwargs(**kwargs) current_span.set_tag("cloud_account_id", elastigroup_data.account_id) current_span.set_tag("elastigroup_id", elastigroup_data.group_id) current_span.set_tag("elastigroup_name", elastigroup_data.group_name) current_span.set_tag("span.kind", "client") client = SpotinstClient(auth_token=elastigroup_data.access_token, account_id=elastigroup_data.account_id, print_output=False) try: return client.get_elastigroup_active_instances( elastigroup_data.group_id) except Exception: current_span.set_tag('error', True) current_span.log_kv({'exception': traceback.format_exc()}) logger.exception( 'Failed to get instance status for elastigroup {} {} in account {}' .format(elastigroup_data.group_name, elastigroup_data.group_id, elastigroup_data.account_id)) return []
class SpotinstSubscriptionTestCase(unittest.TestCase): def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='act-1234567') self.mock_event_subscription_json = self.load_json( path='test_lib/input/event/subscription.json') def create_formatted_event_subscription_request(self, subscription): subscription_request = SubscriptionRequest(subscription) excluded_subscription_dict = self.client.exclude_missing( json.loads(subscription_request.toJSON())) formatted_subscription_dict = self.client.convert_json( excluded_subscription_dict, self.client.underscore_to_camel) return formatted_subscription_dict @staticmethod def load_json(path): with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), path)) as subscription_json: return json.load(subscription_json)
def client(): client = SpotinstClient() def get_token_from_file(): credentials_file = os.path.isfile('~/.spotinst/credentials') if credentials_file: client = SpotinstClient(credentials_file='~/.spotinst/credentials', profile='default') def get_token_from_env(): if 'spotinst_token' in os.environ: spotinst_token = os.environ.get("spotinst_token") if "account_id" in os.environ: account_id = os.environ.get("account_id") client = SpotinstClient(auth_token=spotinst_token, account_id=account_id) return client
ImportParser.add_argument('--spotinst_token', dest='api_token', required=True, help='Spotinst API Token') ImportParser.add_argument('--spotinst_accountid', dest='api_accountid', required=True, help='Spotinst Account ID linked to token') ImportParser.add_argument('-v', '--verbose', dest='VerboseMode', action='count', default=int(0), help='enable verbose output (-vv for more)') options = ImportParser.parse_args() api_token = options.api_token api_accountid = options.api_accountid VerboseMode = options.VerboseMode logger.Logging().configure(VerboseMode) log = logger.logging.getLogger('Logger') #loggers = [logger.logging.getLogger(name) for name in logger.logging.root.manager.loggerDict] #print(loggers) client = SpotinstClient(auth_token=api_token, account_id=api_accountid) getaccounts = client.get_accounts() for account in getaccounts: print('Spotinst Account ID: {} AWS Account ID: {} Account Name: {}'. format(account['account_id'], account['provider_external_id'], account['name']))
def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='act-1234567') self.mock_emr_json = self.load_emr_json()
action='count', default=int(0), help='enable verbose output (-vv for more)') ImportParser.add_argument( '--dryrun', dest='DryrunMode', action='store_true', default=False, help='Show what you would happen but dont make any changes') options = ImportParser.parse_args() AwsProfile = options.AwsProfile spotinst_token = options.spotinst_token spotinst_accountid = options.spotinst_accountid DryrunMode = options.DryrunMode VerboseMode = options.VerboseMode logger.Logging().configure(VerboseMode) log = logger.logging.getLogger('Logger') ExternalIdCode = aws_setup.randomStringwithDigits(16) ExternalId = 'spotinst:aws:extid:' + ExternalIdCode client = SpotinstClient(auth_token=spotinst_token, account_id=spotinst_accountid) #account = client.create_account(AwsProfile) #log.debug(account) log.info('Setting Cloud credentials in Spotinst') #log.debug('Role ARN: {} ExternalId: {}'.format(role_response['Role']['Arn'], ExternalId)) spotinst_set_cloud_creds = client.set_cloud_credentials(IamArn, ExternalId) log.debug(spotinst_set_cloud_creds)
def setUp(self): self.client = SpotinstClient( auth_token='dummy-token', account_id='dummy-account') self.mock_group_json = self.load_group_json()
def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='dummy-account') self.mock_app_json = self.load_json("test_lib/input/function/app.json") self.mock_env_json = self.load_json("test_lib/input/function/env.json")
def setUp(self): self.client = SpotinstClient( auth_token='dummy-token', account_id='act-1234567') self.mock_clone_emr_json = self.load_json(path='test_lib/input/emr/clone_emr.json') self.mock_new_emr_json = self.load_json(path='test_lib/input/emr/new_emr.json')
def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='act-1234567')
def get_token_from_env(): if 'spotinst_token' in os.environ: spotinst_token = os.environ.get("spotinst_token") if "account_id" in os.environ: account_id = os.environ.get("account_id") client = SpotinstClient(auth_token=spotinst_token, account_id=account_id)
def get_token_from_file(): credentials_file = os.path.isfile('~/.spotinst/credentials') if credentials_file: client = SpotinstClient(credentials_file='~/.spotinst/credentials', profile='default')
def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='act-1234567') self.mock_event_subscription_json = self.load_json( path='test_lib/input/event/subscription.json')
class SpotinstEMRTestCase(unittest.TestCase): def setUp(self): self.client = SpotinstClient(auth_token='dummy-token', account_id='act-1234567') self.mock_balancer_json = self.load_json( "test_lib/input/mlb/balancer.json") self.mock_target_set_json = self.load_json( "test_lib/input/mlb/target_set.json") self.mock_target_json = self.load_json( "test_lib/input/mlb/target.json") self.mock_targets_json = self.load_json( "test_lib/input/mlb/targets.json") self.mock_listener_json = self.load_json( "test_lib/input/mlb/listener.json") self.mock_middleware_json = self.load_json( "test_lib/input/mlb/middleware.json") self.mock_routing_rule_json = self.load_json( "test_lib/input/mlb/routing_rule.json") def create_formatted_balancer_request(self, balancer): balancer = BalancerRequest(balancer) excluded_balancer_dict = self.client.exclude_missing( json.loads(balancer.toJSON())) formatted_balancer_dict = self.client.convert_json( excluded_balancer_dict, self.client.underscore_to_camel) return formatted_balancer_dict def create_formatted_target_set_request(self, target_set): target_set = TargetSetRequest(target_set) excluded_target_set_dict = self.client.exclude_missing( json.loads(target_set.toJSON())) formatted_target_set_dict = self.client.convert_json( excluded_target_set_dict, self.client.underscore_to_camel) return formatted_target_set_dict def create_formatted_target_request(self, target): target = TargetRequest(target) excluded_target_dict = self.client.exclude_missing( json.loads(target.toJSON())) formatted_target_dict = self.client.convert_json( excluded_target_dict, self.client.underscore_to_camel) return formatted_target_dict def create_formatted_targets_request(self, targets): targets = TargetsRequest(targets) excluded_targets_dict = self.client.exclude_missing( json.loads(targets.toJSON())) formatted_targets_dict = self.client.convert_json( excluded_targets_dict, self.client.underscore_to_camel) return formatted_targets_dict def create_formatted_middleware_request(self, middleware): middleware = MiddlewareRequest(middleware) excluded_middleware_dict = self.client.exclude_missing( json.loads(middleware.toJSON())) formatted_middleware_dict = self.client.convert_json( excluded_middleware_dict, self.client.underscore_to_camel) return formatted_middleware_dict def create_formatted_routing_rule_request(self, routing_rule): routing_rule = RoutingRuleRequest(routing_rule) excluded_routing_rule_dict = self.client.exclude_missing( json.loads(routing_rule.toJSON())) formatted_routing_rule_dict = self.client.convert_json( excluded_routing_rule_dict, self.client.underscore_to_camel) return formatted_routing_rule_dict def create_formatted_listener_request(self, listener): listener = ListenerRequest(listener) excluded_listener_dict = self.client.exclude_missing( json.loads(listener.toJSON())) formatted_listener_dict = self.client.convert_json( excluded_listener_dict, self.client.underscore_to_camel) return formatted_listener_dict @staticmethod def load_json(input_file): with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), input_file)) as output_json: return json.load(output_json)