Skip to content

Commit db6ecfa

Browse files
committed
add azure account methods, and fix readme examples
1 parent 916e754 commit db6ecfa

15 files changed

+636
-43
lines changed

README.md

Lines changed: 156 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ This SDK requires a [Cloudcraft API key](https://developers.cloudcraft.co/#authe
3030
## Installation
3131

3232
```
33-
python -m pip install cloudcraftco
33+
python3.10 -m pip install cloudcraftco
3434
```
3535

3636
## Usage
3737

3838
The API is accessed through the `Cloudcraft` class. An API key available through the Cloudcraft user interface is required when instantiating `Cloudcraft`. It can be passed to the class as an argument or through the `CLOUDCRAFT_API_KEY` environment variable:
3939

4040
```python
41-
from cloudcraftco import Cloudcraft
41+
from cloudcraftco.cloudcraft import Cloudcraft
4242

43+
# assumes CLOUDCRAFT_API_KEY exported
4344
cloudcraft = Cloudcraft()
4445

4546
profile = cloudcraft.read_user_profile()
@@ -52,7 +53,7 @@ profile = cloudcraft.read_user_profile()
5253
The package can be initialized with several options:
5354

5455
```python
55-
from cloudcraftco import Cloudcraft
56+
from cloudcraftco.cloudcraft import Cloudcraft
5657

5758
cloudcraft = Cloudcraft({"api_key": "api-key-value", "timeout": 30000})
5859
```
@@ -85,6 +86,9 @@ Options may also be specified by environment variable...
8586
#### List blueprints
8687

8788
```python
89+
from cloudcraftco.cloudcraft import Cloudcraft
90+
91+
# assumes CLOUDCRAFT_API_KEY exported
8892
cloudcraft = Cloudcraft()
8993

9094
blueprints = cloudcraft.list_blueprints()
@@ -93,6 +97,9 @@ blueprints = cloudcraft.list_blueprints()
9397
#### Retrieve blueprint
9498

9599
```python
100+
from cloudcraftco.cloudcraft import Cloudcraft
101+
102+
# assumes CLOUDCRAFT_API_KEY exported
96103
cloudcraft = Cloudcraft()
97104

98105
blueprint_id = "BLUEPRINT-ID" # valid blueprint uuid
@@ -102,6 +109,9 @@ blueprint = cloudcraft.read_blueprint(blueprint_id)
102109
#### Create blueprint
103110

104111
```python
112+
from cloudcraftco.cloudcraft import Cloudcraft
113+
114+
# assumes CLOUDCRAFT_API_KEY exported
105115
cloudcraft = Cloudcraft()
106116

107117
data = {"data": {"grid": "standard", "name": "New blueprint"}}
@@ -111,16 +121,34 @@ blueprint = cloudcraft.create_blueprint(data)
111121
#### Update blueprint
112122

113123
```python
124+
from cloudcraftco.cloudcraft import Cloudcraft
125+
126+
# assumes CLOUDCRAFT_API_KEY exported
114127
cloudcraft = Cloudcraft()
115128

116129
blueprint_id = "BLUEPRINT-ID" # valid blueprint uuid
117130
data = {"data": {"grid": "standard", "name": "Updated blueprint"}}
118131
cloudcraft.update_blueprint(blueprint_id, data)
119132
```
120133

134+
#### Delete blueprint
135+
136+
```python
137+
from cloudcraftco.cloudcraft import Cloudcraft
138+
139+
# assumes CLOUDCRAFT_API_KEY exported
140+
cloudcraft = Cloudcraft()
141+
142+
blueprint_id = "BLUEPRINT-ID" # valid blueprint uuid
143+
cloudcraft.delete_blueprint(blueprint_id)
144+
```
145+
121146
#### Export blueprint as image
122147

123148
```python
149+
from cloudcraftco.cloudcraft import Cloudcraft
150+
151+
# assumes CLOUDCRAFT_API_KEY exported
124152
cloudcraft = Cloudcraft()
125153
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
126154

@@ -133,20 +161,14 @@ with open(bp_file, "wb") as binary_file:
133161
binary_file.write(export)
134162
```
135163

136-
#### Delete blueprint
137-
138-
```python
139-
cloudcraft = Cloudcraft()
140-
141-
blueprint_id = "BLUEPRINT-ID" # valid blueprint uuid
142-
cloudcraft.delete_blueprint(blueprint_id)
143-
```
144-
145164
### AWS Accounts
146165

147166
#### Add AWS account
148167

149168
```python
169+
from cloudcraftco.cloudcraft import Cloudcraft
170+
171+
# assumes CLOUDCRAFT_API_KEY exported
150172
cloudcraft = Cloudcraft()
151173

152174
# role must exist and match your api_key/account
@@ -158,14 +180,57 @@ result = cloudcraft.create_aws_account(data)
158180
#### List AWS accounts
159181

160182
```python
183+
from cloudcraftco.cloudcraft import Cloudcraft
184+
185+
# assumes CLOUDCRAFT_API_KEY exported
161186
cloudcraft = Cloudcraft()
162187

163188
accounts = cloudcraft.list_aws_accounts()
164189
```
165190

191+
#### Update AWS account
192+
193+
```python
194+
from cloudcraftco.cloudcraft import Cloudcraft
195+
196+
# assumes CLOUDCRAFT_API_KEY exported
197+
cloudcraft = Cloudcraft()
198+
199+
account_id = "AWS-ACCOUNT" # valid account uuid for api-key
200+
role = "AWS-ROLE" # valid role for AWS Account
201+
data = {"name": "Updated Playground AWS Account", "roleArn": role}
202+
result = cloudcraft.update_aws_account(account_id, data)
203+
```
204+
205+
#### Delete AWS account
206+
207+
```python
208+
from cloudcraftco.cloudcraft import Cloudcraft
209+
210+
# assumes CLOUDCRAFT_API_KEY exported
211+
cloudcraft = Cloudcraft()
212+
213+
account_id = "AWS-ACCOUNT" # valid account uuid for api-key
214+
cloudcraft.delete_aws_account(account_id)
215+
```
216+
217+
#### Get my AWS IAM Role parameters
218+
219+
```python
220+
from cloudcraftco.cloudcraft import Cloudcraft
221+
222+
# assumes CLOUDCRAFT_API_KEY exported
223+
cloudcraft = Cloudcraft()
224+
225+
iam_parameters = cloudcraft.read_aws_role_parameters()
226+
```
227+
166228
#### Snapshot AWS account
167229

168230
```python
231+
from cloudcraftco.cloudcraft import Cloudcraft
232+
233+
# assumes CLOUDCRAFT_API_KEY exported
169234
cloudcraft = Cloudcraft()
170235
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
171236

@@ -179,39 +244,95 @@ with open(ss_file, "wb") as binary_file:
179244
binary_file.write(snapshot)
180245
```
181246

182-
#### Update AWS account
247+
### Azure Accounts
248+
249+
#### Add Azure account
183250

184251
```python
252+
from cloudcraftco.cloudcraft import Cloudcraft
253+
254+
# assumes CLOUDCRAFT_API_KEY exported
185255
cloudcraft = Cloudcraft()
186256

187-
account_id = "AWS-ACCOUNT" # valid account uuid for api-key
188-
role = "AWS-ROLE" # valid role for AWS Account
189-
data = {"name": "Updated Playground AWS Account.", "roleArn": role}
190-
result = cloudcraft.update_aws_account(account_id, data)
257+
# id and secret values must be valid
258+
data = {
259+
"name": "Azure Account",
260+
"subscriptionId": "subscriptionId",
261+
"directoryId": "directoryId",
262+
"applicationId": "applicationId",
263+
"clientSecret": "clientSecret"
264+
}
265+
result = cloudcraft.create_azure_account(data)
191266
```
192267

193-
#### Delete AWS account
268+
#### List Azure accounts
194269

195270
```python
271+
from cloudcraftco.cloudcraft import Cloudcraft
272+
273+
# assumes CLOUDCRAFT_API_KEY exported
196274
cloudcraft = Cloudcraft()
197275

198-
account_id = "AWS-ACCOUNT" # valid account uuid for api-key
199-
cloudcraft.delete_aws_account(account_id)
276+
accounts = cloudcraft.list_azure_accounts()
200277
```
201278

202-
#### Get my AWS IAM Role parameters
279+
#### Update Azure account
203280

204281
```python
282+
from cloudcraftco.cloudcraft import Cloudcraft
283+
284+
# assumes CLOUDCRAFT_API_KEY exported
205285
cloudcraft = Cloudcraft()
206286

207-
iam_parameters = cloudcraft.read_aws_role_parameters()
287+
account_id = "AZURE-ACCOUNT" # valid account uuid for api-key
288+
data = {
289+
"name": "Updated Azure Account",
290+
"subscriptionId": "subscriptionId",
291+
"directoryId": "directoryId",
292+
"applicationId": "applicationId",
293+
}
294+
result = cloudcraft.update_azure_account(account_id, data)
295+
```
296+
297+
#### Delete Azure account
298+
299+
```python
300+
from cloudcraftco.cloudcraft import Cloudcraft
301+
302+
# assumes CLOUDCRAFT_API_KEY exported
303+
cloudcraft = Cloudcraft()
304+
305+
account_id = "AZURE-ACCOUNT" # valid account uuid for api-key
306+
cloudcraft.delete_azure_account(account_id)
307+
```
308+
309+
#### Snapshot Azure account
310+
311+
```python
312+
from cloudcraftco.cloudcraft import Cloudcraft
313+
314+
# assumes CLOUDCRAFT_API_KEY exported
315+
cloudcraft = Cloudcraft()
316+
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
317+
318+
ss_account = "AZURE-ACCOUNT" # valid account uuid for api-key
319+
ss_location = "canadaeast"
320+
ss_format = "png"
321+
ss_file = script_dir + ss_location + "." + ss_format
322+
snapshot = cloudcraft.snapshot_azure_account(ss_account, ss_location, ss_format)
323+
324+
with open(ss_file, "wb") as binary_file:
325+
binary_file.write(snapshot)
208326
```
209327

210328
### Budgets
211329

212330
#### Export budget for a blueprint
213331

214332
```python
333+
from cloudcraftco.cloudcraft import Cloudcraft
334+
335+
# assumes CLOUDCRAFT_API_KEY exported
215336
cloudcraft = Cloudcraft()
216337
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
217338

@@ -229,6 +350,9 @@ with open(bp_file, "wb") as binary_file:
229350
#### Get Cloudcraft account info
230351

231352
```python
353+
from cloudcraftco.cloudcraft import Cloudcraft
354+
355+
# assumes CLOUDCRAFT_API_KEY exported
232356
cloudcraft = Cloudcraft()
233357

234358
profile = cloudcraft.read_user_profile()
@@ -264,14 +388,21 @@ Testing accounts requires care, since creating an account requires valid role.
264388
% cd {repo-directory}
265389
% poetry env use python3.10
266390
% poetry shell
391+
% poetry install
267392
% export CLOUDCRAFT_API_KEY={{ api-key }}
268-
% export CLOUDCRAFT_TEST_ROLE={{ your-test-role-arn }}
269-
% python3 dev_playgrounds/accounts.py
270393
% python3 dev_playgrounds/blueprints.py
271394
% python3 dev_playgrounds/budgets.py
272395
% python3 dev_playgrounds/exports.py
273-
% python3 dev_playgrounds/snapshots.py
396+
% python3 dev_playgrounds/snapshots_aws.py
397+
% python3 dev_playgrounds/snapshots_azure.py
274398
% python3 dev_playgrounds/users.py
399+
% export CLOUDCRAFT_TEST_ROLE={{ your-role-arn }}
400+
% python3 dev_playgrounds/accounts_aws.py
401+
% export CLOUDCRAFT_TEST_SUBSCRIPTION={{ your-subscription-id }}
402+
% export CLOUDCRAFT_TEST_DIRECTORY={{ your-directory-id }}
403+
% export CLOUDCRAFT_TEST_APPLICATION={{ your-application-id }}
404+
% export CLOUDCRAFT_TEST_SECRET={{ your-client-secret }}
405+
% python3 dev_playgrounds/accounts_azure.py
275406
```
276407

277408
### Running Tests

cloudcraftco/api_account.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,69 @@ def snapshot_aws_account_handler(
7676
timeout=cloudcraft.timeout_seconds,
7777
params=options,
7878
)
79+
80+
81+
def list_azure_accounts_handler(cloudcraft):
82+
"""List Azure account records via Cloudcraft api."""
83+
# /azure/account
84+
# method: get
85+
api_route = "/azure/account"
86+
url = cloudcraft.url_base.format(api_route)
87+
return requests.get(
88+
url, headers=cloudcraft.headers, timeout=cloudcraft.timeout_seconds
89+
)
90+
91+
92+
def create_azure_account_handler(cloudcraft, data):
93+
"""Create Azure account record via Cloudcraft api."""
94+
# /azure/account
95+
# method: post
96+
api_route = "/azure/account"
97+
url = cloudcraft.url_base.format(api_route)
98+
return requests.post(
99+
url, json=data, headers=cloudcraft.headers, timeout=cloudcraft.timeout_seconds
100+
)
101+
102+
103+
def update_azure_account_handler(cloudcraft, account_id, data):
104+
"""Update Azure account record via Cloudcraft api."""
105+
# /azure/account/{id}
106+
# method: put
107+
api_route = "/azure/account/" + account_id
108+
url = cloudcraft.url_base.format(api_route)
109+
return requests.put(
110+
url, json=data, headers=cloudcraft.headers, timeout=cloudcraft.timeout_seconds
111+
)
112+
113+
114+
def delete_azure_account_handler(cloudcraft, account_id):
115+
"""Delete Azure account record via Coudcraft api."""
116+
# /azure/account/{id}
117+
# method: delete
118+
api_route = "/azure/account/" + account_id
119+
url = cloudcraft.url_base.format(api_route)
120+
return requests.delete(
121+
url, headers=cloudcraft.headers, timeout=cloudcraft.timeout_seconds
122+
)
123+
124+
125+
def snapshot_azure_account_handler(
126+
cloudcraft, account_id, location, ss_format, options=None
127+
):
128+
"""Request Azure account snapshot via Cloudcraft api."""
129+
# /azure/account/{id}/{location}/{format}
130+
# with: optional query parameters
131+
# method: get
132+
# location: azure location, e.g. "eastus"
133+
# format in ["json", "svg", "png", "pdf", "mxGraph"]
134+
# options is dist of optional parameters
135+
if options is None:
136+
options = {}
137+
api_route = "/azure/account/{}/{}/{}".format(account_id, location, ss_format)
138+
url = cloudcraft.url_base.format(api_route)
139+
return requests.get(
140+
url,
141+
headers=cloudcraft.headers,
142+
timeout=cloudcraft.timeout_seconds,
143+
params=options,
144+
)

0 commit comments

Comments
 (0)