Skip to content

Instantly share code, notes, and snippets.

@greyhoundforty
Last active February 28, 2023 18:57
Show Gist options
  • Save greyhoundforty/7d99419a1bf6442c88b49e9a99d5df4a to your computer and use it in GitHub Desktop.
Save greyhoundforty/7d99419a1bf6442c88b49e9a99d5df4a to your computer and use it in GitHub Desktop.
Schematics Python Example
import os
import sys
import json 
from pprint import pprint
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_cloud_sdk_core import ApiException
from ibm_schematics.schematics_v1 import SchematicsV1

# Set up IAM authenticator
authenticator = IAMAuthenticator(os.environ.get('IBMCLOUD_API_KEY'))

# Set up Schematics service client and endpoint 
schematics_service = SchematicsV1(authenticator=authenticator)
schematics_service.set_service_url('https://us.schematics.cloud.ibm.com')

# Fetch the workspace ID and refresh token from environment variables
workspaceId = os.environ.get('WORKSPACE_ID')
refreshToken = os.environ.get('REFRESH_TOKEN')

# Construct the terraform taint command model
terraform_command_model = {
  'command': 'taint',
  'command_params': 'random_integer.location',
  'command_name': 'location-taint',
  'command_desc': 'Run taint on location resource',
}

workspace_activity_command_result = schematics_service.run_workspace_commands(
    w_id=workspaceId,
    refresh_token=refreshToken,
    commands=[terraform_command_model],
    operation_name='taint-location',
    description='taint-location'
).get_result()

taintActivityId = workspace_activity_command_result.get('activityid')
pprint(taintActivityId)

job = schematics_service.get_job(job_id=taintActivityId).get_result()
jobStatus = job.get('status')
pprint(jobStatus)

Next step is to set up a while loop for jobStatus = Pending and then when completed, run the plan, repeat the logic check, and then apply the plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment