Skip to content

Commit

Permalink
[PLAT-1549] Add Python API client examples (for list & create provider)
Browse files Browse the repository at this point in the history
Summary:
This diff adds examples for create / list providers.

A few minor changes were necessary to make these examples work:
- Modify Platform API to not return null fields from API responses.
- Modify devops code to handle null fields.
- Modify sever API responses to not "require" potentially-null fields.

Test Plan:
- Itest (gcp, aws, k8s)
- Unit tests
- UI tests

Reviewers: sb-yb

Reviewed By: sb-yb

Subscribers: yugaware, jenkins-bot

Differential Revision: https://phabricator.dev.yugabyte.com/D12691
  • Loading branch information
jvigil-yugabyte committed Aug 24, 2021
1 parent ea03c55 commit 0e431dd
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 20 deletions.
2 changes: 2 additions & 0 deletions managed/api-examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv
.ipynb_checkpoints
155 changes: 155 additions & 0 deletions managed/api-examples/create-provider.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"* Import packages.\n",
"* Define platform API endpoint (modify for your environment).\n",
"* Define platform API key (found on user page).\n",
"* Create platform API client."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"import os\n",
"import yb_platform_client\n",
"from yb_platform_client.api import provider_api, session_api\n",
"from yb_platform_client.model.provider import Provider\n",
"from yb_platform_client.model.region import Region\n",
"from pprint import pprint\n",
"\n",
"platform_address = 'http://localhost:9000'\n",
"platform_api_key = os.getenv('YB_API_KEY')\n",
"\n",
"api_client = yb_platform_client.ApiClient(yb_platform_client.Configuration(\n",
" host = platform_address,\n",
" api_key = {\n",
" 'apiKeyAuth': platform_api_key,\n",
" }\n",
"))"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"Make an API call to session endpoint to determine customer UUID."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"session_api = session_api.SessionApi(api_client)\n",
"\n",
"try:\n",
" session_info = session_api.get_session_info()\n",
"except yb_platform_client.ApiException as e:\n",
" print(\"Error get_session_info: %s\" % e)\n",
" raise\n",
"\n",
"customer_uuid = session_info.get('customer_uuid')\n",
"print('Customer UUID:\\n%s' % customer_uuid)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"Define new provider object."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"new_provider = Provider(\n",
" air_gap_install=False,\n",
" code=\"gcp\",\n",
" config={\n",
" \"GCE_EMAIL\": \"<service acct email>\",\n",
" \"GCE_HOST_PROJECT\": \"<gcp project>\",\n",
" \"GCE_PROJECT\": \"<gcp project>\",\n",
" \"YB_FIREWALL_TAGS\": \"<vpc tags>\",\n",
" \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n",
" \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n",
" \"client_email\": \"<service account email>\",\n",
" \"client_id\": \"<service account id>\",\n",
" \"client_x509_cert_url\": \"<service account cert url>\",\n",
" \"private_key\": \"<service account key>\",\n",
" \"private_key_id\": \"<service account key id>\",\n",
" \"project_id\": \"<gcp project>\",\n",
" \"token_uri\": \"https://accounts.google.com/o/oauth2/token\",\n",
" \"type\": \"service_account\",\n",
" },\n",
" dest_vpc_id=\"yugabyte-network\",\n",
" name=\"api-test\",\n",
" regions=[\n",
" Region(\n",
" code=\"us-central1\",\n",
" name=\"us-central1\",\n",
" zones=[],\n",
" ),\n",
" ],\n",
" ssh_port=54422,\n",
")"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"Make API call to provider endpoint to create new provider."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"provider_api = provider_api.ProviderApi(api_client)\n",
"\n",
"try:\n",
" api_response = provider_api.create_providers(customer_uuid, new_provider)\n",
"except yb_platform_client.ApiException as e:\n",
" print('Error create_providers: %s' % e)\n",
" raise\n",
"\n",
"pprint(api_response)"
],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.10 64-bit ('venv')"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"interpreter": {
"hash": "57f28aa4ce40fd00633621e172c0b6004aa3a4c49cc0dd486e1853a51500889f"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
109 changes: 109 additions & 0 deletions managed/api-examples/list-providers.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"* Import packages.\n",
"* Define platform API endpoint (modify for your environment).\n",
"* Define platform API key (found on user page).\n",
"* Create platform API client."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"import os\n",
"import yb_platform_client\n",
"from yb_platform_client.api import provider_api, session_api\n",
"from yb_platform_client.model.provider import Provider\n",
"from yb_platform_client.model.session_info import SessionInfo\n",
"\n",
"platform_address = 'http://localhost:9000'\n",
"platform_api_key = os.getenv('YB_API_KEY')\n",
"\n",
"api_client = yb_platform_client.ApiClient(yb_platform_client.Configuration(\n",
" host = platform_address,\n",
" api_key = {\n",
" 'apiKeyAuth': platform_api_key,\n",
" }\n",
"))"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"Make an API call to session endpoint to determine customer UUID."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"session_api = session_api.SessionApi(api_client)\n",
"\n",
"try:\n",
" session_info = session_api.get_session_info()\n",
"except yb_platform_client.ApiException as e:\n",
" print(\"Error get_session_info: %s\" % e)\n",
" raise\n",
"\n",
"customer_uuid = session_info.get('customer_uuid')\n",
"print('Customer UUID:\\n%s' % customer_uuid)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"Make API call to provider endpoint to list providers."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"provider_api = provider_api.ProviderApi(api_client)\n",
"\n",
"try:\n",
" provider_list = provider_api.get_list_of_providers(customer_uuid)\n",
"except yb_platform_client.ApiException as e:\n",
" print('Error get_list_of_providers: %s' % e)\n",
" raise\n",
"\n",
"print('Providers:\\n%s' % provider_list)"
],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"interpreter": {
"hash": "57f28aa4ce40fd00633621e172c0b6004aa3a4c49cc0dd486e1853a51500889f"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.10 64-bit ('venv')"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
14 changes: 14 additions & 0 deletions managed/api-examples/setup.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/bash

set -eo pipefail

if [ ! -d venv ];
then
python3 -m venv venv
. venv/bin/activate
pip3 install --upgrade pip
# see: https://anbasile.github.io/posts/2017-06-25-jupyter-venv/
pip3 install ipykernel
ipython kernel install --user --name=yugabyte-db-client
pip3 install -e ~/code/yugabyte-db/managed/client/python/generated
fi
2 changes: 1 addition & 1 deletion managed/devops/opscli/ybops/cloud/aws/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def network_bootstrap(self, args):
metadata_subset = self._subset_region_data(per_region_meta)
# Override region CIDR info, if any.
for k in metadata_subset:
custom_cidr = per_region_meta[k]["vpcCidr"]
custom_cidr = per_region_meta[k].get("vpcCidr")
if custom_cidr is not None:
cidr_pieces = custom_cidr.split(".")
if len(cidr_pieces) != 4:
Expand Down
4 changes: 4 additions & 0 deletions managed/src/main/java/AppInit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) YugaByte, Inc.

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.yugabyte.yw.cloud.AWSInitializer;
Expand All @@ -24,6 +25,7 @@
import play.Configuration;
import play.Environment;
import play.Logger;
import play.libs.Json;

