Using Python
Using Python
Astra Automation
NetApp
December 04, 2023
For simplicity, the NetApp Astra Control Python SDK will be referred to as the SDK throughout
the remainder this page.
Astra SDK
The Astra SDK provides the core platform functionality. It includes a set of Python classes which abstract the
underlying REST API calls. The classes support administrative actions on various Astra Control resources,
including apps, backups, snapshots, and clusters.
The Astra SDK is one part of the package and is provided in the single astraSDK.py file. You can import this
file into your environment and use the classes directly.
The NetApp Astra Control Python SDK (or just SDK) is the name of the entire package. The
Astra SDK refers to the core Python classes in the single file astraSDK.py.
Toolkit script
In addition to the Astra SDK file, the toolkit.py script is also available. This script operates at a higher level
of abstraction by providing access to discrete administrative actions defined internally as Python functions. The
script imports the Astra SDK and makes calls to the classes as needed.
How to access
You can access the SDK in the following ways.
Python package
The SDK is available at Python Package Index under the name actoolkit. The package is assigned a version
number and will continue to be updated as needed. You must use the PiP package management utility to
install the package into your environment.
Once installed, the astraSDK.py classes can be utilized by placing import astraSDK in your scripts.
Additionally, actoolkit can be invoked directly on your command prompt, and is equivalent to toolkit.py
(actoolkit list clusters is the same as ./toolkit.py list clusters).
See PyPI: NetApp Astra Control Python SDK for more information.
1
• astraSDK.py (Astra SDK with Python classes)
• toolkit.py (higher level function-based script)
• Detailed installation requirements and instructions
• Installation scripts
• Additional documentation
You can clone the GitHub: Netapp/netapp-astra-toolkits repository to your local environment.
• Use the prepared Docker: NetApp/astra-toolkits image, which has all necessary dependencies installed,
including actoolkit
• Use Pip to install the actoolkit package from PyPI into your Python environment
• Clone the GitHub repository and copy/modify the two core Python files so they are accessible to your
Python client code
Native Python
Before you begin
Python is a popular development language for datacenter automation. Before using the
2
native features of Python together with several common packages, you need to prepare
the environment and the required input files.
In addition to accessing the Astra Control REST API directly using Python, NetApp also provides
a toolkit package which abstracts the API and removes some of the complexities. See NetApp
Astra Control Python SDK for more information.
The basic configuration requirements to run the Python scripts are described below.
Python 3
You need to have the latest version of Python 3 installed.
Additional libraries
The Requests and urllib3 libraries must be installed. You can use pip or another Python management tool as
appropriate for your environment.
Network access
The workstation where the scripts run must have network access and be able to reach Astra Control. When
using Astra Control Service, you must be connected to the internet and be able to connect to the service at
https://astra.netapp.io.
Identity information
You need a valid Astra account with the account identifier and API token. See Get an API token for more
information.
The Python scripts rely on configuration information contained in JSON input files. Sample files are provided
below.
Identity information
The following file contains the API token and Astra account. You need to pass this file to Python scripts using
the -i (or --identity) CLI parameter.
{
"api_token": "kH4CA_uVIa8q9UuPzhJaAHaGlaR7-no901DkkrVjIXk=",
"account_id": "5131dfdf-03a4-5218-ad4b-fe84442b9786"
}
See Before you begin for an example of the required JSON input file.
3
1 #!/usr/bin/env python3
2
4
##------------------------------------------------------------------------
------
3 #
4 # Usage: python3 list_man_apps.py -i identity_file.json
5 #
6 # (C) Copyright 2022 NetApp, Inc.
7 #
8 # This sample code is provided AS IS, with no support or warranties of
9 # any kind, including but not limited for warranties of
merchantability
10 # or fitness of any kind, expressed or implied. Permission to use,
11 # reproduce, modify and create derivatives of the sample code is
granted
12 # solely for the purpose of researching, designing, developing and
13 # testing a software application product for use with NetApp products,
14 # provided that the above copyright notice appears in all copies and
15 # that the software application product is distributed pursuant to
terms
16 # no less restrictive than those set forth herein.
17 #
18
##------------------------------------------------------------------------
------
19
20 import argparse
21 import json
22 import requests
23 import urllib3
24 import sys
25
26 # Global variables
27 api_token = ""
28 account_id = ""
29
30 def get_managed_apps():
31 ''' Get and print the list of apps '''
32
33 # Global variables
34 global api_token
35 global account_id
36
37 # Create an HTTP session
38 sess1 = requests.Session()
39
40 # Suppress SSL unsigned certificate warning
5
41 urllib3.disable_warnings(urllib3.exceptions.
InsecureRequestWarning)
42
43 # Create URL
44 url1 = "https://astra.netapp.io/accounts/" + account_id +
"/k8s/v2/apps"
45
46 # Headers and response output
47 req_headers = {}
48 resp_headers = {}
49 resp_data = {}
50
51 # Prepare the request headers
52 req_headers.clear
53 req_headers['Authorization'] = "Bearer " + api_token
54 req_headers['Content-Type'] = "application/astra-app+json"
55 req_headers['Accept'] = "application/astra-app+json"
56
57 # Make the REST call
58 try:
59 resp1 = sess1.request('get', url1, headers=req_headers,
allow_redirects=True, verify=False)
60
61 except requests.exceptions.ConnectionError:
62 print("Connection failed")
63 sys.exit(1)
64
65 # Retrieve the output
66 http_code = resp1.status_code
67 resp_headers = resp1.headers
68
69 # Print the list of apps
70 if resp1.ok:
71 resp_data = json.loads(resp1.text)
72 items = resp_data['items']
73 for i in items:
74 print(" ")
75 print("Name: " + i['name'])
76 print("ID: " + i['id'])
77 print("State: " + i['state'])
78 else:
79 print("Failed with HTTP status code: " + str(http_code))
80
81 print(" ")
82
83 # Close the session
6
84 sess1.close()
85
86 return
87
88 def read_id_file(idf):
89 ''' Read the identity file and save values '''
90
91 # Global variables
92 global api_token
93 global account_id
94
95 with open(idf) as f:
96 data = json.load(f)
97
98 api_token = data['api_token']
99 account_id = data['account_id']
100
101 return
102
103 def main(args):
104 ''' Main top level function '''
105
106 # Global variables
107 global api_token
108 global account_id
109
110 # Retrieve name of JSON input file
111 identity_file = args.id_file
112
113 # Get token and account
114 read_id_file(identity_file)
115
116 # Issue REST call
117 get_managed_apps()
118
119 return
120
121 def parseArgs():
122 ''' Parse the CLI input parameters '''
123
124 parser = argparse.ArgumentParser(description='Astra REST API -
List the apps',
125 add_help = True)
126 parser.add_argument("-i", "--identity", action="store", dest
="id_file", default=None,
127 help='(Req) Name of the identity input
7
file', required=True)
128
129 return parser.parse_args()
130
131 if __name__ == '__main__':
132 ''' Begin here '''
133
134 # Parse input parameters
135 args = parseArgs()
136
137 # Call main function
138 main(args)
8
Copyright information
Copyright © 2023 NetApp, Inc. All Rights Reserved. Printed in the U.S. No part of this document covered by
copyright may be reproduced in any form or by any means—graphic, electronic, or mechanical, including
photocopying, recording, taping, or storage in an electronic retrieval system—without prior written permission
of the copyright owner.
Software derived from copyrighted NetApp material is subject to the following license and disclaimer:
THIS SOFTWARE IS PROVIDED BY NETAPP “AS IS” AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE, WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
NETAPP BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NetApp reserves the right to change any products described herein at any time, and without notice. NetApp
assumes no responsibility or liability arising from the use of products described herein, except as expressly
agreed to in writing by NetApp. The use or purchase of this product does not convey a license under any
patent rights, trademark rights, or any other intellectual property rights of NetApp.
The product described in this manual may be protected by one or more U.S. patents, foreign patents, or
pending applications.
LIMITED RIGHTS LEGEND: Use, duplication, or disclosure by the government is subject to restrictions as set
forth in subparagraph (b)(3) of the Rights in Technical Data -Noncommercial Items at DFARS 252.227-7013
(FEB 2014) and FAR 52.227-19 (DEC 2007).
Data contained herein pertains to a commercial product and/or commercial service (as defined in FAR 2.101)
and is proprietary to NetApp, Inc. All NetApp technical data and computer software provided under this
Agreement is commercial in nature and developed solely at private expense. The U.S. Government has a non-
exclusive, non-transferrable, nonsublicensable, worldwide, limited irrevocable license to use the Data only in
connection with and in support of the U.S. Government contract under which the Data was delivered. Except
as provided herein, the Data may not be used, disclosed, reproduced, modified, performed, or displayed
without the prior written approval of NetApp, Inc. United States Government license rights for the Department
of Defense are limited to those rights identified in DFARS clause 252.227-7015(b) (FEB 2014).
Trademark information
NETAPP, the NETAPP logo, and the marks listed at http://www.netapp.com/TM are trademarks of NetApp, Inc.
Other company and product names may be trademarks of their respective owners.