def test_post_duplicate_cluster(self, redshift_clusters): input_string = simplejson.dumps(REDSHIFT_CLUSTER_INPUT_DICT) result = post_cluster(redshift_clusters, input_string) assert 'post_accepted' in result assert result['post_accepted'] is True with pytest.raises(ValueError): post_cluster(redshift_clusters, input_string)
def test_post_bad_cluster(self, redshift_clusters, cluster_name, port): input_dict = dict(REDSHIFT_CLUSTER_INPUT_DICT) input_dict["redshift_id"] = cluster_name input_dict["port"] = port input_string = simplejson.dumps(input_dict) with pytest.raises(ValueError): post_cluster(redshift_clusters, input_string)
def test_post_insufficient_key(self, missing_key): test_dict = dict(REDSHIFT_CLUSTER_INPUT_DICT) test_dict.pop(missing_key) input_string = simplejson.dumps(test_dict) with pytest.raises(ValueError) as e: post_cluster(None, input_string) expected_message = "ValueError: missing the following required args {0}".format( [missing_key]) assert e.exconly() == expected_message
def clusters(request): """ clusters handles GET and POST requests from the clusters endpoint **GET /v1/clusters/** Example: ``/v1/clusters/`` *Example Response* :: [ { 'redshift_id': 'cluster-1', 'port': 5439, 'host': 'cluster-1.account.region.redshift.amazonaws.com', 'db_schema': 'public', 'groups': ['search_infra', 'biz'] }, { 'redshift_id': 'cluster-1-user', 'port': 5439, 'host': 'cluster-1-user.account.region.redshift.amazonaws.com', 'db_schema': 'public', 'groups': ['search_infra', 'log_infra'] }, { 'redshift_id': 'cluster-2', 'port': 5439, 'host': cluster-2.account.region.redshift.amazonaws.com, 'db_schema': 'public', 'groups': ['mobile', 'log_infra'] }, ] ============ =========== Status Code Description ============ =========== **200** Success **500** unknown exception ============ =========== * **Encoding type:** *application/json* **POST /v1/clusters/** Example: ``/v1/clusters`` **Query Parameters:** * **request.body** -- the json string of cluster details *Example request.body* :: "{ 'redshift_id': 'cluster-2', 'port': 5439, 'host': 'cluster-2.account.region.redshift.amazonaws.com' }" ============ =========== Status Code Description ============ =========== **200** Success **404** invalid cluster parameters **500** unknown exception ============ =========== * **Encoding type:** *application/json* """ try: if request.method == "POST": return 200, post_cluster( TableConnection.get_connection('RedshiftClusters'), request.body) elif request.method == "GET": return 200, list_all_clusters( TableConnection.get_connection('RedshiftClusters')) except PrimaryKeyError as e: return 400, {'error': 'bad hash_key or missing required arguments'} except ValueError as e: if "ConditionalCheckFailedException" in repr(e): return 404, { 'error': "ConditionalCheckFailed; possible duplicate cluster" } return 404, {'error': repr(e)} except Exception as unknown_exception: return 500, {'error': repr(unknown_exception)}
def test_post_no_kwargs(self): with pytest.raises(JSONDecodeError): post_cluster(None, "")
def test_post_acceptable_cluster(self, redshift_clusters): input_string = simplejson.dumps(REDSHIFT_CLUSTER_INPUT_DICT) result = post_cluster(redshift_clusters, input_string) assert 'post_accepted' in result assert result['post_accepted'] is True