/** We will use this singleton to do actions specific to the app environment, like db seed etc. */
@Singleton
Expand Down Expand Up @@ -118,6 +120,8 @@ public AppInit(
// Add checksums for all certificates that don't have a checksum.
CertificateHelper.createChecksums();

Json.mapper().setSerializationInclusion(Include.NON_NULL);

Logger.info("AppInit completed");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
import com.yugabyte.yw.models.Universe;
import com.yugabyte.yw.models.Users;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -62,6 +65,7 @@
import java.util.stream.Collectors;
import javax.persistence.PersistenceException;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.input.ReversedLinesFileReader;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.ProfileManager;
Expand Down Expand Up @@ -129,17 +133,26 @@ private CommonProfile getProfile() {
.orElseThrow(() -> new YWServiceException(INTERNAL_SERVER_ERROR, "Unable to get profile"));
}

@Data
@ApiModel(description = "Session information")
@RequiredArgsConstructor
public static class SessionInfo {
final String authToken;
final String apiToken;
final UUID customerUUID;
final UUID userUUID;
@ApiModelProperty(value = "Auth token")
public final String authToken;

@ApiModelProperty(value = "API token")
public final String apiToken;

@ApiModelProperty(value = "Customer UUID")
public final UUID customerUUID;

@ApiModelProperty(value = "User UUID")
public final UUID userUUID;
}

@ApiOperation(
nickname = "getSessionInfo",
value = "Get current user/customer uuid auth/api token",
authorizations = @Authorization(AbstractPlatformController.API_KEY_AUTH),
response = SessionInfo.class)
@With(TokenAuthenticator.class)
public Result getSessionInfo() {
Expand Down
Loading

0 comments on commit 0e431dd

Please sign in to comment.