Skip to content
Permalink
Browse files

Complete restructure

  • Loading branch information...
digitalformula committed Jun 6, 2019
1 parent 737e2d5 commit 84514936fbfcfc2464de71a0be471e886d55c439

This file was deleted.

BIN +14.8 KB csharp/app_running.png
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file not shown.
BIN +20.2 KB csharp/new_project.png
Binary file not shown.
@@ -0,0 +1,44 @@
Nutanix Developer Portal Code Samples - C#
##########################################

To use the C# code samples, the following environment is recommended.

- Visual Studio Community_ (free & recommended) or full Visual Studio (commercial software)
- Access to a Nutanix Cluster for API testing purposes.
- Nutanix Community Edition is supported but may not always provide the exact same APIs as a "full" Nutanix cluster.
- The Newtonsoft.Json extension for .NET. To install Newtonsoft.Json, please see the official documentation_. **This will need to be done before continuing below**.

.. note:: Please note that instructions provided in this repository will assume the use of Visual Studio Community software.

Console Applications
....................

The provided C# console applications are designed to demonstrate use of the Nutanix Prism REST APIs from C#. To use the samples in your environment, please follow the instructions below.

The examples and screenshots below are from the **list_vm_v3.cs** sample.

#. Open Visual Studio Community_
#. Select **Create a new project**

.. figure:: new_project.png

#. Select **Console App (.NET Framework)** from the list of options (it may be easier to search for **console** in the search bar)

.. figure:: new_console_app.png

#. Click **Next** and configure the app as appropriate for your environment (example shown below).

.. figure:: configure_console_app.png

#. Click **Create**

#. When the new console application is created, the default Program.cs contents can be completely replaced with the code from the repository sample you are using.

#. In the default sample code, edit the **Parameters** object so that the **URI**, **Username** and **Password** are correct for your environment.

#. Either build (Ctrl-Shift-B) or run (F5) the application. The complete JSON response will be shown in the console application window.

.. figure:: app_running.png

.. _Community: https://visualstudio.microsoft.com/vs/community/
.. _Documentation: https://www.nuget.org/packages/Newtonsoft.Json/
Binary file not shown.
@@ -23,7 +23,7 @@ $parameters = [RequestParameters]::new()
$parameters.vm_name = "BasicVMViaAPIv3"

# set the basic properties for the request
$parameters.cluster_ip = "192.168.1.131"
$parameters.cluster_ip = "10.0.0.1"
$parameters.uri = "https://" + $parameters.cluster_ip + ":9440/api/nutanix/v3/vms"

# the payload for this sample script creates a basic VM named as per the $parameters.vm_name variable
@@ -42,7 +42,7 @@ $parameters.payload = "{ `

$parameters.method = "POST"

# set a sensible timeout - you may want to increase this
# set a sensible timeout - you may want to increase this
$parameters.timeout = 5

# get the user's credentials
@@ -86,12 +86,12 @@ $certCallback = @"
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
@@ -0,0 +1,38 @@
Nutanix Developer Portal Code Samples - PowerShell
##################################################

To use the PowerShell code samples, the following environment is recommended.

- Windows PowerShell 5.1 or later
- A suitable PowerShell editor, e.g. Microsoft Visual Studio Code_ or Visual Studio Community_. Instructions for extending Visual Studio Code with PowerShell support are provided below.
- An alternative editor, built into some versions of Windows, is **PowerShell ISE**
- Access to a Nutanix Cluster for API testing purposes.
- Nutanix Community Edition is supported but may not always provide the exact same APIs as a "full" Nutanix cluster.

.. note:: Please note that instructions provided in this repository will assume the use of Visual Studio Code software.

PowerShell Script Usage
.......................

The provided PowerShell scripts are designed to be standalone and can be run without additional dependencies.

The examples and screenshots below are from the **create_vm_v3_basic.ps1** sample.

#. Open Windows PowerShell (administrative permissions are not required).
#. Verify your PowerShell version by entering **$psversiontable** or **$PSVersionTable** (the variable is not case-sensitive)

.. figure:: check_ps_version.png

#. Select **View** > **Extensions**.
#. In the search field, enter **PowerShell**.
#. Install the **PowerShell** extension created by **Microsoft**.
#. Using the selected code sample, create a new PowerShell file (extension **.ps1**, by default).
#. Select the **$parameters.cluster_ip** variable with the correct IP address/FQDN of your cluster (the scripts will prompt for credentials).
#. Click **Debug** > **Start Debugging**.
#. The script will run and prompt for a username and password in the **Terminal** window.
#. After entering credentials, and if the request was successful, the JSON response will show a status as shown below.

.. figure:: script_output.png

