def consumer_sets_list(args: Namespace) -> None: body = dedent(""" query allConsumerSets($clusterId: String, $productId: String, $projectId: String) { allConsumerSets(clusterId: $clusterId, productId: $productId, projectId: $projectId) { id name projectId instances config { cluster { id schema table } consumerGroup leaseStorageContainer } } } """ # noqa ).strip() vars = clean_dict({ "projectId": args.project_id, "productId": args.product_id, "clusterId": args.cluster_id, }) query = Query(body, args, endpoint="/product/graphql") query.execute(vars) print_query(query, "allConsumerSets")
def roles_remove(args: Namespace) -> None: """ Removes a role from a user """ mutation = textwrap.dedent(""" mutation removeRoleFromUser($input: UserRoleInput!) { removeRoleFromUser(input: $input) { success } } """).strip() vars = clean_dict({ "input": { "userId": args.user, "roleFqn": args.role, "resourceId": args.resource, } }) query = Query(mutation, args) query.execute(vars) print_query(query, "removeRoleFromUser", "Successfully removed role from user.")
def clusters_list(args: Namespace) -> None: """ Lists all projects for the current user in the specified region """ body = textwrap.dedent(""" query allClusters($filter: [ClusterFilter]) { allClusters(sort: [CRATE_VERSION_DESC], filter: $filter) { data { id name numNodes crateVersion projectId username fqdn } } } """).strip() project_filter = ({ "by": "PROJECT_ID", "op": "EQ", "value": args.project_id } if args.project_id else None) vars = clean_dict({"filter": [project_filter] if project_filter else None}) query = Query(body, args) query.execute(vars) print_query(query, "allClusters")
def test_print_query_no_data(print_info, load_config, response, key): query = Query("", Namespace(env="test")) query._response = response print_query(query, key) expected_message = "Result contained no data to print." print_info.assert_called_once_with(expected_message)
def test_print_query_success_with_message(print_success, load_config, response, key, message, expected_message): query = Query("", Namespace(env="test")) query._response = response print_query(query, key, message) print_success.assert_called_once_with(expected_message)
def users_list(args: Namespace) -> None: """ List all users within organizations that the logged in user is part of """ body = textwrap.dedent( """ query allUsers($queryArgs: UserQueryArgs) { allUsers(sort: EMAIL, queryArgs: $queryArgs) { data { uid email username } } } """ ).strip() vars = clean_dict( {"queryArgs": {"noOrg": args.no_org, "organizationId": args.org_id}} ) query = Query(body, args) query.execute(vars) print_query(query, "allUsers")
def test_print_query_malformatted_response(print_error, load_config): query = Query("", Namespace(env="test")) query._response = 42 print_query(query) expected_message = "Result has no proper format to print." print_error.assert_called_once_with(expected_message)
def organizations_list(args: Namespace) -> None: """ Lists organizations """ _query = """ { allOrganizations { data { id, name, planType, notification { alert { email, enabled } } } } } """ query = Query(_query, args) query.execute() print_query(query, "allOrganizations")
def test_print_query_not_successful(print_error, load_config, response, key, expected_message): query = Query("", Namespace(env="test")) query._response = response print_query(query, key) print_error.assert_called_once_with(expected_message)
def product_deploy(args: Namespace) -> None: """ Deploy a new CrateDB Cloud for Azure IoT product. """ mutation = textwrap.dedent(""" mutation createProduct( $name: String! $projectId: String! $tier: String! $unit: Int $cluster: CreateClusterInput! $consumer: CreateConsumerSetInput! ) { createProduct( name: $name projectId: $projectId tier: $tier unit: $unit cluster: $cluster consumer: $consumer ) { id url } } """).strip() vars = clean_dict({ "tier": args.tier, "unit": args.unit, "projectId": args.project_id, "name": args.product_name, "cluster": { "version": args.version, "username": args.username, "password": args.password, }, "consumer": { "eventhub": { "connectionString": args.consumer_eventhub_connection_string, "consumerGroup": args.consumer_eventhub_consumer_group, "leaseStorage": { "connectionString": args. consumer_eventhub_lease_storage_connection_string, # noqa "container": args.consumer_eventhub_lease_storage_container, # noqa }, }, "schema": args.consumer_schema, "table": args.consumer_table, }, }) query = Query(mutation, args, endpoint="/product/graphql") query.execute(vars) print_query(query, "createProduct")
def org_users_remove(args: Namespace): mutation = textwrap.dedent(""" mutation removeUserFromOrganization($input: RemoveUserFromOrganizationInput!) { removeUserFromOrganization(input: $input) { success } } """).strip() vars = {"input": {"user": args.user, "organizationId": args.org_id}} query = Query(mutation, args) query.execute(vars) print_query(query, "removeUserFromOrganization")
def me(args: Namespace) -> None: """ Prints the current logged in user """ _query = """ { me { email username } } """ query = Query(_query, args) query.execute() print_query(query, "me")
def roles_list(args: Namespace) -> None: """ Lists all roles a user can be assigned to """ _query = textwrap.dedent(""" query { allRoles { data { fqn friendlyName } } } """).strip() query = Query(_query, args) query.execute() print_query(query, "allRoles")
def test_execute_query_with_variables(load_config): body = """ query { me { uid } } """ vars = {"a": "foo", "b": 42} result_future = asyncio.Future() result_future.set_result({"data": []}) query = Query(body, Namespace(env="test")) with patch.object(query, "_fetch_data", return_value=result_future) as fetch_data: query.execute(vars) fetch_data.assert_called_once_with(body, vars)
def project_create(args: Namespace) -> None: """ Creates a project in the organization the user belongs to. """ _query = f""" mutation {{ createProject(input: {{ name: "{args.name}", organizationId: "{args.org_id}" }}) {{ id }} }} """ query = Query(_query, args) query.execute() print_query(query, "createProject")
def org_users_add(args: Namespace): mutation = textwrap.dedent(""" mutation addUserToOrganization($input: AddUserToOrganizationInput!) { addUserToOrganization(input: $input) { user { uid email organizationId } } } """).strip() vars = {"input": {"user": args.user, "organizationId": args.org_id}} if args.role is not None: vars["input"]["roleFqn"] = args.role query = Query(mutation, args) query.execute(vars) print_query(query, "addUserToOrganization")
def project_user_remove(args: Namespace) -> None: """ Removes a user to a project. """ _query = f""" mutation {{ removeUserFromProject(input: {{ projectId: "{args.project_id}", user: "******" }}) {{ success }} }} """ query = Query(_query, args) query.execute() print_query(query, "removeUserFromProject", "Successfully removed user from project.")
def project_user_add(args: Namespace) -> None: """ Adds a user to a project. """ _query = f""" mutation {{ addUserToProject(input: {{ projectId: "{args.project_id}", user: "******" }}) {{ success }} }} """ query = Query(_query, args) query.execute() print_query(query, "addUserToProject", "Successfully added user to project.")
def organizations_create(args: Namespace) -> None: """ Creates an organization """ _query = f""" mutation {{ createOrganization(input: {{ name: "{args.name}", planType: {args.plan_type} }}) {{ id name planType }} }} """ query = Query(_query, args) query.execute() print_query(query, "createOrganization")
def projects_list(args: Namespace) -> None: """ Lists all projects for the current user in the specified region """ _query = """ { allProjects { data { id name region organizationId } } } """ query = Query(_query, args) query.execute() print_query(query, "allProjects")
def consumer_sets_edit(args: Namespace) -> None: body = dedent(""" mutation editConsumerSet($id: String!, $input: EditConsumerSetInput!) { editConsumerSet( id: $id, input: $input ) { id } } """ # noqa ).strip() vars = clean_dict({ "id": args.consumer_set_id, "input": { "eventhub": { "connectionString": args.consumer_eventhub_connection_string, "consumerGroup": args.consumer_eventhub_consumer_group, "leaseStorage": { "connectionString": args. consumer_eventhub_lease_storage_connection_string, # noqa "container": args.consumer_eventhub_lease_storage_container, }, }, "cluster": { "schema": args.consumer_schema, "table": args.consumer_table, }, }, }) query = Query(body, args, endpoint="/product/graphql") query.execute(vars) print_query(query, "editConsumerSet")
def roles_add(args: Namespace) -> None: """ Adds a new role to a user """ mutation = textwrap.dedent(""" mutation addRoleToUser($input: UserRoleInput!) { addRoleToUser(input: $input) { success } } """).strip() vars = clean_dict({ "input": { "userId": args.user, "roleFqn": args.role, "resourceId": args.resource, } }) query = Query(mutation, args) query.execute(vars) print_query(query, "addRoleToUser")
def test_print_query_error(print_error, load_config): query = Query("", Namespace(env="test")) query._error = "This is a GraphQL error message" print_query(query) print_error.assert_called_once_with(query._error)
def test_print_query_data(print_format, load_config, response, key, format): query = Query("", Namespace(env="test", output_fmt=format)) query._response = response print_query(query, key) print_format.assert_called_once_with(["foo", "bar"], format)
def test_region_set(mock_load_config, input, expected): query = Query("{}", input) assert query._region == expected
def test_output_fmt_set(mock_load_config, input, expected): query = Query("{}", input) assert query._output_fmt == expected
def test_env_set(mock_load_config, input, expected): query = Query("{}", input) assert query._env == expected