|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import time |
| 4 | +from uuid import UUID |
| 5 | + |
| 6 | +from stackit.automation.api.default_api import DefaultApi |
| 7 | +from stackit.automation.models.automation_schedule_trigger import ( |
| 8 | + AutomationScheduleTrigger, |
| 9 | +) |
| 10 | +from stackit.automation.models.automation_triggers import AutomationTriggers |
| 11 | +from stackit.automation.models.create_volume_automation_payload import ( |
| 12 | + CreateVolumeAutomationPayload, |
| 13 | +) |
| 14 | +from stackit.automation.models.partial_update_volume_automation_payload import ( |
| 15 | + PartialUpdateVolumeAutomationPayload, |
| 16 | +) |
| 17 | +from stackit.automation.models.volume_automation_input import VolumeAutomationInput |
| 18 | +from stackit.core.configuration import Configuration |
| 19 | + |
| 20 | + |
| 21 | +def main(): |
| 22 | + # Credentials are read from the credentialsFile in |
| 23 | + # `~/.stackit/credentials.json` or the env |
| 24 | + # STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY |
| 25 | + config = Configuration() |
| 26 | + automation_api = DefaultApi(config) |
| 27 | + |
| 28 | + # the id of your STACKIT project, read from env var for this example |
| 29 | + project_id = os.getenv("PROJECT_ID") |
| 30 | + if not project_id: |
| 31 | + print("Environment variable 'PROJECT_ID' not found.", file=sys.stderr) |
| 32 | + return |
| 33 | + |
| 34 | + # the region which should be used to interact with the automation service |
| 35 | + region = "eu01" |
| 36 | + |
| 37 | + try: |
| 38 | + # /////////////////////////////////////////////////////// |
| 39 | + # // V O L U M E T E M P L A T E S // |
| 40 | + # /////////////////////////////////////////////////////// |
| 41 | + |
| 42 | + # list all available volume templates |
| 43 | + list_templates = automation_api.list_volume_templates(project_id=project_id, region=region) |
| 44 | + print("Listing volume templates:") |
| 45 | + if list_templates.items: |
| 46 | + for template in list_templates.items: |
| 47 | + print(f"* Template ID: {template.id}") |
| 48 | + |
| 49 | + # if there is a next_page_token, fetch next page of results |
| 50 | + while list_templates.next_page_token: |
| 51 | + list_templates = automation_api.list_volume_templates( |
| 52 | + project_id=project_id, |
| 53 | + region=region, |
| 54 | + page_token=list_templates.next_page_token, |
| 55 | + ) |
| 56 | + print("Listing next page of volume templates:") |
| 57 | + if list_templates.items: |
| 58 | + for template in list_templates.items: |
| 59 | + print(f"* Template ID: {template.id}") |
| 60 | + |
| 61 | + if not list_templates.items: |
| 62 | + print("No volume templates found.") |
| 63 | + return |
| 64 | + |
| 65 | + # get one specific volume template |
| 66 | + template = automation_api.get_volume_template( |
| 67 | + project_id=project_id, |
| 68 | + region=region, |
| 69 | + template_id=str(list_templates.items[0].id), |
| 70 | + ) |
| 71 | + print("\n\nFetched volume template:") |
| 72 | + print(f"* Template ID: {template.id}") |
| 73 | + print(f"* Template name: {template.name}") |
| 74 | + print(f"* Template description: {template.description}") |
| 75 | + |
| 76 | + # /////////////////////////////////////////////////////// |
| 77 | + # // V O L U M E A U T O M A T I O N // |
| 78 | + # /////////////////////////////////////////////////////// |
| 79 | + |
| 80 | + # create a volume automation with a schedule trigger |
| 81 | + volume_automation = automation_api.create_volume_automation( |
| 82 | + project_id=project_id, |
| 83 | + region=region, |
| 84 | + create_volume_automation_payload=CreateVolumeAutomationPayload( |
| 85 | + description="Creates daily recovery points for all volumes with specified label", |
| 86 | + name="My Daily Volume Recovery Point Creation Automation", |
| 87 | + template_id=UUID(str(template.id)), |
| 88 | + input=VolumeAutomationInput( |
| 89 | + kind="VolumeRecoveryPointManagement", |
| 90 | + additional_properties={ |
| 91 | + "inheritVolumeLabels": True, |
| 92 | + "recoveryPointLabels": { |
| 93 | + "exampleLabelKey1": "exampleLabelValue1", |
| 94 | + }, |
| 95 | + "snapshotRetentionPolicy": { |
| 96 | + "kind": "count", |
| 97 | + "value": 2, |
| 98 | + }, |
| 99 | + "volumeLabelSelector": "myLabelkey1=myLabelValue,myLabelKey2=myOtherLabelValue", |
| 100 | + }, |
| 101 | + ), |
| 102 | + triggers=AutomationTriggers( |
| 103 | + schedule=AutomationScheduleTrigger( |
| 104 | + rrule="DTSTART;TZID=Europe/Sofia:20200803T023000\nRRULE:FREQ=DAILY;INTERVAL=1", |
| 105 | + ), |
| 106 | + ), |
| 107 | + ), |
| 108 | + ) |
| 109 | + print("\n\nCreated volume automation:") |
| 110 | + print(f"* Automation ID: {volume_automation.id}") |
| 111 | + print(f"* Automation name: {volume_automation.name}") |
| 112 | + print(f"* Automation description: {volume_automation.description}") |
| 113 | + |
| 114 | + # list all volume automations |
| 115 | + list_automations = automation_api.list_volume_automations(project_id=project_id, region=region) |
| 116 | + print("\n\nListing volume automations:") |
| 117 | + if list_automations.items: |
| 118 | + for automation in list_automations.items: |
| 119 | + print(f"* Automation ID: {automation.id}") |
| 120 | + |
| 121 | + # if there is a next_page_token, fetch next page of results |
| 122 | + while list_automations.next_page_token: |
| 123 | + list_automations = automation_api.list_volume_automations( |
| 124 | + project_id=project_id, |
| 125 | + region=region, |
| 126 | + page_token=list_automations.next_page_token, |
| 127 | + ) |
| 128 | + print("\nListing next page of volume automations:") |
| 129 | + if list_automations.items: |
| 130 | + for automation in list_automations.items: |
| 131 | + print(f"* Automation ID: {automation.id}") |
| 132 | + |
| 133 | + # update an automation |
| 134 | + updated_automation = automation_api.partial_update_volume_automation( |
| 135 | + project_id=project_id, |
| 136 | + region=region, |
| 137 | + automation_id=str(volume_automation.id), |
| 138 | + partial_update_volume_automation_payload=PartialUpdateVolumeAutomationPayload( |
| 139 | + description="Updated daily recovery points automation", |
| 140 | + name="Updated Daily Volume Recovery Point Creation Automation", |
| 141 | + ), |
| 142 | + ) |
| 143 | + print("\n\nUpdated volume automation:") |
| 144 | + print(f"* Automation ID: {updated_automation.id}") |
| 145 | + print(f"* Automation name: {updated_automation.name}") |
| 146 | + print(f"* Automation description: {updated_automation.description}") |
| 147 | + |
| 148 | + # get one specific volume automation |
| 149 | + fetched_automation = automation_api.get_volume_automation( |
| 150 | + project_id=project_id, |
| 151 | + region=region, |
| 152 | + automation_id=str(volume_automation.id), |
| 153 | + ) |
| 154 | + print("\n\nFetched updated volume template:") |
| 155 | + print(f"* Automation ID: {fetched_automation.id}") |
| 156 | + print(f"* Automation name: {fetched_automation.name}") |
| 157 | + print(f"* Automation description: {fetched_automation.description}") |
| 158 | + |
| 159 | + # /////////////////////////////////////////////////////// |
| 160 | + # // E X E C U T I O N S // |
| 161 | + # /////////////////////////////////////////////////////// |
| 162 | + |
| 163 | + # trigger an automation execution |
| 164 | + created_execution = automation_api.create_volume_execution( |
| 165 | + project_id=project_id, |
| 166 | + region=region, |
| 167 | + automation_id=str(volume_automation.id), |
| 168 | + ) |
| 169 | + print("\n\nTriggered volume automation execution:") |
| 170 | + print(f"* Execution ID: {created_execution.id}") |
| 171 | + print(f"* Execution status: {created_execution.status}") |
| 172 | + |
| 173 | + # wait for the automation execution to complete |
| 174 | + while str(created_execution.status).upper() in ["PENDING", "RUNNING"]: |
| 175 | + print("* Waiting for automation execution to complete ...") |
| 176 | + time.sleep(2) |
| 177 | + created_execution = automation_api.get_volume_execution( |
| 178 | + project_id=project_id, |
| 179 | + region=region, |
| 180 | + automation_id=str(volume_automation.id), |
| 181 | + execution_id=str(created_execution.id), |
| 182 | + ) |
| 183 | + print("* Automation execution completed") |
| 184 | + |
| 185 | + # list all executions of a specific volume automation |
| 186 | + list_executions = automation_api.list_volume_executions( |
| 187 | + project_id=project_id, |
| 188 | + region=region, |
| 189 | + automation_id=str(volume_automation.id), |
| 190 | + ) |
| 191 | + print("\n\nListing executions of the volume automation:") |
| 192 | + if list_executions.items: |
| 193 | + for execution in list_executions.items: |
| 194 | + print(f"* Execution ID: {execution.id}") |
| 195 | + |
| 196 | + # /////////////////////////////////////////////////////// |
| 197 | + # // D E L E T I O N // |
| 198 | + # /////////////////////////////////////////////////////// |
| 199 | + |
| 200 | + # trigger deletion of the created volume automation |
| 201 | + print("\n\nDeleting created volume automation") |
| 202 | + automation_api.delete_volume_automation( |
| 203 | + project_id=project_id, |
| 204 | + region=region, |
| 205 | + automation_id=str(volume_automation.id), |
| 206 | + ) |
| 207 | + print(f'* Successfully deleted automation with ID "{volume_automation.id}"') |
| 208 | + |
| 209 | + except Exception as e: |
| 210 | + print(f"Exception when calling AutomationApi: {e}", file=sys.stderr) |
| 211 | + |
| 212 | + |
| 213 | +if __name__ == "__main__": |
| 214 | + main() |
0 commit comments