.. _Community: https://visualstudio.microsoft.com/vs/community/
.. _Code: https://code.visualstudio.com/
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -22,8 +22,10 @@

'''
setup our command line parameters
for this example we only require the a single parameter - the name of the JSON file that contains our request parameters
this is a very clean way of passing parameters to this sort of script, without the need for excessive parameters on the command line
for this example we only require the a single parameter
- the name of the JSON file that contains our request parameters
this is a very clean way of passing parameters to this sort of
script, without the need for excessive parameters on the command line
'''
parser = argparse.ArgumentParser()
parser.add_argument('json',
@@ -42,24 +44,28 @@
print(f'{args.json} parameters file not found.')
sys.exit()
except json.decoder.JSONDecodeError:
print(f'{args.json} does not appear to contain valid JSON. Please check the file and try again.')
print(f'{args.json} does not appear to contain valid JSON. \
Please check the file and try again.')
sys.exit()

# get the cluster password
cluster_password = getpass.getpass(prompt='Please enter your cluster password: ',stream=None)
cluster_password = getpass.getpass(prompt='Please enter your cluster \
password: ', stream=None)

try:

'''
setup the HTTP Basic Authorization header based on the supplied username and password
setup the HTTP Basic Authorization header based on the
supplied username and password
done this way so that passwords are not supplied on the command line
'''
encoded_credentials = b64encode(bytes(f"{json_data['username']}:{cluster_password}",
encoding="ascii")).decode("ascii")
encoded_credentials = b64encode(bytes(
f'{json_data["username"]}:{cluster_password}',
encoding='ascii')).decode('ascii')
auth_header = f'Basic {encoded_credentials}'

# setup the URL that will be used for the API request
url = f"https://{json_data['cluster_ip']}:9440/api/nutanix/v3/vms"
url = f'https://{json_data["cluster_ip"]}:9440/api/nutanix/v3/vms'

# setup the JSON payload that will be used for this request
payload = f'{{ \
@@ -75,7 +81,8 @@

'''
setup the request headers
note the use of {auth_header} i.e. the Basic Authorization credentials we setup earlier
note the use of {auth_header} i.e. the Basic Authorization
credentials we setup earlier
'''
headers = {
'Accept': "application/json",
@@ -91,11 +98,22 @@
if(response.ok):
print(response.text)
else:
print(f'An error occurred while connecting to {args.ip}.')
# the following line can be uncommented to show detailed error information
# print(response.text)
print(f'An error occurred while connecting to {json_data["cluster_ip"]}.')
'''
the following line can be uncommented to show
detailed error information
'''
print(response.text)
except Exception as ex:
print(f'An {type(ex).__name__} exception occurred while connecting to {args.ip}.\nArgument: {ex.args}.')
print(f'An {type(ex).__name__} exception occurred while \
connecting to {json_data["cluster_ip"]}.\nArgument: {ex.args}.')

except KeyError:
print(f'{args.json} file does not appear to contain the required fields. Please check the file and try again.')
print(f'{args.json} file does not appear to contain the required \
fields. Please check the file and try again.')

'''
wait for the enter key before continuing
this is to prevent terminal flashing if being run inside VS Code, for example
'''
input('Press ENTER to exit.')
File renamed without changes.
@@ -1,5 +1,3 @@
#!/usr/bin/python3.6

'''
use the Prism REST API v3 to create a detailed VM
configures VM with most commonly-required fields
@@ -126,15 +124,22 @@
# submit the request
try:
response = requests.request("POST", url, data=payload, headers=headers,
verify=False)
verify=False,
timeout=1)
if(response.ok):
print(response.text)
else:
print(f'An error occurred while connecting to {args.ip}.')
print(f'An error occurred while connecting to {json_data["cluster_ip"]}.')
# the following line can be uncommented to show detailed error information
# print(response.text)
except Exception as ex:
print(f'An {type(ex).__name__} exception occurred while connecting to {args.ip}.\nArgument: {ex.args}.')
print(f'An {type(ex).__name__} exception occurred while connecting to {json_data["cluster_ip"]}.\nArgument: {ex.args}.')

except KeyError:
print(f'{args.json} file does not appear to contain the required fields. Please check the file and try again.')
print(f'{args.json} file does not appear to contain the required fields. Please check the file and try again.')

'''
wait for the enter key before continuing
this is to prevent terminal flashing if being run inside VS Code, for example
'''
input('Press ENTER to exit.')
@@ -0,0 +1,46 @@
Nutanix Developer Portal Code Samples - Python
##############################################

To use the Python code samples, the following environment is recommended.

- Python >=3.7 (3.7 or later ...)
- Python **requests** (official documentation for requests_) and **urllib3** (official install for urllib3_) libraries. This will need to be done **before** running the Python scripts.
- The accompanying JSON files from this repository (instructions and usage provided below).
- A suitable Python editor, e.g. Microsoft Visual Studio Code_ for GUI editing.
- Access to a Nutanix Cluster for API testing purposes.
- Nutanix Community Edition is supported but may not always provide the exact same APIs as a "full" Nutanix cluster.

.. note:: Please note that instructions provided in this repository will assume the use of Visual Studio Code for GUI editing purposes. Otherwise ubiquitous editors such as **vi** can be used.

Python Script Usage
...................

The provided Python scripts are designed to be standalone and can be run without additional dependencies beyond those provided above.

The examples and screenshots below are from the **create_vm_v3_basic.py** sample. This sample uses **create_vm_v3_basic.json** for the request parameters.

#. Open Visual Studio Code.
#. Create a new file, then copy and paste the code sample from this repository into the editor.
#. Save the file with **.py** extension, e.g. **create_vm_v3_basic.py**.
#. Visual Studio Code will ask if you would like to install the 'Python' and 'Pylint' extensions - these is highly recommended.
#. Create a new file, then copy and paste the contents of **create_vm_v3_basic.json** into the editor.
#. Save the file with **.json** extension, e.g. **create_vm_v3_basic.json**.
#. Edit the JSON values for **cluster_ip**, **username** and **vm_name** so that they match your environment & requirements. The script will prompt for the password when run.
#. Within Visual Studio Code, click **View** > **Command Palette** and start typing **Terminal**. Select **Python: Create Terminal** when the option is shown.

.. note:: Configuring Visual Studio Code for "real" debugging is beyond the scope of this readme. However, an extensive guide can be found at https://code.visualstudio.com/docs/editor/debugging.

#. **Important note before continuing**: The Python 3.7 executable location will vary between Linux, OS X and Windows. Please run the executable for your operating system in the steps below.

.. code-block: bash
/usr/bin/python3.7 ./create_vm_v3_basic.py create_vm_v3_basic.json
#. The script will prompt for your account password, then submit the request based on the script spec. An example based on **create_vm_v3_basic.py** is shown below, running on Ubuntu 19.04.

.. figure:: script_output.png

.. _Community: https://visualstudio.microsoft.com/vs/community/
.. _Code: https://code.visualstudio.com/
.. _requests: https://2.python-requests.org/en/master/user/install/
.. _urllib3: https://pypi.org/project/urllib3/
Binary file not shown.
@@ -3,6 +3,8 @@ Nutanix Developer Portal Code Samples

A collection of code samples designed for distribution via the Nutanix Developer Portal_.

**Note**: The structure of this repository changed on June 6th 2019. Please update any existing bookmarks you may have saved.

.. _Portal: https://developer.nutanix.com

Please feel free to use these for your own environments if you find them useful.
@@ -12,16 +14,15 @@ Assumptions

Detailed instructions on how to setup a development/testing environment are beyond the scope of this repo and the scripts contained within it.

For example, many of the Python examples rely on the **requests** and **urllib3** modules. Steps to install modules are covered in the official Python 3 documentation_.

.. _documentation: https://docs.python.org/3/installing/index.html
Each script language covered in this repository has an accompanying readme file that suggests how the environment should be setup, although individual preferences will nearly always override those suggestions.

Sections
--------

- **storage**: Storage scripts, e.g. interactions with storage_containers, storage stats
- **vm**: VM scripts, e.g. create basic VM, create detailed VM
- **cluster**: Cluster scripts, e.g. list VMs
- **python**: Python scripts based on Python >=3.7
- **powershell**: Windows PowerShell scripts, based on PowerShell >= 5.1
- **shell**: Bash scripts to run in a Linux terminal (please note that the Windows 10 Bash Shell has not been tested with these scripts!)
- **csharp**: Single-file/script Microsoft C# applications, typically run in the console

Support
-------
@@ -24,7 +24,7 @@ else
USERNAME=`echo -n $JSON_CONTENTS | jq -r ".username"`
VM_NAME=`echo -n $JSON_CONTENTS | jq -r ".vm_name"`


# get the password from the user
echo "Please enter your cluster password (will not be shown on screen):"
read -s PASSWORD
@@ -34,6 +34,7 @@ else

# submit the request
curl --insecure -X POST \
--connect-timeout 5 \
https://$CLUSTER_IP:9440/api/nutanix/v3/vms \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
@@ -0,0 +1 @@
{"cluster_ip":"192.168.1.131","username":"admin","vm_name":"BasicVMViaAPIv3"}

0 comments on commit 8451493

Please sign in to comment.
You can’t perform that action at this time.